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