| [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 | /* | 
|---|
| [09eaac] | 24 | * FillSurfaceAction.cpp | 
|---|
| [2db053] | 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" | 
|---|
| [df9f20] | 48 | #include "Filling/Preparators/ShapeSurfaceFillerPreparator.hpp" | 
|---|
| [cb7676] | 49 | #include "molecule.hpp" | 
|---|
| [2db053] | 50 | #include "Shapes/BaseShapes.hpp" | 
|---|
| [842aac] | 51 | #include "Shapes/ShapeRegistry.hpp" | 
|---|
| [df9f20] | 52 | #include "Shapes/ShapeType.hpp" | 
|---|
| [2db053] | 53 | #include "World.hpp" | 
|---|
|  | 54 |  | 
|---|
|  | 55 |  | 
|---|
|  | 56 | #include <algorithm> | 
|---|
|  | 57 | #include <boost/foreach.hpp> | 
|---|
|  | 58 | #include <boost/function.hpp> | 
|---|
|  | 59 | #include <iostream> | 
|---|
|  | 60 | #include <string> | 
|---|
|  | 61 | #include <vector> | 
|---|
|  | 62 |  | 
|---|
| [09eaac] | 63 | #include "Actions/FillAction/FillSurfaceAction.hpp" | 
|---|
| [2db053] | 64 |  | 
|---|
|  | 65 | using namespace MoleCuilder; | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // and construct the stuff | 
|---|
| [09eaac] | 68 | #include "FillSurfaceAction.def" | 
|---|
| [2db053] | 69 | #include "Action_impl_pre.hpp" | 
|---|
|  | 70 | /** =========== define the function ====================== */ | 
|---|
| [b5b01e] | 71 | ActionState::ptr FillSurfaceAction::performCall() { | 
|---|
| [80ca29] | 72 | // get the filler molecule | 
|---|
| [cb7676] | 73 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules(); | 
|---|
| [80ca29] | 74 | std::vector<AtomicInfo> movedatoms; | 
|---|
| [cb7676] | 75 | if (molecules.size() != 1) { | 
|---|
| [26b4d62] | 76 | STATUS("No exactly one molecule selected, aborting,"); | 
|---|
| [2db053] | 77 | return Action::failure; | 
|---|
|  | 78 | } | 
|---|
| [cb7676] | 79 | molecule *filler = *(molecules.begin()); | 
|---|
| [80ca29] | 80 | for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter) | 
|---|
|  | 81 | movedatoms.push_back( AtomicInfo(*(*iter)) ); | 
|---|
| [cb7676] | 82 | LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms."); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | // center filler's tip at origin | 
|---|
| [833b15] | 85 | filler->CenterEdge(); | 
|---|
| [cb7676] | 86 |  | 
|---|
|  | 87 | // determine center with respect to alignment axis | 
|---|
|  | 88 | Vector sum = zeroVec; | 
|---|
|  | 89 | for (molecule::iterator it2=filler->begin();it2 !=filler->end();++it2) { | 
|---|
| [f10b0c] | 90 | const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second; | 
|---|
| [cb7676] | 91 | sum += helper; | 
|---|
|  | 92 | } | 
|---|
|  | 93 | sum *= 1./filler->size(); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | // translate molecule's closest atom to origin (such that is resides on the filler spot) | 
|---|
|  | 96 | LOG(1, "DEBUG: molecule is off Alignment axis by " << sum << ", shifting ..."); | 
|---|
|  | 97 | { | 
|---|
|  | 98 | Vector translater = -1.*sum; | 
|---|
| [833b15] | 99 | filler->Translate(translater); | 
|---|
| [2db053] | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [df9f20] | 102 | // prepare the filler preparator | 
|---|
|  | 103 | if (ShapeRegistry::getInstance().countSelectedShapes() != (size_t)1) { | 
|---|
|  | 104 | STATUS("Not exactly one shape selected."); | 
|---|
|  | 105 | return Action::failure; | 
|---|
|  | 106 | } | 
|---|
|  | 107 | const std::vector<Shape*> shapes = ShapeRegistry::getInstance().getSelectedShapes(); | 
|---|
|  | 108 | const Shape &shape = **shapes.begin(); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // hard check whether shape is of allowed type, not all are implemented | 
|---|
|  | 111 | // but these only fail with an assertion, hence not with disable-debug | 
|---|
|  | 112 | switch (shape.getType()) { | 
|---|
|  | 113 | case NowhereType: | 
|---|
|  | 114 | case EverywhereType: | 
|---|
|  | 115 | STATUS("The shape type "+toString(shape.getType())+" is currently not supported."); | 
|---|
|  | 116 | return Action::failure; | 
|---|
|  | 117 | break; | 
|---|
|  | 118 | default: | 
|---|
|  | 119 | break; | 
|---|
|  | 120 | } | 
|---|
| [842aac] | 121 |  | 
|---|
| [df9f20] | 122 | ShapeSurfaceFillerPreparator filler_preparator(filler); | 
|---|
|  | 123 | if (params.SphereRadius.get() != 0.) { | 
|---|
|  | 124 | if (World::getInstance().beginAtomSelection() == World::getInstance().endAtomSelection()) { | 
|---|
|  | 125 | STATUS("You have given a sphere radius "+toString(params.SphereRadius.get()) | 
|---|
|  | 126 | +" != 0, but have not select any atoms."); | 
|---|
| [842aac] | 127 | return Action::failure; | 
|---|
|  | 128 | } | 
|---|
| [df9f20] | 129 | std::vector<atom*> atoms(World::getInstance().getSelectedAtoms()); | 
|---|
|  | 130 | filler_preparator.addSurfacePredicate( | 
|---|
|  | 131 | params.SphereRadius.get(), | 
|---|
|  | 132 | atoms); | 
|---|
|  | 133 | } | 
|---|
|  | 134 | filler_preparator.addVoidPredicate(params.mindistance.get()); | 
|---|
|  | 135 | filler_preparator.addSurfaceRandomInserter( | 
|---|
|  | 136 | shape, | 
|---|
|  | 137 | params.AlignedAxis.get(), | 
|---|
|  | 138 | params.RandAtomDisplacement.get(), | 
|---|
|  | 139 | params.RandMoleculeDisplacement.get()); | 
|---|
|  | 140 | filler_preparator.addShapeMesh( | 
|---|
|  | 141 | shape, | 
|---|
|  | 142 | params.N.get()); | 
|---|
|  | 143 | if (!filler_preparator()) { | 
|---|
|  | 144 | STATUS("Filler was not fully constructed."); | 
|---|
|  | 145 | return Action::failure; | 
|---|
|  | 146 | } | 
|---|
| [09eaac] | 147 |  | 
|---|
| [df9f20] | 148 | // use filler | 
|---|
|  | 149 | bool successflag = false; | 
|---|
|  | 150 | FillSurfaceState *UndoState = NULL; | 
|---|
|  | 151 | { | 
|---|
| [2db053] | 152 | // fill | 
|---|
|  | 153 | { | 
|---|
| [df9f20] | 154 | Filler *fillerFunction = filler_preparator.obtainFiller(); | 
|---|
| [aeb694] | 155 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) ); | 
|---|
| [cb7676] | 156 | CopyAtoms_withBonds copyMethod; | 
|---|
| [42b6de] | 157 | Filler::ClusterVector_t ClonedClusters; | 
|---|
|  | 158 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters); | 
|---|
| [2db053] | 159 | delete fillerFunction; | 
|---|
| [42b6de] | 160 |  | 
|---|
| [80ca29] | 161 | // append each cluster's atoms to clonedatoms (however not selected ones) | 
|---|
|  | 162 | std::vector<const atom *> clonedatoms; | 
|---|
|  | 163 | std::vector<AtomicInfo> clonedatominfos; | 
|---|
| [42b6de] | 164 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin(); | 
|---|
|  | 165 | iter != ClonedClusters.end(); ++iter) { | 
|---|
|  | 166 | const AtomIdSet &atoms = (*iter)->getAtomIds(); | 
|---|
|  | 167 | clonedatoms.reserve(clonedatoms.size()+atoms.size()); | 
|---|
|  | 168 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter) | 
|---|
| [80ca29] | 169 | if (!filler->containsAtom(*atomiter)) { | 
|---|
|  | 170 | clonedatoms.push_back( *atomiter ); | 
|---|
|  | 171 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) ); | 
|---|
|  | 172 | } | 
|---|
| [42b6de] | 173 | } | 
|---|
| [80ca29] | 174 | std::vector< BondInfo > clonedbonds; | 
|---|
|  | 175 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds); | 
|---|
|  | 176 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with " | 
|---|
|  | 177 | << clonedbonds.size()/2 << " bonds."); | 
|---|
|  | 178 |  | 
|---|
| [42b6de] | 179 | if (!successflag) { | 
|---|
| [26b4d62] | 180 | STATUS("Insertion failed, removing inserted clusters, translating original one back"); | 
|---|
| [80ca29] | 181 | RemoveAtomsFromAtomicInfo(clonedatominfos); | 
|---|
| [42b6de] | 182 | clonedatoms.clear(); | 
|---|
| [80ca29] | 183 | SetAtomsFromAtomicInfo(movedatoms); | 
|---|
|  | 184 | } else { | 
|---|
|  | 185 | std::vector<Vector> MovedToVector(filler->size(), zeroVec); | 
|---|
|  | 186 | std::transform(filler->begin(), filler->end(), MovedToVector.begin(), | 
|---|
|  | 187 | boost::bind(&AtomInfo::getPosition, _1) ); | 
|---|
| [09eaac] | 188 | UndoState = new FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params); | 
|---|
| [42b6de] | 189 | } | 
|---|
| [2db053] | 190 | } | 
|---|
|  | 191 | } | 
|---|
|  | 192 |  | 
|---|
|  | 193 | if (successflag) | 
|---|
| [b5b01e] | 194 | return ActionState::ptr(UndoState); | 
|---|
| [2db053] | 195 | else | 
|---|
|  | 196 | return Action::failure; | 
|---|
|  | 197 | } | 
|---|
|  | 198 |  | 
|---|
| [b5b01e] | 199 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) { | 
|---|
| [09eaac] | 200 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get()); | 
|---|
| [42b6de] | 201 |  | 
|---|
|  | 202 | // remove all created atoms | 
|---|
|  | 203 | RemoveAtomsFromAtomicInfo(state->clonedatoms); | 
|---|
| [80ca29] | 204 | // add the original cluster | 
|---|
|  | 205 | SetAtomsFromAtomicInfo(state->movedatoms); | 
|---|
| [42b6de] | 206 |  | 
|---|
| [b5b01e] | 207 | return ActionState::ptr(_state); | 
|---|
| [2db053] | 208 | } | 
|---|
|  | 209 |  | 
|---|
| [b5b01e] | 210 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){ | 
|---|
| [09eaac] | 211 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get()); | 
|---|
| [42b6de] | 212 |  | 
|---|
| [80ca29] | 213 | // place filler cluster again at new spot | 
|---|
|  | 214 | ResetAtomPosition(state->movedatoms, state->MovedToVector); | 
|---|
|  | 215 |  | 
|---|
|  | 216 | // re-create all clusters | 
|---|
|  | 217 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms); | 
|---|
|  | 218 |  | 
|---|
|  | 219 | // re-create the bonds | 
|---|
|  | 220 | statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds); | 
|---|
|  | 221 | if (statusflag) | 
|---|
| [b5b01e] | 222 | return ActionState::ptr(_state); | 
|---|
| [26b4d62] | 223 | else { | 
|---|
|  | 224 | STATUS("Failed to re-added filled in atoms."); | 
|---|
| [42b6de] | 225 | return Action::failure; | 
|---|
| [26b4d62] | 226 | } | 
|---|
| [2db053] | 227 | } | 
|---|
|  | 228 |  | 
|---|
| [09eaac] | 229 | bool FillSurfaceAction::canUndo() { | 
|---|
| [42b6de] | 230 | return true; | 
|---|
| [2db053] | 231 | } | 
|---|
|  | 232 |  | 
|---|
| [09eaac] | 233 | bool FillSurfaceAction::shouldUndo() { | 
|---|
| [42b6de] | 234 | return true; | 
|---|
| [2db053] | 235 | } | 
|---|
|  | 236 | /** =========== end of function ====================== */ | 
|---|