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 "Actions/UndoRedoHelpers.hpp"
|
---|
23 | #include "Atom/atom.hpp"
|
---|
24 | #include "Atom/AtomicInfo.hpp"
|
---|
25 | #include "Atom/CopyAtoms/CopyAtoms_withBonds.hpp"
|
---|
26 | #include "CodePatterns/Log.hpp"
|
---|
27 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
|
---|
28 | #include "Filling/Cluster.hpp"
|
---|
29 | #include "Filling/Filler.hpp"
|
---|
30 | #include "Filling/Inserter/Inserter.hpp"
|
---|
31 | #include "Filling/Inserter/RandomInserter.hpp"
|
---|
32 | #include "Filling/Mesh/CubeMesh.hpp"
|
---|
33 | #include "Filling/Predicates/IsInsideSurface_FillPredicate.hpp"
|
---|
34 | #include "Filling/Predicates/IsVoidNode_FillPredicate.hpp"
|
---|
35 | #include "Filling/Predicates/Ops_FillPredicate.hpp"
|
---|
36 | #include "LinkedCell/linkedcell.hpp"
|
---|
37 | #include "LinkedCell/PointCloudAdaptor.hpp"
|
---|
38 | #include "molecule.hpp"
|
---|
39 | #include "MoleculeListClass.hpp"
|
---|
40 | #include "Parser/FormatParserInterface.hpp"
|
---|
41 | #include "Parser/FormatParserStorage.hpp"
|
---|
42 | #include "Shapes/BaseShapes.hpp"
|
---|
43 | #include "Tesselation/tesselation.hpp"
|
---|
44 | #include "Tesselation/BoundaryLineSet.hpp"
|
---|
45 | #include "Tesselation/BoundaryTriangleSet.hpp"
|
---|
46 | #include "Tesselation/CandidateForTesselation.hpp"
|
---|
47 | #include "World.hpp"
|
---|
48 |
|
---|
49 |
|
---|
50 | #include <algorithm>
|
---|
51 | #include <iostream>
|
---|
52 | #include <string>
|
---|
53 | #include <vector>
|
---|
54 |
|
---|
55 | #include "Actions/FillAction/FillRegularGridAction.hpp"
|
---|
56 |
|
---|
57 | using namespace MoleCuilder;
|
---|
58 |
|
---|
59 | // and construct the stuff
|
---|
60 | #include "FillRegularGridAction.def"
|
---|
61 | #include "Action_impl_pre.hpp"
|
---|
62 | /** =========== define the function ====================== */
|
---|
63 | Action::state_ptr FillRegularGridAction::performCall() {
|
---|
64 | typedef std::vector<atom*> AtomVector;
|
---|
65 |
|
---|
66 | // obtain information
|
---|
67 | getParametersfromValueStorage();
|
---|
68 |
|
---|
69 | // get the filler molecule and move to origin
|
---|
70 | const std::vector< molecule *> molecules = World::getInstance().getSelectedMolecules();
|
---|
71 | if (molecules.size() != 1) {
|
---|
72 | ELOG(1, "No exactly one molecule selected, aborting,");
|
---|
73 | return Action::failure;
|
---|
74 | }
|
---|
75 | molecule *filler = *(molecules.begin());
|
---|
76 | LOG(1, "INFO: Chosen molecule has " << filler->size() << " atoms.");
|
---|
77 |
|
---|
78 | // check for selected molecules and create surfaces from them
|
---|
79 | std::vector<atom *> atoms(World::getInstance().getSelectedAtoms());
|
---|
80 | FillPredicate * surface_predicate = NULL;
|
---|
81 | LinkedCell_deprecated * LC = NULL;
|
---|
82 | Tesselation * TesselStruct = NULL;
|
---|
83 | if (params.SphereRadius != 0.) {
|
---|
84 | if ( molecules.size() == 0) {
|
---|
85 | ELOG(1, "You have given a sphere radius " << params.SphereRadius
|
---|
86 | << " != 0, but have not select any molecules.");
|
---|
87 | }
|
---|
88 | // create adaptor for the selected atoms
|
---|
89 | PointCloudAdaptor< std::vector<atom *> > cloud(&atoms, std::string("Selected atoms"));
|
---|
90 |
|
---|
91 | // create tesselation
|
---|
92 | LC = new LinkedCell_deprecated(cloud, 2.*params.SphereRadius);
|
---|
93 | TesselStruct = new Tesselation;
|
---|
94 | (*TesselStruct)(cloud, params.SphereRadius);
|
---|
95 |
|
---|
96 | // and create predicate
|
---|
97 | surface_predicate = new FillPredicate( IsInsideSurface_FillPredicate( *TesselStruct, *LC ) );
|
---|
98 | }
|
---|
99 |
|
---|
100 | // create predicate, mesh, and filler
|
---|
101 | std::vector<AtomicInfo> clonedatoms;
|
---|
102 | bool successflag = false;
|
---|
103 | {
|
---|
104 | FillPredicate *voidnode_predicate = new FillPredicate(
|
---|
105 | IsVoidNode_FillPredicate(
|
---|
106 | Sphere(zeroVec, params.mindistance)
|
---|
107 | )
|
---|
108 | );
|
---|
109 | FillPredicate Andpredicate = (*voidnode_predicate);
|
---|
110 | if (surface_predicate != NULL)
|
---|
111 | Andpredicate = (Andpredicate) && !(*surface_predicate);
|
---|
112 | Mesh *mesh = new CubeMesh(params.counts, params.offset, World::getInstance().getDomain().getM());
|
---|
113 | Inserter *inserter = new Inserter(
|
---|
114 | Inserter::impl_ptr(
|
---|
115 | new RandomInserter(
|
---|
116 | params.RandAtomDisplacement,
|
---|
117 | params.RandMoleculeDisplacement,
|
---|
118 | params.DoRotate)
|
---|
119 | )
|
---|
120 | );
|
---|
121 |
|
---|
122 | // fill
|
---|
123 | {
|
---|
124 | Filler *fillerFunction = new Filler(*mesh, Andpredicate, *inserter);
|
---|
125 | ClusterInterface::Cluster_impl cluster( new Cluster( filler->getAtomIds(), filler->getBoundingShape()) );
|
---|
126 | CopyAtoms_withBonds copyMethod;
|
---|
127 | Filler::ClusterVector_t ClonedClusters;
|
---|
128 | successflag = (*fillerFunction)(copyMethod, cluster, ClonedClusters);
|
---|
129 | delete fillerFunction;
|
---|
130 |
|
---|
131 | // append each cluster's atoms to clonedatoms
|
---|
132 | for (Filler::ClusterVector_t::const_iterator iter = ClonedClusters.begin();
|
---|
133 | iter != ClonedClusters.end(); ++iter) {
|
---|
134 | const AtomIdSet &atoms = (*iter)->getAtomIds();
|
---|
135 | clonedatoms.reserve(clonedatoms.size()+atoms.size());
|
---|
136 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); atomiter != atoms.end(); ++atomiter)
|
---|
137 | clonedatoms.push_back( AtomicInfo(*(*atomiter)) );
|
---|
138 | }
|
---|
139 | if (!successflag) {
|
---|
140 | ELOG(1, "Insertion failed, removing inserted clusters again.");
|
---|
141 | RemoveAtomsFromAtomicInfo(clonedatoms);
|
---|
142 | clonedatoms.clear();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | // remove
|
---|
147 | delete mesh;
|
---|
148 | delete inserter;
|
---|
149 | delete voidnode_predicate;
|
---|
150 | delete surface_predicate;
|
---|
151 | delete LC;
|
---|
152 | delete TesselStruct;
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (successflag)
|
---|
156 | return Action::state_ptr(new FillRegularGridState(clonedatoms,params));
|
---|
157 | else
|
---|
158 | return Action::failure;
|
---|
159 | }
|
---|
160 |
|
---|
161 | Action::state_ptr FillRegularGridAction::performUndo(Action::state_ptr _state) {
|
---|
162 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
163 |
|
---|
164 | // remove all created atoms
|
---|
165 | RemoveAtomsFromAtomicInfo(state->clonedatoms);
|
---|
166 |
|
---|
167 | return Action::state_ptr(_state);
|
---|
168 | }
|
---|
169 |
|
---|
170 | Action::state_ptr FillRegularGridAction::performRedo(Action::state_ptr _state){
|
---|
171 | FillRegularGridState *state = assert_cast<FillRegularGridState*>(_state.get());
|
---|
172 |
|
---|
173 | if (AddAtomsFromAtomicInfo(state->clonedatoms))
|
---|
174 | return Action::state_ptr(_state);
|
---|
175 | else
|
---|
176 | return Action::failure;
|
---|
177 | }
|
---|
178 |
|
---|
179 | bool FillRegularGridAction::canUndo() {
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool FillRegularGridAction::shouldUndo() {
|
---|
184 | return true;
|
---|
185 | }
|
---|
186 | /** =========== end of function ====================== */
|
---|