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 "Atom/atom.hpp"
|
---|
23 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
|
---|
24 | #include "CodePatterns/Log.hpp"
|
---|
25 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
|
---|
26 | #include "Filling/Cluster.hpp"
|
---|
27 | #include "Filling/Filler.hpp"
|
---|
28 | #include "Filling/Mesh/CubeMesh.hpp"
|
---|
29 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
|
---|
30 | #include "MoleculeListClass.hpp"
|
---|
31 | #include "Parser/FormatParserInterface.hpp"
|
---|
32 | #include "Parser/FormatParserStorage.hpp"
|
---|
33 | #include "Shapes/BaseShapes.hpp"
|
---|
34 | #include "World.hpp"
|
---|
35 |
|
---|
36 |
|
---|
37 | #include <algorithm>
|
---|
38 | #include <iostream>
|
---|
39 | #include <string>
|
---|
40 | #include <vector>
|
---|
41 |
|
---|
42 | #include "Actions/FillAction/FillRegularGridAction.hpp"
|
---|
43 |
|
---|
44 | using namespace MoleCuilder;
|
---|
45 |
|
---|
46 | // and construct the stuff
|
---|
47 | #include "FillRegularGridAction.def"
|
---|
48 | #include "Action_impl_pre.hpp"
|
---|
49 | /** =========== define the function ====================== */
|
---|
50 | Action::state_ptr FillRegularGridAction::performCall() {
|
---|
51 | typedef std::vector<atom*> AtomVector;
|
---|
52 |
|
---|
53 | // obtain information
|
---|
54 | getParametersfromValueStorage();
|
---|
55 |
|
---|
56 | // get all present atoms for UndoState
|
---|
57 | AtomVector presentatoms = World::getInstance().getAllAtoms();
|
---|
58 |
|
---|
59 | // get selected atoms and find containing sphere
|
---|
60 | World::AtomComposite atoms = World::getInstance().getSelectedAtoms();
|
---|
61 | Vector center;
|
---|
62 | double radius = 0.;
|
---|
63 | {
|
---|
64 | center.Zero();
|
---|
65 | for(World::AtomComposite::const_iterator iter = atoms.begin();
|
---|
66 | iter != atoms.end(); ++iter)
|
---|
67 | center += (*iter)->getPosition();
|
---|
68 | center *= 1./(double)atoms.size();
|
---|
69 | for(World::AtomComposite::const_iterator iter = atoms.begin();
|
---|
70 | iter != atoms.end(); ++iter) {
|
---|
71 | const Vector &position = (*iter)->getPosition();
|
---|
72 | for (size_t i=0;i<NDIM;++i) {
|
---|
73 | const double temp_distance = fabs(position[i] - center[i]);
|
---|
74 | if (temp_distance > radius)
|
---|
75 | radius = temp_distance;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | // always move cluster to be centered at origin
|
---|
80 | for(World::AtomComposite::iterator iter = atoms.begin();
|
---|
81 | iter != atoms.end(); ++iter)
|
---|
82 | (*iter)->setPosition((*iter)->getPosition() - center);
|
---|
83 | if (radius == 0.) { // never have zero radius: set to one or less
|
---|
84 | ASSERT( (params.counts[0] != 0.) && (params.counts[1] != 0.) && (params.counts[2] != 0.),
|
---|
85 | "FillRegularGridAction::performCall() - counts must not be zero.");
|
---|
86 | const Vector diag(1./(double)(params.counts[0]+1), 1./(double)(params.counts[1]+1), 1./(double)(params.counts[2]+1));
|
---|
87 | radius = std::min(1.,World::getInstance().getDomain().translateIn(diag).Norm()/2.);
|
---|
88 | }
|
---|
89 | // add some small boundary
|
---|
90 | radius += 1e+6*std::numeric_limits<double>::epsilon();
|
---|
91 | radius = std::max(params.mindistance, radius);
|
---|
92 | LOG(1, "INFO: The " << atoms.size() << " are contained in a sphere at "
|
---|
93 | << center << " with radius " << radius << ".");
|
---|
94 | Shape *s = new Shape(Sphere(zeroVec, radius));
|
---|
95 | LOG(1, "INFO: Created sphere at " << s->getCenter() << " and radius " << s->getRadius() << ".");
|
---|
96 |
|
---|
97 | // create set of atomic ids
|
---|
98 | std::vector<atomId_t> atomIds(atoms.size(), (size_t)-1);
|
---|
99 | std::transform(atoms.begin(), atoms.end(), atomIds.begin(),
|
---|
100 | boost::bind(&atom::getId, _1) );
|
---|
101 | Cluster::atomIdSet atomset(atomIds.begin(), atomIds.end());
|
---|
102 | LOG(1, "INFO: Created atomset of size " << atomset.size() << ".");
|
---|
103 |
|
---|
104 | // create cluster
|
---|
105 | ClusterInterface::Cluster_impl cluster(new Cluster(atomset, *s));
|
---|
106 | LOG(1, "INFO: Created cluster of size " << atomset.size() << ".");
|
---|
107 |
|
---|
108 | // create predicate, mesh, and filler
|
---|
109 | {
|
---|
110 | FillPredicate *predicate = new FillPredicate( IsVoidNode_FillPredicate(*s) );
|
---|
111 | Mesh *mesh = new CubeMesh(params.counts, params.offset, World::getInstance().getDomain().getM());
|
---|
112 | Filler *fillerFunction = new Filler(*mesh, *predicate);
|
---|
113 |
|
---|
114 | // fill
|
---|
115 | CopyAtoms_withBonds copyMethod;
|
---|
116 | (*fillerFunction)(copyMethod, cluster);
|
---|
117 |
|
---|
118 | // remove
|
---|
119 | delete fillerFunction;
|
---|
120 | delete mesh;
|
---|
121 | delete predicate;
|
---|
122 | delete s;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // generate list of newly created atoms
|
---|
126 | // (we can in general remove more quickly from a list than a vector)
|
---|
127 | AtomVector filleratoms = World::getInstance().getAllAtoms();
|
---|
128 | // LOG(0, filleratoms.size() << " atoms are present.");
|
---|
129 | std::list<atom *> filleratoms_list;
|
---|
130 | std::copy( filleratoms.begin(), filleratoms.end(), std::back_inserter( filleratoms_list ));
|
---|
131 | // LOG(0, filleratoms_list.size() << " atoms have been copied.");
|
---|
132 | for (AtomVector::const_iterator iter = presentatoms.begin();
|
---|
133 | iter != presentatoms.end();
|
---|
134 | ++iter) {
|
---|
135 | filleratoms_list.remove(*iter);
|
---|
136 | }
|
---|
137 | // LOG(0, filleratoms_list.size() << " atoms left after removal.");
|
---|
138 | filleratoms.clear();
|
---|
139 | std::copy(filleratoms_list.begin(), filleratoms_list.end(), std::back_inserter( filleratoms ));
|
---|
140 |
|
---|
141 | // LOG(0, filleratoms.size() << " atoms have been inserted.");
|
---|
142 |
|
---|
143 | return Action::state_ptr(new FillRegularGridState(filleratoms,params));
|
---|
144 | }
|
---|
145 |
|
---|
146 | Action::state_ptr FillRegularGridAction::performUndo(Action::state_ptr _state) {
|
---|
147 | // FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
148 | //
|
---|
149 | // BOOST_FOREACH(atom *_atom, state->filleratoms) {
|
---|
150 | // World::getInstance().destroyAtom(Walker);
|
---|
151 | // }
|
---|
152 | //
|
---|
153 | // // as atoms and atoms from state are removed, we have to create a new one
|
---|
154 | // std::vector<atom*> filleratoms;
|
---|
155 | // return Action::state_ptr(new FillRegularGridState(filleratoms,state->params));
|
---|
156 | return Action::failure;
|
---|
157 | }
|
---|
158 |
|
---|
159 | Action::state_ptr FillRegularGridAction::performRedo(Action::state_ptr _state){
|
---|
160 | //FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
161 |
|
---|
162 | return Action::failure;
|
---|
163 | //return Action::state_ptr(_state);
|
---|
164 | }
|
---|
165 |
|
---|
166 | bool FillRegularGridAction::canUndo() {
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool FillRegularGridAction::shouldUndo() {
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 | /** =========== end of function ====================== */
|
---|