| [57dd40] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [57dd40] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * UndoRedoHelpers.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Apr 5, 2012
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "UndoRedoHelpers.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include <boost/foreach.hpp>
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include "Atom/atom.hpp"
 | 
|---|
| [8ea3e7] | 43 | #include "molecule.hpp"
 | 
|---|
| [57dd40] | 44 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [8ea3e7] | 45 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| [57dd40] | 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 47 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 48 | #include "World.hpp"
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | bool MoleCuilder::AddAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
 | 
|---|
 | 51 | {
 | 
|---|
 | 52 |   size_t i=0;
 | 
|---|
 | 53 |   for (; i<atoms.size(); ++i) {
 | 
|---|
 | 54 |     // re-create the atom
 | 
|---|
| [af9be32] | 55 |     LOG(3, "DEBUG: Re-adding atom " << atoms[i].getId() << ".");
 | 
|---|
| [57dd40] | 56 |     atom *Walker = World::getInstance().createAtom();
 | 
|---|
 | 57 |     if (!atoms[i].setAtom(*Walker)) {
 | 
|---|
 | 58 |       ELOG(1, "Failed to set id.");
 | 
|---|
 | 59 |       World::getInstance().destroyAtom(Walker);
 | 
|---|
 | 60 |       break;
 | 
|---|
 | 61 |     }
 | 
|---|
 | 62 |   }
 | 
|---|
 | 63 |   if (i<atoms.size()) {
 | 
|---|
 | 64 |     // remove all previous ones, too
 | 
|---|
 | 65 |     for (size_t j=0;j<i;++j)
 | 
|---|
 | 66 |       World::getInstance().destroyAtom(atoms[j].getId());
 | 
|---|
 | 67 |     // and announce the failure
 | 
|---|
 | 68 |     return false;
 | 
|---|
 | 69 |   }
 | 
|---|
 | 70 |   return true;
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | void MoleCuilder::RemoveAtomsFromAtomicInfo(std::vector<AtomicInfo> &atoms)
 | 
|---|
 | 74 | {
 | 
|---|
 | 75 |   BOOST_FOREACH(const AtomicInfo &_atom, atoms) {
 | 
|---|
 | 76 |     World::getInstance().destroyAtom(_atom.getId());
 | 
|---|
 | 77 |   }
 | 
|---|
 | 78 | }
 | 
|---|
 | 79 | 
 | 
|---|
| [af9be32] | 80 | void MoleCuilder::StoreBondInformationFromAtoms(
 | 
|---|
 | 81 |     const std::vector<const atom*> &atoms,
 | 
|---|
 | 82 |     std::vector< BondInfo > &bonds)
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   ASSERT( bonds.empty(),
 | 
|---|
 | 85 |       "StoreBondInformationFromAtoms() - give bonds vector is not empty.");
 | 
|---|
 | 86 |   bonds.reserve(atoms.size()*4);
 | 
|---|
 | 87 |   for (std::vector<const atom*>::const_iterator atomiter = atoms.begin();
 | 
|---|
 | 88 |       atomiter != atoms.end(); ++atomiter) {
 | 
|---|
 | 89 |     const BondList & _atom_bonds = (*atomiter)->getListOfBonds();
 | 
|---|
 | 90 |     for(BondList::const_iterator iter = _atom_bonds.begin(); iter != _atom_bonds.end(); ++iter)
 | 
|---|
 | 91 |       bonds.push_back( BondInfo(*iter) );
 | 
|---|
 | 92 |   }
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 | bool MoleCuilder::AddBondsFromBondInfo(const std::vector< BondInfo > &bonds)
 | 
|---|
 | 96 | {
 | 
|---|
 | 97 |   bool status = true;
 | 
|---|
 | 98 |   for(std::vector< BondInfo >::const_iterator iter = bonds.begin();
 | 
|---|
 | 99 |       iter != bonds.end(); ++iter)
 | 
|---|
 | 100 |     if (!(*iter).RecreateBond())
 | 
|---|
 | 101 |       status = false;
 | 
|---|
 | 102 |   return status;
 | 
|---|
 | 103 | }
 | 
|---|
 | 104 | 
 | 
|---|
| [57dd40] | 105 | void MoleCuilder::SetAtomsFromAtomicInfo(std::vector<AtomicInfo> &movedatoms)
 | 
|---|
 | 106 | {
 | 
|---|
 | 107 |   BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
 | 
|---|
 | 108 |     const atomId_t id = _atominfo.getId();
 | 
|---|
 | 109 |     atom * const _atom = World::getInstance().getAtom(AtomById(id));
 | 
|---|
 | 110 |     ASSERT( _atom != NULL,
 | 
|---|
 | 111 |         "MoleCuilder::SetAtomsFromAtomicInfo() - cannot find atom with id "
 | 
|---|
 | 112 |         +toString(id)+" in the world.");
 | 
|---|
 | 113 |     _atominfo.setAtom( *_atom );
 | 
|---|
 | 114 |   }
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | void MoleCuilder::SelectAtomsFromAtomicInfo(std::vector<AtomicInfo> &movedatoms)
 | 
|---|
 | 118 | {
 | 
|---|
 | 119 |   BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
 | 
|---|
 | 120 |     const atomId_t id = _atominfo.getId();
 | 
|---|
 | 121 |     World::getInstance().selectAtom(id);
 | 
|---|
 | 122 |   }
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void MoleCuilder::ResetAtomPosition(const std::vector<AtomicInfo> &movedatoms, const std::vector<Vector> &MovedToVector)
 | 
|---|
 | 126 | {
 | 
|---|
 | 127 |   std::vector<Vector>::const_iterator positer = MovedToVector.begin();
 | 
|---|
 | 128 |   ASSERT(movedatoms.size() == MovedToVector.size(),
 | 
|---|
 | 129 |       "MoleCuilder::ResetAtomPosition() -  the number of atoms "
 | 
|---|
 | 130 |       +toString(movedatoms.size())+" and the number of positions "
 | 
|---|
 | 131 |       +toString(MovedToVector.size())+" is not the same.");
 | 
|---|
 | 132 |   BOOST_FOREACH( const AtomicInfo &_atominfo, movedatoms) {
 | 
|---|
 | 133 |     const atomId_t id = _atominfo.getId();
 | 
|---|
 | 134 |     atom * const _atom = World::getInstance().getAtom(AtomById(id));
 | 
|---|
 | 135 |     ASSERT( _atom != NULL,
 | 
|---|
 | 136 |         "FillSphericalSurfaceAction::performRedo() - cannot find atom with id "
 | 
|---|
 | 137 |         +toString(id)+" in the world.");
 | 
|---|
 | 138 |     _atom->setPosition( *positer );
 | 
|---|
 | 139 |     ++positer;
 | 
|---|
 | 140 |   }
 | 
|---|
 | 141 | }
 | 
|---|
| [8ea3e7] | 142 | 
 | 
|---|
 | 143 | void MoleCuilder::RemoveMoleculesWithAtomsByIds(const std::vector<moleculeId_t> &ids)
 | 
|---|
 | 144 | {
 | 
|---|
 | 145 |   for (std::vector<moleculeId_t>::const_iterator iter = ids.begin();
 | 
|---|
 | 146 |       iter != ids.end(); ++iter) {
 | 
|---|
 | 147 |     molecule * const mol = World::getInstance().getMolecule(MoleculeById(*iter));
 | 
|---|
 | 148 |     if (mol != NULL) {
 | 
|---|
 | 149 |       mol->removeAtomsinMolecule();
 | 
|---|
 | 150 |       World::getInstance().destroyMolecule(mol);
 | 
|---|
 | 151 |     }
 | 
|---|
 | 152 |   }
 | 
|---|
 | 153 | }
 | 
|---|