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 "molecule.hpp"
|
---|
49 | #include "Shapes/BaseShapes.hpp"
|
---|
50 | #include "Shapes/ShapeRegistry.hpp"
|
---|
51 | #include "World.hpp"
|
---|
52 |
|
---|
53 |
|
---|
54 | #include <algorithm>
|
---|
55 | #include <boost/foreach.hpp>
|
---|
56 | #include <boost/function.hpp>
|
---|
57 | #include <iostream>
|
---|
58 | #include <string>
|
---|
59 | #include <vector>
|
---|
60 |
|
---|
61 | #include "Actions/FillAction/FillSurfaceAction.hpp"
|
---|
62 |
|
---|
63 | using namespace MoleCuilder;
|
---|
64 |
|
---|
65 | // and construct the stuff
|
---|
66 | #include "FillSurfaceAction.def"
|
---|
67 | #include "Action_impl_pre.hpp"
|
---|
68 | /** =========== define the function ====================== */
|
---|
69 | ActionState::ptr FillSurfaceAction::performCall() {
|
---|
70 | // get the filler molecule
|
---|
71 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
72 | std::vector<AtomicInfo> movedatoms;
|
---|
73 | if (molecules.size() != 1) {
|
---|
74 | STATUS("No exactly one molecule selected, aborting,");
|
---|
75 | return Action::failure;
|
---|
76 | }
|
---|
77 | molecule *filler = *(molecules.begin());
|
---|
78 | for(molecule::const_iterator iter = filler->begin(); iter != filler->end(); ++iter)
|
---|
79 | movedatoms.push_back( AtomicInfo(*(*iter)) );
|
---|
80 | LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
|
---|
81 |
|
---|
82 | // center filler's tip at origin
|
---|
83 | filler->CenterEdge();
|
---|
84 |
|
---|
85 | // determine center with respect to alignment axis
|
---|
86 | Vector sum = zeroVec;
|
---|
87 | for (molecule::iterator it2=filler->begin();it2 !=filler->end();++it2) {
|
---|
88 | const Vector helper = (**it2).getPosition().partition(params.AlignedAxis.get()).second;
|
---|
89 | sum += helper;
|
---|
90 | }
|
---|
91 | sum *= 1./filler->size();
|
---|
92 |
|
---|
93 | // translate molecule's closest atom to origin (such that is resides on the filler spot)
|
---|
94 | LOG(1, "DEBUG: molecule is off Alignment axis by " << sum << ", shifting ...");
|
---|
95 | {
|
---|
96 | Vector translater = -1.*sum;
|
---|
97 | filler->Translate(translater);
|
---|
98 | }
|
---|
99 |
|
---|
100 | // create predicate, mesh, and filler
|
---|
101 | FillSurfaceState *UndoState = NULL;
|
---|
102 | bool successflag = false;
|
---|
103 | {
|
---|
104 | FillPredicate *voidnode_predicate = new FillPredicate(
|
---|
105 | IsVoidNode_FillPredicate(
|
---|
106 | Sphere(zeroVec, params.mindistance.get())
|
---|
107 | )
|
---|
108 | );
|
---|
109 |
|
---|
110 |
|
---|
111 | std::vector<Shape*> selectedShapes = ShapeRegistry::getInstance().getSelectedShapes();
|
---|
112 | if (selectedShapes.size() != 1){
|
---|
113 | STATUS("There has to be exactly 1 selected shape.");
|
---|
114 | return Action::failure;
|
---|
115 | }
|
---|
116 |
|
---|
117 | boost::function<const NodeSet ()> func =
|
---|
118 | boost::bind(&Shape::getHomogeneousPointsOnSurface, boost::ref(*selectedShapes[0]), params.N.get());
|
---|
119 | Mesh *mesh = new MeshAdaptor(func);
|
---|
120 | Inserter *inserter = new Inserter(
|
---|
121 | Inserter::impl_ptr(new SurfaceInserter(*selectedShapes[0], params.AlignedAxis.get())));
|
---|
122 |
|
---|
123 | // fill
|
---|
124 | {
|
---|
125 | Filler *fillerFunction = new Filler(*mesh, *voidnode_predicate, *inserter);
|
---|
126 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingSphere() ) );
|
---|
127 | CopyAtoms_withBonds copyMethod;
|
---|
128 | Filler::ClusterVector_t ClonedClusters;
|
---|
129 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
|
---|
130 | delete fillerFunction;
|
---|
131 |
|
---|
132 | // append each cluster's atoms to clonedatoms (however not selected ones)
|
---|
133 | std::vector<const atom *> clonedatoms;
|
---|
134 | std::vector<AtomicInfo> clonedatominfos;
|
---|
135 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
|
---|
136 | iter != ClonedClusters.end(); ++iter) {
|
---|
137 | const AtomIdSet &atoms = (*iter)->getAtomIds();
|
---|
138 | clonedatoms.reserve(clonedatoms.size()+atoms.size());
|
---|
139 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
|
---|
140 | if (!filler->containsAtom(*atomiter)) {
|
---|
141 | clonedatoms.push_back( *atomiter );
|
---|
142 | clonedatominfos.push_back( AtomicInfo(*(*atomiter)) );
|
---|
143 | }
|
---|
144 | }
|
---|
145 | std::vector< BondInfo > clonedbonds;
|
---|
146 | StoreBondInformationFromAtoms(clonedatoms, clonedbonds);
|
---|
147 | LOG(2, "DEBUG: There are " << clonedatominfos.size() << " newly created atoms with "
|
---|
148 | << clonedbonds.size()/2 << " bonds.");
|
---|
149 |
|
---|
150 | if (!successflag) {
|
---|
151 | STATUS("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 FillSurfaceState(clonedatominfos,clonedbonds,movedatoms,MovedToVector,params);
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | // remove
|
---|
164 | delete mesh;
|
---|
165 | delete inserter;
|
---|
166 | delete voidnode_predicate;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (successflag)
|
---|
170 | return ActionState::ptr(UndoState);
|
---|
171 | else
|
---|
172 | return Action::failure;
|
---|
173 | }
|
---|
174 |
|
---|
175 | ActionState::ptr FillSurfaceAction::performUndo(ActionState::ptr _state) {
|
---|
176 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
|
---|
177 |
|
---|
178 | // remove all created atoms
|
---|
179 | RemoveAtomsFromAtomicInfo(state->clonedatoms);
|
---|
180 | // add the original cluster
|
---|
181 | SetAtomsFromAtomicInfo(state->movedatoms);
|
---|
182 |
|
---|
183 | return ActionState::ptr(_state);
|
---|
184 | }
|
---|
185 |
|
---|
186 | ActionState::ptr FillSurfaceAction::performRedo(ActionState::ptr _state){
|
---|
187 | FillSurfaceState *state = assert_cast<FillSurfaceState*>(_state.get());
|
---|
188 |
|
---|
189 | // place filler cluster again at new spot
|
---|
190 | ResetAtomPosition(state->movedatoms, state->MovedToVector);
|
---|
191 |
|
---|
192 | // re-create all clusters
|
---|
193 | bool statusflag = AddAtomsFromAtomicInfo(state->clonedatoms);
|
---|
194 |
|
---|
195 | // re-create the bonds
|
---|
196 | statusflag = statusflag && AddBondsFromBondInfo(state->clonedbonds);
|
---|
197 | if (statusflag)
|
---|
198 | return ActionState::ptr(_state);
|
---|
199 | else {
|
---|
200 | STATUS("Failed to re-added filled in atoms.");
|
---|
201 | return Action::failure;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | bool FillSurfaceAction::canUndo() {
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 |
|
---|
209 | bool FillSurfaceAction::shouldUndo() {
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 | /** =========== end of function ====================== */
|
---|