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