| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * FillSphericalSurfaceAction.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 29, 2012
 | 
|---|
| 27 |  *      Author: heber, bollerhe
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "Actions/UndoRedoHelpers.hpp"
 | 
|---|
| 38 | #include "Atom/atom.hpp"
 | 
|---|
| 39 | #include "Atom/AtomicInfo.hpp"
 | 
|---|
| 40 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
 | 
|---|
| 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 42 | #include "Filling/Cluster.hpp"
 | 
|---|
| 43 | #include "Filling/Filler.hpp"
 | 
|---|
| 44 | #include "Filling/Inserter/Inserter.hpp"
 | 
|---|
| 45 | #include "Filling/Inserter/SurfaceInserter.hpp"
 | 
|---|
| 46 | #include "Filling/Mesh/MeshAdaptor.hpp"
 | 
|---|
| 47 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 50 | #include "World.hpp"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | 
 | 
|---|
| 53 | #include <algorithm>
 | 
|---|
| 54 | #include <boost/foreach.hpp>
 | 
|---|
| 55 | #include <boost/function.hpp>
 | 
|---|
| 56 | #include <iostream>
 | 
|---|
| 57 | #include <string>
 | 
|---|
| 58 | #include <vector>
 | 
|---|
| 59 | 
 | 
|---|
| 60 | #include "Actions/FillAction/FillSphericalSurfaceAction.hpp"
 | 
|---|
| 61 | 
 | 
|---|
| 62 | using namespace MoleCuilder;
 | 
|---|
| 63 | 
 | 
|---|
| 64 | // and construct the stuff
 | 
|---|
| 65 | #include "FillSphericalSurfaceAction.def"
 | 
|---|
| 66 | #include "Action_impl_pre.hpp"
 | 
|---|
| 67 | /** =========== define the function ====================== */
 | 
|---|
| 68 | Action::state_ptr FillSphericalSurfaceAction::performCall() {
 | 
|---|
| 69 |   // get the filler molecule
 | 
|---|
| 70 |   const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 71 |   std::vector<AtomicInfo> movedatoms;
 | 
|---|
| 72 |   if (molecules.size() != 1) {
 | 
|---|
| 73 |     ELOG(1, "No exactly one molecule selected, aborting,");
 | 
|---|
| 74 |     return Action::failure;
 | 
|---|
| 75 |   }
 | 
|---|
| 76 |   molecule *filler = *(molecules.begin());
 | 
|---|
| 77 |   for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter)
 | 
|---|
| 78 |     movedatoms.push_back( AtomicInfo(*(*iter)) );
 | 
|---|
| 79 |   LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   // center filler's tip at origin
 | 
|---|
| 82 |   Vector max;
 | 
|---|
| 83 |   filler->CenterEdge(&max);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   // determine center with respect to alignment axis
 | 
|---|
| 86 |   Vector sum = zeroVec;
 | 
|---|
| 87 |   for (molecule::iterator it2=filler->begin();it2 !=filler->end();++it2) {
 | 
|---|
| 88 |     const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second;
 | 
|---|
| 89 |     sum += helper;
 | 
|---|
| 90 |   }
 | 
|---|
| 91 |   sum *= 1./filler->size();
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // translate molecule's closest atom to origin (such that is resides on the filler spot)
 | 
|---|
| 94 |   LOG(1, "DEBUG: molecule is off Alignment axis by " << sum << ", shifting ...");
 | 
|---|
| 95 |   {
 | 
|---|
| 96 |     Vector translater = -1.*sum;
 | 
|---|
| 97 |     filler->Translate(&translater);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   // create predicate, mesh, and filler
 | 
|---|
| 101 |   FillSphericalSurfaceState *UndoState = NULL;
 | 
|---|
| 102 |   bool successflag = false;
 | 
|---|
| 103 |   {
 | 
|---|
| 104 |     FillPredicate *voidnode_predicate = new FillPredicate(
 | 
|---|
| 105 |         IsVoidNode_FillPredicate(
 | 
|---|
| 106 |             Sphere(zeroVec, params.mindistance.get())
 | 
|---|
| 107 |             )
 | 
|---|
| 108 |         );
 | 
|---|
| 109 |     Shape s = Sphere(params.center.get(), params.radius.get());
 | 
|---|
| 110 |     boost::function<const NodeSet ()> func = 
 | 
|---|
| 111 |         boost::bind(&Shape::getHomogeneousPointsOnSurface, boost::ref(s), params.N.get());
 | 
|---|
| 112 |     Mesh *mesh = new MeshAdaptor(func);
 | 
|---|
| 113 |     Inserter *inserter = new Inserter(
 | 
|---|
| 114 |         Inserter::impl_ptr(new SurfaceInserter(s, params.AlignedAxis.get())));
 | 
|---|
| 115 | 
 | 
|---|
| 116 |     // fill
 | 
|---|
| 117 |     {
 | 
|---|
| 118 |       Filler *fillerFunction = new Filler(*mesh, *voidnode_predicate, *inserter);
 | 
|---|
| 119 |       ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingShape() ) );
 | 
|---|
| 120 |       CopyAtoms_withBonds copyMethod;
 | 
|---|
| 121 |       Filler::ClusterVector_t ClonedClusters;
 | 
|---|
| 122 |       successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
 | 
|---|
| 123 |       delete fillerFunction;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |       // append each cluster's atoms to clonedatoms (however not selected ones)
 | 
|---|
| 126 |       std::vector<const atom *> clonedatoms;
 | 
|---|
| 127 |       std::vector<AtomicInfo> clonedatominfos;
 | 
|---|
| 128 |       for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
 | 
|---|
| 129 |           iter != ClonedClusters.end(); ++iter) {
 | 
|---|
| 130 |         const AtomIdSet &atoms = (*iter)->getAtomIds();
 | 
|---|
| 131 |         clonedatoms.reserve(clonedatoms.size()+atoms.size());
 | 
|---|
| 132 |         for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
 | 
|---|
| 133 |           if (!filler->containsAtom(*atomiter)) {
 | 
|---|
| 134 |             clonedatoms.push_back( *atomiter );
 | 
|---|
| 135 |             clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
 | 
|---|
| 136 |           }
 | 
|---|
| 137 |       }
 | 
|---|
| 138 |       std::vector< BondInfo > clonedbonds;
 | 
|---|
| 139 |       StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
 | 
|---|
| 140 |       LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with "
 | 
|---|
| 141 |           << clonedbonds.size()/2 << " bonds.");
 | 
|---|
| 142 | 
 | 
|---|
| 143 |       if (!successflag) {
 | 
|---|
| 144 |         ELOG(1, "Insertion failed, removing inserted clusters, translating original one back");
 | 
|---|
| 145 |         RemoveAtomsFromAtomicInfo(clonedatominfos);
 | 
|---|
| 146 |         clonedatoms.clear();
 | 
|---|
| 147 |         SetAtomsFromAtomicInfo(movedatoms);
 | 
|---|
| 148 |       } else {
 | 
|---|
| 149 |         std::vector<Vector> MovedToVector(filler->size(), zeroVec);
 | 
|---|
| 150 |         std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
 | 
|---|
| 151 |             boost::bind(&AtomInfo::getPosition, _1) );
 | 
|---|
| 152 |         UndoState = new FillSphericalSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
 | 
|---|
| 153 |       }
 | 
|---|
| 154 |     }
 | 
|---|
| 155 | 
 | 
|---|
| 156 |     // remove
 | 
|---|
| 157 |     delete mesh;
 | 
|---|
| 158 |     delete inserter;
 | 
|---|
| 159 |     delete voidnode_predicate;
 | 
|---|
| 160 |   }
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   if (successflag)
 | 
|---|
| 163 |     return Action::state_ptr(UndoState);
 | 
|---|
| 164 |   else
 | 
|---|
| 165 |     return Action::failure;
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | Action::state_ptr FillSphericalSurfaceAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 169 |   FillSphericalSurfaceState *state = assert_cast<FillSphericalSurfaceState*>(_state.get());
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   // remove all created atoms
 | 
|---|
| 172 |   RemoveAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| 173 |   // add the original cluster
 | 
|---|
| 174 |   SetAtomsFromAtomicInfo(state->movedatoms);
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   return Action::state_ptr(_state);
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | Action::state_ptr FillSphericalSurfaceAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 180 |   FillSphericalSurfaceState *state = assert_cast<FillSphericalSurfaceState*>(_state.get());
 | 
|---|
| 181 | 
 | 
|---|
| 182 |   // place filler cluster again at new spot
 | 
|---|
| 183 |   ResetAtomPosition(state->movedatoms, state->MovedToVector);
 | 
|---|
| 184 | 
 | 
|---|
| 185 |   // re-create all clusters
 | 
|---|
| 186 |   bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
 | 
|---|
| 187 | 
 | 
|---|
| 188 |   // re-create the bonds
 | 
|---|
| 189 |   statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds);
 | 
|---|
| 190 |   if (statusflag)
 | 
|---|
| 191 |     return Action::state_ptr(_state);
 | 
|---|
| 192 |   else
 | 
|---|
| 193 |     return Action::failure;
 | 
|---|
| 194 | }
 | 
|---|
| 195 | 
 | 
|---|
| 196 | bool FillSphericalSurfaceAction::canUndo() {
 | 
|---|
| 197 |   return true;
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | bool FillSphericalSurfaceAction::shouldUndo() {
 | 
|---|
| 201 |   return true;
 | 
|---|
| 202 | }
 | 
|---|
| 203 | /** =========== end of function ====================== */
 | 
|---|