[a88452] | 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 |
|
---|
[42b6de] | 22 | #include "Actions/UndoRedoHelpers.hpp"
|
---|
[a88452] | 23 | #include "Atom/atom.hpp"
|
---|
[42b6de] | 24 | #include "Atom/AtomicInfo.hpp"
|
---|
[a88452] | 25 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
|
---|
[80ca29] | 26 | #include "Bond/BondInfo.hpp"
|
---|
[a88452] | 27 | #include "CodePatterns/Log.hpp"
|
---|
| 28 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
|
---|
| 29 | #include "Filling/Cluster.hpp"
|
---|
| 30 | #include "Filling/Filler.hpp"
|
---|
[f61f61] | 31 | #include "Filling/Inserter/Inserter.hpp"
|
---|
[896f4a] | 32 | #include "Filling/Inserter/RandomInserter.hpp"
|
---|
[a88452] | 33 | #include "Filling/Mesh/CubeMesh.hpp"
|
---|
[345eda] | 34 | #include "Filling/Predicates/IsInsideSurface_FillPredicate.hpp"
|
---|
[a88452] | 35 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
|
---|
[345eda] | 36 | #include "Filling/Predicates/Ops_FillPredicate.hpp"
|
---|
| 37 | #include "LinkedCell/linkedcell.hpp"
|
---|
| 38 | #include "LinkedCell/PointCloudAdaptor.hpp"
|
---|
| 39 | #include "molecule.hpp"
|
---|
[a88452] | 40 | #include "MoleculeListClass.hpp"
|
---|
| 41 | #include "Parser/FormatParserInterface.hpp"
|
---|
| 42 | #include "Parser/FormatParserStorage.hpp"
|
---|
| 43 | #include "Shapes/BaseShapes.hpp"
|
---|
[345eda] | 44 | #include "Tesselation/tesselation.hpp"
|
---|
| 45 | #include "Tesselation/BoundaryLineSet.hpp"
|
---|
| 46 | #include "Tesselation/BoundaryTriangleSet.hpp"
|
---|
| 47 | #include "Tesselation/CandidateForTesselation.hpp"
|
---|
[a88452] | 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 | // obtain information
|
---|
| 68 | getParametersfromValueStorage();
|
---|
| 69 |
|
---|
[80ca29] | 70 | // get the filler molecule
|
---|
| 71 | std::vector<AtomicInfo> movedatoms;
|
---|
[e9ad43] | 72 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
| 73 | if (molecules.size() != 1) {
|
---|
| 74 | ELOG(1, "No exactly one molecule selected, aborting,");
|
---|
[5a4cbc] | 75 | return Action::failure;
|
---|
| 76 | }
|
---|
[e9ad43] | 77 | molecule *filler = *(molecules.begin());
|
---|
[80ca29] | 78 | for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter)
|
---|
| 79 | movedatoms.push_back( AtomicInfo(*(*iter)) );
|
---|
[e9ad43] | 80 | LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
|
---|
[a88452] | 81 |
|
---|
[345eda] | 82 | // check for selected molecules and create surfaces from them
|
---|
[e9ad43] | 83 | std::vector<atom *> atoms(World::getInstance().getSelectedAtoms());
|
---|
| 84 | FillPredicate * surface_predicate = NULL;
|
---|
| 85 | LinkedCell_deprecated * LC = NULL;
|
---|
| 86 | Tesselation * TesselStruct = NULL;
|
---|
[345eda] | 87 | if (params.SphereRadius != 0.) {
|
---|
[80ca29] | 88 | if ( atoms.size() == 0) {
|
---|
[345eda] | 89 | ELOG(1, "You have given a sphere radius " << params.SphereRadius
|
---|
| 90 | << " != 0, but have not select any molecules.");
|
---|
[80ca29] | 91 | return Action::failure;
|
---|
[345eda] | 92 | }
|
---|
[e9ad43] | 93 | // create adaptor for the selected atoms
|
---|
| 94 | PointCloudAdaptor< std::vector<atom *> > cloud(&atoms, std::string("Selected atoms"));
|
---|
| 95 |
|
---|
| 96 | // create tesselation
|
---|
| 97 | LC = new LinkedCell_deprecated(cloud, 2.*params.SphereRadius);
|
---|
| 98 | TesselStruct = new Tesselation;
|
---|
| 99 | (*TesselStruct)(cloud, params.SphereRadius);
|
---|
| 100 |
|
---|
| 101 | // and create predicate
|
---|
| 102 | surface_predicate = new FillPredicate( IsInsideSurface_FillPredicate( *TesselStruct, *LC ) );
|
---|
[345eda] | 103 | }
|
---|
| 104 |
|
---|
[a88452] | 105 | // create predicate, mesh, and filler
|
---|
[80ca29] | 106 | FillRegularGridState *UndoState = NULL;
|
---|
[68abe5] | 107 | bool successflag = false;
|
---|
[a88452] | 108 | {
|
---|
[e9ad43] | 109 | FillPredicate *voidnode_predicate = new FillPredicate(
|
---|
| 110 | IsVoidNode_FillPredicate(
|
---|
| 111 | Sphere(zeroVec, params.mindistance)
|
---|
| 112 | )
|
---|
| 113 | );
|
---|
[345eda] | 114 | FillPredicate Andpredicate = (*voidnode_predicate);
|
---|
[e9ad43] | 115 | if (surface_predicate != NULL)
|
---|
| 116 | Andpredicate = (Andpredicate) && !(*surface_predicate);
|
---|
[a88452] | 117 | Mesh *mesh = new CubeMesh(params.counts, params.offset, World::getInstance().getDomain().getM());
|
---|
[896f4a] | 118 | Inserter *inserter = new Inserter(
|
---|
| 119 | Inserter::impl_ptr(
|
---|
| 120 | new RandomInserter(
|
---|
| 121 | params.RandAtomDisplacement,
|
---|
| 122 | params.RandMoleculeDisplacement,
|
---|
| 123 | params.DoRotate)
|
---|
| 124 | )
|
---|
| 125 | );
|
---|
[a88452] | 126 |
|
---|
| 127 | // fill
|
---|
[e9ad43] | 128 | {
|
---|
[f61f61] | 129 | Filler *fillerFunction = new Filler(*mesh, Andpredicate, *inserter);
|
---|
[e9ad43] | 130 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingShape()) );
|
---|
| 131 | CopyAtoms_withBonds copyMethod;
|
---|
[42b6de] | 132 | Filler::ClusterVector_t ClonedClusters;
|
---|
| 133 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
|
---|
[e9ad43] | 134 | delete fillerFunction;
|
---|
[42b6de] | 135 |
|
---|
[80ca29] | 136 | // append each cluster's atoms to clonedatoms (however not selected ones)
|
---|
| 137 | std::vector<const atom *> clonedatoms;
|
---|
| 138 | std::vector<AtomicInfo> clonedatominfos;
|
---|
[42b6de] | 139 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
|
---|
| 140 | iter != ClonedClusters.end(); ++iter) {
|
---|
| 141 | const AtomIdSet &atoms = (*iter)->getAtomIds();
|
---|
| 142 | clonedatoms.reserve(clonedatoms.size()+atoms.size());
|
---|
| 143 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
|
---|
[80ca29] | 144 | if (!filler->containsAtom(*atomiter)) {
|
---|
| 145 | clonedatoms.push_back( *atomiter );
|
---|
| 146 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
|
---|
| 147 | }
|
---|
[42b6de] | 148 | }
|
---|
[80ca29] | 149 | std::vector< BondInfo > clonedbonds;
|
---|
| 150 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
|
---|
| 151 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms.");
|
---|
| 152 |
|
---|
[42b6de] | 153 | if (!successflag) {
|
---|
[80ca29] | 154 | ELOG(1, "Insertion failed, removing inserted clusters, translating original one back");
|
---|
| 155 | RemoveAtomsFromAtomicInfo(clonedatominfos);
|
---|
[42b6de] | 156 | clonedatoms.clear();
|
---|
[80ca29] | 157 | SetAtomsFromAtomicInfo(movedatoms);
|
---|
| 158 | } else {
|
---|
| 159 | std::vector<Vector> MovedToVector(filler->size(), zeroVec);
|
---|
| 160 | std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
|
---|
| 161 | boost::bind(&AtomInfo::getPosition, _1) );
|
---|
| 162 | UndoState = new FillRegularGridState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
|
---|
[42b6de] | 163 | }
|
---|
[e9ad43] | 164 | }
|
---|
[a88452] | 165 |
|
---|
| 166 | // remove
|
---|
| 167 | delete mesh;
|
---|
[f61f61] | 168 | delete inserter;
|
---|
[345eda] | 169 | delete voidnode_predicate;
|
---|
[e9ad43] | 170 | delete surface_predicate;
|
---|
| 171 | delete LC;
|
---|
| 172 | delete TesselStruct;
|
---|
[a88452] | 173 | }
|
---|
| 174 |
|
---|
[68abe5] | 175 | if (successflag)
|
---|
[80ca29] | 176 | return Action::state_ptr(UndoState);
|
---|
[68abe5] | 177 | else
|
---|
| 178 | return Action::failure;
|
---|
[a88452] | 179 | }
|
---|
| 180 |
|
---|
| 181 | Action::state_ptr FillRegularGridAction::performUndo(Action::state_ptr _state) {
|
---|
[42b6de] | 182 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
| 183 |
|
---|
| 184 | // remove all created atoms
|
---|
| 185 | RemoveAtomsFromAtomicInfo(state->clonedatoms);
|
---|
[80ca29] | 186 | // add the original cluster
|
---|
| 187 | SetAtomsFromAtomicInfo(state->movedatoms);
|
---|
[42b6de] | 188 |
|
---|
| 189 | return Action::state_ptr(_state);
|
---|
[a88452] | 190 | }
|
---|
| 191 |
|
---|
| 192 | Action::state_ptr FillRegularGridAction::performRedo(Action::state_ptr _state){
|
---|
[42b6de] | 193 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
[a88452] | 194 |
|
---|
[80ca29] | 195 | // place filler cluster again at new spot
|
---|
| 196 | ResetAtomPosition(state->movedatoms, state->MovedToVector);
|
---|
| 197 |
|
---|
| 198 | // re-create all clusters
|
---|
| 199 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
|
---|
| 200 |
|
---|
| 201 | // re-create the bonds
|
---|
| 202 | if (statusflag)
|
---|
| 203 | AddBondsFromBondInfo(state->clonedbonds);
|
---|
| 204 | if (statusflag)
|
---|
[42b6de] | 205 | return Action::state_ptr(_state);
|
---|
| 206 | else
|
---|
| 207 | return Action::failure;
|
---|
[a88452] | 208 | }
|
---|
| 209 |
|
---|
| 210 | bool FillRegularGridAction::canUndo() {
|
---|
[42b6de] | 211 | return true;
|
---|
[a88452] | 212 | }
|
---|
| 213 |
|
---|
| 214 | bool FillRegularGridAction::shouldUndo() {
|
---|
[42b6de] | 215 | return true;
|
---|
[a88452] | 216 | }
|
---|
| 217 | /** =========== end of function ====================== */
|
---|