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