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