| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /*
 | 
|---|
| 25 |  * GLMoleculeObject_atom.cpp
 | 
|---|
| 26 |  *
 | 
|---|
| 27 |  *  Created on: Aug 17, 2011
 | 
|---|
| 28 |  *      Author: heber
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | 
 | 
|---|
| 31 | // include config.h
 | 
|---|
| 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 33 | #include <config.h>
 | 
|---|
| 34 | #endif
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "GLMoleculeObject_atom.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include <Qt3D/qglscenenode.h>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 44 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Atom/atom.hpp"
 | 
|---|
| 47 | #include "Bond/bond.hpp"
 | 
|---|
| 48 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 49 | #include "Element/element.hpp"
 | 
|---|
| 50 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 51 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| 52 | #include "World.hpp"
 | 
|---|
| 53 | #include "WorldTime.hpp"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atomId_t _id) :
 | 
|---|
| 56 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| 57 |   Observer(std::string("GLMoleculeObject_atom")+toString(_id)),
 | 
|---|
| 58 |   atomicid(_id),
 | 
|---|
| 59 |   uptodatePosition(false),
 | 
|---|
| 60 |   uptodateElement(false)
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| 63 |   const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 64 |   if (_atom != NULL) {
 | 
|---|
| 65 |     _atom->signOn(this, AtomObservable::IndexChanged);
 | 
|---|
| 66 |     _atom->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
| 67 |     _atom->signOn(this, AtomObservable::ElementChanged);
 | 
|---|
| 68 |     _atom->signOn(this, AtomObservable::BondsAdded);
 | 
|---|
| 69 |   } else {
 | 
|---|
| 70 |     ELOG(2, "Atom with id "+toString(atomicid)+" is already gone.");
 | 
|---|
| 71 |   }
 | 
|---|
| 72 |   World::getInstance().signOn(this, World::SelectionChanged);
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   // set the object's id
 | 
|---|
| 75 |   resetProperties();
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   LOG(2, "INFO: Created sphere for atom " << atomicid << ".");
 | 
|---|
| 78 | 
 | 
|---|
| 79 |   connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 85 |   if (_atom != NULL){
 | 
|---|
| 86 |     _atom->signOff(this, AtomObservable::IndexChanged);
 | 
|---|
| 87 |     _atom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 88 |     _atom->signOff(this, AtomObservable::ElementChanged);
 | 
|---|
| 89 |     _atom->signOff(this, AtomObservable::BondsAdded);
 | 
|---|
| 90 |   }
 | 
|---|
| 91 |   World::getInstance().signOff(this, World::SelectionChanged);
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | void GLMoleculeObject_atom::update(Observable *publisher)
 | 
|---|
| 95 | {
 | 
|---|
| 96 | #ifdef LOG_OBSERVER
 | 
|---|
| 97 |   observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from atom "+toString(atomicid)+".";
 | 
|---|
| 98 | #endif
 | 
|---|
| 99 |   resetProperties();
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | void GLMoleculeObject_atom::resetPosition()
 | 
|---|
| 103 | {
 | 
|---|
| 104 |   const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 105 |   if (_atom != NULL) {
 | 
|---|
| 106 |     const Vector Position = _atom->getPosition();
 | 
|---|
| 107 |     LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
 | 
|---|
| 108 |     setPosition(QVector3D(Position[0], Position[1], Position[2]));
 | 
|---|
| 109 |   } else {
 | 
|---|
| 110 |     ELOG(2, "Atom with id "+toString(atomicid)+" is already gone.");
 | 
|---|
| 111 |   }
 | 
|---|
| 112 |   uptodatePosition = true;
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | void GLMoleculeObject_atom::resetElement()
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   size_t elementno = 0;
 | 
|---|
| 118 |   const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 119 |   const element *_type = NULL;
 | 
|---|
| 120 |   if (_atom != NULL) {
 | 
|---|
| 121 |     _type = _atom->getType();
 | 
|---|
| 122 |   } else {
 | 
|---|
| 123 |     ELOG(2, "Atom with id "+toString(atomicid)+" is already gone.");
 | 
|---|
| 124 |   }
 | 
|---|
| 125 |   if (_type != NULL) {
 | 
|---|
| 126 |     elementno = _type->getAtomicNumber();
 | 
|---|
| 127 |   } else { // if no element yet, set to hydrogen
 | 
|---|
| 128 |     elementno = 1;
 | 
|---|
| 129 |   }
 | 
|---|
| 130 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   // set materials
 | 
|---|
| 133 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
| 134 |   ASSERT(elementmaterial != NULL,
 | 
|---|
| 135 |       "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
 | 
|---|
| 136 |   setMaterial(elementmaterial);
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // set scale
 | 
|---|
| 139 |   double radius = 0.;
 | 
|---|
| 140 |   if (_type != NULL) {
 | 
|---|
| 141 |     radius = _type->getVanDerWaalsRadius();
 | 
|---|
| 142 |   } else {
 | 
|---|
| 143 |     radius = 0.5;
 | 
|---|
| 144 |   }
 | 
|---|
| 145 |   setScale( radius / 4. );
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   uptodateElement = true;
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | void GLMoleculeObject_atom::resetIndex()
 | 
|---|
| 151 | {
 | 
|---|
| 152 |   int oldId = objectId();
 | 
|---|
| 153 |   LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(atomicid)+".");
 | 
|---|
| 154 |   setObjectId(atomicid);
 | 
|---|
| 155 | 
 | 
|---|
| 156 |   emit indexChanged(this, oldId, atomicid);
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|
| 159 | void GLMoleculeObject_atom::resetProperties()
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   // set position
 | 
|---|
| 162 |   resetPosition();
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   // set element
 | 
|---|
| 165 |   resetElement();
 | 
|---|
| 166 | 
 | 
|---|
| 167 |   // set the object's id
 | 
|---|
| 168 |   resetIndex();
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // selected?
 | 
|---|
| 171 |   setSelected(World::getInstance().isAtomSelected(atomicid));
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   // hook to update prior to painting
 | 
|---|
| 177 |   if (!uptodatePosition)
 | 
|---|
| 178 |     resetPosition();
 | 
|---|
| 179 |   if (!uptodateElement)
 | 
|---|
| 180 |     resetElement();
 | 
|---|
| 181 | 
 | 
|---|
| 182 |   // call old hook to do the actual paining
 | 
|---|
| 183 |   GLMoleculeObject::draw(painter, cameraPlane);
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
 | 
|---|
| 187 | {
 | 
|---|
| 188 |   // remove id such that we don't sign off accidentally from a different atom
 | 
|---|
| 189 |   const_cast<atomId_t &>(atomicid) = -1;
 | 
|---|
| 190 | }
 | 
|---|
| 191 | 
 | 
|---|
| 192 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 195 |   if (publisher == dynamic_cast<const Observable*>(_atom)){
 | 
|---|
| 196 |     // notofication from atom
 | 
|---|
| 197 | #ifdef LOG_OBSERVER
 | 
|---|
| 198 |     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
 | 
|---|
| 199 |           << " received notification from atom " << _atom->getId() << " for channel "
 | 
|---|
| 200 |           << notification->getChannelNo() << ".";
 | 
|---|
| 201 | #endif
 | 
|---|
| 202 |     switch (notification->getChannelNo()) {
 | 
|---|
| 203 |       case AtomObservable::ElementChanged:
 | 
|---|
| 204 |         uptodateElement = false;
 | 
|---|
| 205 |         break;
 | 
|---|
| 206 |       case AtomObservable::IndexChanged:
 | 
|---|
| 207 |         resetIndex();
 | 
|---|
| 208 |         break;
 | 
|---|
| 209 |       case AtomObservable::PositionChanged:
 | 
|---|
| 210 |         uptodatePosition = false;
 | 
|---|
| 211 |         break;
 | 
|---|
| 212 |       case AtomObservable::BondsAdded:
 | 
|---|
| 213 |       {
 | 
|---|
| 214 |         const atom *_atom = World::getInstance().getAtom(AtomById(atomicid));
 | 
|---|
| 215 |         if (_atom != NULL) {
 | 
|---|
| 216 |           // make sure position is up-to-date
 | 
|---|
| 217 |           if (!uptodatePosition)
 | 
|---|
| 218 |             resetPosition();
 | 
|---|
| 219 |           ASSERT(!_atom->getListOfBonds().empty(),
 | 
|---|
| 220 |               "GLMoleculeObject_atom::recieveNotification() - received BondsAdded but ListOfBonds is empty.");
 | 
|---|
| 221 |           const bond::ptr _bond = *(_atom->getListOfBonds().rbegin());
 | 
|---|
| 222 |           const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
 | 
|---|
| 223 |               GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
 | 
|---|
| 224 |           emit BondsInserted(_bond, side);
 | 
|---|
| 225 |         } else {
 | 
|---|
| 226 |           ELOG(2, "Atom with id "+toString(atomicid)+" is already gone.");
 | 
|---|
| 227 |         }
 | 
|---|
| 228 |         break;
 | 
|---|
| 229 |       }
 | 
|---|
| 230 |       default:
 | 
|---|
| 231 |         //setProperties();
 | 
|---|
| 232 |         break;
 | 
|---|
| 233 |     }
 | 
|---|
| 234 |   }else if (static_cast<World *>(publisher) == World::getPointer()) {
 | 
|---|
| 235 |     // notification from world
 | 
|---|
| 236 | #ifdef LOG_OBSERVER
 | 
|---|
| 237 |     observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
 | 
|---|
| 238 |           << " received notification from world for channel "
 | 
|---|
| 239 |           << notification->getChannelNo() << ".";
 | 
|---|
| 240 | #endif
 | 
|---|
| 241 |     switch (notification->getChannelNo()) {
 | 
|---|
| 242 |       case World::SelectionChanged:
 | 
|---|
| 243 |         if (_atom != NULL)
 | 
|---|
| 244 |           setSelected(World::getInstance().isSelected(_atom));
 | 
|---|
| 245 |         break;
 | 
|---|
| 246 |       default:
 | 
|---|
| 247 |         break;
 | 
|---|
| 248 |     }
 | 
|---|
| 249 |   }
 | 
|---|
| 250 | }
 | 
|---|
| 251 | 
 | 
|---|
| 252 | void GLMoleculeObject_atom::wasClicked()
 | 
|---|
| 253 | {
 | 
|---|
| 254 |   LOG(4, "INFO: GLMoleculeObject_atom: atom " << atomicid << " has been clicked");
 | 
|---|
| 255 |   emit clicked(atomicid);
 | 
|---|
| 256 | }
 | 
|---|