| 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 "CodePatterns/Assert.hpp" | 
|---|
| 47 | #include "CodePatterns/IteratorAdaptors.hpp" | 
|---|
| 48 | #include "CodePatterns/Log.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 51 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 52 | #include "LinkedCell/linkedcell.hpp" | 
|---|
| 53 | #include "Tesselation/tesselation.hpp" | 
|---|
| 54 | #include "Tesselation/BoundaryLineSet.hpp" | 
|---|
| 55 | #include "Tesselation/BoundaryTriangleSet.hpp" | 
|---|
| 56 | #include "Tesselation/CandidateForTesselation.hpp" | 
|---|
| 57 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp" | 
|---|
| 58 | #include "Atom/TesselPoint.hpp" | 
|---|
| 59 |  | 
|---|
| 60 | static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo) | 
|---|
| 61 | { | 
|---|
| 62 | // Build a mesh from the geometry. | 
|---|
| 63 | QGLBuilder builder; | 
|---|
| 64 | builder.addTriangles(_geo); | 
|---|
| 65 | QGLSceneNode *mesh = builder.finalizedSceneNode(); | 
|---|
| 66 | return mesh; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | GLMoleculeObject_molecule::GLMoleculeObject_molecule( | 
|---|
| 70 | QObject *parent, | 
|---|
| 71 | QtObservedMolecule::ptr &_ObservedMolecule) : | 
|---|
| 72 | GLMoleculeObject((QGLSceneNode *)NULL, parent), | 
|---|
| 73 | hoverAtomId(-1), | 
|---|
| 74 | ObservedMolecule(_ObservedMolecule) | 
|---|
| 75 | { | 
|---|
| 76 | init(); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | GLMoleculeObject_molecule::GLMoleculeObject_molecule( | 
|---|
| 80 | QGLSceneNode *mesh[], | 
|---|
| 81 | QObject *parent, | 
|---|
| 82 | QtObservedMolecule::ptr &_ObservedMolecule) : | 
|---|
| 83 | GLMoleculeObject(mesh, parent), | 
|---|
| 84 | TesselationUptodate(false), | 
|---|
| 85 | hoverAtomId(-1), | 
|---|
| 86 | ObservedMolecule(_ObservedMolecule) | 
|---|
| 87 | { | 
|---|
| 88 | init(); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | void GLMoleculeObject_molecule::init() | 
|---|
| 92 | { | 
|---|
| 93 | setObjectId(ObservedMolecule->getMolIndex()); | 
|---|
| 94 | setMaterial(getMaterial(1)); | 
|---|
| 95 |  | 
|---|
| 96 | m_selected = ObservedMolecule->getMolSelected(); | 
|---|
| 97 |  | 
|---|
| 98 | // initially, atoms and bonds should be visible | 
|---|
| 99 | m_visible = false; | 
|---|
| 100 |  | 
|---|
| 101 | connect (ObservedMolecule.get(), SIGNAL(tesselationhullChanged()), this, SLOT(setTesselationOutOfDate())); | 
|---|
| 102 | connect (ObservedMolecule.get(), SIGNAL(boundingboxChanged()), this, SLOT(resetBoundingBox())); | 
|---|
| 103 | connect (ObservedMolecule.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex())); | 
|---|
| 104 | /// these are channeled through GLWorldScene instead to ensure synchronicity | 
|---|
| 105 | //  connect (ObservedMolecule.get(), SIGNAL(atomInserted(QtObservedAtom::ptr)), | 
|---|
| 106 | //      this, SLOT(atomInserted(QtObservedAtom::ptr)) ); | 
|---|
| 107 | //  connect (ObservedMolecule.get(), SIGNAL(atomRemoved(const atomId_t)), | 
|---|
| 108 | //      this, SLOT(atomRemoved(const atomId_t)) ); | 
|---|
| 109 | connect (ObservedMolecule.get(), SIGNAL(selectedChanged()), this, SLOT(resetSelected())); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | GLMoleculeObject_molecule::~GLMoleculeObject_molecule() | 
|---|
| 113 | {} | 
|---|
| 114 |  | 
|---|
| 115 | QGeometryData GLMoleculeObject_molecule::updateTesselationHull() const | 
|---|
| 116 | { | 
|---|
| 117 | QGeometryData geo; | 
|---|
| 118 |  | 
|---|
| 119 | const molecule * const molref = | 
|---|
| 120 | QtObservedMolecule::getMolecule(ObservedMolecule->getMolIndex()); | 
|---|
| 121 | if (molref == NULL) { | 
|---|
| 122 | ELOG(1, "Could not createMoleculeMesh, molecule with id " | 
|---|
| 123 | << ObservedMolecule->getMolIndex() << " already gone."); | 
|---|
| 124 | return geo; | 
|---|
| 125 | } | 
|---|
| 126 | double minradius = 2.; // TODO: set to maximum bond length value | 
|---|
| 127 | LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius); | 
|---|
| 128 | // check minimum bond radius in molecule | 
|---|
| 129 | double minlength = std::numeric_limits<double>::max(); | 
|---|
| 130 | size_t NoAtoms = 0; | 
|---|
| 131 | for (molecule::const_iterator iter = molref->begin(); | 
|---|
| 132 | iter != molref->end(); ++iter) { | 
|---|
| 133 | const BondList &ListOfBonds = (*iter)->getListOfBonds(); | 
|---|
| 134 | for (BondList::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 135 | bonditer != ListOfBonds.end(); ++bonditer) { | 
|---|
| 136 | const double bond_distance = (*bonditer)->GetDistance(); | 
|---|
| 137 | minlength = std::min(bond_distance, minlength); | 
|---|
| 138 | } | 
|---|
| 139 | ++NoAtoms; | 
|---|
| 140 | } | 
|---|
| 141 | minradius = std::max( std::max(minradius, minlength), 1.); | 
|---|
| 142 |  | 
|---|
| 143 | // we need at least three points for tesselation | 
|---|
| 144 | if (NoAtoms >= 3) { | 
|---|
| 145 | // Tesselate the points. | 
|---|
| 146 | Tesselation T; | 
|---|
| 147 | PointCloudAdaptor<molecule> cloud( | 
|---|
| 148 | const_cast<molecule *>(molref), | 
|---|
| 149 | ObservedMolecule->getMolName()); | 
|---|
| 150 | T(cloud, minradius); | 
|---|
| 151 |  | 
|---|
| 152 | // Fill the points into a Qt geometry. | 
|---|
| 153 | LinkedCell_deprecated LinkedList(cloud, minradius); | 
|---|
| 154 | std::map<int, int> indices; | 
|---|
| 155 | std::map<int, Vector> normals; | 
|---|
| 156 | int index = 0; | 
|---|
| 157 | for (PointMap::const_iterator piter = T.PointsOnBoundary.begin(); | 
|---|
| 158 | piter != T.PointsOnBoundary.end(); ++piter) { | 
|---|
| 159 | const Vector &point = piter->second->getPosition(); | 
|---|
| 160 | // add data to the primitive | 
|---|
| 161 | geo.appendVertex(QVector3D(point[0], point[1], point[2])); | 
|---|
| 162 | Vector normalvector; | 
|---|
| 163 | for (LineMap::const_iterator lineiter = piter->second->lines.begin(); | 
|---|
| 164 | lineiter != piter->second->lines.end(); ++lineiter) | 
|---|
| 165 | for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin(); | 
|---|
| 166 | triangleiter != lineiter->second->triangles.end(); ++triangleiter) | 
|---|
| 167 | normalvector += | 
|---|
| 168 | triangleiter->second->NormalVector; | 
|---|
| 169 | normalvector.Normalize(); | 
|---|
| 170 | geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2])); | 
|---|
| 171 | geo.appendColor(QColor(1, 1, 1, 1)); | 
|---|
| 172 | geo.appendTexCoord(QVector2D(0, 0)); | 
|---|
| 173 | indices.insert( std::make_pair( piter->second->getNr(), index++)); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | // Fill the tesselated triangles into the geometry. | 
|---|
| 177 | for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin(); | 
|---|
| 178 | runner != T.TrianglesOnBoundary.end(); runner++) { | 
|---|
| 179 | int v[3]; | 
|---|
| 180 | for (size_t i=0; i<3; ++i) | 
|---|
| 181 | v[i] = runner->second->endpoints[i]->getNr(); | 
|---|
| 182 |  | 
|---|
| 183 | // Sort the vertices so the triangle is clockwise (relative to the normal vector). | 
|---|
| 184 | Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition(); | 
|---|
| 185 | cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition()); | 
|---|
| 186 | if (cross.ScalarProduct(runner->second->NormalVector) > 0) | 
|---|
| 187 | geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]); | 
|---|
| 188 | else | 
|---|
| 189 | geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]); | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | return geo; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | void GLMoleculeObject_molecule::setTesselationOutOfDate() | 
|---|
| 197 | { | 
|---|
| 198 | TesselationUptodate = false; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | void GLMoleculeObject_molecule::resetTesselationHull() | 
|---|
| 202 | { | 
|---|
| 203 | if (!TesselationUptodate) { | 
|---|
| 204 | TesselationHull = updateTesselationHull(); | 
|---|
| 205 | updateMesh(createMoleculeMesh(TesselationHull)); | 
|---|
| 206 | TesselationUptodate = true; | 
|---|
| 207 | } | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | void GLMoleculeObject_molecule::resetBoundingBox() | 
|---|
| 211 | { | 
|---|
| 212 | molecule::BoundingBoxInfo info = ObservedMolecule->getBoundingBox(); | 
|---|
| 213 | setPosition(QVector3D(info.position[0], info.position[1], info.position[2])); | 
|---|
| 214 | setScale(info.radius + 0.3); // getBoundingSphere() only sees atoms as points, so make the box a bit bigger | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | void GLMoleculeObject_molecule::resetIndex() | 
|---|
| 218 | { | 
|---|
| 219 | const atomId_t newId = ObservedMolecule->getMolIndex(); | 
|---|
| 220 | const size_t oldId = objectId(); | 
|---|
| 221 | ASSERT( newId != oldId, | 
|---|
| 222 | "GLMoleculeObject_molecule::resetIndex() - index "+toString(newId)+" did not change."); | 
|---|
| 223 | LOG(4, "INFO: GLMoleculeObject_molecule: new index is "+toString(newId)+"."); | 
|---|
| 224 | setObjectId(newId); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | void GLMoleculeObject_molecule::resetSelected() | 
|---|
| 228 | { | 
|---|
| 229 | const bool new_selected = ObservedMolecule->getMolSelected(); | 
|---|
| 230 | m_selected = new_selected; | 
|---|
| 231 |  | 
|---|
| 232 | emit changed(); | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter) | 
|---|
| 236 | { | 
|---|
| 237 | // Initialize all of the mesh objects that we have as children. | 
|---|
| 238 | if (m_visible) { | 
|---|
| 239 | GLMoleculeObject::initialize(view, painter); | 
|---|
| 240 | } else { | 
|---|
| 241 | foreach (QObject *obj, children()) { | 
|---|
| 242 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 243 | if (meshobj) | 
|---|
| 244 | meshobj->initialize(view, painter); | 
|---|
| 245 | } | 
|---|
| 246 | } | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane) | 
|---|
| 250 | { | 
|---|
| 251 | // draw either molecule's mesh or all atoms and bonds | 
|---|
| 252 | if (m_visible) { | 
|---|
| 253 | resetTesselationHull(); | 
|---|
| 254 |  | 
|---|
| 255 | painter->modelViewMatrix().push(); | 
|---|
| 256 |  | 
|---|
| 257 | // Apply the material and effect to the painter. | 
|---|
| 258 | QGLMaterial *material; | 
|---|
| 259 | if (m_hovering) | 
|---|
| 260 | material = m_hoverMaterial; | 
|---|
| 261 | else if (m_selected) | 
|---|
| 262 | material = m_selectionMaterial; | 
|---|
| 263 | else | 
|---|
| 264 | material = m_material; | 
|---|
| 265 |  | 
|---|
| 266 | ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL"); | 
|---|
| 267 |  | 
|---|
| 268 | painter->setColor(material->diffuseColor()); | 
|---|
| 269 | painter->setFaceMaterial(QGL::AllFaces, material); | 
|---|
| 270 | if (m_effect) | 
|---|
| 271 | painter->setUserEffect(m_effect); | 
|---|
| 272 | else | 
|---|
| 273 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 274 |  | 
|---|
| 275 | // Mark the object for object picking purposes. | 
|---|
| 276 | int prevObjectId = painter->objectPickId(); | 
|---|
| 277 | if (m_objectId != -1) | 
|---|
| 278 | painter->setObjectPickId(m_objectId); | 
|---|
| 279 |  | 
|---|
| 280 | m_mesh[0]->draw(painter); | 
|---|
| 281 |  | 
|---|
| 282 | // Turn off the user effect, if present. | 
|---|
| 283 | if (m_effect) | 
|---|
| 284 | painter->setStandardEffect(QGL::LitMaterial); | 
|---|
| 285 |  | 
|---|
| 286 | // Revert to the previous object identifier. | 
|---|
| 287 | painter->setObjectPickId(prevObjectId); | 
|---|
| 288 |  | 
|---|
| 289 | // Restore the modelview matrix. | 
|---|
| 290 | painter->modelViewMatrix().pop(); | 
|---|
| 291 |  | 
|---|
| 292 | //    GLMoleculeObject::draw(painter, cameraPlane); | 
|---|
| 293 | } else { | 
|---|
| 294 | // Draw all of the mesh objects that we have as children. | 
|---|
| 295 | foreach (QObject *obj, children()) { | 
|---|
| 296 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj); | 
|---|
| 297 | if (meshobj) | 
|---|
| 298 | meshobj->draw(painter, cameraPlane); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | // update bounding box prior to selection | 
|---|
| 302 | resetBoundingBox(); | 
|---|
| 303 |  | 
|---|
| 304 | painter->modelViewMatrix().push(); | 
|---|
| 305 | painter->modelViewMatrix().translate(m_position); | 
|---|
| 306 | if (m_rotationAngle != 0.0f) | 
|---|
| 307 | painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector); | 
|---|
| 308 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f)) | 
|---|
| 309 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ); | 
|---|
| 310 |  | 
|---|
| 311 | // Draw a box around the mesh, if selected. | 
|---|
| 312 | if (m_selected) | 
|---|
| 313 | drawSelectionBox(painter); | 
|---|
| 314 |  | 
|---|
| 315 | // Restore the modelview matrix. | 
|---|
| 316 | painter->modelViewMatrix().pop(); | 
|---|
| 317 | } | 
|---|
| 318 | } | 
|---|
| 319 |  | 
|---|
| 320 | void GLMoleculeObject_molecule::setVisible(bool value) | 
|---|
| 321 | { | 
|---|
| 322 | // first update the mesh if we are going to be visible now | 
|---|
| 323 | if (value) | 
|---|
| 324 | updateTesselationHull(); | 
|---|
| 325 | // then emit onward | 
|---|
| 326 | GLMoleculeObject::setVisible(value); | 
|---|
| 327 |  | 
|---|
| 328 | emit changed(); | 
|---|
| 329 | emit changeOccured(); | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t) | 
|---|
| 333 | { | 
|---|
| 334 | ost << t.first << "," << t.second; | 
|---|
| 335 | return ost; | 
|---|
| 336 | } | 
|---|