[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"
|
---|
[704d59] | 55 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
[c67518] | 56 | #include "Element/element.hpp"
|
---|
| 57 | #include "LinearAlgebra/Vector.hpp"
|
---|
[34e7fdb] | 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"
|
---|
[c67518] | 65 | #include "World.hpp"
|
---|
| 66 |
|
---|
[8c001a] | 67 | #include "GLMoleculeObject_atom.hpp"
|
---|
| 68 |
|
---|
[704d59] | 69 | static QGLSceneNode *createMoleculeMesh(const moleculeId_t molid, QObject *parent)
|
---|
[34e7fdb] | 70 | {
|
---|
[63fb7a] | 71 | const molecule *molref = const_cast<const World &>(World::getInstance()).
|
---|
| 72 | getMolecule(MoleculeById(molid));
|
---|
[704d59] | 73 | if (molref == NULL) {
|
---|
| 74 | ELOG(1, "Could not createMoleculeMesh, molecule with id " << molid << " already gone.");
|
---|
| 75 | return NULL;
|
---|
| 76 | }
|
---|
[34e7fdb] | 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);
|
---|
[b9b49e] | 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.);
|
---|
[34e7fdb] | 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 |
|
---|
[704d59] | 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)),
|
---|
[2b596f] | 152 | isBoundingBoxUptodate(true),
|
---|
[34e7fdb] | 153 | isSignedOn(false),
|
---|
[704d59] | 154 | moleculeid(molid),
|
---|
[7b5984] | 155 | TesselationHullUptodate(true),
|
---|
[704d59] | 156 | hoverAtomId(-1)
|
---|
[34e7fdb] | 157 | {
|
---|
| 158 | // sign on as observer (obtain non-const instance before)
|
---|
[63fb7a] | 159 | const molecule *_molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 160 | getMolecule(MoleculeById(moleculeid));
|
---|
[704d59] | 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 | }
|
---|
[34e7fdb] | 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 |
|
---|
[2b596f] | 180 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
|
---|
| 181 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
|
---|
[9a7ef9] | 182 |
|
---|
| 183 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
[34e7fdb] | 184 | }
|
---|
| 185 |
|
---|
[704d59] | 186 | GLMoleculeObject_molecule::GLMoleculeObject_molecule(QGLSceneNode *mesh[], QObject *parent, const moleculeId_t molid) :
|
---|
[bca99d] | 187 | GLMoleculeObject(mesh, parent),
|
---|
[704d59] | 188 | Observer(std::string("GLMoleculeObject_molecule")+toString(molid)),
|
---|
[2b596f] | 189 | isBoundingBoxUptodate(true),
|
---|
[34e7fdb] | 190 | isSignedOn(false),
|
---|
[704d59] | 191 | moleculeid(molid),
|
---|
[7b5984] | 192 | TesselationHullUptodate(true),
|
---|
[704d59] | 193 | hoverAtomId(-1)
|
---|
[c67518] | 194 | {
|
---|
| 195 | // sign on as observer (obtain non-const instance before)
|
---|
[63fb7a] | 196 | const molecule *_molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 197 | getMolecule(MoleculeById(moleculeid));
|
---|
[704d59] | 198 | if (_molecule != NULL) {
|
---|
| 199 | _molecule->signOn(this, molecule::AtomInserted);
|
---|
| 200 | _molecule->signOn(this, molecule::AtomRemoved);
|
---|
| 201 | _molecule->signOn(this, molecule::AtomMoved);
|
---|
| 202 | isSignedOn = true;
|
---|
| 203 | } else {
|
---|
| 204 | ELOG(1, "GLMoleculeObject_molecule() - added null object for not present mol id " << moleculeid);
|
---|
| 205 | }
|
---|
[c67518] | 206 | /*molref->signOn(this, AtomObservable::IndexChanged);
|
---|
| 207 | molref->signOn(this, AtomObservable::PositionChanged);
|
---|
| 208 | molref->signOn(this, AtomObservable::ElementChanged);
|
---|
| 209 | molref->signOn(this, AtomObservable::BondsAdded);*/
|
---|
[3b229e] | 210 | setMaterial(getMaterial(1));
|
---|
[3927ef] | 211 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
[d6203a] | 212 | updateBoundingBox();
|
---|
[8c001a] | 213 |
|
---|
[739ee9] | 214 | // initially, atoms and bonds should be visible
|
---|
| 215 | m_visible = false;
|
---|
| 216 |
|
---|
[2b596f] | 217 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
|
---|
| 218 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
|
---|
[9a7ef9] | 219 |
|
---|
| 220 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
[c67518] | 221 | }
|
---|
| 222 |
|
---|
| 223 | GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
|
---|
| 224 | {
|
---|
[34e7fdb] | 225 | if (isSignedOn) {
|
---|
[63fb7a] | 226 | const molecule *_molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 227 | getMolecule(MoleculeById(moleculeid));
|
---|
[704d59] | 228 | if (_molecule != NULL) {
|
---|
| 229 | _molecule->signOff(this, molecule::AtomInserted);
|
---|
| 230 | _molecule->signOff(this, molecule::AtomRemoved);
|
---|
| 231 | _molecule->signOff(this, molecule::AtomMoved);
|
---|
| 232 | } else {
|
---|
| 233 | ELOG(1, "GLMoleculeObject_molecule cannot sign off, molecule with "
|
---|
| 234 | << moleculeid << " has disappeared.");
|
---|
| 235 | }
|
---|
[34e7fdb] | 236 | }
|
---|
[c67518] | 237 | /*_atom->signOff(this, AtomObservable::IndexChanged);
|
---|
| 238 | _atom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 239 | _atom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 240 | _atom->signOff(this, AtomObservable::BondsAdded);*/
|
---|
| 241 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[8c001a] | 244 | void GLMoleculeObject_molecule::addAtomBonds(
|
---|
| 245 | const bond::ptr &_bond,
|
---|
| 246 | const GLMoleculeObject_bond::SideOfBond _side
|
---|
| 247 | )
|
---|
| 248 | {
|
---|
| 249 | bool bond_present = false;
|
---|
| 250 | const BondIds ids = getBondIds(_bond, _side);
|
---|
| 251 | // check whether bond is not present already
|
---|
| 252 | bond_present = BondsinSceneMap.count(ids);
|
---|
| 253 | if (!bond_present)
|
---|
| 254 | bondInserted(_bond, _side);
|
---|
| 255 | else {
|
---|
| 256 | BondsinSceneMap[ids]->resetPosition();
|
---|
| 257 | BondsinSceneMap[ids]->resetWidth();
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | void GLMoleculeObject_molecule::addAtomBonds(
|
---|
| 262 | const atom *_atom)
|
---|
| 263 | {
|
---|
| 264 | const bool atom_present = AtomsinSceneMap.count(_atom->getId());
|
---|
| 265 | const BondList &bondlist = _atom->getListOfBonds();
|
---|
| 266 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
| 267 | (bonditer != bondlist.end()) && atom_present;
|
---|
| 268 | ++bonditer) {
|
---|
| 269 | const bond::ptr _bond = *bonditer;
|
---|
| 270 | // check if OtherAtom's sphere is already present
|
---|
| 271 | const atom *OtherAtom = _bond->GetOtherAtom(_atom);
|
---|
| 272 | const bool otheratom_present = AtomsinSceneMap.count(OtherAtom->getId());
|
---|
| 273 | if (otheratom_present && atom_present) {
|
---|
| 274 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
|
---|
| 275 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 276 | const GLMoleculeObject_bond::SideOfBond otherside = (_bond->leftatom == _atom) ?
|
---|
| 277 | GLMoleculeObject_bond::right : GLMoleculeObject_bond::left;
|
---|
| 278 | addAtomBonds(_bond, side);
|
---|
| 279 | addAtomBonds(_bond, otherside);
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[d6203a] | 284 | void GLMoleculeObject_molecule::updateBoundingBox()
|
---|
| 285 | {
|
---|
[2b596f] | 286 | isBoundingBoxUptodate = true;
|
---|
[63fb7a] | 287 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 288 | getMolecule(MoleculeById(moleculeid));
|
---|
[704d59] | 289 | if (_molecule == NULL) {
|
---|
| 290 | ELOG(1, "GLMoleculeObject_molecule cannot updateBoundingBox, molecule with "
|
---|
| 291 | << moleculeid << " has disappeared.");
|
---|
| 292 | return;
|
---|
| 293 | }
|
---|
[aeb694] | 294 | Shape shape = _molecule->getBoundingSphere();
|
---|
[d6203a] | 295 | Vector v = shape.getCenter();
|
---|
| 296 | setPosition(QVector3D(v[0], v[1], v[2]));
|
---|
| 297 | setScale(shape.getRadius() + 0.3); // getBoundingShape() only sees atoms as points, so make the box a bit bigger
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[c67518] | 300 | void GLMoleculeObject_molecule::update(Observable *publisher)
|
---|
| 301 | {
|
---|
| 302 | #ifdef LOG_OBSERVER
|
---|
[a2a2f7] | 303 | const molecule *_mol = static_cast<molecule *>(publisher);
|
---|
| 304 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from molecule "+toString(_mol->getId())+".";
|
---|
[c67518] | 305 | #endif
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | void GLMoleculeObject_molecule::subjectKilled(Observable *publisher)
|
---|
[34e7fdb] | 309 | {
|
---|
| 310 | isSignedOn = false;
|
---|
| 311 | }
|
---|
[c67518] | 312 |
|
---|
| 313 | void GLMoleculeObject_molecule::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 314 | {
|
---|
[63fb7a] | 315 | const molecule * const _molecule = const_cast<const World &>(World::getInstance()).
|
---|
| 316 | getMolecule(MoleculeById(moleculeid));
|
---|
[c67518] | 317 | if (publisher == dynamic_cast<const Observable*>(_molecule)){
|
---|
| 318 | // notofication from atom
|
---|
| 319 | #ifdef LOG_OBSERVER
|
---|
[708277] | 320 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
[c67518] | 321 | << " received notification from molecule " << _molecule->getId() << " for channel "
|
---|
| 322 | << notification->getChannelNo() << ".";
|
---|
| 323 | #endif
|
---|
[8c001a] | 324 | switch (notification->getChannelNo()) {
|
---|
| 325 | case molecule::AtomInserted:
|
---|
| 326 | {
|
---|
| 327 | const atomId_t _id = _molecule->lastChanged()->getId();
|
---|
| 328 | #ifdef LOG_OBSERVER
|
---|
| 329 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
|
---|
| 330 | #endif
|
---|
[7b5984] | 331 | TesselationHullUptodate = false;
|
---|
[2b596f] | 332 | isBoundingBoxUptodate = false;
|
---|
[8c001a] | 333 | atomInserted(_id);
|
---|
| 334 | break;
|
---|
| 335 | }
|
---|
| 336 | case World::AtomRemoved:
|
---|
| 337 | {
|
---|
| 338 | const atomId_t _id = _molecule->lastChanged()->getId();
|
---|
| 339 | #ifdef LOG_OBSERVER
|
---|
| 340 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been removed.";
|
---|
| 341 | #endif
|
---|
[7b5984] | 342 | TesselationHullUptodate = false;
|
---|
[2b596f] | 343 | isBoundingBoxUptodate = false;
|
---|
[8c001a] | 344 | atomRemoved(_id);
|
---|
| 345 | break;
|
---|
| 346 | }
|
---|
[7b5984] | 347 | case molecule::AtomMoved:
|
---|
| 348 | {
|
---|
| 349 | #ifdef LOG_OBSERVER
|
---|
[2b596f] | 350 | const atomId_t _id = _molecule->lastChanged()->getId();
|
---|
[7b5984] | 351 | observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
|
---|
| 352 | #endif
|
---|
| 353 | TesselationHullUptodate = false;
|
---|
[2b596f] | 354 | isBoundingBoxUptodate = false;
|
---|
[7b5984] | 355 | break;
|
---|
| 356 | }
|
---|
[8c001a] | 357 | default:
|
---|
| 358 | break;
|
---|
| 359 | }
|
---|
[c67518] | 360 | }else{
|
---|
| 361 | // notification from world
|
---|
| 362 | #ifdef LOG_OBSERVER
|
---|
[708277] | 363 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
|
---|
[c67518] | 364 | << " received notification from world for channel "
|
---|
| 365 | << notification->getChannelNo() << ".";
|
---|
| 366 | #endif
|
---|
| 367 | switch (notification->getChannelNo()) {
|
---|
| 368 | case World::SelectionChanged:
|
---|
[704d59] | 369 | if (_molecule != NULL) {
|
---|
| 370 | setSelected(World::getInstance().isSelected(_molecule));
|
---|
| 371 | } else {
|
---|
| 372 | ELOG(1, "GLMoleculeObject_molecule cannot change selection, molecule with "
|
---|
| 373 | << moleculeid << " has disappeared.");
|
---|
| 374 | }
|
---|
[c67518] | 375 | break;
|
---|
| 376 | default:
|
---|
| 377 | break;
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[8c001a] | 382 | void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
|
---|
| 383 | {
|
---|
| 384 | // Initialize all of the mesh objects that we have as children.
|
---|
[2b596f] | 385 | if (m_visible) {
|
---|
| 386 | GLMoleculeObject::initialize(view, painter);
|
---|
| 387 | } else {
|
---|
[8c001a] | 388 | foreach (QObject *obj, children()) {
|
---|
| 389 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 390 | if (meshobj)
|
---|
| 391 | meshobj->initialize(view, painter);
|
---|
| 392 | }
|
---|
[2b596f] | 393 | }
|
---|
[8c001a] | 394 | }
|
---|
| 395 |
|
---|
| 396 | void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
| 397 | {
|
---|
[739ee9] | 398 | // draw either molecule's mesh or all atoms and bonds
|
---|
| 399 | if (m_visible) {
|
---|
[7b5984] | 400 | updateTesselationHull();
|
---|
| 401 |
|
---|
[34e7fdb] | 402 | painter->modelViewMatrix().push();
|
---|
| 403 |
|
---|
| 404 | // Apply the material and effect to the painter.
|
---|
| 405 | QGLMaterial *material;
|
---|
| 406 | if (m_hovering)
|
---|
| 407 | material = m_hoverMaterial;
|
---|
| 408 | else if (m_selected)
|
---|
| 409 | material = m_selectionMaterial;
|
---|
| 410 | else
|
---|
| 411 | material = m_material;
|
---|
| 412 |
|
---|
| 413 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
|
---|
| 414 |
|
---|
| 415 | painter->setColor(material->diffuseColor());
|
---|
| 416 | painter->setFaceMaterial(QGL::AllFaces, material);
|
---|
| 417 | if (m_effect)
|
---|
| 418 | painter->setUserEffect(m_effect);
|
---|
| 419 | else
|
---|
| 420 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 421 |
|
---|
| 422 | // Mark the object for object picking purposes.
|
---|
| 423 | int prevObjectId = painter->objectPickId();
|
---|
| 424 | if (m_objectId != -1)
|
---|
| 425 | painter->setObjectPickId(m_objectId);
|
---|
| 426 |
|
---|
| 427 | m_mesh[0]->draw(painter);
|
---|
| 428 |
|
---|
| 429 | // Turn off the user effect, if present.
|
---|
| 430 | if (m_effect)
|
---|
| 431 | painter->setStandardEffect(QGL::LitMaterial);
|
---|
| 432 |
|
---|
| 433 | // Revert to the previous object identifier.
|
---|
| 434 | painter->setObjectPickId(prevObjectId);
|
---|
| 435 |
|
---|
| 436 | // Restore the modelview matrix.
|
---|
| 437 | painter->modelViewMatrix().pop();
|
---|
| 438 |
|
---|
| 439 | // GLMoleculeObject::draw(painter, cameraPlane);
|
---|
[739ee9] | 440 | } else {
|
---|
| 441 | // Draw all of the mesh objects that we have as children.
|
---|
| 442 | foreach (QObject *obj, children()) {
|
---|
| 443 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 444 | if (meshobj)
|
---|
| 445 | meshobj->draw(painter, cameraPlane);
|
---|
| 446 | }
|
---|
[2b596f] | 447 |
|
---|
| 448 | // update bounding box prior to selection
|
---|
| 449 | if (!isBoundingBoxUptodate)
|
---|
| 450 | updateBoundingBox();
|
---|
| 451 |
|
---|
| 452 | painter->modelViewMatrix().push();
|
---|
| 453 | painter->modelViewMatrix().translate(m_position);
|
---|
| 454 | if (m_rotationAngle != 0.0f)
|
---|
| 455 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
|
---|
[f47efd4] | 456 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
|
---|
| 457 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
|
---|
[2b596f] | 458 |
|
---|
| 459 | // Draw a box around the mesh, if selected.
|
---|
| 460 | if (m_selected)
|
---|
| 461 | drawSelectionBox(painter);
|
---|
| 462 |
|
---|
| 463 | // Restore the modelview matrix.
|
---|
| 464 | painter->modelViewMatrix().pop();
|
---|
[739ee9] | 465 | }
|
---|
[8c001a] | 466 | }
|
---|
| 467 |
|
---|
| 468 | /** Adds an atom of this molecule to the scene.
|
---|
| 469 | *
|
---|
| 470 | * @param _atom atom to add
|
---|
| 471 | */
|
---|
| 472 | void GLMoleculeObject_molecule::atomInserted(const atomicNumber_t _id)
|
---|
| 473 | {
|
---|
| 474 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+".");
|
---|
[8923ad8] | 475 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(GLMoleculeObject::meshSphere, this, _id);
|
---|
| 476 | ASSERT( atomObject != NULL,
|
---|
| 477 | "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id));
|
---|
| 478 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
|
---|
| 479 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
| 480 | "GLMoleculeObject_molecule::atomInserted - same atom with id "+toString(_id)+" added again.");
|
---|
| 481 | AtomsinSceneMap.insert( make_pair(_id, atomObject) );
|
---|
| 482 |
|
---|
| 483 | qRegisterMetaType<atomId_t>("atomId_t");
|
---|
| 484 | qRegisterMetaType<bond::ptr>("bond::ptr");
|
---|
| 485 | qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
|
---|
| 486 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t)));
|
---|
| 487 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
| 488 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
|
---|
| 489 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
|
---|
| 490 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
---|
| 491 | connect (atomObject, SIGNAL(BondsInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)));
|
---|
| 492 | connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
|
---|
| 493 |
|
---|
| 494 | isBoundingBoxUptodate = false;
|
---|
| 495 |
|
---|
| 496 | if (m_objectId == -1)
|
---|
| 497 | setObjectId(_id);
|
---|
[8c001a] | 498 |
|
---|
[52cd7b] | 499 | // add all bonds
|
---|
[f01769] | 500 | const atom * const Walker = const_cast<const World &>(World::getInstance()).
|
---|
| 501 | getAtom(AtomById(_id));
|
---|
[704d59] | 502 | if (Walker != NULL)
|
---|
| 503 | addAtomBonds(Walker);
|
---|
| 504 | else
|
---|
| 505 | ELOG(1, "GLMoleculeObject_atom disappeared while about to add bonds.");
|
---|
[52cd7b] | 506 |
|
---|
[8c001a] | 507 | emit changeOccured();
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | /** Removes an atom of this molecule from the scene.
|
---|
| 511 | *
|
---|
| 512 | * We just the id as the atom might have already been destroyed.
|
---|
| 513 | *
|
---|
| 514 | * @param _id id of atom to remove
|
---|
| 515 | */
|
---|
| 516 | void GLMoleculeObject_molecule::atomRemoved(const atomicNumber_t _id)
|
---|
| 517 | {
|
---|
| 518 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+".");
|
---|
| 519 | // bonds are removed by signal coming from ~bond
|
---|
[2b596f] | 520 |
|
---|
[704d59] | 521 | if ((unsigned int)m_objectId == _id)
|
---|
[2b596f] | 522 | setObjectId(-1);
|
---|
| 523 |
|
---|
[8c001a] | 524 | // remove atoms
|
---|
| 525 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
|
---|
| 526 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
| 527 | "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display.");
|
---|
| 528 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
| 529 | AtomsinSceneMap.erase(iter);
|
---|
[704d59] | 530 | atomObject->disconnect();
|
---|
[8c001a] | 531 | delete atomObject;
|
---|
| 532 |
|
---|
[2b596f] | 533 | isBoundingBoxUptodate = false;
|
---|
[8c001a] | 534 |
|
---|
| 535 | emit changeOccured();
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob)
|
---|
| 539 | {
|
---|
| 540 | // Find the atom, ob corresponds to.
|
---|
[704d59] | 541 | hoverAtomId = -1;
|
---|
[8c001a] | 542 | GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
|
---|
| 543 | if (atomObject){
|
---|
| 544 | for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
|
---|
| 545 | if (iter->second == atomObject)
|
---|
[704d59] | 546 | hoverAtomId = iter->first;
|
---|
[8c001a] | 547 | }
|
---|
| 548 |
|
---|
[2b596f] | 549 | // Propagate signal.
|
---|
[704d59] | 550 | emit hoverChanged(hoverAtomId);
|
---|
[2b596f] | 551 | } else {
|
---|
| 552 | // Find the atom, ob corresponds to.
|
---|
| 553 | GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob);
|
---|
| 554 | if (moleculeObject == this){
|
---|
| 555 | // Propagate signal.
|
---|
[704d59] | 556 | emit hoverChanged(moleculeid, 0);
|
---|
[2b596f] | 557 | }
|
---|
| 558 | }
|
---|
[8c001a] | 559 | }
|
---|
| 560 |
|
---|
| 561 |
|
---|
| 562 | /** Helper function to get bond ids in the correct order for BondNodeMap.
|
---|
| 563 | *
|
---|
| 564 | * \return pair of ids in correct order.
|
---|
| 565 | */
|
---|
| 566 | GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds(
|
---|
| 567 | const bond::ptr _bond,
|
---|
| 568 | const enum GLMoleculeObject_bond::SideOfBond _side)
|
---|
| 569 | {
|
---|
| 570 | BondIds ids;
|
---|
| 571 | switch (_side) {
|
---|
| 572 | case GLMoleculeObject_bond::left:
|
---|
| 573 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
---|
| 574 | break;
|
---|
| 575 | case GLMoleculeObject_bond::right:
|
---|
| 576 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
---|
| 577 | break;
|
---|
| 578 | }
|
---|
| 579 | return ids;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | /** Adds a bond to the scene.
|
---|
| 583 | *
|
---|
| 584 | * @param _bond bond to add
|
---|
| 585 | * @param side which side of the bond (left or right)
|
---|
| 586 | */
|
---|
| 587 | void GLMoleculeObject_molecule::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side)
|
---|
| 588 | {
|
---|
| 589 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
| 590 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
---|
| 591 |
|
---|
| 592 | const BondIds ids = getBondIds(_bond, _side);
|
---|
| 593 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
---|
| 594 | if (iter == BondsinSceneMap.end()) {
|
---|
| 595 | GLMoleculeObject_bond * bondObject =
|
---|
| 596 | new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, _bond, _side);
|
---|
| 597 | connect (
|
---|
| 598 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
---|
| 599 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
---|
| 600 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
---|
| 601 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
---|
| 602 | // BondIdsinSceneMap.insert( Leftids );
|
---|
| 603 | } else {
|
---|
| 604 | iter->second->resetPosition();
|
---|
| 605 | iter->second->resetWidth();
|
---|
| 606 | }
|
---|
| 607 | emit changeOccured();
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | /** Removes a bond from the scene.
|
---|
| 611 | *
|
---|
| 612 | * @param _bond bond to remove
|
---|
| 613 | */
|
---|
| 614 | void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
| 615 | {
|
---|
| 616 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
---|
| 617 | {
|
---|
| 618 | // left bond
|
---|
| 619 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
| 620 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
| 621 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
| 622 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
| 623 | +toString(rightnr)+" not on display.");
|
---|
| 624 | GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
| 625 | bondObject->disconnect();
|
---|
| 626 | BondsinSceneMap.erase(leftiter);
|
---|
| 627 | delete bondObject; // is done by signal from bond itself
|
---|
| 628 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | emit changeOccured();
|
---|
| 632 | }
|
---|
| 633 |
|
---|
[34e7fdb] | 634 | void GLMoleculeObject_molecule::setVisible(bool value)
|
---|
| 635 | {
|
---|
| 636 | // first update the mesh if we are going to be visible now
|
---|
| 637 | if (value)
|
---|
[7b5984] | 638 | updateTesselationHull();
|
---|
[34e7fdb] | 639 | // then emit onward
|
---|
| 640 | GLMoleculeObject::setVisible(value);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
[7b5984] | 643 | void GLMoleculeObject_molecule::updateTesselationHull()
|
---|
| 644 | {
|
---|
| 645 | if (!TesselationHullUptodate) {
|
---|
[704d59] | 646 | updateMesh(createMoleculeMesh(moleculeid, parent()));
|
---|
[7b5984] | 647 | TesselationHullUptodate = true;
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[8c001a] | 651 | std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t)
|
---|
| 652 | {
|
---|
| 653 | ost << t.first << "," << t.second;
|
---|
| 654 | return ost;
|
---|
| 655 | }
|
---|
[34e7fdb] | 656 |
|
---|
[9a7ef9] | 657 | void GLMoleculeObject_molecule::wasClicked()
|
---|
| 658 | {
|
---|
[704d59] | 659 | LOG(4, "INFO: GLMoleculeObject_molecule: atom " << moleculeid << " has been clicked");
|
---|
| 660 | emit moleculeClicked(moleculeid);
|
---|
[9a7ef9] | 661 | }
|
---|
[8d3ee6] | 662 |
|
---|
| 663 | void GLMoleculeObject_molecule::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
|
---|
| 664 | {
|
---|
| 665 | LOG(3, "INFO: GLMoleculeObject_molecule - change atom id " << oldId << " to " << newId << ".");
|
---|
| 666 |
|
---|
| 667 | // Remove from map.
|
---|
| 668 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
|
---|
| 669 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
| 670 | "GLMoleculeObject_molecule::changeAtomId() - atom with old id "+toString(oldId)+" not on display.");
|
---|
| 671 | ASSERT(iter->second == ob,
|
---|
| 672 | "GLMoleculeObject_molecule::changeAtomId() - atom with id "
|
---|
| 673 | +toString(oldId)+" does not match with object in AtomsinSceneMap.");
|
---|
| 674 | AtomsinSceneMap.erase(iter);
|
---|
| 675 |
|
---|
| 676 | // Reinsert with new id.
|
---|
| 677 | {
|
---|
| 678 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(newId);
|
---|
| 679 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
| 680 | "GLMoleculeObject_molecule::changeAtomId() - atom with new id "+toString(newId)+" already known.");
|
---|
| 681 | }
|
---|
| 682 | AtomsinSceneMap.insert( make_pair(newId, ob) );
|
---|
| 683 | }
|
---|