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 | * FillRegularGridAction.cpp
|
---|
25 | *
|
---|
26 | * Created on: Jan 12, 2012
|
---|
27 | * Author: heber
|
---|
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 "Bond/BondInfo.hpp"
|
---|
42 | #include "CodePatterns/Log.hpp"
|
---|
43 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
|
---|
44 | #include "Filling/Cluster.hpp"
|
---|
45 | #include "Filling/Filler.hpp"
|
---|
46 | #include "Filling/Preparators/BoxFillerPreparator.hpp"
|
---|
47 | #include "molecule.hpp"
|
---|
48 | #include "MoleculeListClass.hpp"
|
---|
49 | #include "Parser/FormatParserInterface.hpp"
|
---|
50 | #include "Parser/FormatParserStorage.hpp"
|
---|
51 | #include "World.hpp"
|
---|
52 |
|
---|
53 | #include <algorithm>
|
---|
54 | #include <iostream>
|
---|
55 | #include <string>
|
---|
56 | #include <vector>
|
---|
57 |
|
---|
58 | #include "Actions/FillAction/FillRegularGridAction.hpp"
|
---|
59 |
|
---|
60 | using namespace MoleCuilder;
|
---|
61 |
|
---|
62 | // and construct the stuff
|
---|
63 | #include "FillRegularGridAction.def"
|
---|
64 | #include "Action_impl_pre.hpp"
|
---|
65 | /** =========== define the function ====================== */
|
---|
66 | ActionState::ptr FillRegularGridAction::performCall() {
|
---|
67 | typedef std::vector<atom*> AtomVector;
|
---|
68 |
|
---|
69 | // get the filler molecule
|
---|
70 | std::vector<AtomicInfo> movedatoms;
|
---|
71 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
72 | if (molecules.size() != 1) {
|
---|
73 | STATUS("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 | filler->CenterEdge();
|
---|
83 |
|
---|
84 | // prepare the filler preparator
|
---|
85 | BoxFillerPreparator filler_preparator(filler);
|
---|
86 | if (params.SphereRadius.get() != 0.) {
|
---|
87 | if (World::getInstance().beginAtomSelection() == World::getInstance().endAtomSelection()) {
|
---|
88 | STATUS("You have given a sphere radius "+toString(params.SphereRadius.get())
|
---|
89 | +" != 0, but have not select any atoms.");
|
---|
90 | return Action::failure;
|
---|
91 | }
|
---|
92 | std::vector<atom*> atoms(World::getInstance().getSelectedAtoms());
|
---|
93 | filler_preparator.addSurfacePredicate(
|
---|
94 | params.SphereRadius.get(),
|
---|
95 | atoms);
|
---|
96 | }
|
---|
97 | filler_preparator.addVoidPredicate(params.mindistance.get());
|
---|
98 | filler_preparator.addRandomInserter(
|
---|
99 | params.RandAtomDisplacement.get(),
|
---|
100 | params.RandMoleculeDisplacement.get(),
|
---|
101 | params.DoRotate.get());
|
---|
102 | filler_preparator.addCubeMesh(
|
---|
103 | params.counts.get(),
|
---|
104 | params.offset.get(),
|
---|
105 | World::getInstance().getDomain().getM());
|
---|
106 | if (!filler_preparator()) {
|
---|
107 | STATUS("Filler was not fully constructed.");
|
---|
108 | return Action::failure;
|
---|
109 | }
|
---|
110 |
|
---|
111 | // use filler
|
---|
112 | bool successflag = false;
|
---|
113 | FillRegularGridState *UndoState = NULL;
|
---|
114 | {
|
---|
115 | // fill
|
---|
116 | Filler *fillerFunction = filler_preparator.obtainFiller();
|
---|
117 | // TODO: When molecule::getBoundingSphere() does not use a sphere anymore,
|
---|
118 | // we need to check whether we rotate the molecule randomly. For this to
|
---|
119 | // work we need a sphere!
|
---|
120 | const Shape s = filler->getBoundingSphere(params.RandAtomDisplacement.get());
|
---|
121 | ClusterInterface::Cluster_impl cluster( new Cluster(filler->getAtomIds(), s) );
|
---|
122 | CopyAtoms_withBonds copyMethod;
|
---|
123 | Filler::ClusterVector_t ClonedClusters;
|
---|
124 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
|
---|
125 | delete fillerFunction;
|
---|
126 |
|
---|
127 | // append each cluster's atoms to clonedatoms (however not selected ones)
|
---|
128 | std::vector<const atom *> clonedatoms;
|
---|
129 | std::vector<AtomicInfo> clonedatominfos;
|
---|
130 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
|
---|
131 | iter != ClonedClusters.end(); ++iter) {
|
---|
132 | const AtomIdSet &atoms = (*iter)->getAtomIds();
|
---|
133 | clonedatoms.reserve(clonedatoms.size()+atoms.size());
|
---|
134 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
|
---|
135 | if (!filler->containsAtom(*atomiter)) {
|
---|
136 | clonedatoms.push_back( *atomiter );
|
---|
137 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
|
---|
138 | }
|
---|
139 | }
|
---|
140 | std::vector< BondInfo > clonedbonds;
|
---|
141 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
|
---|
142 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms.");
|
---|
143 |
|
---|
144 | if (!successflag) {
|
---|
145 | STATUS("Insertion failed, removing inserted clusters, translating original one back");
|
---|
146 | RemoveAtomsFromAtomicInfo(clonedatominfos);
|
---|
147 | clonedatoms.clear();
|
---|
148 | SetAtomsFromAtomicInfo(movedatoms);
|
---|
149 | } else {
|
---|
150 | std::vector<Vector> MovedToVector(filler->size(), zeroVec);
|
---|
151 | std::transform(filler->begin(), filler->end(), MovedToVector.begin(),
|
---|
152 | boost::bind(&AtomInfo::getPosition, _1) );
|
---|
153 | UndoState = new FillRegularGridState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (successflag)
|
---|
158 | return ActionState::ptr(UndoState);
|
---|
159 | else {
|
---|
160 | return Action::failure;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | ActionState::ptr FillRegularGridAction::performUndo(ActionState::ptr _state) {
|
---|
165 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
166 |
|
---|
167 | // remove all created atoms
|
---|
168 | RemoveAtomsFromAtomicInfo(state->clonedatoms);
|
---|
169 | // add the original cluster
|
---|
170 | SetAtomsFromAtomicInfo(state->movedatoms);
|
---|
171 |
|
---|
172 | return ActionState::ptr(_state);
|
---|
173 | }
|
---|
174 |
|
---|
175 | ActionState::ptr FillRegularGridAction::performRedo(ActionState::ptr _state){
|
---|
176 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
177 |
|
---|
178 | // place filler cluster again at new spot
|
---|
179 | ResetAtomPosition(state->movedatoms, state->MovedToVector);
|
---|
180 |
|
---|
181 | // re-create all clusters
|
---|
182 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
|
---|
183 |
|
---|
184 | // re-create the bonds
|
---|
185 | if (statusflag)
|
---|
186 | AddBondsFromBondInfo(state->clonedbonds);
|
---|
187 | if (statusflag)
|
---|
188 | return ActionState::ptr(_state);
|
---|
189 | else {
|
---|
190 | STATUS("Failed re-adding filled in atoms.");
|
---|
191 | return Action::failure;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | bool FillRegularGridAction::canUndo() {
|
---|
196 | return true;
|
---|
197 | }
|
---|
198 |
|
---|
199 | bool FillRegularGridAction::shouldUndo() {
|
---|
200 | return true;
|
---|
201 | }
|
---|
202 | /** =========== end of function ====================== */
|
---|