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