| 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_molecule.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Mar 30, 2012 | 
|---|
| 28 | *      Author: ankele | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | // include config.h | 
|---|
| 36 | #ifdef HAVE_CONFIG_H | 
|---|
| 37 | #include <config.h> | 
|---|
| 38 | #endif | 
|---|
| 39 |  | 
|---|
| 40 | #include "GLMoleculeObject_molecule.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include <Qt3D/qglscenenode.h> | 
|---|
| 43 | #include <Qt3D/qglbuilder.h> | 
|---|
| 44 |  | 
|---|
| 45 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include "CodePatterns/Assert.hpp" | 
|---|
| 48 | #include "CodePatterns/Log.hpp" | 
|---|
| 49 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| 50 | #include "CodePatterns/Observer/ObserverLog.hpp" | 
|---|
| 51 |  | 
|---|
| 52 | #include "Atom/atom.hpp" | 
|---|
| 53 | #include "molecule.hpp" | 
|---|
| 54 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 55 | #include "Descriptors/MoleculeIdDescriptor.hpp" | 
|---|
| 56 | #include "Element/element.hpp" | 
|---|
| 57 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 58 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 59 | #include "LinkedCell/linkedcell.hpp" | 
|---|
| 60 | #include "Tesselation/tesselation.hpp" | 
|---|
| 61 | #include "Tesselation/BoundaryLineSet.hpp" | 
|---|
| 62 | #include "Tesselation/BoundaryTriangleSet.hpp" | 
|---|
| 63 | #include "Tesselation/CandidateForTesselation.hpp" | 
|---|
| 64 | #include "Atom/TesselPoint.hpp" | 
|---|
| 65 | #include "World.hpp" | 
|---|
| 66 |  | 
|---|
| 67 | #include "GLMoleculeObject_atom.hpp" | 
|---|
| 68 |  | 
|---|
| 69 | static QGLSceneNode *createMoleculeMesh(const moleculeId_t molid, QObject *parent) | 
|---|
| 70 | { | 
|---|
| 71 | const molecule *molref = const_cast<const World &>(World::getInstance()). | 
|---|
| 72 | getMolecule(MoleculeById(molid)); | 
|---|
| 73 | if (molref == NULL) { | 
|---|
| 74 | ELOG(1, "Could not createMoleculeMesh, molecule with id " << molid << " already gone."); | 
|---|
| 75 | return NULL; | 
|---|
| 76 | } | 
|---|
| 77 | //  Shape shape = molref->getBoundingSphere(); | 
|---|
| 78 | double minradius = 2.; // TODO: set to maximum bond length value | 
|---|
| 79 | LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius); | 
|---|
| 80 | // check minimum bond radius in molecule | 
|---|
| 81 | double minlength = std::numeric_limits<double>::max(); | 
|---|
| 82 | for (molecule::const_iterator iter = molref->begin(); | 
|---|
| 83 | iter != molref->end(); ++iter) { | 
|---|
| 84 | const BondList &ListOfBonds = (*iter)->getListOfBonds(); | 
|---|
| 85 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 86 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 87 | const double bond_distance = (*bonditer)->GetDistance(); | 
|---|
| 88 | minlength = std::min(bond_distance, minlength); | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | minradius = std::max( std::max(minradius, minlength), 1.); | 
|---|
| 92 |  | 
|---|
| 93 | QGeometryData geo; | 
|---|
| 94 | // we need at least three points for tesselation | 
|---|
| 95 | if (molref->getAtomCount() >= 3) { | 
|---|
| 96 | // Tesselate the points. | 
|---|
| 97 | Tesselation T; | 
|---|
| 98 | PointCloudAdaptor<molecule> cloud(const_cast<molecule *>(molref), molref->getName()); | 
|---|
| 99 | T(cloud, minradius); | 
|---|
| 100 |  | 
|---|
| 101 | // Fill the points into a Qt geometry. | 
|---|
| 102 | LinkedCell_deprecated LinkedList(cloud, minradius); | 
|---|
| 103 | std::map<int, int> indices; | 
|---|
| 104 | std::map<int, Vector> normals; | 
|---|
| 105 | int index = 0; | 
|---|
| 106 | for (PointMap::const_iterator piter = T.PointsOnBoundary.begin(); | 
|---|
| 107 | piter != T.PointsOnBoundary.end(); ++piter) { | 
|---|
| 108 | const Vector &point = piter->second->getPosition(); | 
|---|
| 109 | // add data to the primitive | 
|---|
| 110 | geo.appendVertex(QVector3D(point[0], point[1], point[2])); | 
|---|
| 111 | Vector normalvector; | 
|---|
| 112 | for (LineMap::const_iterator lineiter = piter->second->lines.begin(); | 
|---|
| 113 | lineiter != piter->second->lines.end(); ++lineiter) | 
|---|
| 114 | for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin(); | 
|---|
| 115 | triangleiter != lineiter->second->triangles.end(); ++triangleiter) | 
|---|
| 116 | normalvector += | 
|---|
| 117 | triangleiter->second->NormalVector; | 
|---|
| 118 | normalvector.Normalize(); | 
|---|
| 119 | geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2])); | 
|---|
| 120 | geo.appendColor(QColor(1, 1, 1, 1)); | 
|---|
| 121 | geo.appendTexCoord(QVector2D(0, 0)); | 
|---|
| 122 | indices.insert( std::make_pair( piter->second->getNr(), index++)); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | // Fill the tesselated triangles into the geometry. | 
|---|
| 126 | for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin(); | 
|---|
| 127 | runner != T.TrianglesOnBoundary.end(); runner++) { | 
|---|
| 128 | int v[3]; | 
|---|
| 129 | for (size_t i=0; i<3; ++i) | 
|---|
| 130 | v[i] = runner->second->endpoints[i]->getNr(); | 
|---|
| 131 |  | 
|---|
| 132 | // Sort the vertices so the triangle is clockwise (relative to the normal vector). | 
|---|
| 133 | Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition(); | 
|---|
| 134 | cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition()); | 
|---|
| 135 | if (cross.ScalarProduct(runner->second->NormalVector) > 0) | 
|---|
| 136 | geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]); | 
|---|
| 137 | else | 
|---|
| 138 | geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | // Build a mesh from the geometry. | 
|---|
| 143 | QGLBuilder builder; | 
|---|
| 144 | builder.addTriangles(geo); | 
|---|
| 145 | QGLSceneNode *mesh = builder.finalizedSceneNode(); | 
|---|
| 146 | return mesh; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | GLMoleculeObject_molecule::GLMoleculeObject_molecule(QObject *parent, const moleculeId_t molid) : | 
|---|
| 150 | GLMoleculeObject(createMoleculeMesh(molid, parent), parent), | 
|---|
| 151 | Observer(std::string("GLMoleculeObject_molecule")+toString(molid)), | 
|---|
| 152 | isBoundingBoxUptodate(true), | 
|---|
| 153 | isSignedOn(false), | 
|---|
| 154 | moleculeid(molid), | 
|---|
| 155 | TesselationHullUptodate(true), | 
|---|
| 156 | hoverAtomId(-1) | 
|---|
| 157 | { | 
|---|
| 158 | // sign on as observer (obtain non-const instance before) | 
|---|
| 159 | const molecule *_molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 160 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 161 | if (_molecule != NULL) { | 
|---|
| 162 | _molecule->signOn(this, molecule::AtomInserted); | 
|---|
| 163 | _molecule->signOn(this, molecule::AtomRemoved); | 
|---|
| 164 | _molecule->signOn(this, molecule::AtomMoved); | 
|---|
| 165 | isSignedOn = true; | 
|---|
| 166 | } else { | 
|---|
| 167 | ELOG(1, "GLMoleculeObject_molecule() - added null object for not present mol id " << moleculeid); | 
|---|
| 168 | } | 
|---|
| 169 | /*molref->signOn(this, AtomObservable::IndexChanged); | 
|---|
| 170 | molref->signOn(this, AtomObservable::PositionChanged); | 
|---|
| 171 | molref->signOn(this, AtomObservable::ElementChanged); | 
|---|
| 172 | molref->signOn(this, AtomObservable::BondsAdded);*/ | 
|---|
| 173 | setMaterial(getMaterial(1)); | 
|---|
| 174 | World::getInstance().signOn(this, World::SelectionChanged); | 
|---|
| 175 | updateBoundingBox(); | 
|---|
| 176 |  | 
|---|
| 177 | // initially, atoms and bonds should be visible | 
|---|
| 178 | m_visible = false; | 
|---|
| 179 |  | 
|---|
| 180 | init(); | 
|---|
| 181 |  | 
|---|
| 182 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 183 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 184 |  | 
|---|
| 185 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked())); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | GLMoleculeObject_molecule::GLMoleculeObject_molecule(QGLSceneNode *mesh[], QObject *parent, const moleculeId_t molid) : | 
|---|
| 189 | GLMoleculeObject(mesh, parent), | 
|---|
| 190 | Observer(std::string("GLMoleculeObject_molecule")+toString(molid)), | 
|---|
| 191 | isBoundingBoxUptodate(true), | 
|---|
| 192 | isSignedOn(false), | 
|---|
| 193 | moleculeid(molid), | 
|---|
| 194 | TesselationHullUptodate(true), | 
|---|
| 195 | hoverAtomId(-1) | 
|---|
| 196 | { | 
|---|
| 197 | // sign on as observer (obtain non-const instance before) | 
|---|
| 198 | const molecule *_molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 199 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 200 | if (_molecule != NULL) { | 
|---|
| 201 | _molecule->signOn(this, molecule::AtomInserted); | 
|---|
| 202 | _molecule->signOn(this, molecule::AtomRemoved); | 
|---|
| 203 | _molecule->signOn(this, molecule::AtomMoved); | 
|---|
| 204 | isSignedOn = true; | 
|---|
| 205 | } else { | 
|---|
| 206 | ELOG(1, "GLMoleculeObject_molecule() - added null object for not present mol id " << moleculeid); | 
|---|
| 207 | } | 
|---|
| 208 | /*molref->signOn(this, AtomObservable::IndexChanged); | 
|---|
| 209 | molref->signOn(this, AtomObservable::PositionChanged); | 
|---|
| 210 | molref->signOn(this, AtomObservable::ElementChanged); | 
|---|
| 211 | molref->signOn(this, AtomObservable::BondsAdded);*/ | 
|---|
| 212 | setMaterial(getMaterial(1)); | 
|---|
| 213 | World::getInstance().signOn(this, World::SelectionChanged); | 
|---|
| 214 | updateBoundingBox(); | 
|---|
| 215 |  | 
|---|
| 216 | // initially, atoms and bonds should be visible | 
|---|
| 217 | m_visible = false; | 
|---|
| 218 |  | 
|---|
| 219 | init(); | 
|---|
| 220 |  | 
|---|
| 221 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 222 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 223 |  | 
|---|
| 224 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked())); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | GLMoleculeObject_molecule::~GLMoleculeObject_molecule() | 
|---|
| 228 | { | 
|---|
| 229 | if (isSignedOn) { | 
|---|
| 230 | const molecule *_molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 231 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 232 | if (_molecule != NULL) { | 
|---|
| 233 | _molecule->signOff(this, molecule::AtomInserted); | 
|---|
| 234 | _molecule->signOff(this, molecule::AtomRemoved); | 
|---|
| 235 | _molecule->signOff(this, molecule::AtomMoved); | 
|---|
| 236 | } else { | 
|---|
| 237 | ELOG(1, "GLMoleculeObject_molecule cannot sign off, molecule with " | 
|---|
| 238 | << moleculeid << " has disappeared."); | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 | /*_atom->signOff(this, AtomObservable::IndexChanged); | 
|---|
| 242 | _atom->signOff(this, AtomObservable::PositionChanged); | 
|---|
| 243 | _atom->signOff(this, AtomObservable::ElementChanged); | 
|---|
| 244 | _atom->signOff(this, AtomObservable::BondsAdded);*/ | 
|---|
| 245 | World::getInstance().signOff(this, World::SelectionChanged); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | /** Initialise the WorldScene with molecules and atoms from World. | 
|---|
| 249 | * | 
|---|
| 250 | */ | 
|---|
| 251 | void GLMoleculeObject_molecule::init() | 
|---|
| 252 | { | 
|---|
| 253 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 254 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 255 | if (_molecule == NULL) { | 
|---|
| 256 | ELOG(1, "GLMoleculeObject_molecule cannot init, molecule with " | 
|---|
| 257 | << moleculeid << " has disappeared."); | 
|---|
| 258 | return; | 
|---|
| 259 | } | 
|---|
| 260 | if (_molecule->begin() != _molecule->end()) { | 
|---|
| 261 | int atomicid = -1; | 
|---|
| 262 | for (molecule::const_iterator atomiter = _molecule->begin(); | 
|---|
| 263 | atomiter != _molecule->end(); | 
|---|
| 264 | atomiter++) { | 
|---|
| 265 | // create atom objects in scene | 
|---|
| 266 | atomicid = (*atomiter)->getId(); | 
|---|
| 267 | atomInserted(atomicid); | 
|---|
| 268 |  | 
|---|
| 269 | // create bond objects in scene | 
|---|
| 270 | const BondList &bondlist = (*atomiter)->getListOfBonds(); | 
|---|
| 271 | for (BondList::const_iterator bonditer = bondlist.begin(); | 
|---|
| 272 | bonditer != bondlist.end(); | 
|---|
| 273 | ++bonditer) { | 
|---|
| 274 | const bond::ptr _bond = *bonditer; | 
|---|
| 275 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ? | 
|---|
| 276 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right; | 
|---|
| 277 | bondInserted(_bond, side); | 
|---|
| 278 | } | 
|---|
| 279 | } | 
|---|
| 280 | // set id to one of the atom's as either mol or atoms is present at the same time | 
|---|
| 281 | setObjectId(atomicid); | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | void GLMoleculeObject_molecule::addAtomBonds( | 
|---|
| 286 | const bond::ptr &_bond, | 
|---|
| 287 | const GLMoleculeObject_bond::SideOfBond _side | 
|---|
| 288 | ) | 
|---|
| 289 | { | 
|---|
| 290 | bool bond_present = false; | 
|---|
| 291 | const BondIds ids = getBondIds(_bond, _side); | 
|---|
| 292 | // check whether bond is not present already | 
|---|
| 293 | bond_present = BondsinSceneMap.count(ids); | 
|---|
| 294 | if (!bond_present) | 
|---|
| 295 | bondInserted(_bond, _side); | 
|---|
| 296 | else { | 
|---|
| 297 | BondsinSceneMap[ids]->resetPosition(); | 
|---|
| 298 | BondsinSceneMap[ids]->resetWidth(); | 
|---|
| 299 | } | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | void GLMoleculeObject_molecule::addAtomBonds( | 
|---|
| 303 | const atom *_atom) | 
|---|
| 304 | { | 
|---|
| 305 | const bool atom_present = AtomsinSceneMap.count(_atom->getId()); | 
|---|
| 306 | const BondList &bondlist = _atom->getListOfBonds(); | 
|---|
| 307 | for (BondList::const_iterator bonditer = bondlist.begin(); | 
|---|
| 308 | (bonditer != bondlist.end()) && atom_present; | 
|---|
| 309 | ++bonditer) { | 
|---|
| 310 | const bond::ptr _bond = *bonditer; | 
|---|
| 311 | // check if OtherAtom's sphere is already present | 
|---|
| 312 | const atom *OtherAtom = _bond->GetOtherAtom(_atom); | 
|---|
| 313 | const bool otheratom_present = AtomsinSceneMap.count(OtherAtom->getId()); | 
|---|
| 314 | if (otheratom_present && atom_present) { | 
|---|
| 315 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ? | 
|---|
| 316 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right; | 
|---|
| 317 | const GLMoleculeObject_bond::SideOfBond otherside = (_bond->leftatom == _atom) ? | 
|---|
| 318 | GLMoleculeObject_bond::right : GLMoleculeObject_bond::left; | 
|---|
| 319 | addAtomBonds(_bond, side); | 
|---|
| 320 | addAtomBonds(_bond, otherside); | 
|---|
| 321 | } | 
|---|
| 322 | } | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | void GLMoleculeObject_molecule::reinit() | 
|---|
| 326 | { | 
|---|
| 327 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 328 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 329 | if (_molecule == NULL) { | 
|---|
| 330 | ELOG(1, "GLMoleculeObject_molecule cannot reinit, molecule with " | 
|---|
| 331 | << moleculeid << " has disappeared."); | 
|---|
| 332 | return; | 
|---|
| 333 | } | 
|---|
| 334 | if (_molecule->getAtomCount() > 0) { | 
|---|
| 335 | for (molecule::const_iterator atomiter = _molecule->begin(); | 
|---|
| 336 | atomiter != _molecule->end(); | 
|---|
| 337 | atomiter++) { | 
|---|
| 338 | // check whether atom already exists | 
|---|
| 339 | const atomId_t atomid = (*atomiter)->getId(); | 
|---|
| 340 | const bool atom_present = AtomsinSceneMap.count(atomid); | 
|---|
| 341 | if (!atom_present) | 
|---|
| 342 | atomInserted((*atomiter)->getId()); | 
|---|
| 343 | else | 
|---|
| 344 | AtomsinSceneMap[atomid]->resetPosition(); | 
|---|
| 345 |  | 
|---|
| 346 |  | 
|---|
| 347 | // create bond objects in scene | 
|---|
| 348 | addAtomBonds(*atomiter); | 
|---|
| 349 | } | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | void GLMoleculeObject_molecule::updateBoundingBox() | 
|---|
| 354 | { | 
|---|
| 355 | isBoundingBoxUptodate = true; | 
|---|
| 356 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 357 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 358 | if (_molecule == NULL) { | 
|---|
| 359 | ELOG(1, "GLMoleculeObject_molecule cannot updateBoundingBox, molecule with " | 
|---|
| 360 | << moleculeid << " has disappeared."); | 
|---|
| 361 | return; | 
|---|
| 362 | } | 
|---|
| 363 | Shape shape = _molecule->getBoundingSphere(); | 
|---|
| 364 | Vector v = shape.getCenter(); | 
|---|
| 365 | setPosition(QVector3D(v[0], v[1], v[2])); | 
|---|
| 366 | setScale(shape.getRadius() + 0.3); // getBoundingShape() only sees atoms as points, so make the box a bit bigger | 
|---|
| 367 | } | 
|---|
| 368 |  | 
|---|
| 369 | void GLMoleculeObject_molecule::update(Observable *publisher) | 
|---|
| 370 | { | 
|---|
| 371 | #ifdef LOG_OBSERVER | 
|---|
| 372 | const molecule *_mol = static_cast<molecule *>(publisher); | 
|---|
| 373 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from molecule "+toString(_mol->getId())+"."; | 
|---|
| 374 | #endif | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | void GLMoleculeObject_molecule::subjectKilled(Observable *publisher) | 
|---|
| 378 | { | 
|---|
| 379 | isSignedOn = false; | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | void GLMoleculeObject_molecule::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
| 383 | { | 
|---|
| 384 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()). | 
|---|
| 385 | getMolecule(MoleculeById(moleculeid)); | 
|---|
| 386 | if (publisher == dynamic_cast<const Observable*>(_molecule)){ | 
|---|
| 387 | // notofication from atom | 
|---|
| 388 | #ifdef LOG_OBSERVER | 
|---|
| 389 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this)) | 
|---|
| 390 | << " received notification from molecule " << _molecule->getId() << " for channel " | 
|---|
| 391 | << notification->getChannelNo() << "."; | 
|---|
| 392 | #endif | 
|---|
| 393 | switch (notification->getChannelNo()) { | 
|---|
| 394 | case molecule::AtomInserted: | 
|---|
| 395 | { | 
|---|
| 396 | const atomId_t _id = _molecule->lastChanged()->getId(); | 
|---|
| 397 | #ifdef LOG_OBSERVER | 
|---|
| 398 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted."; | 
|---|
| 399 | #endif | 
|---|
| 400 | TesselationHullUptodate = false; | 
|---|
| 401 | isBoundingBoxUptodate = false; | 
|---|
| 402 | atomInserted(_id); | 
|---|
| 403 | break; | 
|---|
| 404 | } | 
|---|
| 405 | case World::AtomRemoved: | 
|---|
| 406 | { | 
|---|
| 407 | const atomId_t _id = _molecule->lastChanged()->getId(); | 
|---|
| 408 | #ifdef LOG_OBSERVER | 
|---|
| 409 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been removed."; | 
|---|
| 410 | #endif | 
|---|
| 411 | TesselationHullUptodate = false; | 
|---|
| 412 | isBoundingBoxUptodate = false; | 
|---|
| 413 | atomRemoved(_id); | 
|---|
| 414 | break; | 
|---|
| 415 | } | 
|---|
| 416 | case molecule::AtomMoved: | 
|---|
| 417 | { | 
|---|
| 418 | #ifdef LOG_OBSERVER | 
|---|
| 419 | const atomId_t _id = _molecule->lastChanged()->getId(); | 
|---|
| 420 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted."; | 
|---|
| 421 | #endif | 
|---|
| 422 | TesselationHullUptodate = false; | 
|---|
| 423 | isBoundingBoxUptodate = false; | 
|---|
| 424 | break; | 
|---|
| 425 | } | 
|---|
| 426 | default: | 
|---|
| 427 | break; | 
|---|
| 428 | } | 
|---|
| 429 | }else{ | 
|---|
| 430 | // notification from world | 
|---|
| 431 | #ifdef LOG_OBSERVER | 
|---|
| 432 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this)) | 
|---|
| 433 | << " received notification from world for channel " | 
|---|
| 434 | << notification->getChannelNo() << "."; | 
|---|
| 435 | #endif | 
|---|
| 436 | switch (notification->getChannelNo()) { | 
|---|
| 437 | case World::SelectionChanged: | 
|---|
| 438 | if (_molecule != NULL) { | 
|---|
| 439 | setSelected(World::getInstance().isSelected(_molecule)); | 
|---|
| 440 | } else { | 
|---|
| 441 | ELOG(1, "GLMoleculeObject_molecule cannot change selection, molecule with " | 
|---|
| 442 | << moleculeid << " has disappeared."); | 
|---|
| 443 | } | 
|---|
| 444 | break; | 
|---|
| 445 | default: | 
|---|
| 446 | break; | 
|---|
| 447 | } | 
|---|
| 448 | } | 
|---|
| 449 | } | 
|---|
| 450 |  | 
|---|
| 451 | void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter) | 
|---|
| 452 | { | 
|---|
| 453 | // Initialize all of the mesh objects that we have as children. | 
|---|
| 454 | if (m_visible) { | 
|---|
| 455 | GLMoleculeObject::initialize(view, painter); | 
|---|
| 456 | } else { | 
|---|
| 457 | foreach (QObject *obj, children()) { | 
|---|
| 458 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 459 | if (meshobj) | 
|---|
| 460 | meshobj->initialize(view, painter); | 
|---|
| 461 | } | 
|---|
| 462 | } | 
|---|
| 463 | } | 
|---|
| 464 |  | 
|---|
| 465 | void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| 466 | { | 
|---|
| 467 | // draw either molecule's mesh or all atoms and bonds | 
|---|
| 468 | if (m_visible) { | 
|---|
| 469 | updateTesselationHull(); | 
|---|
| 470 |  | 
|---|
| 471 | painter->modelViewMatrix().push(); | 
|---|
| 472 |  | 
|---|
| 473 | // Apply the material and effect to the painter. | 
|---|
| 474 | QGLMaterial *material; | 
|---|
| 475 | if (m_hovering) | 
|---|
| 476 | material = m_hoverMaterial; | 
|---|
| 477 | else if (m_selected) | 
|---|
| 478 | material = m_selectionMaterial; | 
|---|
| 479 | else | 
|---|
| 480 | material = m_material; | 
|---|
| 481 |  | 
|---|
| 482 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL"); | 
|---|
| 483 |  | 
|---|
| 484 | painter->setColor(material->diffuseColor()); | 
|---|
| 485 | painter->setFaceMaterial(QGL::AllFaces, material); | 
|---|
| 486 | if (m_effect) | 
|---|
| 487 | painter->setUserEffect(m_effect); | 
|---|
| 488 | else | 
|---|
| 489 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 490 |  | 
|---|
| 491 | // Mark the object for object picking purposes. | 
|---|
| 492 | int prevObjectId = painter->objectPickId(); | 
|---|
| 493 | if (m_objectId != -1) | 
|---|
| 494 | painter->setObjectPickId(m_objectId); | 
|---|
| 495 |  | 
|---|
| 496 | m_mesh[0]->draw(painter); | 
|---|
| 497 |  | 
|---|
| 498 | // Turn off the user effect, if present. | 
|---|
| 499 | if (m_effect) | 
|---|
| 500 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 501 |  | 
|---|
| 502 | // Revert to the previous object identifier. | 
|---|
| 503 | painter->setObjectPickId(prevObjectId); | 
|---|
| 504 |  | 
|---|
| 505 | // Restore the modelview matrix. | 
|---|
| 506 | painter->modelViewMatrix().pop(); | 
|---|
| 507 |  | 
|---|
| 508 | //    GLMoleculeObject::draw(painter, cameraPlane); | 
|---|
| 509 | } else { | 
|---|
| 510 | // Draw all of the mesh objects that we have as children. | 
|---|
| 511 | foreach (QObject *obj, children()) { | 
|---|
| 512 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 513 | if (meshobj) | 
|---|
| 514 | meshobj->draw(painter, cameraPlane); | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | // update bounding box prior to selection | 
|---|
| 518 | if (!isBoundingBoxUptodate) | 
|---|
| 519 | updateBoundingBox(); | 
|---|
| 520 |  | 
|---|
| 521 | painter->modelViewMatrix().push(); | 
|---|
| 522 | painter->modelViewMatrix().translate(m_position); | 
|---|
| 523 | if (m_rotationAngle != 0.0f) | 
|---|
| 524 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector); | 
|---|
| 525 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f)) | 
|---|
| 526 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ); | 
|---|
| 527 |  | 
|---|
| 528 | // Draw a box around the mesh, if selected. | 
|---|
| 529 | if (m_selected) | 
|---|
| 530 | drawSelectionBox(painter); | 
|---|
| 531 |  | 
|---|
| 532 | // Restore the modelview matrix. | 
|---|
| 533 | painter->modelViewMatrix().pop(); | 
|---|
| 534 | } | 
|---|
| 535 | } | 
|---|
| 536 |  | 
|---|
| 537 | /** Adds an atom of this molecule to the scene. | 
|---|
| 538 | * | 
|---|
| 539 | * @param _atom atom to add | 
|---|
| 540 | */ | 
|---|
| 541 | void GLMoleculeObject_molecule::atomInserted(const atomicNumber_t _id) | 
|---|
| 542 | { | 
|---|
| 543 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+"."); | 
|---|
| 544 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(GLMoleculeObject::meshSphere, this, _id); | 
|---|
| 545 | ASSERT( atomObject != NULL, | 
|---|
| 546 | "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id)); | 
|---|
| 547 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id); | 
|---|
| 548 | ASSERT(iter == AtomsinSceneMap.end(), | 
|---|
| 549 | "GLMoleculeObject_molecule::atomInserted - same atom with id "+toString(_id)+" added again."); | 
|---|
| 550 | AtomsinSceneMap.insert( make_pair(_id, atomObject) ); | 
|---|
| 551 |  | 
|---|
| 552 | qRegisterMetaType<atomId_t>("atomId_t"); | 
|---|
| 553 | qRegisterMetaType<bond::ptr>("bond::ptr"); | 
|---|
| 554 | qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond"); | 
|---|
| 555 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t))); | 
|---|
| 556 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 557 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 558 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 559 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed())); | 
|---|
| 560 | connect (atomObject, SIGNAL(BondsInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond))); | 
|---|
| 561 | connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SIGNAL(changeAtomId(GLMoleculeObject_atom*, int, int))); | 
|---|
| 562 |  | 
|---|
| 563 | isBoundingBoxUptodate = false; | 
|---|
| 564 |  | 
|---|
| 565 | if (m_objectId  == -1) | 
|---|
| 566 | setObjectId(_id); | 
|---|
| 567 |  | 
|---|
| 568 | // add all bonds | 
|---|
| 569 | const atom *Walker = World::getInstance().getAtom(AtomById(_id)); | 
|---|
| 570 | if (Walker != NULL) | 
|---|
| 571 | addAtomBonds(Walker); | 
|---|
| 572 | else | 
|---|
| 573 | ELOG(1, "GLMoleculeObject_atom disappeared while about to add bonds."); | 
|---|
| 574 |  | 
|---|
| 575 | emit changeOccured(); | 
|---|
| 576 | } | 
|---|
| 577 |  | 
|---|
| 578 | /** Removes an atom of this molecule from the scene. | 
|---|
| 579 | * | 
|---|
| 580 | * We just the id as the atom might have already been destroyed. | 
|---|
| 581 | * | 
|---|
| 582 | * @param _id id of atom to remove | 
|---|
| 583 | */ | 
|---|
| 584 | void GLMoleculeObject_molecule::atomRemoved(const atomicNumber_t _id) | 
|---|
| 585 | { | 
|---|
| 586 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+"."); | 
|---|
| 587 | // bonds are removed by signal coming from ~bond | 
|---|
| 588 |  | 
|---|
| 589 | if ((unsigned int)m_objectId == _id) | 
|---|
| 590 | setObjectId(-1); | 
|---|
| 591 |  | 
|---|
| 592 | // remove atoms | 
|---|
| 593 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id); | 
|---|
| 594 | ASSERT(iter != AtomsinSceneMap.end(), | 
|---|
| 595 | "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display."); | 
|---|
| 596 | GLMoleculeObject_atom *atomObject = iter->second; | 
|---|
| 597 | AtomsinSceneMap.erase(iter); | 
|---|
| 598 | atomObject->disconnect(); | 
|---|
| 599 | delete atomObject; | 
|---|
| 600 |  | 
|---|
| 601 | isBoundingBoxUptodate = false; | 
|---|
| 602 |  | 
|---|
| 603 | emit changeOccured(); | 
|---|
| 604 | } | 
|---|
| 605 |  | 
|---|
| 606 | void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob) | 
|---|
| 607 | { | 
|---|
| 608 | // Find the atom, ob corresponds to. | 
|---|
| 609 | hoverAtomId = -1; | 
|---|
| 610 | GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob); | 
|---|
| 611 | if (atomObject){ | 
|---|
| 612 | for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){ | 
|---|
| 613 | if (iter->second == atomObject) | 
|---|
| 614 | hoverAtomId = iter->first; | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | // Propagate signal. | 
|---|
| 618 | emit hoverChanged(hoverAtomId); | 
|---|
| 619 | } else { | 
|---|
| 620 | // Find the atom, ob corresponds to. | 
|---|
| 621 | GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob); | 
|---|
| 622 | if (moleculeObject == this){ | 
|---|
| 623 | // Propagate signal. | 
|---|
| 624 | emit hoverChanged(moleculeid, 0); | 
|---|
| 625 | } | 
|---|
| 626 | } | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 |  | 
|---|
| 630 | /** Helper function to get bond ids in the correct order for BondNodeMap. | 
|---|
| 631 | * | 
|---|
| 632 | * \return pair of ids in correct order. | 
|---|
| 633 | */ | 
|---|
| 634 | GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds( | 
|---|
| 635 | const bond::ptr _bond, | 
|---|
| 636 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 637 | { | 
|---|
| 638 | BondIds ids; | 
|---|
| 639 | switch (_side) { | 
|---|
| 640 | case GLMoleculeObject_bond::left: | 
|---|
| 641 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()); | 
|---|
| 642 | break; | 
|---|
| 643 | case GLMoleculeObject_bond::right: | 
|---|
| 644 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()); | 
|---|
| 645 | break; | 
|---|
| 646 | } | 
|---|
| 647 | return ids; | 
|---|
| 648 | } | 
|---|
| 649 |  | 
|---|
| 650 | /** Adds a bond to the scene. | 
|---|
| 651 | * | 
|---|
| 652 | * @param _bond bond to add | 
|---|
| 653 | * @param side which side of the bond (left or right) | 
|---|
| 654 | */ | 
|---|
| 655 | void GLMoleculeObject_molecule::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 656 | { | 
|---|
| 657 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+"."); | 
|---|
| 658 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << "."); | 
|---|
| 659 |  | 
|---|
| 660 | const BondIds ids = getBondIds(_bond, _side); | 
|---|
| 661 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids); | 
|---|
| 662 | if (iter == BondsinSceneMap.end()) { | 
|---|
| 663 | GLMoleculeObject_bond * bondObject = | 
|---|
| 664 | new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, _bond, _side); | 
|---|
| 665 | connect ( | 
|---|
| 666 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)), | 
|---|
| 667 | this, SLOT(bondRemoved(const atomId_t, const atomId_t))); | 
|---|
| 668 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 669 | BondsinSceneMap.insert( make_pair(ids, bondObject) ); | 
|---|
| 670 | //    BondIdsinSceneMap.insert( Leftids ); | 
|---|
| 671 | } else { | 
|---|
| 672 | iter->second->resetPosition(); | 
|---|
| 673 | iter->second->resetWidth(); | 
|---|
| 674 | } | 
|---|
| 675 | emit changeOccured(); | 
|---|
| 676 | } | 
|---|
| 677 |  | 
|---|
| 678 | /** Removes a bond from the scene. | 
|---|
| 679 | * | 
|---|
| 680 | * @param _bond bond to remove | 
|---|
| 681 | */ | 
|---|
| 682 | void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr) | 
|---|
| 683 | { | 
|---|
| 684 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+"."); | 
|---|
| 685 | { | 
|---|
| 686 | // left bond | 
|---|
| 687 | const BondIds Leftids( make_pair(leftnr, rightnr) ); | 
|---|
| 688 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids ); | 
|---|
| 689 | ASSERT(leftiter != BondsinSceneMap.end(), | 
|---|
| 690 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-" | 
|---|
| 691 | +toString(rightnr)+" not on display."); | 
|---|
| 692 | GLMoleculeObject_bond *bondObject = leftiter->second; | 
|---|
| 693 | bondObject->disconnect(); | 
|---|
| 694 | BondsinSceneMap.erase(leftiter); | 
|---|
| 695 | delete bondObject; // is done by signal from bond itself | 
|---|
| 696 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << "."); | 
|---|
| 697 | } | 
|---|
| 698 |  | 
|---|
| 699 | emit changeOccured(); | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | void GLMoleculeObject_molecule::setVisible(bool value) | 
|---|
| 703 | { | 
|---|
| 704 | // first update the mesh if we are going to be visible now | 
|---|
| 705 | if (value) | 
|---|
| 706 | updateTesselationHull(); | 
|---|
| 707 | // then emit onward | 
|---|
| 708 | GLMoleculeObject::setVisible(value); | 
|---|
| 709 | } | 
|---|
| 710 |  | 
|---|
| 711 | void GLMoleculeObject_molecule::updateTesselationHull() | 
|---|
| 712 | { | 
|---|
| 713 | if (!TesselationHullUptodate) { | 
|---|
| 714 | updateMesh(createMoleculeMesh(moleculeid, parent())); | 
|---|
| 715 | TesselationHullUptodate = true; | 
|---|
| 716 | } | 
|---|
| 717 | } | 
|---|
| 718 |  | 
|---|
| 719 | std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t) | 
|---|
| 720 | { | 
|---|
| 721 | ost << t.first << "," << t.second; | 
|---|
| 722 | return ost; | 
|---|
| 723 | } | 
|---|
| 724 |  | 
|---|
| 725 | void GLMoleculeObject_molecule::wasClicked() | 
|---|
| 726 | { | 
|---|
| 727 | LOG(4, "INFO: GLMoleculeObject_molecule: atom " << moleculeid << " has been clicked"); | 
|---|
| 728 | emit moleculeClicked(moleculeid); | 
|---|
| 729 | } | 
|---|