| 1 | /* | 
|---|
| 2 | * atom.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 3, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef ATOM_HPP_ | 
|---|
| 9 | #define ATOM_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | using namespace std; | 
|---|
| 12 |  | 
|---|
| 13 | /*********************************************** includes ***********************************/ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include <iosfwd> | 
|---|
| 21 | #include <list> | 
|---|
| 22 | #include <vector> | 
|---|
| 23 |  | 
|---|
| 24 | #include "atom_atominfo.hpp" | 
|---|
| 25 | #include "atom_bondedparticle.hpp" | 
|---|
| 26 | #include "atom_graphnode.hpp" | 
|---|
| 27 | #include "atom_observable.hpp" | 
|---|
| 28 | #include "atom_particleinfo.hpp" | 
|---|
| 29 | #include "Atom/TesselPoint.hpp" | 
|---|
| 30 | #include "types.hpp" | 
|---|
| 31 |  | 
|---|
| 32 | #include "CodePatterns/Observer/Observer.hpp" | 
|---|
| 33 | #include "CodePatterns/enumeration.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | /****************************************** forward declarations *****************************/ | 
|---|
| 36 |  | 
|---|
| 37 | class AtomicInfo; | 
|---|
| 38 | class Vector; | 
|---|
| 39 | class World; | 
|---|
| 40 | class molecule; | 
|---|
| 41 | class Shape; | 
|---|
| 42 |  | 
|---|
| 43 | /********************************************** declarations *******************************/ | 
|---|
| 44 |  | 
|---|
| 45 | /** Single atom. | 
|---|
| 46 | * Class incorporates position, type | 
|---|
| 47 | */ | 
|---|
| 48 | class atom : | 
|---|
| 49 | public GraphNode, | 
|---|
| 50 | public BondedParticle, | 
|---|
| 51 | public TesselPoint | 
|---|
| 52 | { | 
|---|
| 53 | friend atom* NewAtom(atomId_t); | 
|---|
| 54 | friend void  DeleteAtom(atom*); | 
|---|
| 55 |  | 
|---|
| 56 | atom *father;   //!< In many-body bond order fragmentations points to originating atom | 
|---|
| 57 | int *sort;      //!< sort criteria | 
|---|
| 58 |  | 
|---|
| 59 | public: | 
|---|
| 60 |  | 
|---|
| 61 | /** Clones this atom. | 
|---|
| 62 | * | 
|---|
| 63 | * Does not clone the bonds! | 
|---|
| 64 | * | 
|---|
| 65 | * @return reference to atom | 
|---|
| 66 | */ | 
|---|
| 67 | virtual atom *clone(); | 
|---|
| 68 |  | 
|---|
| 69 | /** Pushes back another step in all trajectory vectors. | 
|---|
| 70 | * | 
|---|
| 71 | * This allows to extend all trajectories contained in different classes | 
|---|
| 72 | * consistently. This is implemented by the topmost class which calls the | 
|---|
| 73 | * real functions, \sa AppendTrajectoryStep(), by all necessary subclasses. | 
|---|
| 74 | */ | 
|---|
| 75 | virtual void UpdateStep(const unsigned int _step); | 
|---|
| 76 |  | 
|---|
| 77 | /** Pops the last step in all trajectory vectors. | 
|---|
| 78 | * | 
|---|
| 79 | * This allows to decrease all trajectories contained in different classes | 
|---|
| 80 | * by one consistently. This is implemented by the topmost class which calls | 
|---|
| 81 | * the real functions, \sa removeTrajectoryStep(), by all necessary subclasses. | 
|---|
| 82 | */ | 
|---|
| 83 | virtual void removeStep(const unsigned int _step); | 
|---|
| 84 |  | 
|---|
| 85 | /** Output of a single atom with given numbering. | 
|---|
| 86 | * \param ElementNo cardinal number of the element | 
|---|
| 87 | * \param AtomNo cardinal number among these atoms of the same element | 
|---|
| 88 | * \param *out stream to output to | 
|---|
| 89 | * \param *comment commentary after '#' sign | 
|---|
| 90 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 91 | */ | 
|---|
| 92 | bool OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment = NULL) const; | 
|---|
| 93 |  | 
|---|
| 94 | /** Output of a single atom with numbering from array according to atom::type. | 
|---|
| 95 | * \param *ElementNo cardinal number of the element | 
|---|
| 96 | * \param *AtomNo cardinal number among these atoms of the same element | 
|---|
| 97 | * \param *out stream to output to | 
|---|
| 98 | * \param *comment commentary after '#' sign | 
|---|
| 99 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 100 | */ | 
|---|
| 101 | bool OutputArrayIndexed(ostream * const out,const enumeration<const element*>&, int *AtomNo, const char *comment = NULL) const; | 
|---|
| 102 |  | 
|---|
| 103 | /** Initialises the component number array. | 
|---|
| 104 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1) | 
|---|
| 105 | */ | 
|---|
| 106 | void InitComponentNr(); | 
|---|
| 107 |  | 
|---|
| 108 | /** Resets GraphNr to -1. | 
|---|
| 109 | * | 
|---|
| 110 | */ | 
|---|
| 111 | void resetGraphNr(); | 
|---|
| 112 |  | 
|---|
| 113 | /** Check whether father is equal to given atom. | 
|---|
| 114 | * \param *ptr atom to compare father to | 
|---|
| 115 | * \param **res return value (only set if atom::father is equal to \a *ptr) | 
|---|
| 116 | */ | 
|---|
| 117 | void EqualsFather ( const atom *ptr, const atom **res ) const; | 
|---|
| 118 |  | 
|---|
| 119 | /** States whether the given \a *ptr is our father. | 
|---|
| 120 | * | 
|---|
| 121 | * @param ptr atom to compare atom::Father with | 
|---|
| 122 | * @return true - \a *ptr is father, false - not | 
|---|
| 123 | */ | 
|---|
| 124 | bool isFather(const atom *ptr); | 
|---|
| 125 |  | 
|---|
| 126 | /** If we are copy of copy, we are linked to be just a copy. | 
|---|
| 127 | * | 
|---|
| 128 | */ | 
|---|
| 129 | void CorrectFather(); | 
|---|
| 130 |  | 
|---|
| 131 | /** Climbs up the father list until NULL, last is returned. | 
|---|
| 132 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen) | 
|---|
| 133 | */ | 
|---|
| 134 | atom *GetTrueFather(); | 
|---|
| 135 |  | 
|---|
| 136 | /** Const version of \sa GetTrueFather(). | 
|---|
| 137 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen) | 
|---|
| 138 | */ | 
|---|
| 139 | const atom *GetTrueFather() const; | 
|---|
| 140 |  | 
|---|
| 141 | /** Const getter for the atoms father. | 
|---|
| 142 | * | 
|---|
| 143 | * \return father of this atom | 
|---|
| 144 | */ | 
|---|
| 145 | atom * const getFather() const | 
|---|
| 146 | { return father; } | 
|---|
| 147 |  | 
|---|
| 148 | /** Sets the father for this atom. | 
|---|
| 149 | * | 
|---|
| 150 | * \param _father ptr to father atom | 
|---|
| 151 | */ | 
|---|
| 152 | void setFather(atom * const _father); | 
|---|
| 153 |  | 
|---|
| 154 | /** Compares the indices of \a this atom with a given \a ptr. | 
|---|
| 155 | * \param ptr atom to compare index against | 
|---|
| 156 | * \return true - this one's is smaller, false - not | 
|---|
| 157 | */ | 
|---|
| 158 | bool Compare(const atom &ptr) const; | 
|---|
| 159 |  | 
|---|
| 160 | /** Returns distance to a given vector. | 
|---|
| 161 | * \param origin vector to calculate distance to | 
|---|
| 162 | * \return distance | 
|---|
| 163 | */ | 
|---|
| 164 | double DistanceToVector(const Vector &origin) const; | 
|---|
| 165 |  | 
|---|
| 166 | /** Returns squared distance to a given vector. | 
|---|
| 167 | * \param origin vector to calculate distance to | 
|---|
| 168 | * \return distance squared | 
|---|
| 169 | */ | 
|---|
| 170 | double DistanceSquaredToVector(const Vector &origin) const; | 
|---|
| 171 |  | 
|---|
| 172 | // getter and setter | 
|---|
| 173 |  | 
|---|
| 174 | /** | 
|---|
| 175 | * returns the World that contains this atom. | 
|---|
| 176 | * Use this if you need to get the world without locking | 
|---|
| 177 | * the singleton for example. | 
|---|
| 178 | * | 
|---|
| 179 | */ | 
|---|
| 180 | World *getWorld(); | 
|---|
| 181 | void setWorld(World*); | 
|---|
| 182 |  | 
|---|
| 183 | virtual atomId_t getId() const; | 
|---|
| 184 | virtual bool changeId(atomId_t newId); | 
|---|
| 185 |  | 
|---|
| 186 | /** | 
|---|
| 187 | * this function sets the Id without notifying the world. Only use it, if the world has already | 
|---|
| 188 | * gotten an ID for this Atom. | 
|---|
| 189 | */ | 
|---|
| 190 | virtual void setId(atomId_t); | 
|---|
| 191 |  | 
|---|
| 192 | /** Returns pointer to the molecule which atom belongs to. | 
|---|
| 193 | * \return containing molecule | 
|---|
| 194 | */ | 
|---|
| 195 | const molecule* getMolecule() const; | 
|---|
| 196 |  | 
|---|
| 197 | /** Erases the atom in atom::mol's list of atoms and sets it to zero. | 
|---|
| 198 | */ | 
|---|
| 199 | void removeFromMolecule(); | 
|---|
| 200 |  | 
|---|
| 201 | /** Changes the molecule internal ParticleInfo::Nr of this atom. | 
|---|
| 202 | * | 
|---|
| 203 | * @param newNr new ParticleInfo::Nr to set | 
|---|
| 204 | * @return true - change successful, false - changed not successful, id remains the old one | 
|---|
| 205 | */ | 
|---|
| 206 | bool changeNr(int newNr); | 
|---|
| 207 |  | 
|---|
| 208 | /** Getter for ParticleInfo::Nr of the atom. | 
|---|
| 209 | * | 
|---|
| 210 | * @return index | 
|---|
| 211 | */ | 
|---|
| 212 | int getNr() const; | 
|---|
| 213 |  | 
|---|
| 214 | // Output operator | 
|---|
| 215 | std::ostream & operator << (std::ostream &ost) const; | 
|---|
| 216 |  | 
|---|
| 217 | protected: | 
|---|
| 218 |  | 
|---|
| 219 | /** | 
|---|
| 220 | * Protected constructor to ensure construction of atoms through the world. | 
|---|
| 221 | * see World::createAtom() | 
|---|
| 222 | */ | 
|---|
| 223 | atom(); | 
|---|
| 224 |  | 
|---|
| 225 | /** | 
|---|
| 226 | * Protected copy-constructor to ensure construction of atoms by cloning. | 
|---|
| 227 | * see atom::clone() | 
|---|
| 228 | */ | 
|---|
| 229 | atom(class atom *pointer); | 
|---|
| 230 |  | 
|---|
| 231 | /** | 
|---|
| 232 | * Protected destructor to ensure destruction of atoms through the world. | 
|---|
| 233 | * see World::destroyAtom() | 
|---|
| 234 | */ | 
|---|
| 235 | virtual ~atom(); | 
|---|
| 236 | private: | 
|---|
| 237 | friend class molecule; | 
|---|
| 238 | friend class AtomicInfo; | 
|---|
| 239 | /** Makes the atom be contained in the new molecule \a *_mol. | 
|---|
| 240 | * Uses atom::removeFromMolecule() to delist from old molecule. | 
|---|
| 241 | * \param *_mol pointer to new molecule | 
|---|
| 242 | */ | 
|---|
| 243 | void setMolecule(molecule*); | 
|---|
| 244 |  | 
|---|
| 245 | //!> grant World (only) access to selection state changers | 
|---|
| 246 | friend class World; | 
|---|
| 247 |  | 
|---|
| 248 | /** Sets the internal selection state to true. | 
|---|
| 249 | * | 
|---|
| 250 | */ | 
|---|
| 251 | void select(); | 
|---|
| 252 |  | 
|---|
| 253 | /** Unsets the internal selection state to true. | 
|---|
| 254 | * | 
|---|
| 255 | */ | 
|---|
| 256 | void unselect(); | 
|---|
| 257 |  | 
|---|
| 258 | //    virtual void update(Observable *publisher); | 
|---|
| 259 | //    virtual void recieveNotification(Observable *publisher, Notification_ptr notification); | 
|---|
| 260 | virtual void subjectKilled(Observable *publisher); | 
|---|
| 261 |  | 
|---|
| 262 | public: | 
|---|
| 263 |  | 
|---|
| 264 | /** Getter to internal selection status. | 
|---|
| 265 | * | 
|---|
| 266 | * \return true - atom is selected, false - else | 
|---|
| 267 | */ | 
|---|
| 268 | bool getSelected() const { return selected; } | 
|---|
| 269 |  | 
|---|
| 270 | private: | 
|---|
| 271 | molecule *mol; // !< the molecule this atom belongs to | 
|---|
| 272 | World* world; | 
|---|
| 273 | atomId_t id; | 
|---|
| 274 | //!> internal state whether atom is selected or not | 
|---|
| 275 | bool selected; | 
|---|
| 276 | }; | 
|---|
| 277 |  | 
|---|
| 278 | /** | 
|---|
| 279 | * Global output operator for class atom. | 
|---|
| 280 | */ | 
|---|
| 281 | std::ostream & operator << (std::ostream &ost, const atom &_atom); | 
|---|
| 282 |  | 
|---|
| 283 | /** | 
|---|
| 284 | * internal method used by the world. Do not use if you don't know what you are doing. | 
|---|
| 285 | * You might get burned... | 
|---|
| 286 | * Use World::createAtom() instead. | 
|---|
| 287 | */ | 
|---|
| 288 | atom* NewAtom(atomId_t _id); | 
|---|
| 289 |  | 
|---|
| 290 | /** | 
|---|
| 291 | * internal method used by the world. Do not use if you don't know what you are doing. | 
|---|
| 292 | * You might get burned... | 
|---|
| 293 | * Use World::destroyAtom() instead. | 
|---|
| 294 | */ | 
|---|
| 295 | void  DeleteAtom(atom*); | 
|---|
| 296 |  | 
|---|
| 297 | /** | 
|---|
| 298 | * Simple function to compare atoms by their elements to allow sorting of atoms by this criteria | 
|---|
| 299 | */ | 
|---|
| 300 | bool compareAtomElements(atom* atom1,atom* atom2); | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 | #endif /* ATOM_HPP_ */ | 
|---|