source: src/Actions/UndoRedoHelpers.cpp@ c099dc

Candidate_v1.7.0 stable
Last change on this file since c099dc was 9782e20, checked in by Frederik Heber <frederik.heber@…>, 20 months ago

BondifyAction: added undo/redo.

  • DOC: fixed small typo in Action's explanation.
  • TEST: added regression test (also undo/redo).
  • Property mode set to 100644
File size: 8.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013-2014 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * UndoRedoHelpers.cpp
26 *
27 * Created on: Apr 5, 2012
28 * Author: heber
29 */
30
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37//#include "CodePatterns/MemDebug.hpp"
38
39#include "UndoRedoHelpers.hpp"
40
41#include <boost/bind.hpp>
42#include <boost/foreach.hpp>
43#include <boost/function.hpp>
44
45#include "Atom/atom.hpp"
46#include "molecule.hpp"
47#include "Descriptors/AtomIdDescriptor.hpp"
48#include "Descriptors/MoleculeIdDescriptor.hpp"
49#include "CodePatterns/Assert.hpp"
50#include "CodePatterns/Log.hpp"
51#include "World.hpp"
52#include "WorldTime.hpp"
53
54bool MoleCuilder::AddAtomsFromAtomicInfo(const std::vector<AtomicInfo> &atoms)
55{
56 size_t i=0;
57 for (; i<atoms.size(); ++i) {
58 // re-create the atom
59 LOG(3, "DEBUG: Re-adding atom " << atoms[i].getId() << ".");
60 atom *Walker = World::getInstance().recreateAtom(atoms[i].getId());
61 if ((Walker != NULL) && (!atoms[i].setAtom(*Walker))) {
62 ELOG(1, "Failed to set id.");
63 if (Walker != NULL)
64 World::getInstance().destroyAtom(Walker);
65 break;
66 }
67 }
68 if (i<atoms.size()) {
69 // remove all previous ones, too
70 for (size_t j=0;j<i;++j)
71 World::getInstance().destroyAtom(atoms[j].getId());
72 // and announce the failure
73 return false;
74 }
75 return true;
76}
77
78bool MoleCuilder::AddMoleculesFromAtomicInfo(std::map< moleculeId_t, std::vector<AtomicInfo> > &mol_atoms)
79{
80 bool status = true;
81 for (std::map< moleculeId_t, std::vector<AtomicInfo> >::const_iterator iter = mol_atoms.begin();
82 iter != mol_atoms.end(); ++iter) {
83 // re-create the atom
84 LOG(3, "DEBUG: Re-adding molecule " << iter->first << ".");
85 molecule *mol_Walker = World::getInstance().recreateMolecule(iter->first);
86 status &= (mol_Walker != NULL);
87
88 // add all its atoms
89 status &= AddAtomsFromAtomicInfo(iter->second);
90 }
91 if (!status) {
92 // remove all molecules again
93 for (std::map< moleculeId_t, std::vector<AtomicInfo> >::const_iterator iter = mol_atoms.begin();
94 iter != mol_atoms.end(); ++iter) {
95 molecule * mol = World::getInstance().getMolecule(MoleculeById(iter->first));
96 if (mol != NULL)
97 removeAtomsinMolecule(mol);
98 }
99 // and announce the failure
100 return false;
101 }
102 return true;
103}
104
105void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
106{
107 BOOST_FOREACH(const AtomicInfo &_atom, atoms) {
108 World::getInstance().destroyAtom(_atom.getId());
109 }
110}
111
112void MoleCuilder::StoreBondInformationFromAtoms(
113 const std::vector<const atom*> &atoms,
114 std::vector< BondInfo > &bonds)
115{
116 ASSERT( bonds.empty(),
117 "StoreBondInformationFromAtoms() - give bonds vector is not empty.");
118 bonds.reserve(atoms.size()*4);
119 for (std::vector<const atom*>::const_iterator atomiter = atoms.begin();
120 atomiter != atoms.end(); ++atomiter) {
121 const BondList & _atom_bonds = (*atomiter)->getListOfBonds();
122 for(BondList::const_iterator iter = _atom_bonds.begin(); iter != _atom_bonds.end(); ++iter)
123 bonds.push_back( BondInfo(*iter) );
124 }
125}
126
127bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds)
128{
129 bool status = true;
130 std::vector< BondInfo >::const_iterator iter = bonds.begin();
131 for(;iter != bonds.end(); ++iter) {
132 if (!(*iter).RecreateBond()) {
133 status = false;
134 break;
135 }
136 }
137 if (!status) {
138 // remove all added bonds again
139 for(std::vector< BondInfo >::const_iterator removeiter = bonds.begin();
140 removeiter != iter; ++removeiter) {
141 removeiter->RemoveBond();
142 }
143 }
144 return status;
145}
146
147bool MoleCuilder::RemoveBondsFromBondInfo(const std::vector< BondInfo > &bonds)
148{
149 bool status = true;
150 std::vector< BondInfo >::const_iterator iter = bonds.begin();
151 for(;iter != bonds.end(); ++iter) {
152 if (!(*iter).RemoveBond()) {
153 status = false;
154 break;
155 }
156 }
157 if (!status) {
158 // remove all added bonds again
159 for(std::vector< BondInfo >::const_iterator removeiter = bonds.begin();
160 removeiter != iter; ++removeiter) {
161 removeiter->RecreateBond();
162 }
163 }
164 return status;
165}
166
167
168void MoleCuilder::SetAtomsFromAtomicInfo(
169 const std::vector<AtomicInfo> &_movedatoms,
170 const unsigned int _step)
171{
172 BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
173 const atomId_t id = _atominfo.getId();
174 atom * const _atom = World::getInstance().getAtom(AtomById(id));
175 ASSERT( _atom != NULL,
176 "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
177 +toString(id)+" in the world.");
178 _atominfo.setAtom( *_atom, _step );
179 }
180}
181
182void MoleCuilder::SelectAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
183{
184 BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
185 const atomId_t id = _atominfo.getId();
186 World::getInstance().selectAtom(id);
187 }
188}
189
190void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector)
191{
192 boost::function<void(atom *, const Vector&)> setter =
193 boost::bind(&atom::setPosition, _1, _2);
194 ResetByFunction(movedatoms, MovedToVector, setter);
195}
196
197void MoleCuilder::ResetAtomVelocity(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &VelocityVector)
198{
199 boost::function<void(atom *, const Vector&)> setter =
200 boost::bind(&atom::setAtomicVelocity, _1, _2);
201 ResetByFunction(movedatoms, VelocityVector, setter);
202}
203
204void MoleCuilder::ResetAtomForce(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &ForceVector)
205{
206 boost::function<void(atom *, const Vector&)> setter =
207 boost::bind(&atom::setAtomicForce, _1, _2);
208 ResetByFunction(movedatoms, ForceVector, setter);
209}
210
211void MoleCuilder::ResetByFunction(
212 const std::vector<AtomicInfo> &movedatoms,
213 const std::vector<Vector> &MovedToVector,
214 boost::function<void(atom *, const Vector&)> &setter)
215{
216 std::vector<Vector>::const_iterator positer = MovedToVector.begin();
217 ASSERT(movedatoms.size() == MovedToVector.size(),
218 "MoleCuilder::ResetAtomPosition() - the number of atoms "
219 +toString(movedatoms.size())+" and the number of positions "
220 +toString(MovedToVector.size())+" is not the same.");
221 BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
222 const atomId_t id = _atominfo.getId();
223 atom * const _atom = World::getInstance().getAtom(AtomById(id));
224 ASSERT( _atom != NULL,
225 "FillSphericalSurfaceAction::performRedo() - cannot find atom with id "
226 +toString(id)+" in the world.");
227 setter(_atom, *positer );
228 ++positer;
229 }
230}
231
232void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids)
233{
234 for (std::vector<moleculeId_t>::const_iterator iter = ids.begin();
235 iter != ids.end(); ++iter) {
236 molecule * mol = World::getInstance().getMolecule(MoleculeById(*iter));
237 if (mol != NULL) {
238 removeAtomsinMolecule(mol);
239 // molecules are automatically removed when empty
240 }
241 }
242}
243
244void MoleCuilder::removeSteps(
245 const std::vector<atomId_t> &movedatoms,
246 const unsigned int _firststep,
247 const unsigned int _laststep)
248{
249 for (std::vector<atomId_t>::const_iterator iter = movedatoms.begin();
250 iter != movedatoms.end(); ++iter) {
251 atom * const _atom = World::getInstance().getAtom(AtomById(*iter));
252 _atom->removeSteps(_firststep, _laststep);
253 }
254}
255
256void MoleCuilder::addNewStep(const std::vector<AtomicInfo> &_movedatoms, const unsigned int _step)
257{
258 for(size_t i=0; i< _movedatoms.size(); ++i) {
259 atom * const _atom = World::getInstance().getAtom(AtomById(_movedatoms[i].getId()));
260 _atom->UpdateStep(_step);
261 }
262}
263
264void MoleCuilder::addNewStep(const std::vector<atomId_t> &_ids, const unsigned int _step)
265{
266 for(size_t i=0; i< _ids.size(); ++i) {
267 atom * const _atom = World::getInstance().getAtom(AtomById(_ids[i]));
268 _atom->UpdateStep(_step);
269 }
270}
271
272std::vector<atomId_t> MoleCuilder::getIdsFromAtomicInfo(const std::vector<AtomicInfo> &movedatoms)
273{
274 std::vector<atomId_t> ids(movedatoms.size(), (size_t)-1);
275 std::transform(
276 movedatoms.begin(), movedatoms.end(),
277 ids.begin(),
278 boost::bind(&AtomicInfo::getId, _1));
279 return ids;
280}
Note: See TracBrowser for help on using the repository browser.