| 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 | // include config.h | 
|---|
| 33 | #ifdef HAVE_CONFIG_H | 
|---|
| 34 | #include <config.h> | 
|---|
| 35 | #endif | 
|---|
| 36 |  | 
|---|
| 37 | #include "GLMoleculeObject_molecule.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include <Qt3D/qglscenenode.h> | 
|---|
| 40 | #include <Qt3D/qglbuilder.h> | 
|---|
| 41 |  | 
|---|
| 42 | #include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #include <boost/assign.hpp> | 
|---|
| 47 |  | 
|---|
| 48 | #include "CodePatterns/Assert.hpp" | 
|---|
| 49 | #include "CodePatterns/IteratorAdaptors.hpp" | 
|---|
| 50 | #include "CodePatterns/Log.hpp" | 
|---|
| 51 |  | 
|---|
| 52 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 53 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 54 | #include "LinkedCell/linkedcell.hpp" | 
|---|
| 55 | #include "Tesselation/tesselation.hpp" | 
|---|
| 56 | #include "Tesselation/BoundaryLineSet.hpp" | 
|---|
| 57 | #include "Tesselation/BoundaryTriangleSet.hpp" | 
|---|
| 58 | #include "Tesselation/CandidateForTesselation.hpp" | 
|---|
| 59 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp" | 
|---|
| 60 | #include "Atom/TesselPoint.hpp" | 
|---|
| 61 | #include "World.hpp" | 
|---|
| 62 |  | 
|---|
| 63 | using namespace boost::assign; | 
|---|
| 64 |  | 
|---|
| 65 | static Observable::channels_t getAllAtomicChangesChannels() | 
|---|
| 66 | { | 
|---|
| 67 | Observable::channels_t channels; | 
|---|
| 68 | channels += molecule::AtomInserted, molecule::AtomRemoved, molecule::AtomMoved; | 
|---|
| 69 | return channels; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | const Observable::channels_t GLMoleculeObject_molecule::HullChannels(getAllAtomicChangesChannels()); | 
|---|
| 73 |  | 
|---|
| 74 | static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo) | 
|---|
| 75 | { | 
|---|
| 76 | // Build a mesh from the geometry. | 
|---|
| 77 | QGLBuilder builder; | 
|---|
| 78 | builder.addTriangles(_geo); | 
|---|
| 79 | QGLSceneNode *mesh = builder.finalizedSceneNode(); | 
|---|
| 80 | return mesh; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | GLMoleculeObject_molecule::GLMoleculeObject_molecule( | 
|---|
| 84 | QObject *parent, | 
|---|
| 85 | QtObservedInstanceBoard &_board, | 
|---|
| 86 | QtObservedMolecule::ptr &_ObservedMolecule) : | 
|---|
| 87 | GLMoleculeObject((QGLSceneNode *)NULL, parent), | 
|---|
| 88 | owner(NULL), | 
|---|
| 89 | molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())), | 
|---|
| 90 | hoverAtomId(-1), | 
|---|
| 91 | board(_board), | 
|---|
| 92 | ObservedMolecule(_ObservedMolecule) | 
|---|
| 93 | { | 
|---|
| 94 | init(ObservedMolecule->getMolIndex()); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | GLMoleculeObject_molecule::GLMoleculeObject_molecule( | 
|---|
| 98 | QGLSceneNode *mesh[], | 
|---|
| 99 | QObject *parent, | 
|---|
| 100 | QtObservedInstanceBoard &_board, | 
|---|
| 101 | QtObservedMolecule::ptr &_ObservedMolecule) : | 
|---|
| 102 | GLMoleculeObject(mesh, parent), | 
|---|
| 103 | owner(NULL), | 
|---|
| 104 | molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())), | 
|---|
| 105 | hoverAtomId(-1), | 
|---|
| 106 | board(_board), | 
|---|
| 107 | ObservedMolecule(_ObservedMolecule) | 
|---|
| 108 | { | 
|---|
| 109 | init(ObservedMolecule->getMolIndex()); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void GLMoleculeObject_molecule::init(const moleculeId_t _molid) | 
|---|
| 113 | { | 
|---|
| 114 | setObjectId(_molid); | 
|---|
| 115 | setMaterial(getMaterial(1)); | 
|---|
| 116 |  | 
|---|
| 117 | m_selected = const_cast<const World &>(World::getInstance()).isMoleculeSelected(_molid); | 
|---|
| 118 |  | 
|---|
| 119 | // initially, atoms and bonds should be visible | 
|---|
| 120 | m_visible = false; | 
|---|
| 121 |  | 
|---|
| 122 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 123 | connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 124 | connect (ObservedMolecule.get(), SIGNAL(tesselationhullChanged()), this, SLOT(resetTesselationHull())); | 
|---|
| 125 | connect (ObservedMolecule.get(), SIGNAL(boundingboxChanged()), this, SLOT(resetBoundingBox())); | 
|---|
| 126 | connect (ObservedMolecule.get(), SIGNAL(indexChanged(const moleculeId_t, const moleculeId_t)), | 
|---|
| 127 | this, SLOT(resetIndex(const moleculeId_t, const moleculeId_t))); | 
|---|
| 128 | /// these are channeled through GLWorldScene instead to ensure synchronicity | 
|---|
| 129 | //  connect (ObservedMolecule.get(), SIGNAL(atomInserted(QtObservedAtom::ptr)), | 
|---|
| 130 | //      this, SLOT(atomInserted(QtObservedAtom::ptr)) ); | 
|---|
| 131 | //  connect (ObservedMolecule.get(), SIGNAL(atomRemoved(const atomId_t)), | 
|---|
| 132 | //      this, SLOT(atomRemoved(const atomId_t)) ); | 
|---|
| 133 | connect (ObservedMolecule.get(), SIGNAL(selectedChanged()), this, SLOT(resetSelected())); | 
|---|
| 134 |  | 
|---|
| 135 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked())); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | GLMoleculeObject_molecule::~GLMoleculeObject_molecule() | 
|---|
| 139 | {} | 
|---|
| 140 |  | 
|---|
| 141 | void GLMoleculeObject_molecule::addAtomBonds( | 
|---|
| 142 | const bond::ptr &_bond, | 
|---|
| 143 | const GLMoleculeObject_bond::SideOfBond _side | 
|---|
| 144 | ) | 
|---|
| 145 | { | 
|---|
| 146 | bool bond_present = false; | 
|---|
| 147 | const BondIds ids = getBondIds(_bond, _side); | 
|---|
| 148 | // check whether bond is not present already | 
|---|
| 149 | bond_present = BondsinSceneMap.count(ids); | 
|---|
| 150 | if (!bond_present) | 
|---|
| 151 | bondInserted(ids.first, ids.second, _side); | 
|---|
| 152 | else { | 
|---|
| 153 | BondsinSceneMap[ids]->resetPosition(); | 
|---|
| 154 | BondsinSceneMap[ids]->resetWidth(); | 
|---|
| 155 | } | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | QGeometryData GLMoleculeObject_molecule::updateTesselationHull() const | 
|---|
| 159 | { | 
|---|
| 160 | QGeometryData geo; | 
|---|
| 161 |  | 
|---|
| 162 | const molecule * const molref = | 
|---|
| 163 | QtObservedMolecule::getMolecule(ObservedMolecule->getMolIndex()); | 
|---|
| 164 | if (molref == NULL) { | 
|---|
| 165 | ELOG(1, "Could not createMoleculeMesh, molecule with id " | 
|---|
| 166 | << ObservedMolecule->getMolIndex() << " already gone."); | 
|---|
| 167 | return geo; | 
|---|
| 168 | } | 
|---|
| 169 | double minradius = 2.; // TODO: set to maximum bond length value | 
|---|
| 170 | LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius); | 
|---|
| 171 | // check minimum bond radius in molecule | 
|---|
| 172 | double minlength = std::numeric_limits<double>::max(); | 
|---|
| 173 | for (molecule::const_iterator iter = molref->begin(); | 
|---|
| 174 | iter != molref->end(); ++iter) { | 
|---|
| 175 | const BondList &ListOfBonds = (*iter)->getListOfBonds(); | 
|---|
| 176 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 177 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 178 | const double bond_distance = (*bonditer)->GetDistance(); | 
|---|
| 179 | minlength = std::min(bond_distance, minlength); | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 | minradius = std::max( std::max(minradius, minlength), 1.); | 
|---|
| 183 |  | 
|---|
| 184 | // we need at least three points for tesselation | 
|---|
| 185 | if (getPresentAtoms().size() >= 3) { | 
|---|
| 186 | // Tesselate the points. | 
|---|
| 187 | Tesselation T; | 
|---|
| 188 | PointCloudAdaptor<molecule> cloud( | 
|---|
| 189 | const_cast<molecule *>(molref), | 
|---|
| 190 | ObservedMolecule->getMolName()); | 
|---|
| 191 | T(cloud, minradius); | 
|---|
| 192 |  | 
|---|
| 193 | // Fill the points into a Qt geometry. | 
|---|
| 194 | LinkedCell_deprecated LinkedList(cloud, minradius); | 
|---|
| 195 | std::map<int, int> indices; | 
|---|
| 196 | std::map<int, Vector> normals; | 
|---|
| 197 | int index = 0; | 
|---|
| 198 | for (PointMap::const_iterator piter = T.PointsOnBoundary.begin(); | 
|---|
| 199 | piter != T.PointsOnBoundary.end(); ++piter) { | 
|---|
| 200 | const Vector &point = piter->second->getPosition(); | 
|---|
| 201 | // add data to the primitive | 
|---|
| 202 | geo.appendVertex(QVector3D(point[0], point[1], point[2])); | 
|---|
| 203 | Vector normalvector; | 
|---|
| 204 | for (LineMap::const_iterator lineiter = piter->second->lines.begin(); | 
|---|
| 205 | lineiter != piter->second->lines.end(); ++lineiter) | 
|---|
| 206 | for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin(); | 
|---|
| 207 | triangleiter != lineiter->second->triangles.end(); ++triangleiter) | 
|---|
| 208 | normalvector += | 
|---|
| 209 | triangleiter->second->NormalVector; | 
|---|
| 210 | normalvector.Normalize(); | 
|---|
| 211 | geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2])); | 
|---|
| 212 | geo.appendColor(QColor(1, 1, 1, 1)); | 
|---|
| 213 | geo.appendTexCoord(QVector2D(0, 0)); | 
|---|
| 214 | indices.insert( std::make_pair( piter->second->getNr(), index++)); | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | // Fill the tesselated triangles into the geometry. | 
|---|
| 218 | for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin(); | 
|---|
| 219 | runner != T.TrianglesOnBoundary.end(); runner++) { | 
|---|
| 220 | int v[3]; | 
|---|
| 221 | for (size_t i=0; i<3; ++i) | 
|---|
| 222 | v[i] = runner->second->endpoints[i]->getNr(); | 
|---|
| 223 |  | 
|---|
| 224 | // Sort the vertices so the triangle is clockwise (relative to the normal vector). | 
|---|
| 225 | Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition(); | 
|---|
| 226 | cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition()); | 
|---|
| 227 | if (cross.ScalarProduct(runner->second->NormalVector) > 0) | 
|---|
| 228 | geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]); | 
|---|
| 229 | else | 
|---|
| 230 | geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]); | 
|---|
| 231 | } | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | return geo; | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | void GLMoleculeObject_molecule::resetTesselationHull() | 
|---|
| 238 | { | 
|---|
| 239 | if (owner != NULL) { | 
|---|
| 240 | TesselationHull = updateTesselationHull(); | 
|---|
| 241 | updateMesh(createMoleculeMesh(TesselationHull)); | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | void GLMoleculeObject_molecule::resetBoundingBox() | 
|---|
| 246 | { | 
|---|
| 247 | molecule::BoundingBoxInfo info = ObservedMolecule->getBoundingBox(); | 
|---|
| 248 | setPosition(QVector3D(info.position[0], info.position[1], info.position[2])); | 
|---|
| 249 | setScale(info.radius + 0.3); // getBoundingSphere() only sees atoms as points, so make the box a bit bigger | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | void GLMoleculeObject_molecule::resetIndex(const moleculeId_t, const moleculeId_t) | 
|---|
| 253 | { | 
|---|
| 254 | const atomId_t newId = ObservedMolecule->getMolIndex(); | 
|---|
| 255 | const size_t oldId = objectId(); | 
|---|
| 256 | ASSERT( newId != oldId, | 
|---|
| 257 | "GLMoleculeObject_molecule::resetIndex() - index "+toString(newId)+" did not change."); | 
|---|
| 258 | LOG(4, "INFO: GLMoleculeObject_molecule: new index is "+toString(newId)+"."); | 
|---|
| 259 | setObjectId(newId); | 
|---|
| 260 |  | 
|---|
| 261 | emit indexChanged(this, oldId, newId); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | void GLMoleculeObject_molecule::resetSelected() | 
|---|
| 265 | { | 
|---|
| 266 | const bool new_selected = ObservedMolecule->getMolSelected(); | 
|---|
| 267 | m_selected = new_selected; | 
|---|
| 268 |  | 
|---|
| 269 | emit changed(); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter) | 
|---|
| 273 | { | 
|---|
| 274 | // Initialize all of the mesh objects that we have as children. | 
|---|
| 275 | if (m_visible) { | 
|---|
| 276 | GLMoleculeObject::initialize(view, painter); | 
|---|
| 277 | } else { | 
|---|
| 278 | foreach (QObject *obj, children()) { | 
|---|
| 279 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 280 | if (meshobj) | 
|---|
| 281 | meshobj->initialize(view, painter); | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| 287 | { | 
|---|
| 288 | // draw either molecule's mesh or all atoms and bonds | 
|---|
| 289 | if (m_visible) { | 
|---|
| 290 | resetTesselationHull(); | 
|---|
| 291 |  | 
|---|
| 292 | painter->modelViewMatrix().push(); | 
|---|
| 293 |  | 
|---|
| 294 | // Apply the material and effect to the painter. | 
|---|
| 295 | QGLMaterial *material; | 
|---|
| 296 | if (m_hovering) | 
|---|
| 297 | material = m_hoverMaterial; | 
|---|
| 298 | else if (m_selected) | 
|---|
| 299 | material = m_selectionMaterial; | 
|---|
| 300 | else | 
|---|
| 301 | material = m_material; | 
|---|
| 302 |  | 
|---|
| 303 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL"); | 
|---|
| 304 |  | 
|---|
| 305 | painter->setColor(material->diffuseColor()); | 
|---|
| 306 | painter->setFaceMaterial(QGL::AllFaces, material); | 
|---|
| 307 | if (m_effect) | 
|---|
| 308 | painter->setUserEffect(m_effect); | 
|---|
| 309 | else | 
|---|
| 310 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 311 |  | 
|---|
| 312 | // Mark the object for object picking purposes. | 
|---|
| 313 | int prevObjectId = painter->objectPickId(); | 
|---|
| 314 | if (m_objectId != -1) | 
|---|
| 315 | painter->setObjectPickId(m_objectId); | 
|---|
| 316 |  | 
|---|
| 317 | m_mesh[0]->draw(painter); | 
|---|
| 318 |  | 
|---|
| 319 | // Turn off the user effect, if present. | 
|---|
| 320 | if (m_effect) | 
|---|
| 321 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 322 |  | 
|---|
| 323 | // Revert to the previous object identifier. | 
|---|
| 324 | painter->setObjectPickId(prevObjectId); | 
|---|
| 325 |  | 
|---|
| 326 | // Restore the modelview matrix. | 
|---|
| 327 | painter->modelViewMatrix().pop(); | 
|---|
| 328 |  | 
|---|
| 329 | //    GLMoleculeObject::draw(painter, cameraPlane); | 
|---|
| 330 | } else { | 
|---|
| 331 | // Draw all of the mesh objects that we have as children. | 
|---|
| 332 | foreach (QObject *obj, children()) { | 
|---|
| 333 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 334 | if (meshobj) | 
|---|
| 335 | meshobj->draw(painter, cameraPlane); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | // update bounding box prior to selection | 
|---|
| 339 | resetBoundingBox(); | 
|---|
| 340 |  | 
|---|
| 341 | painter->modelViewMatrix().push(); | 
|---|
| 342 | painter->modelViewMatrix().translate(m_position); | 
|---|
| 343 | if (m_rotationAngle != 0.0f) | 
|---|
| 344 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector); | 
|---|
| 345 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f)) | 
|---|
| 346 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ); | 
|---|
| 347 |  | 
|---|
| 348 | // Draw a box around the mesh, if selected. | 
|---|
| 349 | if (m_selected) | 
|---|
| 350 | drawSelectionBox(painter); | 
|---|
| 351 |  | 
|---|
| 352 | // Restore the modelview matrix. | 
|---|
| 353 | painter->modelViewMatrix().pop(); | 
|---|
| 354 | } | 
|---|
| 355 | } | 
|---|
| 356 |  | 
|---|
| 357 | /** Adds an atom of this molecule to the scene. | 
|---|
| 358 | * | 
|---|
| 359 | * @param _atom atom to add | 
|---|
| 360 | */ | 
|---|
| 361 | void GLMoleculeObject_molecule::atomInserted(QtObservedAtom::ptr _atom) | 
|---|
| 362 | { | 
|---|
| 363 | const atomId_t atomid = _atom->getAtomIndex(); | 
|---|
| 364 | LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomInserted for atom "+toString(atomid)+"."); | 
|---|
| 365 |  | 
|---|
| 366 | if (_atom) { | 
|---|
| 367 | GLMoleculeObject_atom *atomObject = | 
|---|
| 368 | new GLMoleculeObject_atom( | 
|---|
| 369 | GLMoleculeObject::meshSphere, | 
|---|
| 370 | this, | 
|---|
| 371 | _atom); | 
|---|
| 372 | ASSERT( atomObject != NULL, | 
|---|
| 373 | "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(atomid)); | 
|---|
| 374 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(atomid); | 
|---|
| 375 | ASSERT(iter == AtomsinSceneMap.end(), | 
|---|
| 376 | "GLMoleculeObject_molecule::atomInserted - same atom with id "+toString(atomid)+" added again."); | 
|---|
| 377 | AtomsinSceneMap.insert( make_pair(atomid, atomObject) ); | 
|---|
| 378 |  | 
|---|
| 379 | qRegisterMetaType<atomId_t>("atomId_t"); | 
|---|
| 380 | qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond"); | 
|---|
| 381 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t))); | 
|---|
| 382 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 383 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed())); | 
|---|
| 384 | connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *))); | 
|---|
| 385 | //  connect (atomObject, SIGNAL(bondsChanged()), this, SLOT(bondInserted(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond))); | 
|---|
| 386 | connect (atomObject, SIGNAL(BondsAdded(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond))); | 
|---|
| 387 | connect (atomObject, SIGNAL(BondsRemoved(const atomId_t, const atomId_t)), this, SLOT(bondRemoved(const atomId_t, const atomId_t))); | 
|---|
| 388 | connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, const atomId_t, const atomId_t)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, const atomId_t, const atomId_t))); | 
|---|
| 389 |  | 
|---|
| 390 | if (m_objectId  == -1) | 
|---|
| 391 | setObjectId(atomid); | 
|---|
| 392 |  | 
|---|
| 393 | // first reset bonds when signals connections have been made | 
|---|
| 394 | atomObject->resetBonds(); | 
|---|
| 395 |  | 
|---|
| 396 | emit changed(); | 
|---|
| 397 | emit changeOccured(); | 
|---|
| 398 | } | 
|---|
| 399 | } | 
|---|
| 400 |  | 
|---|
| 401 | /** Removes an atom of this molecule from the scene. | 
|---|
| 402 | * | 
|---|
| 403 | * We just the id as the atom might have already been destroyed. | 
|---|
| 404 | * | 
|---|
| 405 | * @param _id id of atom to remove | 
|---|
| 406 | */ | 
|---|
| 407 | void GLMoleculeObject_molecule::atomRemoved(const atomId_t _id) | 
|---|
| 408 | { | 
|---|
| 409 | LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomRemoved for atom "+toString(_id)+"."); | 
|---|
| 410 | // bonds are removed by signal coming from ~bond | 
|---|
| 411 |  | 
|---|
| 412 | if ((unsigned int)m_objectId == _id) | 
|---|
| 413 | setObjectId(-1); | 
|---|
| 414 |  | 
|---|
| 415 | // remove atoms | 
|---|
| 416 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id); | 
|---|
| 417 | ASSERT(iter != AtomsinSceneMap.end(), | 
|---|
| 418 | "GLMoleculeObject_molecule::atomRemoved() - atom "+toString(_id)+" not on display."); | 
|---|
| 419 | GLMoleculeObject_atom *atomObject = iter->second; | 
|---|
| 420 | AtomsinSceneMap.erase(iter); | 
|---|
| 421 | atomObject->disconnect(); | 
|---|
| 422 | delete atomObject; | 
|---|
| 423 |  | 
|---|
| 424 | emit changed(); | 
|---|
| 425 | emit changeOccured(); | 
|---|
| 426 |  | 
|---|
| 427 | if (AtomsinSceneMap.empty()) | 
|---|
| 428 | emit moleculeEmptied(ObservedMolecule); | 
|---|
| 429 | } | 
|---|
| 430 |  | 
|---|
| 431 | void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob) | 
|---|
| 432 | { | 
|---|
| 433 | // Find the atom, ob corresponds to. | 
|---|
| 434 | hoverAtomId = -1; | 
|---|
| 435 | GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob); | 
|---|
| 436 | if (atomObject){ | 
|---|
| 437 | for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){ | 
|---|
| 438 | if (iter->second == atomObject) | 
|---|
| 439 | hoverAtomId = iter->first; | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | // Propagate signal. | 
|---|
| 443 | emit hoverChanged(hoverAtomId); | 
|---|
| 444 | } else { | 
|---|
| 445 | // Find the atom, ob corresponds to. | 
|---|
| 446 | GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob); | 
|---|
| 447 | if (moleculeObject == this){ | 
|---|
| 448 | // Propagate signal. | 
|---|
| 449 | emit hoverChanged(ObservedMolecule->getMolIndex(), 0); | 
|---|
| 450 | } | 
|---|
| 451 | } | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 |  | 
|---|
| 455 | /** Helper function to get bond ids in the correct order for BondNodeMap. | 
|---|
| 456 | * | 
|---|
| 457 | * \return pair of ids in correct order. | 
|---|
| 458 | */ | 
|---|
| 459 | GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds( | 
|---|
| 460 | const bond::ptr _bond, | 
|---|
| 461 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 462 | { | 
|---|
| 463 | BondIds ids; | 
|---|
| 464 | switch (_side) { | 
|---|
| 465 | case GLMoleculeObject_bond::left: | 
|---|
| 466 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()); | 
|---|
| 467 | break; | 
|---|
| 468 | case GLMoleculeObject_bond::right: | 
|---|
| 469 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()); | 
|---|
| 470 | break; | 
|---|
| 471 | } | 
|---|
| 472 | return ids; | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 | /** Adds a bond to the scene. | 
|---|
| 476 | * | 
|---|
| 477 | * @param _bond bond to add | 
|---|
| 478 | * @param side which side of the bond (left or right) | 
|---|
| 479 | */ | 
|---|
| 480 | void GLMoleculeObject_molecule::bondInserted( | 
|---|
| 481 | const atomId_t _left, const atomId_t _right, | 
|---|
| 482 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 483 | { | 
|---|
| 484 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(_left) | 
|---|
| 485 | +toString(_right)+"."); | 
|---|
| 486 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << "."); | 
|---|
| 487 |  | 
|---|
| 488 | const BondIds ids( std::make_pair(_left, _right) ); | 
|---|
| 489 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids); | 
|---|
| 490 | if (iter == BondsinSceneMap.end()) { | 
|---|
| 491 | GLMoleculeObject_bond * bondObject = | 
|---|
| 492 | new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, ids, _side); | 
|---|
| 493 | connect ( | 
|---|
| 494 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)), | 
|---|
| 495 | this, SLOT(bondRemoved(const atomId_t, const atomId_t))); | 
|---|
| 496 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed())); | 
|---|
| 497 | BondsinSceneMap.insert( make_pair(ids, bondObject) ); | 
|---|
| 498 | //    BondIdsinSceneMap.insert( Leftids ); | 
|---|
| 499 | } else { | 
|---|
| 500 | iter->second->resetPosition(); | 
|---|
| 501 | iter->second->resetWidth(); | 
|---|
| 502 | } | 
|---|
| 503 | emit changed(); | 
|---|
| 504 | emit changeOccured(); | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | /** Removes a bond from the scene. | 
|---|
| 508 | * | 
|---|
| 509 | * @param _bond bond to remove | 
|---|
| 510 | */ | 
|---|
| 511 | void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr) | 
|---|
| 512 | { | 
|---|
| 513 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+"."); | 
|---|
| 514 | { | 
|---|
| 515 | // left bond | 
|---|
| 516 | const BondIds Leftids( make_pair(leftnr, rightnr) ); | 
|---|
| 517 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids ); | 
|---|
| 518 | ASSERT(leftiter != BondsinSceneMap.end(), | 
|---|
| 519 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-" | 
|---|
| 520 | +toString(rightnr)+" not on display."); | 
|---|
| 521 | GLMoleculeObject_bond *bondObject = leftiter->second; | 
|---|
| 522 | bondObject->disconnect(); | 
|---|
| 523 | BondsinSceneMap.erase(leftiter); | 
|---|
| 524 | delete bondObject; // is done by signal from bond itself | 
|---|
| 525 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << "."); | 
|---|
| 526 | } | 
|---|
| 527 |  | 
|---|
| 528 | emit changed(); | 
|---|
| 529 | emit changeOccured(); | 
|---|
| 530 | } | 
|---|
| 531 |  | 
|---|
| 532 | void GLMoleculeObject_molecule::setVisible(bool value) | 
|---|
| 533 | { | 
|---|
| 534 | // first update the mesh if we are going to be visible now | 
|---|
| 535 | if (value) | 
|---|
| 536 | updateTesselationHull(); | 
|---|
| 537 | // then emit onward | 
|---|
| 538 | GLMoleculeObject::setVisible(value); | 
|---|
| 539 |  | 
|---|
| 540 | emit changed(); | 
|---|
| 541 | emit changeOccured(); | 
|---|
| 542 | } | 
|---|
| 543 |  | 
|---|
| 544 | std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t) | 
|---|
| 545 | { | 
|---|
| 546 | ost << t.first << "," << t.second; | 
|---|
| 547 | return ost; | 
|---|
| 548 | } | 
|---|
| 549 |  | 
|---|
| 550 | void GLMoleculeObject_molecule::wasClicked() | 
|---|
| 551 | { | 
|---|
| 552 | LOG(4, "INFO: GLMoleculeObject_molecule: atom " | 
|---|
| 553 | << ObservedMolecule->getMolIndex() << " has been clicked"); | 
|---|
| 554 | emit moleculeClicked(ObservedMolecule->getMolIndex()); | 
|---|
| 555 | } | 
|---|
| 556 |  | 
|---|
| 557 | void GLMoleculeObject_molecule::changeAtomId( | 
|---|
| 558 | GLMoleculeObject_atom *ob, | 
|---|
| 559 | const atomId_t oldId, | 
|---|
| 560 | const atomId_t newId) | 
|---|
| 561 | { | 
|---|
| 562 | LOG(3, "INFO: GLMoleculeObject_molecule - change atom id " << oldId << " to " << newId << "."); | 
|---|
| 563 |  | 
|---|
| 564 | // Remove from map. | 
|---|
| 565 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId); | 
|---|
| 566 | ASSERT(iter != AtomsinSceneMap.end(), | 
|---|
| 567 | "GLMoleculeObject_molecule::changeAtomId() - atom with old id "+toString(oldId)+" not on display."); | 
|---|
| 568 | ASSERT(iter->second == ob, | 
|---|
| 569 | "GLMoleculeObject_molecule::changeAtomId() - atom with id " | 
|---|
| 570 | +toString(oldId)+" does not match with object in AtomsinSceneMap."); | 
|---|
| 571 | AtomsinSceneMap.erase(iter); | 
|---|
| 572 |  | 
|---|
| 573 | // Reinsert with new id. | 
|---|
| 574 | { | 
|---|
| 575 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(newId); | 
|---|
| 576 | ASSERT(iter == AtomsinSceneMap.end(), | 
|---|
| 577 | "GLMoleculeObject_molecule::changeAtomId() - atom with new id "+toString(newId)+" already known."); | 
|---|
| 578 | } | 
|---|
| 579 | AtomsinSceneMap.insert( make_pair(newId, ob) ); | 
|---|
| 580 | } | 
|---|
| 581 |  | 
|---|
| 582 |  | 
|---|
| 583 | GLMoleculeObject_molecule::atoms_t GLMoleculeObject_molecule::getPresentAtoms() const | 
|---|
| 584 | { | 
|---|
| 585 | atoms_t returnAtomIds; | 
|---|
| 586 | returnAtomIds.insert( | 
|---|
| 587 | MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).begin()), | 
|---|
| 588 | MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).end())); | 
|---|
| 589 | return returnAtomIds; | 
|---|
| 590 | } | 
|---|