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