| [5d1611] | 1 | /* | 
|---|
|  | 2 | * World.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Feb 3, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #include "World.hpp" | 
|---|
|  | 9 |  | 
|---|
| [d346b6] | 10 | #include "atom.hpp" | 
|---|
| [354859] | 11 | #include "molecule.hpp" | 
|---|
|  | 12 | #include "periodentafel.hpp" | 
|---|
| [fc1b24] | 13 | #include "Descriptors/AtomDescriptor.hpp" | 
|---|
| [865a945] | 14 | #include "Descriptors/AtomDescriptor_impl.hpp" | 
|---|
| [1c51c8] | 15 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
|  | 16 | #include "Descriptors/MoleculeDescriptor_impl.hpp" | 
|---|
| [6e97e5] | 17 | #include "Descriptors/SelectiveIterator_impl.hpp" | 
|---|
| [7c4e29] | 18 | #include "Actions/ManipulateAtomsProcess.hpp" | 
|---|
| [d346b6] | 19 |  | 
|---|
| [23b547] | 20 | #include "Patterns/Singleton_impl.hpp" | 
|---|
|  | 21 |  | 
|---|
| [d346b6] | 22 | using namespace std; | 
|---|
| [4d9c01] | 23 |  | 
|---|
| [5d1611] | 24 | /******************************* getter and setter ************************/ | 
|---|
| [354859] | 25 | periodentafel *&World::getPeriode(){ | 
|---|
| [5d1611] | 26 | return periode; | 
|---|
|  | 27 | } | 
|---|
|  | 28 |  | 
|---|
| [1c51c8] | 29 | // Atoms | 
|---|
|  | 30 |  | 
|---|
| [7a1ce5] | 31 | atom* World::getAtom(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 32 | return descriptor.find(); | 
|---|
|  | 33 | } | 
|---|
|  | 34 |  | 
|---|
| [7a1ce5] | 35 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){ | 
|---|
| [fc1b24] | 36 | return descriptor.findAll(); | 
|---|
|  | 37 | } | 
|---|
|  | 38 |  | 
|---|
| [0e2a47] | 39 | vector<atom*> World::getAllAtoms(){ | 
|---|
|  | 40 | return getAllAtoms(AllAtoms()); | 
|---|
|  | 41 | } | 
|---|
|  | 42 |  | 
|---|
| [354859] | 43 | int World::numAtoms(){ | 
|---|
|  | 44 | return atoms.size(); | 
|---|
|  | 45 | } | 
|---|
|  | 46 |  | 
|---|
| [1c51c8] | 47 | // Molecules | 
|---|
|  | 48 |  | 
|---|
|  | 49 | molecule *World::getMolecule(MoleculeDescriptor descriptor){ | 
|---|
|  | 50 | return descriptor.find(); | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){ | 
|---|
|  | 54 | return descriptor.findAll(); | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
| [354859] | 57 | int World::numMolecules(){ | 
|---|
|  | 58 | return molecules_deprecated->ListOfMolecules.size(); | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [5f612ee] | 61 | // system | 
|---|
|  | 62 |  | 
|---|
|  | 63 | double * World::getDomain() { | 
|---|
|  | 64 | return cell_size; | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
|  | 67 | void World::setDomain(double * matrix) | 
|---|
|  | 68 | { | 
|---|
|  | 69 |  | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | char * World::getDefaultName() { | 
|---|
|  | 73 | return defaultName; | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
|  | 76 | void World::setDefaultName(char * name) | 
|---|
|  | 77 | { | 
|---|
|  | 78 | delete[](defaultName); | 
|---|
|  | 79 | const int length = strlen(name); | 
|---|
|  | 80 | defaultName = new char[length+2]; | 
|---|
|  | 81 | if (length < MAXSTRINGSIZE) | 
|---|
|  | 82 | strncpy(defaultName, name, length); | 
|---|
|  | 83 | else | 
|---|
|  | 84 | strcpy(defaultName, "none"); | 
|---|
|  | 85 | }; | 
|---|
|  | 86 |  | 
|---|
|  | 87 |  | 
|---|
| [afb47f] | 88 | /******************** Methods to change World state *********************/ | 
|---|
|  | 89 |  | 
|---|
| [354859] | 90 | molecule* World::createMolecule(){ | 
|---|
|  | 91 | OBSERVE; | 
|---|
|  | 92 | molecule *mol = NULL; | 
|---|
| [cbc5fb] | 93 | mol = NewMolecule(); | 
|---|
| [d2dbac0] | 94 | assert(!molecules.count(currMoleculeId)); | 
|---|
| [cbc5fb] | 95 | mol->setId(currMoleculeId++); | 
|---|
| [244d26] | 96 | // store the molecule by ID | 
|---|
| [cbc5fb] | 97 | molecules[mol->getId()] = mol; | 
|---|
| [354859] | 98 | mol->signOn(this); | 
|---|
|  | 99 | return mol; | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [cbc5fb] | 102 | void World::destroyMolecule(molecule* mol){ | 
|---|
|  | 103 | OBSERVE; | 
|---|
|  | 104 | destroyMolecule(mol->getId()); | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
|  | 107 | void World::destroyMolecule(moleculeId_t id){ | 
|---|
|  | 108 | OBSERVE; | 
|---|
|  | 109 | molecule *mol = molecules[id]; | 
|---|
|  | 110 | assert(mol); | 
|---|
|  | 111 | DeleteMolecule(mol); | 
|---|
|  | 112 | molecules.erase(id); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [5f612ee] | 115 | double *World::cell_size = NULL; | 
|---|
|  | 116 | char *World::defaultName = NULL; | 
|---|
| [7c4e29] | 117 |  | 
|---|
| [46d958] | 118 | atom *World::createAtom(){ | 
|---|
|  | 119 | OBSERVE; | 
|---|
| [88d586] | 120 | atomId_t id = getNextAtomId(); | 
|---|
|  | 121 | atom *res = NewAtom(id); | 
|---|
| [46d958] | 122 | res->setWorld(this); | 
|---|
| [244d26] | 123 | // store the atom by ID | 
|---|
| [46d958] | 124 | atoms[res->getId()] = res; | 
|---|
|  | 125 | return res; | 
|---|
|  | 126 | } | 
|---|
|  | 127 |  | 
|---|
| [5f612ee] | 128 |  | 
|---|
| [46d958] | 129 | int World::registerAtom(atom *atom){ | 
|---|
|  | 130 | OBSERVE; | 
|---|
| [88d586] | 131 | atomId_t id = getNextAtomId(); | 
|---|
|  | 132 | atom->setId(id); | 
|---|
| [46d958] | 133 | atom->setWorld(this); | 
|---|
|  | 134 | atoms[atom->getId()] = atom; | 
|---|
|  | 135 | return atom->getId(); | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | void World::destroyAtom(atom* atom){ | 
|---|
|  | 139 | OBSERVE; | 
|---|
|  | 140 | int id = atom->getId(); | 
|---|
|  | 141 | destroyAtom(id); | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [cbc5fb] | 144 | void World::destroyAtom(atomId_t id) { | 
|---|
| [46d958] | 145 | OBSERVE; | 
|---|
|  | 146 | atom *atom = atoms[id]; | 
|---|
|  | 147 | assert(atom); | 
|---|
|  | 148 | DeleteAtom(atom); | 
|---|
|  | 149 | atoms.erase(id); | 
|---|
| [88d586] | 150 | releaseAtomId(id); | 
|---|
|  | 151 | } | 
|---|
|  | 152 |  | 
|---|
|  | 153 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){ | 
|---|
|  | 154 | OBSERVE; | 
|---|
|  | 155 | // in case this call did not originate from inside the atom, we redirect it, | 
|---|
|  | 156 | // to also let it know that it has changed | 
|---|
|  | 157 | if(!target){ | 
|---|
|  | 158 | target = atoms[oldId]; | 
|---|
|  | 159 | assert(target && "Atom with that ID not found"); | 
|---|
|  | 160 | return target->changeId(newId); | 
|---|
|  | 161 | } | 
|---|
|  | 162 | else{ | 
|---|
|  | 163 | if(reserveAtomId(newId)){ | 
|---|
|  | 164 | atoms.erase(oldId); | 
|---|
|  | 165 | atoms.insert(pair<atomId_t,atom*>(newId,target)); | 
|---|
|  | 166 | return true; | 
|---|
|  | 167 | } | 
|---|
|  | 168 | else{ | 
|---|
|  | 169 | return false; | 
|---|
|  | 170 | } | 
|---|
|  | 171 | } | 
|---|
| [46d958] | 172 | } | 
|---|
|  | 173 |  | 
|---|
| [7c4e29] | 174 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){ | 
|---|
|  | 175 | return new ManipulateAtomsProcess(op, descr,name,true); | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
| [0e2a47] | 178 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){ | 
|---|
|  | 179 | return manipulateAtoms(op,name,AllAtoms()); | 
|---|
|  | 180 | } | 
|---|
|  | 181 |  | 
|---|
| [afb47f] | 182 | /********************* Internal Change methods for double Callback and Observer mechanism ********/ | 
|---|
|  | 183 |  | 
|---|
|  | 184 | void World::doManipulate(ManipulateAtomsProcess *proc){ | 
|---|
|  | 185 | proc->signOn(this); | 
|---|
|  | 186 | { | 
|---|
|  | 187 | OBSERVE; | 
|---|
|  | 188 | proc->doManipulate(this); | 
|---|
|  | 189 | } | 
|---|
|  | 190 | proc->signOff(this); | 
|---|
|  | 191 | } | 
|---|
| [88d586] | 192 | /******************************* IDManagement *****************************/ | 
|---|
|  | 193 |  | 
|---|
| [57adc7] | 194 | // Atoms | 
|---|
|  | 195 |  | 
|---|
| [88d586] | 196 | atomId_t World::getNextAtomId(){ | 
|---|
|  | 197 | // see if we can reuse some Id | 
|---|
|  | 198 | if(atomIdPool.empty()){ | 
|---|
|  | 199 | return currAtomId++; | 
|---|
|  | 200 | } | 
|---|
|  | 201 | else{ | 
|---|
|  | 202 | // we give out the first ID from the pool | 
|---|
|  | 203 | atomId_t id = *(atomIdPool.begin()); | 
|---|
|  | 204 | atomIdPool.erase(id); | 
|---|
| [23b547] | 205 | return id; | 
|---|
| [88d586] | 206 | } | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
|  | 209 | void World::releaseAtomId(atomId_t id){ | 
|---|
|  | 210 | atomIdPool.insert(id); | 
|---|
|  | 211 | // defragmentation of the pool | 
|---|
|  | 212 | set<atomId_t>::reverse_iterator iter; | 
|---|
|  | 213 | // go through all Ids in the pool that lie immediately below the border | 
|---|
|  | 214 | while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){ | 
|---|
|  | 215 | atomIdPool.erase(--currAtomId); | 
|---|
|  | 216 | } | 
|---|
|  | 217 | } | 
|---|
| [afb47f] | 218 |  | 
|---|
| [88d586] | 219 | bool World::reserveAtomId(atomId_t id){ | 
|---|
|  | 220 | if(id>=currAtomId ){ | 
|---|
|  | 221 | // add all ids between the new one and current border as available | 
|---|
|  | 222 | for(atomId_t pos=currAtomId; pos<id; ++pos){ | 
|---|
|  | 223 | atomIdPool.insert(pos); | 
|---|
|  | 224 | } | 
|---|
|  | 225 | currAtomId=id+1; | 
|---|
|  | 226 | return true; | 
|---|
|  | 227 | } | 
|---|
|  | 228 | else if(atomIdPool.count(id)){ | 
|---|
|  | 229 | atomIdPool.erase(id); | 
|---|
|  | 230 | return true; | 
|---|
|  | 231 | } | 
|---|
|  | 232 | else{ | 
|---|
|  | 233 | // this ID could not be reserved | 
|---|
|  | 234 | return false; | 
|---|
|  | 235 | } | 
|---|
|  | 236 | } | 
|---|
| [57adc7] | 237 |  | 
|---|
|  | 238 | // Molecules | 
|---|
|  | 239 |  | 
|---|
| [865a945] | 240 | /******************************* Iterators ********************************/ | 
|---|
|  | 241 |  | 
|---|
| [6e97e5] | 242 | // Build the AtomIterator from template | 
|---|
|  | 243 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor); | 
|---|
|  | 244 |  | 
|---|
| [865a945] | 245 |  | 
|---|
|  | 246 | World::AtomIterator World::getAtomIter(AtomDescriptor descr){ | 
|---|
| [6e97e5] | 247 | return AtomIterator(descr,atoms); | 
|---|
| [865a945] | 248 | } | 
|---|
| [354859] | 249 |  | 
|---|
| [6e97e5] | 250 | World::AtomIterator World::atomEnd(){ | 
|---|
|  | 251 | return AtomIterator(AllAtoms(),atoms,atoms.end()); | 
|---|
| [7c4e29] | 252 | } | 
|---|
|  | 253 |  | 
|---|
| [6e97e5] | 254 | // build the MoleculeIterator from template | 
|---|
|  | 255 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor); | 
|---|
|  | 256 |  | 
|---|
| [1c51c8] | 257 | World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){ | 
|---|
| [6e97e5] | 258 | return MoleculeIterator(descr,molecules); | 
|---|
| [1c51c8] | 259 | } | 
|---|
|  | 260 |  | 
|---|
| [6e97e5] | 261 | World::MoleculeIterator World::moleculeEnd(){ | 
|---|
|  | 262 | return MoleculeIterator(AllMolecules(),molecules,molecules.end()); | 
|---|
| [1c51c8] | 263 | } | 
|---|
|  | 264 |  | 
|---|
| [5d1611] | 265 | /******************************* Singleton Stuff **************************/ | 
|---|
|  | 266 |  | 
|---|
| [7a1ce5] | 267 | World::World() : | 
|---|
| [354859] | 268 | periode(new periodentafel), | 
|---|
| [d2dbac0] | 269 | atoms(), | 
|---|
| [24a5e0] | 270 | currAtomId(0), | 
|---|
|  | 271 | molecules(), | 
|---|
|  | 272 | currMoleculeId(0), | 
|---|
|  | 273 | molecules_deprecated(new MoleculeListClass(this)) | 
|---|
| [7dad10] | 274 | { | 
|---|
| [b34306] | 275 | cell_size = new double[6]; | 
|---|
| [fd179f] | 276 | cell_size[0] = 20.; | 
|---|
|  | 277 | cell_size[1] = 0.; | 
|---|
|  | 278 | cell_size[2] = 20.; | 
|---|
|  | 279 | cell_size[3] = 0.; | 
|---|
|  | 280 | cell_size[4] = 0.; | 
|---|
|  | 281 | cell_size[5] = 20.; | 
|---|
| [5f612ee] | 282 | defaultName = new char[MAXSTRINGSIZE]; | 
|---|
|  | 283 | strcpy(defaultName, "none"); | 
|---|
| [7dad10] | 284 | molecules_deprecated->signOn(this); | 
|---|
|  | 285 | } | 
|---|
| [5d1611] | 286 |  | 
|---|
|  | 287 | World::~World() | 
|---|
| [354859] | 288 | { | 
|---|
| [028c2e] | 289 | molecules_deprecated->signOff(this); | 
|---|
| [5f612ee] | 290 | delete[] cell_size; | 
|---|
|  | 291 | delete[] defaultName; | 
|---|
| [46d958] | 292 | delete molecules_deprecated; | 
|---|
| [354859] | 293 | delete periode; | 
|---|
| [cbc5fb] | 294 | MoleculeSet::iterator molIter; | 
|---|
|  | 295 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){ | 
|---|
|  | 296 | DeleteMolecule((*molIter).second); | 
|---|
|  | 297 | } | 
|---|
|  | 298 | molecules.clear(); | 
|---|
|  | 299 | AtomSet::iterator atIter; | 
|---|
|  | 300 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){ | 
|---|
|  | 301 | DeleteAtom((*atIter).second); | 
|---|
| [46d958] | 302 | } | 
|---|
|  | 303 | atoms.clear(); | 
|---|
| [354859] | 304 | } | 
|---|
| [5d1611] | 305 |  | 
|---|
| [23b547] | 306 | // Explicit instantiation of the singleton mechanism at this point | 
|---|
| [5d1611] | 307 |  | 
|---|
| [23b547] | 308 | CONSTRUCT_SINGLETON(World) | 
|---|
| [5d1611] | 309 |  | 
|---|
|  | 310 | /******************************* deprecated Legacy Stuff ***********************/ | 
|---|
|  | 311 |  | 
|---|
| [354859] | 312 | MoleculeListClass *&World::getMolecules() { | 
|---|
|  | 313 | return molecules_deprecated; | 
|---|
| [5d1611] | 314 | } | 
|---|