| [2db053] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [2db053] | 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 |  | 
|---|
| [42b6de] | 37 | #include "Actions/UndoRedoHelpers.hpp" | 
|---|
| [80ca29] | 38 | #include "Atom/atom.hpp" | 
|---|
| [42b6de] | 39 | #include "Atom/AtomicInfo.hpp" | 
|---|
| [cb7676] | 40 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp" | 
|---|
| [2db053] | 41 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 42 | #include "Filling/Cluster.hpp" | 
|---|
|  | 43 | #include "Filling/Filler.hpp" | 
|---|
|  | 44 | #include "Filling/Inserter/Inserter.hpp" | 
|---|
| [cb7676] | 45 | #include "Filling/Inserter/SurfaceInserter.hpp" | 
|---|
| [2db053] | 46 | #include "Filling/Mesh/MeshAdaptor.hpp" | 
|---|
|  | 47 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp" | 
|---|
| [cb7676] | 48 | #include "molecule.hpp" | 
|---|
| [2db053] | 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() { | 
|---|
| [80ca29] | 69 | // get the filler molecule | 
|---|
| [cb7676] | 70 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules(); | 
|---|
| [80ca29] | 71 | std::vector<AtomicInfo> movedatoms; | 
|---|
| [cb7676] | 72 | if (molecules.size() != 1) { | 
|---|
|  | 73 | ELOG(1, "No exactly one molecule selected, aborting,"); | 
|---|
| [2db053] | 74 | return Action::failure; | 
|---|
|  | 75 | } | 
|---|
| [cb7676] | 76 | molecule *filler = *(molecules.begin()); | 
|---|
| [80ca29] | 77 | for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter) | 
|---|
|  | 78 | movedatoms.push_back( AtomicInfo(*(*iter)) ); | 
|---|
| [cb7676] | 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) { | 
|---|
| [f10b0c] | 88 | const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second; | 
|---|
| [cb7676] | 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); | 
|---|
| [2db053] | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | // create predicate, mesh, and filler | 
|---|
| [80ca29] | 101 | FillSphericalSurfaceState *UndoState = NULL; | 
|---|
| [2db053] | 102 | bool successflag = false; | 
|---|
|  | 103 | { | 
|---|
|  | 104 | FillPredicate *voidnode_predicate = new FillPredicate( | 
|---|
|  | 105 | IsVoidNode_FillPredicate( | 
|---|
| [f10b0c] | 106 | Sphere(zeroVec, params.mindistance.get()) | 
|---|
| [2db053] | 107 | ) | 
|---|
|  | 108 | ); | 
|---|
| [f10b0c] | 109 | Shape s = Sphere(params.center.get(), params.radius.get()); | 
|---|
| [2db053] | 110 | boost::function<const NodeSet ()> func = | 
|---|
| [f10b0c] | 111 | boost::bind(&Shape::getHomogeneousPointsOnSurface, boost::ref(s), params.N.get()); | 
|---|
| [2db053] | 112 | Mesh *mesh = new MeshAdaptor(func); | 
|---|
|  | 113 | Inserter *inserter = new Inserter( | 
|---|
| [f10b0c] | 114 | Inserter::impl_ptr(new SurfaceInserter(s, params.AlignedAxis.get()))); | 
|---|
| [2db053] | 115 |  | 
|---|
|  | 116 | // fill | 
|---|
|  | 117 | { | 
|---|
|  | 118 | Filler *fillerFunction = new Filler(*mesh, *voidnode_predicate, *inserter); | 
|---|
| [cb7676] | 119 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingShape() ) ); | 
|---|
|  | 120 | CopyAtoms_withBonds copyMethod; | 
|---|
| [42b6de] | 121 | Filler::ClusterVector_t ClonedClusters; | 
|---|
|  | 122 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters); | 
|---|
| [2db053] | 123 | delete fillerFunction; | 
|---|
| [42b6de] | 124 |  | 
|---|
| [80ca29] | 125 | // append each cluster's atoms to clonedatoms (however not selected ones) | 
|---|
|  | 126 | std::vector<const atom *> clonedatoms; | 
|---|
|  | 127 | std::vector<AtomicInfo> clonedatominfos; | 
|---|
| [42b6de] | 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) | 
|---|
| [80ca29] | 133 | if (!filler->containsAtom(*atomiter)) { | 
|---|
|  | 134 | clonedatoms.push_back( *atomiter ); | 
|---|
|  | 135 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) ); | 
|---|
|  | 136 | } | 
|---|
| [42b6de] | 137 | } | 
|---|
| [80ca29] | 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 |  | 
|---|
| [42b6de] | 143 | if (!successflag) { | 
|---|
| [80ca29] | 144 | ELOG(1, "Insertion failed, removing inserted clusters, translating original one back"); | 
|---|
|  | 145 | RemoveAtomsFromAtomicInfo(clonedatominfos); | 
|---|
| [42b6de] | 146 | clonedatoms.clear(); | 
|---|
| [80ca29] | 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); | 
|---|
| [42b6de] | 153 | } | 
|---|
| [2db053] | 154 | } | 
|---|
|  | 155 |  | 
|---|
|  | 156 | // remove | 
|---|
|  | 157 | delete mesh; | 
|---|
|  | 158 | delete inserter; | 
|---|
|  | 159 | delete voidnode_predicate; | 
|---|
|  | 160 | } | 
|---|
|  | 161 |  | 
|---|
|  | 162 | if (successflag) | 
|---|
| [80ca29] | 163 | return Action::state_ptr(UndoState); | 
|---|
| [2db053] | 164 | else | 
|---|
|  | 165 | return Action::failure; | 
|---|
|  | 166 | } | 
|---|
|  | 167 |  | 
|---|
|  | 168 | Action::state_ptr FillSphericalSurfaceAction::performUndo(Action::state_ptr _state) { | 
|---|
| [42b6de] | 169 | FillSphericalSurfaceState *state = assert_cast<FillSphericalSurfaceState*>(_state.get()); | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // remove all created atoms | 
|---|
|  | 172 | RemoveAtomsFromAtomicInfo(state->clonedatoms); | 
|---|
| [80ca29] | 173 | // add the original cluster | 
|---|
|  | 174 | SetAtomsFromAtomicInfo(state->movedatoms); | 
|---|
| [42b6de] | 175 |  | 
|---|
|  | 176 | return Action::state_ptr(_state); | 
|---|
| [2db053] | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | Action::state_ptr FillSphericalSurfaceAction::performRedo(Action::state_ptr _state){ | 
|---|
| [42b6de] | 180 | FillSphericalSurfaceState *state = assert_cast<FillSphericalSurfaceState*>(_state.get()); | 
|---|
|  | 181 |  | 
|---|
| [80ca29] | 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) | 
|---|
| [42b6de] | 191 | return Action::state_ptr(_state); | 
|---|
|  | 192 | else | 
|---|
|  | 193 | return Action::failure; | 
|---|
| [2db053] | 194 | } | 
|---|
|  | 195 |  | 
|---|
|  | 196 | bool FillSphericalSurfaceAction::canUndo() { | 
|---|
| [42b6de] | 197 | return true; | 
|---|
| [2db053] | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | bool FillSphericalSurfaceAction::shouldUndo() { | 
|---|
| [42b6de] | 201 | return true; | 
|---|
| [2db053] | 202 | } | 
|---|
|  | 203 | /** =========== end of function ====================== */ | 
|---|