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