[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 |
|
---|
| 54 | bool MoleCuilder::AddAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
|
---|
| 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 |
|
---|
| 77 | void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
|
---|
| 78 | {
|
---|
| 79 | BOOST_FOREACH(const AtomicInfo &_atom, atoms) {
|
---|
| 80 | World::getInstance().destroyAtom(_atom.getId());
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[af9be32] | 84 | void MoleCuilder::StoreBondInformationFromAtoms(
|
---|
| 85 | const std::vector<const atom*> &atoms,
|
---|
| 86 | std::vector< BondInfo > &bonds)
|
---|
| 87 | {
|
---|
| 88 | ASSERT( bonds.empty(),
|
---|
| 89 | "StoreBondInformationFromAtoms() - give bonds vector is not empty.");
|
---|
| 90 | bonds.reserve(atoms.size()*4);
|
---|
| 91 | for (std::vector<const atom*>::const_iterator atomiter = atoms.begin();
|
---|
| 92 | atomiter != atoms.end(); ++atomiter) {
|
---|
| 93 | const BondList & _atom_bonds = (*atomiter)->getListOfBonds();
|
---|
| 94 | for(BondList::const_iterator iter = _atom_bonds.begin(); iter != _atom_bonds.end(); ++iter)
|
---|
| 95 | bonds.push_back( BondInfo(*iter) );
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds)
|
---|
| 100 | {
|
---|
| 101 | bool status = true;
|
---|
| 102 | for(std::vector< BondInfo >::const_iterator iter = bonds.begin();
|
---|
| 103 | iter != bonds.end(); ++iter)
|
---|
| 104 | if (!(*iter).RecreateBond())
|
---|
| 105 | status = false;
|
---|
| 106 | return status;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[7e51e1] | 109 | void MoleCuilder::SetAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
|
---|
[57dd40] | 110 | {
|
---|
[7e51e1] | 111 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
|
---|
[57dd40] | 112 | const atomId_t id = _atominfo.getId();
|
---|
| 113 | atom * const _atom = World::getInstance().getAtom(AtomById(id));
|
---|
| 114 | ASSERT( _atom != NULL,
|
---|
| 115 | "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
|
---|
| 116 | +toString(id)+" in the world.");
|
---|
| 117 | _atominfo.setAtom( *_atom );
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[7e51e1] | 121 | void MoleCuilder::SelectAtomsFromAtomicInfo(const std::vector<AtomicInfo> &_movedatoms)
|
---|
[57dd40] | 122 | {
|
---|
[7e51e1] | 123 | BOOST_FOREACH( const AtomicInfo &_atominfo, _movedatoms) {
|
---|
[57dd40] | 124 | const atomId_t id = _atominfo.getId();
|
---|
| 125 | World::getInstance().selectAtom(id);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector)
|
---|
[7e51e1] | 130 | {
|
---|
| 131 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 132 | boost::bind(&atom::setPosition, _1, _2);
|
---|
| 133 | ResetByFunction(movedatoms, MovedToVector, setter);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void MoleCuilder::ResetAtomVelocity(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &VelocityVector)
|
---|
| 137 | {
|
---|
| 138 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 139 | boost::bind(&atom::setAtomicVelocity, _1, _2);
|
---|
| 140 | ResetByFunction(movedatoms, VelocityVector, setter);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | void MoleCuilder::ResetAtomForce(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &ForceVector)
|
---|
| 144 | {
|
---|
| 145 | boost::function<void(atom *, const Vector&)> setter =
|
---|
| 146 | boost::bind(&atom::setAtomicForce, _1, _2);
|
---|
| 147 | ResetByFunction(movedatoms, ForceVector, setter);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void MoleCuilder::ResetByFunction(
|
---|
| 151 | const std::vector<AtomicInfo> &movedatoms,
|
---|
| 152 | const std::vector<Vector> &MovedToVector,
|
---|
| 153 | boost::function<void(atom *, const Vector&)> &setter)
|
---|
[57dd40] | 154 | {
|
---|
| 155 | std::vector<Vector>::const_iterator positer = MovedToVector.begin();
|
---|
| 156 | ASSERT(movedatoms.size() == MovedToVector.size(),
|
---|
| 157 | "MoleCuilder::ResetAtomPosition() - the number of atoms "
|
---|
| 158 | +toString(movedatoms.size())+" and the number of positions "
|
---|
| 159 | +toString(MovedToVector.size())+" is not the same.");
|
---|
| 160 | BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
|
---|
| 161 | const atomId_t id = _atominfo.getId();
|
---|
| 162 | atom * const _atom = World::getInstance().getAtom(AtomById(id));
|
---|
| 163 | ASSERT( _atom != NULL,
|
---|
| 164 | "FillSphericalSurfaceAction::performRedo() - cannot find atom with id "
|
---|
| 165 | +toString(id)+" in the world.");
|
---|
[7e51e1] | 166 | setter(_atom, *positer );
|
---|
[57dd40] | 167 | ++positer;
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
[8ea3e7] | 170 |
|
---|
| 171 | void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids)
|
---|
| 172 | {
|
---|
| 173 | for (std::vector<moleculeId_t>::const_iterator iter = ids.begin();
|
---|
| 174 | iter != ids.end(); ++iter) {
|
---|
| 175 | molecule * const mol = World::getInstance().getMolecule(MoleculeById(*iter));
|
---|
| 176 | if (mol != NULL) {
|
---|
| 177 | mol->removeAtomsinMolecule();
|
---|
| 178 | World::getInstance().destroyMolecule(mol);
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[7e51e1] | 182 |
|
---|
| 183 | void MoleCuilder::removeLastStep(const std::vector<atomId_t> &_atoms)
|
---|
| 184 | {
|
---|
| 185 | for (size_t i=0; i<_atoms.size(); ++i) {
|
---|
| 186 | atom * const _atom = World::getInstance().getAtom(AtomById(_atoms[i]));
|
---|
| 187 | _atom->removeSteps();
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void MoleCuilder::addNewStep(const std::vector<AtomicInfo> &_movedatoms)
|
---|
| 192 | {
|
---|
| 193 | for(size_t i=0; i< _movedatoms.size(); ++i) {
|
---|
| 194 | atom * const _atom = World::getInstance().getAtom(AtomById(_movedatoms[i].getId()));
|
---|
| 195 | _atom->UpdateSteps();
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | void MoleCuilder::addNewStep(const std::vector<atomId_t> &_ids)
|
---|
| 200 | {
|
---|
| 201 | for(size_t i=0; i< _ids.size(); ++i) {
|
---|
| 202 | atom * const _atom = World::getInstance().getAtom(AtomById(_ids[i]));
|
---|
| 203 | _atom->UpdateSteps();
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | std::vector<atomId_t> MoleCuilder::getIdsFromAtomicInfo(const std::vector<AtomicInfo> &movedatoms)
|
---|
| 208 | {
|
---|
| 209 | std::vector<atomId_t> ids(movedatoms.size(), (size_t)-1);
|
---|
| 210 | std::transform(
|
---|
| 211 | movedatoms.begin(), movedatoms.end(),
|
---|
| 212 | ids.begin(),
|
---|
| 213 | boost::bind(&AtomicInfo::getId, _1));
|
---|
| 214 | return ids;
|
---|
| 215 | }
|
---|