| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 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/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [5d1611] | 24 | /*
 | 
|---|
 | 25 |  * World.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Feb 3, 2010
 | 
|---|
 | 28 |  *      Author: crueger
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 37 | 
 | 
|---|
| [5d1611] | 38 | #include "World.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [90c4280] | 40 | #include <functional>
 | 
|---|
| [5d1611] | 41 | 
 | 
|---|
| [3139b2] | 42 | #include "Actions/ActionTrait.hpp"
 | 
|---|
| [d297a3] | 43 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
| [6f0841] | 44 | #include "Atom/atom.hpp"
 | 
|---|
| [d297a3] | 45 | #include "Box.hpp"
 | 
|---|
 | 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [8e1f7af] | 47 | #include "config.hpp"
 | 
|---|
| [fc1b24] | 48 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
| [865a945] | 49 | #include "Descriptors/AtomDescriptor_impl.hpp"
 | 
|---|
| [c1d837] | 50 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [ebc499] | 51 | #include "Descriptors/AtomSelectionDescriptor.hpp"
 | 
|---|
| [1c51c8] | 52 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
 | 53 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
 | 
|---|
| [c1d837] | 54 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| [ebc499] | 55 | #include "Descriptors/MoleculeSelectionDescriptor.hpp"
 | 
|---|
| [feb5d0] | 56 | #include "Descriptors/SelectiveConstIterator_impl.hpp"
 | 
|---|
| [6e97e5] | 57 | #include "Descriptors/SelectiveIterator_impl.hpp"
 | 
|---|
| [42127c] | 58 | #include "Element/periodentafel.hpp"
 | 
|---|
| [98dbee] | 59 | #include "Fragmentation/Homology/HomologyContainer.hpp"
 | 
|---|
| [3139b2] | 60 | #include "Graph/BondGraph.hpp"
 | 
|---|
| [4b8630] | 61 | #include "Graph/DepthFirstSearchAnalysis.hpp"
 | 
|---|
| [e4fe8d] | 62 | #include "Helpers/defs.hpp"
 | 
|---|
| [d297a3] | 63 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| [4834f4] | 64 | #include "LinkedCell/LinkedCell_Controller.hpp"
 | 
|---|
 | 65 | #include "LinkedCell/PointCloudAdaptor.hpp"
 | 
|---|
| [d297a3] | 66 | #include "molecule.hpp"
 | 
|---|
| [42127c] | 67 | #include "MoleculeListClass.hpp"
 | 
|---|
| [ab26c3] | 68 | #include "Thermostats/ThermoStatContainer.hpp"
 | 
|---|
| [d297a3] | 69 | #include "WorldTime.hpp"
 | 
|---|
| [d346b6] | 70 | 
 | 
|---|
| [3e4fb6] | 71 | #include "IdPool_impl.hpp"
 | 
|---|
 | 72 | 
 | 
|---|
| [4834f4] | 73 | #include "CodePatterns/IteratorAdaptors.hpp"
 | 
|---|
| [ad011c] | 74 | #include "CodePatterns/Singleton_impl.hpp"
 | 
|---|
| [02ce36] | 75 | #include "CodePatterns/Observer/Channels.hpp"
 | 
|---|
 | 76 | #include "CodePatterns/Observer/ObservedContainer_impl.hpp"
 | 
|---|
| [23b547] | 77 | 
 | 
|---|
| [ce7fdc] | 78 | using namespace MoleCuilder;
 | 
|---|
| [4d9c01] | 79 | 
 | 
|---|
| [7188b1] | 80 | /******************************* Notifications ************************/
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 | atom* World::_lastchangedatom = NULL;
 | 
|---|
 | 84 | molecule* World::_lastchangedmol = NULL;
 | 
|---|
 | 85 | 
 | 
|---|
| [5d1611] | 86 | /******************************* getter and setter ************************/
 | 
|---|
| [f71baf] | 87 | periodentafel *&World::getPeriode()
 | 
|---|
 | 88 | {
 | 
|---|
| [5d1611] | 89 |   return periode;
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
| [f71baf] | 92 | BondGraph *&World::getBondGraph()
 | 
|---|
 | 93 | {
 | 
|---|
 | 94 |   return BG;
 | 
|---|
 | 95 | }
 | 
|---|
 | 96 | 
 | 
|---|
| [98dbee] | 97 | HomologyContainer &World::getHomologies()
 | 
|---|
 | 98 | {
 | 
|---|
 | 99 |   return *homologies;
 | 
|---|
 | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
 | 102 | void World::resetHomologies(HomologyContainer *&_homologies)
 | 
|---|
 | 103 | {
 | 
|---|
 | 104 |   HomologyContainer *oldhomologies = homologies;
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |   // install new instance, resetting given pointer
 | 
|---|
 | 107 |   homologies = _homologies;
 | 
|---|
 | 108 |   _homologies = NULL;
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   // delete old instance which also informs all observers
 | 
|---|
 | 111 |   delete oldhomologies;
 | 
|---|
 | 112 | }
 | 
|---|
 | 113 | 
 | 
|---|
| [f71baf] | 114 | void World::setBondGraph(BondGraph *_BG){
 | 
|---|
 | 115 |   delete (BG);
 | 
|---|
 | 116 |   BG = _BG;
 | 
|---|
 | 117 | }
 | 
|---|
 | 118 | 
 | 
|---|
| [8e1f7af] | 119 | config *&World::getConfig(){
 | 
|---|
 | 120 |   return configuration;
 | 
|---|
 | 121 | }
 | 
|---|
 | 122 | 
 | 
|---|
| [1c51c8] | 123 | // Atoms
 | 
|---|
 | 124 | 
 | 
|---|
| [7a1ce5] | 125 | atom* World::getAtom(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 126 |   return descriptor.find();
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
| [4d72e4] | 129 | World::AtomComposite World::getAllAtoms(AtomDescriptor descriptor){
 | 
|---|
| [fc1b24] | 130 |   return descriptor.findAll();
 | 
|---|
 | 131 | }
 | 
|---|
 | 132 | 
 | 
|---|
| [4d72e4] | 133 | World::AtomComposite World::getAllAtoms(){
 | 
|---|
| [0e2a47] | 134 |   return getAllAtoms(AllAtoms());
 | 
|---|
 | 135 | }
 | 
|---|
 | 136 | 
 | 
|---|
| [354859] | 137 | int World::numAtoms(){
 | 
|---|
 | 138 |   return atoms.size();
 | 
|---|
 | 139 | }
 | 
|---|
 | 140 | 
 | 
|---|
| [1c51c8] | 141 | // Molecules
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
 | 
|---|
 | 144 |   return descriptor.find();
 | 
|---|
 | 145 | }
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
 | 
|---|
 | 148 |   return descriptor.findAll();
 | 
|---|
 | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
| [97ebf8] | 151 | std::vector<molecule*> World::getAllMolecules(){
 | 
|---|
 | 152 |   return getAllMolecules(AllMolecules());
 | 
|---|
 | 153 | }
 | 
|---|
 | 154 | 
 | 
|---|
| [354859] | 155 | int World::numMolecules(){
 | 
|---|
 | 156 |   return molecules_deprecated->ListOfMolecules.size();
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
| [5f612ee] | 159 | // system
 | 
|---|
 | 160 | 
 | 
|---|
| [84c494] | 161 | Box& World::getDomain() {
 | 
|---|
 | 162 |   return *cell_size;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
| [cca9ef] | 165 | void World::setDomain(const RealSpaceMatrix &mat){
 | 
|---|
| [be97a8] | 166 |   OBSERVE;
 | 
|---|
| [84c494] | 167 |   *cell_size = mat;
 | 
|---|
| [5f612ee] | 168 | }
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | void World::setDomain(double * matrix)
 | 
|---|
 | 171 | {
 | 
|---|
| [b9c847] | 172 |   OBSERVE;
 | 
|---|
| [cca9ef] | 173 |   RealSpaceMatrix M = ReturnFullMatrixforSymmetric(matrix);
 | 
|---|
| [84c494] | 174 |   cell_size->setM(M);
 | 
|---|
| [5f612ee] | 175 | }
 | 
|---|
 | 176 | 
 | 
|---|
| [03abd0] | 177 | LinkedCell::LinkedCell_View World::getLinkedCell(double distance)
 | 
|---|
| [4834f4] | 178 | {
 | 
|---|
| [d067e35] | 179 |   ASSERT( distance >= 0,
 | 
|---|
| [03abd0] | 180 |       "World::getLinkedCell() - distance is not positive.");
 | 
|---|
 | 181 |   if (distance < 1.) {
 | 
|---|
 | 182 |     ELOG(2, "Linked cell grid with length less than 1. is very memory-intense!");
 | 
|---|
 | 183 |     distance = 1.;
 | 
|---|
 | 184 |   }
 | 
|---|
| [4834f4] | 185 |   // we have to grope past the ObservedContainer mechanism and transmorph the map
 | 
|---|
 | 186 |   // into a traversable list for the adaptor
 | 
|---|
 | 187 |   PointCloudAdaptor< AtomSet::set_t, MapValueIterator<AtomSet::set_t::iterator> > atomset(
 | 
|---|
 | 188 |       &(atoms.getContent()),
 | 
|---|
 | 189 |       std::string("WorldsAtoms"));
 | 
|---|
 | 190 |   return LCcontroller->getView(distance, atomset);
 | 
|---|
 | 191 | }
 | 
|---|
 | 192 | 
 | 
|---|
| [2a8731] | 193 | const unsigned World::getTime() const
 | 
|---|
 | 194 | {
 | 
|---|
 | 195 |   return WorldTime::getTime();
 | 
|---|
 | 196 | }
 | 
|---|
 | 197 | 
 | 
|---|
| [388ddd] | 198 | bool areBondsPresent(const unsigned int _step)
 | 
|---|
 | 199 | {
 | 
|---|
 | 200 |   bool status = false;
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |   for (World::AtomConstIterator iter = const_cast<const World &>(World::getInstance()).getAtomIter();
 | 
|---|
 | 203 |       (!status) && (iter != const_cast<const World &>(World::getInstance()).atomEnd()); ++iter) {
 | 
|---|
 | 204 |     const atom * const Walker = *iter;
 | 
|---|
 | 205 |     status |= !Walker->getListOfBondsAtStep(_step).empty();
 | 
|---|
 | 206 |   }
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 |   return status;
 | 
|---|
 | 209 | }
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 | void copyBondgraph(const unsigned int _srcstep, const unsigned int _deststep)
 | 
|---|
 | 212 | {
 | 
|---|
 | 213 |   // gather all bonds from _srcstep
 | 
|---|
 | 214 |   std::set<bond *> SetOfBonds;
 | 
|---|
 | 215 |   for (World::AtomConstIterator iter = const_cast<const World &>(World::getInstance()).getAtomIter();
 | 
|---|
 | 216 |       iter != const_cast<const World &>(World::getInstance()).atomEnd(); ++iter) {
 | 
|---|
 | 217 |     const atom * const Walker = *iter;
 | 
|---|
 | 218 |     const BondList bonds = Walker->getListOfBondsAtStep(_srcstep);
 | 
|---|
 | 219 |     BOOST_FOREACH( bond::ptr bondptr, bonds) {
 | 
|---|
 | 220 |       SetOfBonds.insert(bondptr.get());
 | 
|---|
 | 221 |     }
 | 
|---|
 | 222 |   }
 | 
|---|
 | 223 |   LOG(4, "DEBUG: We gathered " << SetOfBonds.size() << " bonds in total.");
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 |   // copy bond to new time step
 | 
|---|
 | 226 |   for (std::set<bond *>::const_iterator bonditer = SetOfBonds.begin();
 | 
|---|
 | 227 |       bonditer != SetOfBonds.end(); ++bonditer) {
 | 
|---|
 | 228 |     const atom * const Walker = (*bonditer)->leftatom;
 | 
|---|
 | 229 |     const atom * const OtherWalker = (*bonditer)->rightatom;
 | 
|---|
| [0763ce] | 230 |     bond::ptr const _bond =
 | 
|---|
 | 231 |         const_cast<atom *>(Walker)->addBond(_deststep, const_cast<atom *>(OtherWalker));
 | 
|---|
 | 232 |     _bond->setDegree((*bonditer)->getDegree());
 | 
|---|
| [388ddd] | 233 |   }
 | 
|---|
 | 234 | }
 | 
|---|
 | 235 | 
 | 
|---|
| [d297a3] | 236 | void World::setTime(const unsigned int _step)
 | 
|---|
 | 237 | {
 | 
|---|
| [76163d] | 238 |   if (_step != WorldTime::getTime()) {
 | 
|---|
| [388ddd] | 239 |     const unsigned int oldstep = WorldTime::getTime();
 | 
|---|
| [46ce1c] | 240 | 
 | 
|---|
 | 241 |     // 1. copy bond graph (such not each addBond causes GUI update)
 | 
|---|
 | 242 |     if (!areBondsPresent(_step)) {
 | 
|---|
 | 243 | //      AtomComposite Set = getAllAtoms();
 | 
|---|
 | 244 | //      BG->cleanAdjacencyList(Set);
 | 
|---|
 | 245 |       copyBondgraph(oldstep, _step);
 | 
|---|
 | 246 |     }
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 |     // 2. set new time
 | 
|---|
| [040a5c] | 249 |     WorldTime::getInstance().setTime(_step);
 | 
|---|
| [46ce1c] | 250 | 
 | 
|---|
 | 251 |     // 4. scan for connected subgraphs => molecules
 | 
|---|
| [4b8630] | 252 |     DepthFirstSearchAnalysis DFS;
 | 
|---|
 | 253 |     DFS();
 | 
|---|
 | 254 |     DFS.UpdateMoleculeStructure();
 | 
|---|
| [76163d] | 255 |   }
 | 
|---|
| [d297a3] | 256 | }
 | 
|---|
 | 257 | 
 | 
|---|
| [387b36] | 258 | std::string World::getDefaultName() {
 | 
|---|
| [5f612ee] | 259 |   return defaultName;
 | 
|---|
 | 260 | }
 | 
|---|
 | 261 | 
 | 
|---|
| [387b36] | 262 | void World::setDefaultName(std::string name)
 | 
|---|
| [5f612ee] | 263 | {
 | 
|---|
| [be97a8] | 264 |   OBSERVE;
 | 
|---|
| [387b36] | 265 |   defaultName = name;
 | 
|---|
| [5f612ee] | 266 | };
 | 
|---|
 | 267 | 
 | 
|---|
| [43dad6] | 268 | class ThermoStatContainer * World::getThermostats()
 | 
|---|
 | 269 | {
 | 
|---|
 | 270 |   return Thermostats;
 | 
|---|
 | 271 | }
 | 
|---|
 | 272 | 
 | 
|---|
 | 273 | 
 | 
|---|
| [e4b5de] | 274 | int World::getExitFlag() {
 | 
|---|
 | 275 |   return ExitFlag;
 | 
|---|
 | 276 | }
 | 
|---|
 | 277 | 
 | 
|---|
 | 278 | void World::setExitFlag(int flag) {
 | 
|---|
 | 279 |   if (ExitFlag < flag)
 | 
|---|
 | 280 |     ExitFlag = flag;
 | 
|---|
 | 281 | }
 | 
|---|
| [5f612ee] | 282 | 
 | 
|---|
| [afb47f] | 283 | /******************** Methods to change World state *********************/
 | 
|---|
 | 284 | 
 | 
|---|
| [354859] | 285 | molecule* World::createMolecule(){
 | 
|---|
 | 286 |   OBSERVE;
 | 
|---|
 | 287 |   molecule *mol = NULL;
 | 
|---|
| [cbc5fb] | 288 |   mol = NewMolecule();
 | 
|---|
| [3e4fb6] | 289 |   moleculeId_t id = moleculeIdPool.getNextId();
 | 
|---|
| [127a8e] | 290 |   ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
 | 
|---|
 | 291 |   mol->setId(id);
 | 
|---|
| [244d26] | 292 |   // store the molecule by ID
 | 
|---|
| [cbc5fb] | 293 |   molecules[mol->getId()] = mol;
 | 
|---|
| [354859] | 294 |   mol->signOn(this);
 | 
|---|
| [7188b1] | 295 |   _lastchangedmol = mol;
 | 
|---|
 | 296 |   NOTIFY(MoleculeInserted);
 | 
|---|
| [354859] | 297 |   return mol;
 | 
|---|
 | 298 | }
 | 
|---|
 | 299 | 
 | 
|---|
| [cbc5fb] | 300 | void World::destroyMolecule(molecule* mol){
 | 
|---|
| [fa7989] | 301 |   ASSERT(mol,"Molecule that was meant to be destroyed did not exist");
 | 
|---|
| [cbc5fb] | 302 |   destroyMolecule(mol->getId());
 | 
|---|
 | 303 | }
 | 
|---|
 | 304 | 
 | 
|---|
 | 305 | void World::destroyMolecule(moleculeId_t id){
 | 
|---|
 | 306 |   molecule *mol = molecules[id];
 | 
|---|
| [6d574a] | 307 |   ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
 | 
|---|
| [38f991] | 308 |   // give notice about immediate removal
 | 
|---|
 | 309 |   {
 | 
|---|
 | 310 |     OBSERVE;
 | 
|---|
 | 311 |     _lastchangedmol = mol;
 | 
|---|
 | 312 |     NOTIFY(MoleculeRemoved);
 | 
|---|
 | 313 |   }
 | 
|---|
| [7d82a5] | 314 |   mol->signOff(this);
 | 
|---|
| [a7aebd] | 315 |   // TODO: removed when depcreated MoleculeListClass is gone
 | 
|---|
 | 316 |   molecules_deprecated->erase(mol);
 | 
|---|
| [cbc5fb] | 317 |   DeleteMolecule(mol);
 | 
|---|
| [38f991] | 318 |   if (isMoleculeSelected(id))
 | 
|---|
 | 319 |     selectedMolecules.erase(id);
 | 
|---|
| [cbc5fb] | 320 |   molecules.erase(id);
 | 
|---|
| [3e4fb6] | 321 |   moleculeIdPool.releaseId(id);
 | 
|---|
| [cbc5fb] | 322 | }
 | 
|---|
 | 323 | 
 | 
|---|
| [46d958] | 324 | atom *World::createAtom(){
 | 
|---|
 | 325 |   OBSERVE;
 | 
|---|
| [3e4fb6] | 326 |   atomId_t id = atomIdPool.getNextId();
 | 
|---|
| [127a8e] | 327 |   ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
 | 
|---|
| [88d586] | 328 |   atom *res = NewAtom(id);
 | 
|---|
| [46d958] | 329 |   res->setWorld(this);
 | 
|---|
| [244d26] | 330 |   // store the atom by ID
 | 
|---|
| [46d958] | 331 |   atoms[res->getId()] = res;
 | 
|---|
| [7188b1] | 332 |   _lastchangedatom = res;
 | 
|---|
 | 333 |   NOTIFY(AtomInserted);
 | 
|---|
| [46d958] | 334 |   return res;
 | 
|---|
 | 335 | }
 | 
|---|
 | 336 | 
 | 
|---|
| [5f612ee] | 337 | 
 | 
|---|
| [46d958] | 338 | int World::registerAtom(atom *atom){
 | 
|---|
 | 339 |   OBSERVE;
 | 
|---|
| [3e4fb6] | 340 |   atomId_t id = atomIdPool.getNextId();
 | 
|---|
| [88d586] | 341 |   atom->setId(id);
 | 
|---|
| [46d958] | 342 |   atom->setWorld(this);
 | 
|---|
 | 343 |   atoms[atom->getId()] = atom;
 | 
|---|
| [65d7ca] | 344 |   _lastchangedatom = atom;
 | 
|---|
 | 345 |   NOTIFY(AtomInserted);
 | 
|---|
| [46d958] | 346 |   return atom->getId();
 | 
|---|
 | 347 | }
 | 
|---|
 | 348 | 
 | 
|---|
 | 349 | void World::destroyAtom(atom* atom){
 | 
|---|
 | 350 |   int id = atom->getId();
 | 
|---|
 | 351 |   destroyAtom(id);
 | 
|---|
 | 352 | }
 | 
|---|
 | 353 | 
 | 
|---|
| [cbc5fb] | 354 | void World::destroyAtom(atomId_t id) {
 | 
|---|
| [46d958] | 355 |   atom *atom = atoms[id];
 | 
|---|
| [6d574a] | 356 |   ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
 | 
|---|
| [ab4a33] | 357 |   // give notice about immediate removal
 | 
|---|
 | 358 |   {
 | 
|---|
 | 359 |     OBSERVE;
 | 
|---|
 | 360 |     _lastchangedatom = atom;
 | 
|---|
 | 361 |     NOTIFY(AtomRemoved);
 | 
|---|
 | 362 |   }
 | 
|---|
| [a7aebd] | 363 |   // check if it's the last atom
 | 
|---|
 | 364 |   molecule *_mol = atom->getMolecule();
 | 
|---|
| [cad383] | 365 |   if ((_mol == NULL) || (_mol->getAtomCount() > 1))
 | 
|---|
| [a7aebd] | 366 |     _mol = NULL;
 | 
|---|
| [46d958] | 367 |   DeleteAtom(atom);
 | 
|---|
| [38f991] | 368 |   if (isAtomSelected(id))
 | 
|---|
 | 369 |     selectedAtoms.erase(id);
 | 
|---|
| [46d958] | 370 |   atoms.erase(id);
 | 
|---|
| [3e4fb6] | 371 |   atomIdPool.releaseId(id);
 | 
|---|
| [a7aebd] | 372 |   // remove molecule if empty
 | 
|---|
 | 373 |   if (_mol != NULL)
 | 
|---|
 | 374 |     destroyMolecule(_mol);
 | 
|---|
| [88d586] | 375 | }
 | 
|---|
 | 376 | 
 | 
|---|
 | 377 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
 | 
|---|
 | 378 |   OBSERVE;
 | 
|---|
 | 379 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
 | 380 |   // to also let it know that it has changed
 | 
|---|
 | 381 |   if(!target){
 | 
|---|
 | 382 |     target = atoms[oldId];
 | 
|---|
| [6d574a] | 383 |     ASSERT(target,"Atom with that ID not found");
 | 
|---|
| [88d586] | 384 |     return target->changeId(newId);
 | 
|---|
 | 385 |   }
 | 
|---|
 | 386 |   else{
 | 
|---|
| [3e4fb6] | 387 |     if(atomIdPool.reserveId(newId)){
 | 
|---|
| [88d586] | 388 |       atoms.erase(oldId);
 | 
|---|
 | 389 |       atoms.insert(pair<atomId_t,atom*>(newId,target));
 | 
|---|
 | 390 |       return true;
 | 
|---|
 | 391 |     }
 | 
|---|
 | 392 |     else{
 | 
|---|
 | 393 |       return false;
 | 
|---|
 | 394 |     }
 | 
|---|
 | 395 |   }
 | 
|---|
| [46d958] | 396 | }
 | 
|---|
 | 397 | 
 | 
|---|
| [a7a087] | 398 | bool World::changeMoleculeId(moleculeId_t oldId, moleculeId_t newId, molecule* target){
 | 
|---|
 | 399 |   OBSERVE;
 | 
|---|
 | 400 |   // in case this call did not originate from inside the atom, we redirect it,
 | 
|---|
 | 401 |   // to also let it know that it has changed
 | 
|---|
 | 402 |   if(!target){
 | 
|---|
 | 403 |     target = molecules[oldId];
 | 
|---|
 | 404 |     ASSERT(target,"Molecule with that ID not found");
 | 
|---|
 | 405 |     return target->changeId(newId);
 | 
|---|
 | 406 |   }
 | 
|---|
 | 407 |   else{
 | 
|---|
| [3e4fb6] | 408 |     if(moleculeIdPool.reserveId(newId)){
 | 
|---|
| [a7a087] | 409 |       molecules.erase(oldId);
 | 
|---|
 | 410 |       molecules.insert(pair<moleculeId_t,molecule*>(newId,target));
 | 
|---|
 | 411 |       return true;
 | 
|---|
 | 412 |     }
 | 
|---|
 | 413 |     else{
 | 
|---|
 | 414 |       return false;
 | 
|---|
 | 415 |     }
 | 
|---|
 | 416 |   }
 | 
|---|
 | 417 | }
 | 
|---|
 | 418 | 
 | 
|---|
| [7c4e29] | 419 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
 | 
|---|
| [3139b2] | 420 |   ActionTrait manipulateTrait(name);
 | 
|---|
| [126867] | 421 |   return new ManipulateAtomsProcess(op, descr,manipulateTrait);
 | 
|---|
| [7c4e29] | 422 | }
 | 
|---|
 | 423 | 
 | 
|---|
| [0e2a47] | 424 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
 | 
|---|
 | 425 |   return manipulateAtoms(op,name,AllAtoms());
 | 
|---|
 | 426 | }
 | 
|---|
 | 427 | 
 | 
|---|
| [afb47f] | 428 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
 | 
|---|
 | 429 | 
 | 
|---|
 | 430 | void World::doManipulate(ManipulateAtomsProcess *proc){
 | 
|---|
 | 431 |   proc->signOn(this);
 | 
|---|
 | 432 |   {
 | 
|---|
 | 433 |     OBSERVE;
 | 
|---|
 | 434 |     proc->doManipulate(this);
 | 
|---|
 | 435 |   }
 | 
|---|
 | 436 |   proc->signOff(this);
 | 
|---|
 | 437 | }
 | 
|---|
| [865a945] | 438 | /******************************* Iterators ********************************/
 | 
|---|
 | 439 | 
 | 
|---|
| [fa0b18] | 440 | // external parts with observers
 | 
|---|
 | 441 | 
 | 
|---|
| [feb5d0] | 442 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor)
 | 
|---|
 | 443 | 
 | 
|---|
 | 444 | CONSTRUCT_SELECTIVE_CONST_ITERATOR(atom*,World::AtomSet,AtomDescriptor)
 | 
|---|
| [6e97e5] | 445 | 
 | 
|---|
| [fa0b18] | 446 | World::AtomIterator
 | 
|---|
 | 447 | World::getAtomIter(AtomDescriptor descr){
 | 
|---|
 | 448 |     return AtomIterator(descr,atoms);
 | 
|---|
 | 449 | }
 | 
|---|
| [865a945] | 450 | 
 | 
|---|
| [feb5d0] | 451 | World::AtomConstIterator
 | 
|---|
 | 452 | World::getAtomIter(AtomDescriptor descr) const{
 | 
|---|
 | 453 |     return AtomConstIterator(descr,atoms);
 | 
|---|
 | 454 | }
 | 
|---|
 | 455 | 
 | 
|---|
| [fa0b18] | 456 | World::AtomIterator
 | 
|---|
 | 457 | World::getAtomIter(){
 | 
|---|
 | 458 |     return AtomIterator(AllAtoms(),atoms);
 | 
|---|
| [865a945] | 459 | }
 | 
|---|
| [354859] | 460 | 
 | 
|---|
| [feb5d0] | 461 | World::AtomConstIterator
 | 
|---|
 | 462 | World::getAtomIter() const{
 | 
|---|
 | 463 |     return AtomConstIterator(AllAtoms(),atoms);
 | 
|---|
 | 464 | }
 | 
|---|
 | 465 | 
 | 
|---|
| [fa0b18] | 466 | World::AtomIterator
 | 
|---|
 | 467 | World::atomEnd(){
 | 
|---|
| [6e97e5] | 468 |   return AtomIterator(AllAtoms(),atoms,atoms.end());
 | 
|---|
| [7c4e29] | 469 | }
 | 
|---|
 | 470 | 
 | 
|---|
| [feb5d0] | 471 | World::AtomConstIterator
 | 
|---|
 | 472 | World::atomEnd() const{
 | 
|---|
 | 473 |   return AtomConstIterator(AllAtoms(),atoms,atoms.end());
 | 
|---|
 | 474 | }
 | 
|---|
 | 475 | 
 | 
|---|
 | 476 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor)
 | 
|---|
 | 477 | 
 | 
|---|
 | 478 | CONSTRUCT_SELECTIVE_CONST_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor)
 | 
|---|
| [6e97e5] | 479 | 
 | 
|---|
| [5d880e] | 480 | World::MoleculeIterator
 | 
|---|
 | 481 | World::getMoleculeIter(MoleculeDescriptor descr){
 | 
|---|
 | 482 |     return MoleculeIterator(descr,molecules);
 | 
|---|
 | 483 | }
 | 
|---|
 | 484 | 
 | 
|---|
| [feb5d0] | 485 | World::MoleculeConstIterator
 | 
|---|
 | 486 | World::getMoleculeIter(MoleculeDescriptor descr) const{
 | 
|---|
 | 487 |     return MoleculeConstIterator(descr,molecules);
 | 
|---|
 | 488 | }
 | 
|---|
 | 489 | 
 | 
|---|
| [5d880e] | 490 | World::MoleculeIterator
 | 
|---|
 | 491 | World::getMoleculeIter(){
 | 
|---|
 | 492 |     return MoleculeIterator(AllMolecules(),molecules);
 | 
|---|
| [1c51c8] | 493 | }
 | 
|---|
 | 494 | 
 | 
|---|
| [feb5d0] | 495 | World::MoleculeConstIterator
 | 
|---|
 | 496 | World::getMoleculeIter() const{
 | 
|---|
 | 497 |     return MoleculeConstIterator(AllMolecules(),molecules);
 | 
|---|
 | 498 | }
 | 
|---|
 | 499 | 
 | 
|---|
| [5d880e] | 500 | World::MoleculeIterator
 | 
|---|
 | 501 | World::moleculeEnd(){
 | 
|---|
| [6e97e5] | 502 |   return MoleculeIterator(AllMolecules(),molecules,molecules.end());
 | 
|---|
| [1c51c8] | 503 | }
 | 
|---|
 | 504 | 
 | 
|---|
| [feb5d0] | 505 | World::MoleculeConstIterator
 | 
|---|
 | 506 | World::moleculeEnd() const{
 | 
|---|
 | 507 |   return MoleculeConstIterator(AllMolecules(),molecules,molecules.end());
 | 
|---|
 | 508 | }
 | 
|---|
 | 509 | 
 | 
|---|
| [fa0b18] | 510 | // Internal parts, without observers
 | 
|---|
 | 511 | 
 | 
|---|
 | 512 | // Build the AtomIterator from template
 | 
|---|
 | 513 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor);
 | 
|---|
 | 514 | 
 | 
|---|
 | 515 | 
 | 
|---|
 | 516 | World::internal_AtomIterator
 | 
|---|
 | 517 | World::getAtomIter_internal(AtomDescriptor descr){
 | 
|---|
 | 518 |   return internal_AtomIterator(descr,atoms.getContent());
 | 
|---|
 | 519 | }
 | 
|---|
 | 520 | 
 | 
|---|
 | 521 | World::internal_AtomIterator
 | 
|---|
 | 522 | World::atomEnd_internal(){
 | 
|---|
 | 523 |   return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal());
 | 
|---|
 | 524 | }
 | 
|---|
 | 525 | 
 | 
|---|
| [6e97e5] | 526 | // build the MoleculeIterator from template
 | 
|---|
| [e3d865] | 527 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor);
 | 
|---|
| [6e97e5] | 528 | 
 | 
|---|
| [e3d865] | 529 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){
 | 
|---|
 | 530 |   return internal_MoleculeIterator(descr,molecules.getContent());
 | 
|---|
| [1c51c8] | 531 | }
 | 
|---|
 | 532 | 
 | 
|---|
| [e3d865] | 533 | World::internal_MoleculeIterator World::moleculeEnd_internal(){
 | 
|---|
 | 534 |   return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal());
 | 
|---|
| [1c51c8] | 535 | }
 | 
|---|
 | 536 | 
 | 
|---|
| [90c4280] | 537 | /************************** Selection of Atoms and molecules ******************/
 | 
|---|
 | 538 | 
 | 
|---|
 | 539 | // Atoms
 | 
|---|
 | 540 | 
 | 
|---|
 | 541 | void World::clearAtomSelection(){
 | 
|---|
| [69643a] | 542 |   OBSERVE;
 | 
|---|
 | 543 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 544 |   selectedAtoms.clear();
 | 
|---|
 | 545 | }
 | 
|---|
 | 546 | 
 | 
|---|
| [ebc499] | 547 | void World::invertAtomSelection(){
 | 
|---|
 | 548 |   // get all atoms not selected
 | 
|---|
 | 549 |   AtomComposite invertedSelection(getAllAtoms());
 | 
|---|
 | 550 |   bool (World::*predicate)(const atom*) const = &World::isSelected; // needed for type resolution of overloaded function
 | 
|---|
 | 551 |   AtomComposite::iterator iter =
 | 
|---|
 | 552 |       std::remove_if(invertedSelection.begin(), invertedSelection.end(),
 | 
|---|
 | 553 |           std::bind1st(std::mem_fun(predicate), this));
 | 
|---|
 | 554 |   invertedSelection.erase(iter, invertedSelection.end());
 | 
|---|
 | 555 |   // apply new selection
 | 
|---|
 | 556 |   selectedAtoms.clear();
 | 
|---|
 | 557 |   void (World::*selector)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
 | 558 |   std::for_each(invertedSelection.begin(),invertedSelection.end(),
 | 
|---|
 | 559 |       std::bind1st(std::mem_fun(selector),this)); // func is select... see above
 | 
|---|
 | 560 | }
 | 
|---|
 | 561 | 
 | 
|---|
| [cad383] | 562 | void World::popAtomSelection(){
 | 
|---|
 | 563 |   OBSERVE;
 | 
|---|
 | 564 |   NOTIFY(SelectionChanged);
 | 
|---|
| [c1d837] | 565 |   const atomIdsVector_t atomids = selectedAtoms_Stack.top();
 | 
|---|
 | 566 |   boost::function<void (const atomId_t)> IdSelector =
 | 
|---|
 | 567 |       boost::bind(static_cast<void (World::*)(const atomId_t)>(&World::selectAtom), this, _1);
 | 
|---|
 | 568 |   selectedAtoms.clear();
 | 
|---|
 | 569 |   std::for_each(atomids.begin(),atomids.end(), IdSelector);
 | 
|---|
| [cad383] | 570 |   selectedAtoms_Stack.pop();
 | 
|---|
 | 571 | }
 | 
|---|
 | 572 | 
 | 
|---|
 | 573 | void World::pushAtomSelection(){
 | 
|---|
 | 574 |   OBSERVE;
 | 
|---|
 | 575 |   NOTIFY(SelectionChanged);
 | 
|---|
| [c1d837] | 576 |   atomIdsVector_t atomids(countSelectedAtoms(), (atomId_t)-1);
 | 
|---|
 | 577 |   std::copy(
 | 
|---|
 | 578 |       MapKeyIterator<AtomSelectionConstIterator>(beginAtomSelection()),
 | 
|---|
 | 579 |       MapKeyIterator<AtomSelectionConstIterator>(endAtomSelection()),
 | 
|---|
 | 580 |       atomids.begin());
 | 
|---|
 | 581 |   selectedAtoms_Stack.push( atomids );
 | 
|---|
| [cad383] | 582 |   selectedAtoms.clear();
 | 
|---|
 | 583 | }
 | 
|---|
 | 584 | 
 | 
|---|
| [e4afb4] | 585 | void World::selectAtom(const atom *_atom){
 | 
|---|
| [69643a] | 586 |   OBSERVE;
 | 
|---|
 | 587 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 588 |   // atom * is unchanged in this function, but we do store entity as changeable
 | 
|---|
 | 589 |   ASSERT(_atom,"Invalid pointer in selection of atom");
 | 
|---|
 | 590 |   selectedAtoms[_atom->getId()]=const_cast<atom *>(_atom);
 | 
|---|
| [90c4280] | 591 | }
 | 
|---|
 | 592 | 
 | 
|---|
| [e4afb4] | 593 | void World::selectAtom(const atomId_t id){
 | 
|---|
| [69643a] | 594 |   OBSERVE;
 | 
|---|
 | 595 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 596 |   ASSERT(atoms.count(id),"Atom Id selected that was not in the world");
 | 
|---|
 | 597 |   selectedAtoms[id]=atoms[id];
 | 
|---|
 | 598 | }
 | 
|---|
 | 599 | 
 | 
|---|
 | 600 | void World::selectAllAtoms(AtomDescriptor descr){
 | 
|---|
| [69643a] | 601 |   OBSERVE;
 | 
|---|
 | 602 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 603 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
 | 604 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
| [e4afb4] | 605 |   void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
| [90c4280] | 606 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 607 | }
 | 
|---|
 | 608 | 
 | 
|---|
| [e4afb4] | 609 | void World::selectAtomsOfMolecule(const molecule *_mol){
 | 
|---|
| [69643a] | 610 |   OBSERVE;
 | 
|---|
 | 611 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 612 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
 | 613 |   // need to make it const to get the fast iterators
 | 
|---|
 | 614 |   const molecule *mol = _mol;
 | 
|---|
| [e4afb4] | 615 |   void (World::*func)(const atom*) = &World::selectAtom; // needed for type resolution of overloaded function
 | 
|---|
| [90c4280] | 616 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 617 | }
 | 
|---|
 | 618 | 
 | 
|---|
| [e4afb4] | 619 | void World::selectAtomsOfMolecule(const moleculeId_t id){
 | 
|---|
| [69643a] | 620 |   OBSERVE;
 | 
|---|
 | 621 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 622 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
 | 623 |   selectAtomsOfMolecule(molecules[id]);
 | 
|---|
 | 624 | }
 | 
|---|
 | 625 | 
 | 
|---|
| [e4afb4] | 626 | void World::unselectAtom(const atom *_atom){
 | 
|---|
| [69643a] | 627 |   OBSERVE;
 | 
|---|
 | 628 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 629 |   ASSERT(_atom,"Invalid pointer in unselection of atom");
 | 
|---|
 | 630 |   unselectAtom(_atom->getId());
 | 
|---|
| [61d655e] | 631 | }
 | 
|---|
 | 632 | 
 | 
|---|
| [e4afb4] | 633 | void World::unselectAtom(const atomId_t id){
 | 
|---|
| [69643a] | 634 |   OBSERVE;
 | 
|---|
 | 635 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 636 |   ASSERT(atoms.count(id),"Atom Id unselected that was not in the world");
 | 
|---|
 | 637 |   selectedAtoms.erase(id);
 | 
|---|
 | 638 | }
 | 
|---|
 | 639 | 
 | 
|---|
 | 640 | void World::unselectAllAtoms(AtomDescriptor descr){
 | 
|---|
| [69643a] | 641 |   OBSERVE;
 | 
|---|
 | 642 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 643 |   internal_AtomIterator begin = getAtomIter_internal(descr);
 | 
|---|
 | 644 |   internal_AtomIterator end = atomEnd_internal();
 | 
|---|
| [e4afb4] | 645 |   void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
| [61d655e] | 646 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
 | 647 | }
 | 
|---|
 | 648 | 
 | 
|---|
| [e4afb4] | 649 | void World::unselectAtomsOfMolecule(const molecule *_mol){
 | 
|---|
| [69643a] | 650 |   OBSERVE;
 | 
|---|
 | 651 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 652 |   ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
 | 
|---|
 | 653 |   // need to make it const to get the fast iterators
 | 
|---|
 | 654 |   const molecule *mol = _mol;
 | 
|---|
| [e4afb4] | 655 |   void (World::*func)(const atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
 | 
|---|
| [992bd5] | 656 |   for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
| [61d655e] | 657 | }
 | 
|---|
 | 658 | 
 | 
|---|
| [e4afb4] | 659 | void World::unselectAtomsOfMolecule(const moleculeId_t id){
 | 
|---|
| [69643a] | 660 |   OBSERVE;
 | 
|---|
 | 661 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 662 |   ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
 | 
|---|
 | 663 |   unselectAtomsOfMolecule(molecules[id]);
 | 
|---|
 | 664 | }
 | 
|---|
 | 665 | 
 | 
|---|
| [e472eab] | 666 | size_t World::countSelectedAtoms() const {
 | 
|---|
| [eacc3b] | 667 |   size_t count = 0;
 | 
|---|
| [e472eab] | 668 |   for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
 | 
|---|
| [eacc3b] | 669 |     count++;
 | 
|---|
 | 670 |   return count;
 | 
|---|
 | 671 | }
 | 
|---|
 | 672 | 
 | 
|---|
| [e4afb4] | 673 | bool World::isSelected(const atom *_atom) const {
 | 
|---|
| [89643d] | 674 |   return isAtomSelected(_atom->getId());
 | 
|---|
 | 675 | }
 | 
|---|
 | 676 | 
 | 
|---|
 | 677 | bool World::isAtomSelected(const atomId_t no) const {
 | 
|---|
 | 678 |   return selectedAtoms.find(no) != selectedAtoms.end();
 | 
|---|
| [e0e156] | 679 | }
 | 
|---|
 | 680 | 
 | 
|---|
| [e472eab] | 681 | const std::vector<atom *> World::getSelectedAtoms() const {
 | 
|---|
 | 682 |   std::vector<atom *> returnAtoms;
 | 
|---|
 | 683 |   returnAtoms.resize(countSelectedAtoms());
 | 
|---|
 | 684 |   int count = 0;
 | 
|---|
 | 685 |   for (AtomSet::const_iterator iter = selectedAtoms.begin(); iter != selectedAtoms.end(); ++iter)
 | 
|---|
 | 686 |     returnAtoms[count++] = iter->second;
 | 
|---|
 | 687 |   return returnAtoms;
 | 
|---|
 | 688 | }
 | 
|---|
 | 689 | 
 | 
|---|
 | 690 | 
 | 
|---|
| [90c4280] | 691 | // Molecules
 | 
|---|
 | 692 | 
 | 
|---|
 | 693 | void World::clearMoleculeSelection(){
 | 
|---|
| [69643a] | 694 |   OBSERVE;
 | 
|---|
 | 695 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 696 |   selectedMolecules.clear();
 | 
|---|
 | 697 | }
 | 
|---|
 | 698 | 
 | 
|---|
| [ebc499] | 699 | void World::invertMoleculeSelection(){
 | 
|---|
 | 700 |   // get all molecules not selected
 | 
|---|
 | 701 |   typedef std::vector<molecule *> MoleculeVector_t;
 | 
|---|
 | 702 |   MoleculeVector_t invertedSelection(getAllMolecules());
 | 
|---|
 | 703 |   bool (World::*predicate)(const molecule*) const = &World::isSelected; // needed for type resolution of overloaded function
 | 
|---|
 | 704 |   MoleculeVector_t::iterator iter =
 | 
|---|
 | 705 |       std::remove_if(invertedSelection.begin(), invertedSelection.end(),
 | 
|---|
 | 706 |           std::bind1st(std::mem_fun(predicate), this));
 | 
|---|
 | 707 |   invertedSelection.erase(iter, invertedSelection.end());
 | 
|---|
 | 708 |   // apply new selection
 | 
|---|
 | 709 |   selectedMolecules.clear();
 | 
|---|
 | 710 |   void (World::*selector)(const molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
 | 
|---|
 | 711 |   std::for_each(invertedSelection.begin(),invertedSelection.end(),
 | 
|---|
 | 712 |       std::bind1st(std::mem_fun(selector),this)); // func is select... see above
 | 
|---|
 | 713 | }
 | 
|---|
 | 714 | 
 | 
|---|
| [cad383] | 715 | void World::popMoleculeSelection(){
 | 
|---|
 | 716 |   OBSERVE;
 | 
|---|
 | 717 |   NOTIFY(SelectionChanged);
 | 
|---|
| [c1d837] | 718 |   const moleculeIdsVector_t moleculeids = selectedMolecules_Stack.top();
 | 
|---|
 | 719 |   boost::function<void (const moleculeId_t)> IdSelector =
 | 
|---|
 | 720 |       boost::bind(static_cast<void (World::*)(const moleculeId_t)>(&World::selectMolecule), this, _1);
 | 
|---|
 | 721 |   selectedMolecules.clear();
 | 
|---|
 | 722 |   std::for_each(moleculeids.begin(),moleculeids.end(), IdSelector);
 | 
|---|
| [cad383] | 723 |   selectedMolecules_Stack.pop();
 | 
|---|
 | 724 | }
 | 
|---|
 | 725 | 
 | 
|---|
 | 726 | void World::pushMoleculeSelection(){
 | 
|---|
 | 727 |   OBSERVE;
 | 
|---|
 | 728 |   NOTIFY(SelectionChanged);
 | 
|---|
| [c1d837] | 729 |   moleculeIdsVector_t moleculeids(countSelectedMolecules(), (moleculeId_t)-1);
 | 
|---|
 | 730 |   boost::function<moleculeId_t (const molecule*)> IdRetriever =
 | 
|---|
 | 731 |       boost::bind(&molecule::getId, _1);
 | 
|---|
 | 732 |   std::copy(
 | 
|---|
 | 733 |       MapKeyIterator<MoleculeSelectionConstIterator>(beginMoleculeSelection()),
 | 
|---|
 | 734 |       MapKeyIterator<MoleculeSelectionConstIterator>(endMoleculeSelection()),
 | 
|---|
 | 735 |       moleculeids.begin());
 | 
|---|
 | 736 |   selectedMolecules_Stack.push( moleculeids );
 | 
|---|
| [cad383] | 737 |   selectedMolecules.clear();
 | 
|---|
 | 738 | }
 | 
|---|
 | 739 | 
 | 
|---|
| [e4afb4] | 740 | void World::selectMolecule(const molecule *_mol){
 | 
|---|
| [69643a] | 741 |   OBSERVE;
 | 
|---|
 | 742 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 743 |   // molecule * is unchanged in this function, but we do store entity as changeable
 | 
|---|
 | 744 |   ASSERT(_mol,"Invalid pointer to molecule in selection");
 | 
|---|
 | 745 |   selectedMolecules[_mol->getId()]=const_cast<molecule *>(_mol);
 | 
|---|
| [90c4280] | 746 | }
 | 
|---|
 | 747 | 
 | 
|---|
| [e4afb4] | 748 | void World::selectMolecule(const moleculeId_t id){
 | 
|---|
| [69643a] | 749 |   OBSERVE;
 | 
|---|
 | 750 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 751 |   ASSERT(molecules.count(id),"Molecule Id selected that was not in the world");
 | 
|---|
 | 752 |   selectedMolecules[id]=molecules[id];
 | 
|---|
 | 753 | }
 | 
|---|
 | 754 | 
 | 
|---|
| [e472eab] | 755 | void World::selectAllMolecules(MoleculeDescriptor descr){
 | 
|---|
| [69643a] | 756 |   OBSERVE;
 | 
|---|
 | 757 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 758 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
 | 759 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
| [e4afb4] | 760 |   void (World::*func)(const molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
 | 
|---|
| [90c4280] | 761 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
 | 
|---|
 | 762 | }
 | 
|---|
 | 763 | 
 | 
|---|
| [e4afb4] | 764 | void World::selectMoleculeOfAtom(const atom *_atom){
 | 
|---|
| [69643a] | 765 |   OBSERVE;
 | 
|---|
 | 766 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 767 |   ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
 | 768 |   molecule *mol=_atom->getMolecule();
 | 
|---|
| [90c4280] | 769 |   // the atom might not be part of a molecule
 | 
|---|
 | 770 |   if(mol){
 | 
|---|
 | 771 |     selectMolecule(mol);
 | 
|---|
 | 772 |   }
 | 
|---|
 | 773 | }
 | 
|---|
 | 774 | 
 | 
|---|
| [e4afb4] | 775 | void World::selectMoleculeOfAtom(const atomId_t id){
 | 
|---|
| [69643a] | 776 |   OBSERVE;
 | 
|---|
 | 777 |   NOTIFY(SelectionChanged);
 | 
|---|
| [90c4280] | 778 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
 | 779 |   selectMoleculeOfAtom(atoms[id]);
 | 
|---|
 | 780 | }
 | 
|---|
 | 781 | 
 | 
|---|
| [e4afb4] | 782 | void World::unselectMolecule(const molecule *_mol){
 | 
|---|
| [69643a] | 783 |   OBSERVE;
 | 
|---|
 | 784 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 785 |   ASSERT(_mol,"invalid pointer in unselection of molecule");
 | 
|---|
 | 786 |   unselectMolecule(_mol->getId());
 | 
|---|
| [61d655e] | 787 | }
 | 
|---|
 | 788 | 
 | 
|---|
| [e4afb4] | 789 | void World::unselectMolecule(const moleculeId_t id){
 | 
|---|
| [69643a] | 790 |   OBSERVE;
 | 
|---|
 | 791 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 792 |   ASSERT(molecules.count(id),"No such molecule with ID in unselection");
 | 
|---|
 | 793 |   selectedMolecules.erase(id);
 | 
|---|
 | 794 | }
 | 
|---|
 | 795 | 
 | 
|---|
| [e472eab] | 796 | void World::unselectAllMolecules(MoleculeDescriptor descr){
 | 
|---|
| [69643a] | 797 |   OBSERVE;
 | 
|---|
 | 798 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 799 |   internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
 | 
|---|
 | 800 |   internal_MoleculeIterator end = moleculeEnd_internal();
 | 
|---|
| [e4afb4] | 801 |   void (World::*func)(const molecule*) = &World::unselectMolecule; // needed for type resolution of overloaded function
 | 
|---|
| [61d655e] | 802 |   for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
 | 
|---|
 | 803 | }
 | 
|---|
 | 804 | 
 | 
|---|
| [e4afb4] | 805 | void World::unselectMoleculeOfAtom(const atom *_atom){
 | 
|---|
| [69643a] | 806 |   OBSERVE;
 | 
|---|
 | 807 |   NOTIFY(SelectionChanged);
 | 
|---|
| [e4afb4] | 808 |   ASSERT(_atom,"Invalid atom pointer in selection of MoleculeOfAtom");
 | 
|---|
 | 809 |   molecule *mol=_atom->getMolecule();
 | 
|---|
| [61d655e] | 810 |   // the atom might not be part of a molecule
 | 
|---|
 | 811 |   if(mol){
 | 
|---|
 | 812 |     unselectMolecule(mol);
 | 
|---|
 | 813 |   }
 | 
|---|
 | 814 | }
 | 
|---|
 | 815 | 
 | 
|---|
| [e4afb4] | 816 | void World::unselectMoleculeOfAtom(const atomId_t id){
 | 
|---|
| [69643a] | 817 |   OBSERVE;
 | 
|---|
 | 818 |   NOTIFY(SelectionChanged);
 | 
|---|
| [61d655e] | 819 |   ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
 | 
|---|
 | 820 |   unselectMoleculeOfAtom(atoms[id]);
 | 
|---|
 | 821 | }
 | 
|---|
 | 822 | 
 | 
|---|
| [e472eab] | 823 | size_t World::countSelectedMolecules() const {
 | 
|---|
| [eacc3b] | 824 |   size_t count = 0;
 | 
|---|
| [e472eab] | 825 |   for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
 | 
|---|
| [eacc3b] | 826 |     count++;
 | 
|---|
 | 827 |   return count;
 | 
|---|
 | 828 | }
 | 
|---|
 | 829 | 
 | 
|---|
| [e4afb4] | 830 | bool World::isSelected(const molecule *_mol) const {
 | 
|---|
| [89643d] | 831 |   return isMoleculeSelected(_mol->getId());
 | 
|---|
 | 832 | }
 | 
|---|
 | 833 | 
 | 
|---|
 | 834 | bool World::isMoleculeSelected(const moleculeId_t no) const {
 | 
|---|
 | 835 |   return selectedMolecules.find(no) != selectedMolecules.end();
 | 
|---|
| [e0e156] | 836 | }
 | 
|---|
 | 837 | 
 | 
|---|
| [e472eab] | 838 | const std::vector<molecule *> World::getSelectedMolecules() const {
 | 
|---|
 | 839 |   std::vector<molecule *> returnMolecules;
 | 
|---|
 | 840 |   returnMolecules.resize(countSelectedMolecules());
 | 
|---|
 | 841 |   int count = 0;
 | 
|---|
 | 842 |   for (MoleculeSet::const_iterator iter = selectedMolecules.begin(); iter != selectedMolecules.end(); ++iter)
 | 
|---|
 | 843 |     returnMolecules[count++] = iter->second;
 | 
|---|
 | 844 |   return returnMolecules;
 | 
|---|
 | 845 | }
 | 
|---|
 | 846 | 
 | 
|---|
| [3839e5] | 847 | /******************* Iterators over Selection *****************************/
 | 
|---|
 | 848 | World::AtomSelectionIterator World::beginAtomSelection(){
 | 
|---|
 | 849 |   return selectedAtoms.begin();
 | 
|---|
 | 850 | }
 | 
|---|
 | 851 | 
 | 
|---|
 | 852 | World::AtomSelectionIterator World::endAtomSelection(){
 | 
|---|
 | 853 |   return selectedAtoms.end();
 | 
|---|
 | 854 | }
 | 
|---|
 | 855 | 
 | 
|---|
| [38f991] | 856 | World::AtomSelectionConstIterator World::beginAtomSelection() const{
 | 
|---|
 | 857 |   return selectedAtoms.begin();
 | 
|---|
 | 858 | }
 | 
|---|
 | 859 | 
 | 
|---|
 | 860 | World::AtomSelectionConstIterator World::endAtomSelection() const{
 | 
|---|
 | 861 |   return selectedAtoms.end();
 | 
|---|
 | 862 | }
 | 
|---|
 | 863 | 
 | 
|---|
| [3839e5] | 864 | 
 | 
|---|
 | 865 | World::MoleculeSelectionIterator World::beginMoleculeSelection(){
 | 
|---|
 | 866 |   return selectedMolecules.begin();
 | 
|---|
 | 867 | }
 | 
|---|
 | 868 | 
 | 
|---|
 | 869 | World::MoleculeSelectionIterator World::endMoleculeSelection(){
 | 
|---|
 | 870 |   return selectedMolecules.end();
 | 
|---|
 | 871 | }
 | 
|---|
 | 872 | 
 | 
|---|
| [38f991] | 873 | World::MoleculeSelectionConstIterator World::beginMoleculeSelection() const{
 | 
|---|
 | 874 |   return selectedMolecules.begin();
 | 
|---|
 | 875 | }
 | 
|---|
 | 876 | 
 | 
|---|
 | 877 | World::MoleculeSelectionConstIterator World::endMoleculeSelection() const{
 | 
|---|
 | 878 |   return selectedMolecules.end();
 | 
|---|
 | 879 | }
 | 
|---|
 | 880 | 
 | 
|---|
| [5d1611] | 881 | /******************************* Singleton Stuff **************************/
 | 
|---|
 | 882 | 
 | 
|---|
| [7a1ce5] | 883 | World::World() :
 | 
|---|
| [cd5047] | 884 |     Observable("World"),
 | 
|---|
| [f71baf] | 885 |     BG(new BondGraph(true)),  // assume Angstroem for the moment
 | 
|---|
| [4ae823] | 886 |     periode(new periodentafel(true)),
 | 
|---|
| [8e1f7af] | 887 |     configuration(new config),
 | 
|---|
| [98dbee] | 888 |     homologies(new HomologyContainer()),
 | 
|---|
| [43dad6] | 889 |     Thermostats(new ThermoStatContainer),
 | 
|---|
| [e4b5de] | 890 |     ExitFlag(0),
 | 
|---|
| [fa0b18] | 891 |     atoms(this),
 | 
|---|
| [90c4280] | 892 |     selectedAtoms(this),
 | 
|---|
| [3e4fb6] | 893 |     atomIdPool(0, 20, 100),
 | 
|---|
| [51be2a] | 894 |     molecules(this),
 | 
|---|
| [90c4280] | 895 |     selectedMolecules(this),
 | 
|---|
| [3e4fb6] | 896 |     moleculeIdPool(0, 20,100),
 | 
|---|
| [24a5e0] | 897 |     molecules_deprecated(new MoleculeListClass(this))
 | 
|---|
| [7dad10] | 898 | {
 | 
|---|
| [84c494] | 899 |   cell_size = new Box;
 | 
|---|
| [cca9ef] | 900 |   RealSpaceMatrix domain;
 | 
|---|
| [84c494] | 901 |   domain.at(0,0) = 20;
 | 
|---|
 | 902 |   domain.at(1,1) = 20;
 | 
|---|
 | 903 |   domain.at(2,2) = 20;
 | 
|---|
 | 904 |   cell_size->setM(domain);
 | 
|---|
| [4834f4] | 905 |   LCcontroller = new LinkedCell::LinkedCell_Controller(*cell_size);
 | 
|---|
| [387b36] | 906 |   defaultName = "none";
 | 
|---|
| [02ce36] | 907 |   Channels *OurChannel = new Channels;
 | 
|---|
| [708277] | 908 |   NotificationChannels.insert( std::make_pair( static_cast<Observable *>(this), OurChannel) );
 | 
|---|
| [7188b1] | 909 |   for (size_t type = 0; type < (size_t)NotificationType_MAX; ++type)
 | 
|---|
| [02ce36] | 910 |     OurChannel->addChannel(type);
 | 
|---|
| [7dad10] | 911 |   molecules_deprecated->signOn(this);
 | 
|---|
 | 912 | }
 | 
|---|
| [5d1611] | 913 | 
 | 
|---|
 | 914 | World::~World()
 | 
|---|
| [354859] | 915 | {
 | 
|---|
| [028c2e] | 916 |   molecules_deprecated->signOff(this);
 | 
|---|
| [4834f4] | 917 |   delete LCcontroller;
 | 
|---|
| [84c494] | 918 |   delete cell_size;
 | 
|---|
| [46d958] | 919 |   delete molecules_deprecated;
 | 
|---|
| [cbc5fb] | 920 |   MoleculeSet::iterator molIter;
 | 
|---|
 | 921 |   for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
 | 
|---|
 | 922 |     DeleteMolecule((*molIter).second);
 | 
|---|
 | 923 |   }
 | 
|---|
 | 924 |   molecules.clear();
 | 
|---|
 | 925 |   AtomSet::iterator atIter;
 | 
|---|
 | 926 |   for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
 | 
|---|
 | 927 |     DeleteAtom((*atIter).second);
 | 
|---|
| [46d958] | 928 |   }
 | 
|---|
 | 929 |   atoms.clear();
 | 
|---|
| [7188b1] | 930 | 
 | 
|---|
| [f71baf] | 931 |   delete BG;
 | 
|---|
| [6cb9c76] | 932 |   delete periode;
 | 
|---|
 | 933 |   delete configuration;
 | 
|---|
 | 934 |   delete Thermostats;
 | 
|---|
| [09f615] | 935 |   delete homologies;
 | 
|---|
| [354859] | 936 | }
 | 
|---|
| [5d1611] | 937 | 
 | 
|---|
| [23b547] | 938 | // Explicit instantiation of the singleton mechanism at this point
 | 
|---|
| [5d1611] | 939 | 
 | 
|---|
| [3e4fb6] | 940 | // moleculeId_t und atomId_t sind gleicher Basistyp, deswegen nur einen von beiden konstruieren
 | 
|---|
| [b97a60] | 941 | CONSTRUCT_IDPOOL(atomId_t, uniqueId)
 | 
|---|
 | 942 | CONSTRUCT_IDPOOL(moleculeId_t, continuousId)
 | 
|---|
| [3e4fb6] | 943 | 
 | 
|---|
| [23b547] | 944 | CONSTRUCT_SINGLETON(World)
 | 
|---|
| [5d1611] | 945 | 
 | 
|---|
| [5f1d5b8] | 946 | CONSTRUCT_OBSERVEDCONTAINER(World::AtomSTLSet)
 | 
|---|
 | 947 | 
 | 
|---|
 | 948 | CONSTRUCT_OBSERVEDCONTAINER(World::MoleculeSTLSet)
 | 
|---|
 | 949 | 
 | 
|---|
| [5d1611] | 950 | /******************************* deprecated Legacy Stuff ***********************/
 | 
|---|
 | 951 | 
 | 
|---|
| [354859] | 952 | MoleculeListClass *&World::getMolecules() {
 | 
|---|
 | 953 |   return molecules_deprecated;
 | 
|---|
| [5d1611] | 954 | }
 | 
|---|