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