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