| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * FillRegularGridAction.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Jan 12, 2012 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "Actions/UndoRedoHelpers.hpp" | 
|---|
| 23 | #include "Atom/atom.hpp" | 
|---|
| 24 | #include "Atom/AtomicInfo.hpp" | 
|---|
| 25 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp" | 
|---|
| 26 | #include "Bond/BondInfo.hpp" | 
|---|
| 27 | #include "CodePatterns/Log.hpp" | 
|---|
| 28 | #include "Descriptors/MoleculeOrderDescriptor.hpp" | 
|---|
| 29 | #include "Filling/Cluster.hpp" | 
|---|
| 30 | #include "Filling/Filler.hpp" | 
|---|
| 31 | #include "Filling/Inserter/Inserter.hpp" | 
|---|
| 32 | #include "Filling/Inserter/RandomInserter.hpp" | 
|---|
| 33 | #include "Filling/Mesh/CubeMesh.hpp" | 
|---|
| 34 | #include "Filling/Predicates/IsInsideSurface_FillPredicate.hpp" | 
|---|
| 35 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp" | 
|---|
| 36 | #include "Filling/Predicates/Ops_FillPredicate.hpp" | 
|---|
| 37 | #include "LinkedCell/linkedcell.hpp" | 
|---|
| 38 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 39 | #include "molecule.hpp" | 
|---|
| 40 | #include "MoleculeListClass.hpp" | 
|---|
| 41 | #include "Parser/FormatParserInterface.hpp" | 
|---|
| 42 | #include "Parser/FormatParserStorage.hpp" | 
|---|
| 43 | #include "Shapes/BaseShapes.hpp" | 
|---|
| 44 | #include "Tesselation/tesselation.hpp" | 
|---|
| 45 | #include "Tesselation/BoundaryLineSet.hpp" | 
|---|
| 46 | #include "Tesselation/BoundaryTriangleSet.hpp" | 
|---|
| 47 | #include "Tesselation/CandidateForTesselation.hpp" | 
|---|
| 48 | #include "World.hpp" | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | #include <algorithm> | 
|---|
| 52 | #include <iostream> | 
|---|
| 53 | #include <string> | 
|---|
| 54 | #include <vector> | 
|---|
| 55 |  | 
|---|
| 56 | #include "Actions/FillAction/FillRegularGridAction.hpp" | 
|---|
| 57 |  | 
|---|
| 58 | using namespace MoleCuilder; | 
|---|
| 59 |  | 
|---|
| 60 | // and construct the stuff | 
|---|
| 61 | #include "FillRegularGridAction.def" | 
|---|
| 62 | #include "Action_impl_pre.hpp" | 
|---|
| 63 | /** =========== define the function ====================== */ | 
|---|
| 64 | Action::state_ptr FillRegularGridAction::performCall() { | 
|---|
| 65 | typedef std::vector<atom*> AtomVector; | 
|---|
| 66 |  | 
|---|
| 67 | // get the filler molecule | 
|---|
| 68 | std::vector<AtomicInfo> movedatoms; | 
|---|
| 69 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules(); | 
|---|
| 70 | if (molecules.size() != 1) { | 
|---|
| 71 | ELOG(1, "No exactly one molecule selected, aborting,"); | 
|---|
| 72 | return Action::failure; | 
|---|
| 73 | } | 
|---|
| 74 | molecule *filler = *(molecules.begin()); | 
|---|
| 75 | for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter) | 
|---|
| 76 | movedatoms.push_back( AtomicInfo(*(*iter)) ); | 
|---|
| 77 | LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms."); | 
|---|
| 78 |  | 
|---|
| 79 | // check for selected molecules and create surfaces from them | 
|---|
| 80 | std::vector<atom *> atoms(World::getInstance().getSelectedAtoms()); | 
|---|
| 81 | FillPredicate * surface_predicate = NULL; | 
|---|
| 82 | LinkedCell_deprecated * LC = NULL; | 
|---|
| 83 | Tesselation * TesselStruct = NULL; | 
|---|
| 84 | if (params.SphereRadius != 0.) { | 
|---|
| 85 | if ( atoms.size() == 0) { | 
|---|
| 86 | ELOG(1, "You have given a sphere radius " << params.SphereRadius | 
|---|
| 87 | << " != 0, but have not select any molecules."); | 
|---|
| 88 | return Action::failure; | 
|---|
| 89 | } | 
|---|
| 90 | // create adaptor for the selected atoms | 
|---|
| 91 | PointCloudAdaptor< std::vector<atom *> > cloud(&atoms, std::string("Selected atoms")); | 
|---|
| 92 |  | 
|---|
| 93 | // create tesselation | 
|---|
| 94 | LC = new LinkedCell_deprecated(cloud, 2.*params.SphereRadius); | 
|---|
| 95 | TesselStruct = new Tesselation; | 
|---|
| 96 | (*TesselStruct)(cloud, params.SphereRadius); | 
|---|
| 97 |  | 
|---|
| 98 | // and create predicate | 
|---|
| 99 | surface_predicate = new FillPredicate( IsInsideSurface_FillPredicate( *TesselStruct, *LC ) ); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | // create predicate, mesh, and filler | 
|---|
| 103 | FillRegularGridState *UndoState = NULL; | 
|---|
| 104 | bool successflag = false; | 
|---|
| 105 | { | 
|---|
| 106 | FillPredicate *voidnode_predicate = new FillPredicate( | 
|---|
| 107 | IsVoidNode_FillPredicate( | 
|---|
| 108 | Sphere(zeroVec, params.mindistance) | 
|---|
| 109 | ) | 
|---|
| 110 | ); | 
|---|
| 111 | FillPredicate Andpredicate = (*voidnode_predicate); | 
|---|
| 112 | if (surface_predicate != NULL) | 
|---|
| 113 | Andpredicate = (Andpredicate) && !(*surface_predicate); | 
|---|
| 114 | Mesh *mesh = new CubeMesh(params.counts, params.offset, World::getInstance().getDomain().getM()); | 
|---|
| 115 | Inserter *inserter = new Inserter( | 
|---|
| 116 | Inserter::impl_ptr( | 
|---|
| 117 | new RandomInserter( | 
|---|
| 118 | params.RandAtomDisplacement, | 
|---|
| 119 | params.RandMoleculeDisplacement, | 
|---|
| 120 | params.DoRotate) | 
|---|
| 121 | ) | 
|---|
| 122 | ); | 
|---|
| 123 |  | 
|---|
| 124 | // fill | 
|---|
| 125 | { | 
|---|
| 126 | Filler *fillerFunction = new Filler(*mesh, Andpredicate, *inserter); | 
|---|
| 127 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingShape()) ); | 
|---|
| 128 | CopyAtoms_withBonds copyMethod; | 
|---|
| 129 | Filler::ClusterVector_t ClonedClusters; | 
|---|
| 130 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters); | 
|---|
| 131 | delete fillerFunction; | 
|---|
| 132 |  | 
|---|
| 133 | // append each cluster's atoms to clonedatoms (however not selected ones) | 
|---|
| 134 | std::vector<const atom *> clonedatoms; | 
|---|
| 135 | std::vector<AtomicInfo> clonedatominfos; | 
|---|
| 136 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin(); | 
|---|
| 137 | iter != ClonedClusters.end(); ++iter) { | 
|---|
| 138 | const AtomIdSet &atoms = (*iter)->getAtomIds(); | 
|---|
| 139 | clonedatoms.reserve(clonedatoms.size()+atoms.size()); | 
|---|
| 140 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter) | 
|---|
| 141 | if (!filler->containsAtom(*atomiter)) { | 
|---|
| 142 | clonedatoms.push_back( *atomiter ); | 
|---|
| 143 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) ); | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 | std::vector< BondInfo > clonedbonds; | 
|---|
| 147 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds); | 
|---|
| 148 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms."); | 
|---|
| 149 |  | 
|---|
| 150 | if (!successflag) { | 
|---|
| 151 | ELOG(1, "Insertion failed, removing inserted clusters, translating original one back"); | 
|---|
| 152 | RemoveAtomsFromAtomicInfo(clonedatominfos); | 
|---|
| 153 | clonedatoms.clear(); | 
|---|
| 154 | SetAtomsFromAtomicInfo(movedatoms); | 
|---|
| 155 | } else { | 
|---|
| 156 | std::vector<Vector> MovedToVector(filler->size(), zeroVec); | 
|---|
| 157 | std::transform(filler->begin(), filler->end(), MovedToVector.begin(), | 
|---|
| 158 | boost::bind(&AtomInfo::getPosition, _1) ); | 
|---|
| 159 | UndoState = new FillRegularGridState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params); | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | // remove | 
|---|
| 164 | delete mesh; | 
|---|
| 165 | delete inserter; | 
|---|
| 166 | delete voidnode_predicate; | 
|---|
| 167 | delete surface_predicate; | 
|---|
| 168 | delete LC; | 
|---|
| 169 | delete TesselStruct; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | if (successflag) | 
|---|
| 173 | return Action::state_ptr(UndoState); | 
|---|
| 174 | else | 
|---|
| 175 | return Action::failure; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | Action::state_ptr FillRegularGridAction::performUndo(Action::state_ptr _state) { | 
|---|
| 179 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get()); | 
|---|
| 180 |  | 
|---|
| 181 | // remove all created atoms | 
|---|
| 182 | RemoveAtomsFromAtomicInfo(state->clonedatoms); | 
|---|
| 183 | // add the original cluster | 
|---|
| 184 | SetAtomsFromAtomicInfo(state->movedatoms); | 
|---|
| 185 |  | 
|---|
| 186 | return Action::state_ptr(_state); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | Action::state_ptr FillRegularGridAction::performRedo(Action::state_ptr _state){ | 
|---|
| 190 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get()); | 
|---|
| 191 |  | 
|---|
| 192 | // place filler cluster again at new spot | 
|---|
| 193 | ResetAtomPosition(state->movedatoms, state->MovedToVector); | 
|---|
| 194 |  | 
|---|
| 195 | // re-create all clusters | 
|---|
| 196 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms); | 
|---|
| 197 |  | 
|---|
| 198 | // re-create the bonds | 
|---|
| 199 | if (statusflag) | 
|---|
| 200 | AddBondsFromBondInfo(state->clonedbonds); | 
|---|
| 201 | if (statusflag) | 
|---|
| 202 | return Action::state_ptr(_state); | 
|---|
| 203 | else | 
|---|
| 204 | return Action::failure; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | bool FillRegularGridAction::canUndo() { | 
|---|
| 208 | return true; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | bool FillRegularGridAction::shouldUndo() { | 
|---|
| 212 | return true; | 
|---|
| 213 | } | 
|---|
| 214 | /** =========== end of function ====================== */ | 
|---|