| [57dd40] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| [7e51e1] | 5 | * Copyright (C)  2013-2014 Frederik Heber. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [57dd40] | 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 |  | 
|---|
| [9eb71b3] | 37 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| [57dd40] | 38 |  | 
|---|
|  | 39 | #include "UndoRedoHelpers.hpp" | 
|---|
|  | 40 |  | 
|---|
| [7e51e1] | 41 | #include <boost/bind.hpp> | 
|---|
| [57dd40] | 42 | #include <boost/foreach.hpp> | 
|---|
| [7e51e1] | 43 | #include <boost/function.hpp> | 
|---|
| [57dd40] | 44 |  | 
|---|
|  | 45 | #include "Atom/atom.hpp" | 
|---|
| [8ea3e7] | 46 | #include "molecule.hpp" | 
|---|
| [57dd40] | 47 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| [8ea3e7] | 48 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
| [57dd40] | 49 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 50 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 51 | #include "World.hpp" | 
|---|
| [7e51e1] | 52 | #include "WorldTime.hpp" | 
|---|
| [57dd40] | 53 |  | 
|---|
| [596cfa] | 54 | bool MoleCuilder::AddAtomsFromAtomicInfo(const std::vector<AtomicInfo> &atoms) | 
|---|
| [57dd40] | 55 | { | 
|---|
|  | 56 | size_t i=0; | 
|---|
|  | 57 | for (; i<atoms.size(); ++i) { | 
|---|
|  | 58 | // re-create the atom | 
|---|
| [af9be32] | 59 | LOG(3, "DEBUG: Re-adding atom " << atoms[i].getId() << "."); | 
|---|
| [8ad68b] | 60 | atom *Walker = World::getInstance().recreateAtom(atoms[i].getId()); | 
|---|
|  | 61 | if ((Walker != NULL) && (!atoms[i].setAtom(*Walker))) { | 
|---|
| [57dd40] | 62 | ELOG(1, "Failed to set id."); | 
|---|
| [8ad68b] | 63 | if (Walker != NULL) | 
|---|
|  | 64 | World::getInstance().destroyAtom(Walker); | 
|---|
| [57dd40] | 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 |  | 
|---|
| [596cfa] | 78 | bool 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 << "."); | 
|---|
| [8ad68b] | 85 | molecule *mol_Walker = World::getInstance().recreateMolecule(iter->first); | 
|---|
|  | 86 | status &= (mol_Walker != NULL); | 
|---|
| [596cfa] | 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 |  | 
|---|
| [57dd40] | 105 | void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms) | 
|---|
|  | 106 | { | 
|---|
|  | 107 | BOOST_FOREACH(const AtomicInfo &_atom, atoms) { | 
|---|
|  | 108 | World::getInstance().destroyAtom(_atom.getId()); | 
|---|
|  | 109 | } | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
| [af9be32] | 112 | void 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 |  | 
|---|
|  | 127 | bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | bool status = true; | 
|---|
| [917300] | 130 | std::vector< BondInfo >::const_iterator iter = bonds.begin(); | 
|---|
|  | 131 | for(;iter != bonds.end(); ++iter) { | 
|---|
|  | 132 | if (!(*iter).RecreateBond()) { | 
|---|
| [af9be32] | 133 | status = false; | 
|---|
| [917300] | 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 | } | 
|---|
| [af9be32] | 144 | return status; | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
| [6145577] | 147 | void MoleCuilder::SetAtomsFromAtomicInfo( | 
|---|
|  | 148 | const std::vector<AtomicInfo> &_movedatoms, | 
|---|
|  | 149 | const unsigned int _step) | 
|---|
| [57dd40] | 150 | { | 
|---|
| [7e51e1] | 151 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) { | 
|---|
| [57dd40] | 152 | const atomId_t id = _atominfo.getId(); | 
|---|
|  | 153 | atom * const _atom = World::getInstance().getAtom(AtomById(id)); | 
|---|
|  | 154 | ASSERT( _atom != NULL, | 
|---|
|  | 155 | "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id " | 
|---|
|  | 156 | +toString(id)+" in the world."); | 
|---|
| [6145577] | 157 | _atominfo.setAtom( *_atom, _step ); | 
|---|
| [57dd40] | 158 | } | 
|---|
|  | 159 | } | 
|---|
|  | 160 |  | 
|---|
| [7e51e1] | 161 | void MoleCuilder::SelectAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms) | 
|---|
| [57dd40] | 162 | { | 
|---|
| [7e51e1] | 163 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) { | 
|---|
| [57dd40] | 164 | const atomId_t id = _atominfo.getId(); | 
|---|
|  | 165 | World::getInstance().selectAtom(id); | 
|---|
|  | 166 | } | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
|  | 169 | void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector) | 
|---|
| [7e51e1] | 170 | { | 
|---|
|  | 171 | boost::function<void(atom *, const Vector&)> setter = | 
|---|
|  | 172 | boost::bind(&atom::setPosition, _1, _2); | 
|---|
|  | 173 | ResetByFunction(movedatoms, MovedToVector, setter); | 
|---|
|  | 174 | } | 
|---|
|  | 175 |  | 
|---|
|  | 176 | void MoleCuilder::ResetAtomVelocity(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &VelocityVector) | 
|---|
|  | 177 | { | 
|---|
|  | 178 | boost::function<void(atom *, const Vector&)> setter = | 
|---|
|  | 179 | boost::bind(&atom::setAtomicVelocity, _1, _2); | 
|---|
|  | 180 | ResetByFunction(movedatoms, VelocityVector, setter); | 
|---|
|  | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | void MoleCuilder::ResetAtomForce(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &ForceVector) | 
|---|
|  | 184 | { | 
|---|
|  | 185 | boost::function<void(atom *, const Vector&)> setter = | 
|---|
|  | 186 | boost::bind(&atom::setAtomicForce, _1, _2); | 
|---|
|  | 187 | ResetByFunction(movedatoms, ForceVector, setter); | 
|---|
|  | 188 | } | 
|---|
|  | 189 |  | 
|---|
|  | 190 | void MoleCuilder::ResetByFunction( | 
|---|
|  | 191 | const std::vector<AtomicInfo> &movedatoms, | 
|---|
|  | 192 | const std::vector<Vector> &MovedToVector, | 
|---|
|  | 193 | boost::function<void(atom *, const Vector&)> &setter) | 
|---|
| [57dd40] | 194 | { | 
|---|
|  | 195 | std::vector<Vector>::const_iterator positer = MovedToVector.begin(); | 
|---|
|  | 196 | ASSERT(movedatoms.size() == MovedToVector.size(), | 
|---|
|  | 197 | "MoleCuilder::ResetAtomPosition() -  the number of atoms " | 
|---|
|  | 198 | +toString(movedatoms.size())+" and the number of positions " | 
|---|
|  | 199 | +toString(MovedToVector.size())+" is not the same."); | 
|---|
|  | 200 | BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) { | 
|---|
|  | 201 | const atomId_t id = _atominfo.getId(); | 
|---|
|  | 202 | atom * const _atom = World::getInstance().getAtom(AtomById(id)); | 
|---|
|  | 203 | ASSERT( _atom != NULL, | 
|---|
|  | 204 | "FillSphericalSurfaceAction::performRedo() - cannot find atom with id " | 
|---|
|  | 205 | +toString(id)+" in the world."); | 
|---|
| [7e51e1] | 206 | setter(_atom, *positer ); | 
|---|
| [57dd40] | 207 | ++positer; | 
|---|
|  | 208 | } | 
|---|
|  | 209 | } | 
|---|
| [8ea3e7] | 210 |  | 
|---|
|  | 211 | void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids) | 
|---|
|  | 212 | { | 
|---|
|  | 213 | for (std::vector<moleculeId_t>::const_iterator iter = ids.begin(); | 
|---|
|  | 214 | iter != ids.end(); ++iter) { | 
|---|
| [a7aebd] | 215 | molecule * mol = World::getInstance().getMolecule(MoleculeById(*iter)); | 
|---|
| [8ea3e7] | 216 | if (mol != NULL) { | 
|---|
| [a7aebd] | 217 | removeAtomsinMolecule(mol); | 
|---|
|  | 218 | // molecules are automatically removed when empty | 
|---|
| [8ea3e7] | 219 | } | 
|---|
|  | 220 | } | 
|---|
|  | 221 | } | 
|---|
| [7e51e1] | 222 |  | 
|---|
| [8c6b68] | 223 | void MoleCuilder::removeSteps( | 
|---|
|  | 224 | const std::vector<atomId_t> &movedatoms, | 
|---|
|  | 225 | const unsigned int _firststep, | 
|---|
|  | 226 | const unsigned int _laststep) | 
|---|
| [7e51e1] | 227 | { | 
|---|
| [8c6b68] | 228 | for (std::vector<atomId_t>::const_iterator iter = movedatoms.begin(); | 
|---|
|  | 229 | iter != movedatoms.end(); ++iter) { | 
|---|
|  | 230 | atom * const _atom = World::getInstance().getAtom(AtomById(*iter)); | 
|---|
|  | 231 | _atom->removeSteps(_firststep, _laststep); | 
|---|
| [7e51e1] | 232 | } | 
|---|
|  | 233 | } | 
|---|
|  | 234 |  | 
|---|
| [8cc22f] | 235 | void MoleCuilder::addNewStep(const std::vector<AtomicInfo> &_movedatoms, const unsigned int _step) | 
|---|
| [7e51e1] | 236 | { | 
|---|
|  | 237 | for(size_t i=0; i< _movedatoms.size(); ++i) { | 
|---|
|  | 238 | atom * const _atom = World::getInstance().getAtom(AtomById(_movedatoms[i].getId())); | 
|---|
| [8cc22f] | 239 | _atom->UpdateStep(_step); | 
|---|
| [7e51e1] | 240 | } | 
|---|
|  | 241 | } | 
|---|
|  | 242 |  | 
|---|
| [8cc22f] | 243 | void MoleCuilder::addNewStep(const std::vector<atomId_t> &_ids, const unsigned int _step) | 
|---|
| [7e51e1] | 244 | { | 
|---|
|  | 245 | for(size_t i=0; i< _ids.size(); ++i) { | 
|---|
|  | 246 | atom * const _atom = World::getInstance().getAtom(AtomById(_ids[i])); | 
|---|
| [8cc22f] | 247 | _atom->UpdateStep(_step); | 
|---|
| [7e51e1] | 248 | } | 
|---|
|  | 249 | } | 
|---|
|  | 250 |  | 
|---|
|  | 251 | std::vector<atomId_t> MoleCuilder::getIdsFromAtomicInfo(const std::vector<AtomicInfo> &movedatoms) | 
|---|
|  | 252 | { | 
|---|
|  | 253 | std::vector<atomId_t> ids(movedatoms.size(), (size_t)-1); | 
|---|
|  | 254 | std::transform( | 
|---|
|  | 255 | movedatoms.begin(), movedatoms.end(), | 
|---|
|  | 256 | ids.begin(), | 
|---|
|  | 257 | boost::bind(&AtomicInfo::getId, _1)); | 
|---|
|  | 258 | return ids; | 
|---|
|  | 259 | } | 
|---|