[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 |
|
---|
| 37 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 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() << ".");
|
---|
[57dd40] | 60 | atom *Walker = World::getInstance().createAtom();
|
---|
| 61 | if (!atoms[i].setAtom(*Walker)) {
|
---|
| 62 | ELOG(1, "Failed to set id.");
|
---|
| 63 | World::getInstance().destroyAtom(Walker);
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | if (i<atoms.size()) {
|
---|
| 68 | // remove all previous ones, too
|
---|
| 69 | for (size_t j=0;j<i;++j)
|
---|
| 70 | World::getInstance().destroyAtom(atoms[j].getId());
|
---|
| 71 | // and announce the failure
|
---|
| 72 | return false;
|
---|
| 73 | }
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[596cfa] | 77 | bool MoleCuilder::AddMoleculesFromAtomicInfo(std::map< moleculeId_t, std::vector<AtomicInfo> > &mol_atoms)
|
---|
| 78 | {
|
---|
| 79 | bool status = true;
|
---|
| 80 | for (std::map< moleculeId_t, std::vector<AtomicInfo> >::const_iterator iter = mol_atoms.begin();
|
---|
| 81 | iter != mol_atoms.end(); ++iter) {
|
---|
| 82 | // re-create the atom
|
---|
| 83 | LOG(3, "DEBUG: Re-adding molecule " << iter->first << ".");
|
---|
| 84 | molecule *mol_Walker = World::getInstance().createMolecule();
|
---|
| 85 |
|
---|
| 86 | // reset the mol id
|
---|
| 87 | bool status = true;
|
---|
| 88 | if (mol_Walker->getId() != iter->first)
|
---|
| 89 | status &= mol_Walker->changeId(iter->first);
|
---|
| 90 |
|
---|
| 91 | // add all its atoms
|
---|
| 92 | status &= AddAtomsFromAtomicInfo(iter->second);
|
---|
| 93 | }
|
---|
| 94 | if (!status) {
|
---|
| 95 | // remove all molecules again
|
---|
| 96 | for (std::map< moleculeId_t, std::vector<AtomicInfo> >::const_iterator iter = mol_atoms.begin();
|
---|
| 97 | iter != mol_atoms.end(); ++iter) {
|
---|
| 98 | molecule * mol = World::getInstance().getMolecule(MoleculeById(iter->first));
|
---|
| 99 | if (mol != NULL)
|
---|
| 100 | removeAtomsinMolecule(mol);
|
---|
| 101 | }
|
---|
| 102 | // and announce the failure
|
---|
| 103 | return false;
|
---|
| 104 | }
|
---|
| 105 | return true;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[57dd40] | 108 | void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
|
---|
| 109 | {
|
---|
| 110 | BOOST_FOREACH(const AtomicInfo &_atom, atoms) {
|
---|
| 111 | World::getInstance().destroyAtom(_atom.getId());
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[af9be32] | 115 | void MoleCuilder::StoreBondInformationFromAtoms(
|
---|
| 116 | const std::vector<const atom*> &atoms,
|
---|
| 117 | std::vector< BondInfo > &bonds)
|
---|
| 118 | {
|
---|
| 119 | ASSERT( bonds.empty(),
|
---|
| 120 | "StoreBondInformationFromAtoms() - give bonds vector is not empty.");
|
---|
| 121 | bonds.reserve(atoms.size()*4);
|
---|
| 122 | for (std::vector<const atom*>::const_iterator atomiter = atoms.begin();
|
---|
| 123 | atomiter != atoms.end(); ++atomiter) {
|
---|
| 124 | const BondList & _atom_bonds = (*atomiter)->getListOfBonds();
|
---|
| 125 | for(BondList::const_iterator iter = _atom_bonds.begin(); iter != _atom_bonds.end(); ++iter)
|
---|
| 126 | bonds.push_back( BondInfo(*iter) );
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds)
|
---|
| 131 | {
|
---|
| 132 | bool status = true;
|
---|
[917300] | 133 | std::vector< BondInfo >::const_iterator iter = bonds.begin();
|
---|
| 134 | for(;iter != bonds.end(); ++iter) {
|
---|
| 135 | if (!(*iter).RecreateBond()) {
|
---|
[af9be32] | 136 | status = false;
|
---|
[917300] | 137 | break;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | if (!status) {
|
---|
| 141 | // remove all added bonds again
|
---|
| 142 | for(std::vector< BondInfo >::const_iterator removeiter = bonds.begin();
|
---|
| 143 | removeiter != iter; ++removeiter) {
|
---|
| 144 | removeiter->RemoveBond();
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
[af9be32] | 147 | return status;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[7e51e1] | 150 | void MoleCuilder::SetAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
|
---|
[57dd40] | 151 | {
|
---|
[7e51e1] | 152 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
|
---|
[57dd40] | 153 | const atomId_t id = _atominfo.getId();
|
---|
| 154 | atom * const _atom = World::getInstance().getAtom(AtomById(id));
|
---|
| 155 | ASSERT( _atom != NULL,
|
---|
| 156 | "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
|
---|
| 157 | +toString(id)+" in the world.");
|
---|
| 158 | _atominfo.setAtom( *_atom );
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[7e51e1] | 162 | void MoleCuilder::SelectAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
|
---|
[57dd40] | 163 | {
|
---|
[7e51e1] | 164 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
|
---|
[57dd40] | 165 | const atomId_t id = _atominfo.getId();
|
---|
| 166 | World::getInstance().selectAtom(id);
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector)
|
---|
[7e51e1] | 171 | {
|
---|
| 172 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 173 | boost::bind(&atom::setPosition, _1, _2);
|
---|
| 174 | ResetByFunction(movedatoms, MovedToVector, setter);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | void MoleCuilder::ResetAtomVelocity(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &VelocityVector)
|
---|
| 178 | {
|
---|
| 179 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 180 | boost::bind(&atom::setAtomicVelocity, _1, _2);
|
---|
| 181 | ResetByFunction(movedatoms, VelocityVector, setter);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void MoleCuilder::ResetAtomForce(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &ForceVector)
|
---|
| 185 | {
|
---|
| 186 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 187 | boost::bind(&atom::setAtomicForce, _1, _2);
|
---|
| 188 | ResetByFunction(movedatoms, ForceVector, setter);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void MoleCuilder::ResetByFunction(
|
---|
| 192 | const std::vector<AtomicInfo> &movedatoms,
|
---|
| 193 | const std::vector<Vector> &MovedToVector,
|
---|
| 194 | boost::function<void(atom *, const Vector&)> &setter)
|
---|
[57dd40] | 195 | {
|
---|
| 196 | std::vector<Vector>::const_iterator positer = MovedToVector.begin();
|
---|
| 197 | ASSERT(movedatoms.size() == MovedToVector.size(),
|
---|
| 198 | "MoleCuilder::ResetAtomPosition() - the number of atoms "
|
---|
| 199 | +toString(movedatoms.size())+" and the number of positions "
|
---|
| 200 | +toString(MovedToVector.size())+" is not the same.");
|
---|
| 201 | BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
|
---|
| 202 | const atomId_t id = _atominfo.getId();
|
---|
| 203 | atom * const _atom = World::getInstance().getAtom(AtomById(id));
|
---|
| 204 | ASSERT( _atom != NULL,
|
---|
| 205 | "FillSphericalSurfaceAction::performRedo() - cannot find atom with id "
|
---|
| 206 | +toString(id)+" in the world.");
|
---|
[7e51e1] | 207 | setter(_atom, *positer );
|
---|
[57dd40] | 208 | ++positer;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
[8ea3e7] | 211 |
|
---|
| 212 | void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids)
|
---|
| 213 | {
|
---|
| 214 | for (std::vector<moleculeId_t>::const_iterator iter = ids.begin();
|
---|
| 215 | iter != ids.end(); ++iter) {
|
---|
[a7aebd] | 216 | molecule * mol = World::getInstance().getMolecule(MoleculeById(*iter));
|
---|
[8ea3e7] | 217 | if (mol != NULL) {
|
---|
[a7aebd] | 218 | removeAtomsinMolecule(mol);
|
---|
| 219 | // molecules are automatically removed when empty
|
---|
[8ea3e7] | 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
[7e51e1] | 223 |
|
---|
[8cc22f] | 224 | void MoleCuilder::removeLastStep(const std::vector<atomId_t> &_atoms, const unsigned int _step)
|
---|
[7e51e1] | 225 | {
|
---|
| 226 | for (size_t i=0; i<_atoms.size(); ++i) {
|
---|
| 227 | atom * const _atom = World::getInstance().getAtom(AtomById(_atoms[i]));
|
---|
[8cc22f] | 228 | _atom->removeStep(_step);
|
---|
[7e51e1] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[8cc22f] | 232 | void MoleCuilder::addNewStep(const std::vector<AtomicInfo> &_movedatoms, const unsigned int _step)
|
---|
[7e51e1] | 233 | {
|
---|
| 234 | for(size_t i=0; i< _movedatoms.size(); ++i) {
|
---|
| 235 | atom * const _atom = World::getInstance().getAtom(AtomById(_movedatoms[i].getId()));
|
---|
[8cc22f] | 236 | _atom->UpdateStep(_step);
|
---|
[7e51e1] | 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[8cc22f] | 240 | void MoleCuilder::addNewStep(const std::vector<atomId_t> &_ids, const unsigned int _step)
|
---|
[7e51e1] | 241 | {
|
---|
| 242 | for(size_t i=0; i< _ids.size(); ++i) {
|
---|
| 243 | atom * const _atom = World::getInstance().getAtom(AtomById(_ids[i]));
|
---|
[8cc22f] | 244 | _atom->UpdateStep(_step);
|
---|
[7e51e1] | 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | std::vector<atomId_t> MoleCuilder::getIdsFromAtomicInfo(const std::vector<AtomicInfo> &movedatoms)
|
---|
| 249 | {
|
---|
| 250 | std::vector<atomId_t> ids(movedatoms.size(), (size_t)-1);
|
---|
| 251 | std::transform(
|
---|
| 252 | movedatoms.begin(), movedatoms.end(),
|
---|
| 253 | ids.begin(),
|
---|
| 254 | boost::bind(&AtomicInfo::getId, _1));
|
---|
| 255 | return ids;
|
---|
| 256 | }
|
---|