[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 |
|
---|
| 46 | #include "CodePatterns/Assert.hpp"
|
---|
[8d5fbf1] | 47 | #include "CodePatterns/IteratorAdaptors.hpp"
|
---|
[c67518] | 48 | #include "CodePatterns/Log.hpp"
|
---|
[494478] | 49 |
|
---|
[c67518] | 50 | #include "LinearAlgebra/Vector.hpp"
|
---|
[34e7fdb] | 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"
|
---|
[2f7988] | 57 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
|
---|
[34e7fdb] | 58 | #include "Atom/TesselPoint.hpp"
|
---|
[c67518] | 59 |
|
---|
[6c16a0] | 60 | static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo)
|
---|
| 61 | {
|
---|
[34e7fdb] | 62 | // Build a mesh from the geometry.
|
---|
| 63 | QGLBuilder builder;
|
---|
[6c16a0] | 64 | builder.addTriangles(_geo);
|
---|
[34e7fdb] | 65 | QGLSceneNode *mesh = builder.finalizedSceneNode();
|
---|
| 66 | return mesh;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[8d5fbf1] | 69 | GLMoleculeObject_molecule::GLMoleculeObject_molecule(
|
---|
| 70 | QObject *parent,
|
---|
[1b07b1] | 71 | QtObservedMolecule::ptr &_ObservedMolecule) :
|
---|
[6c16a0] | 72 | GLMoleculeObject((QGLSceneNode *)NULL, parent),
|
---|
[8d5fbf1] | 73 | hoverAtomId(-1),
|
---|
[494478] | 74 | ObservedMolecule(_ObservedMolecule)
|
---|
[34e7fdb] | 75 | {
|
---|
[96f14a] | 76 | init();
|
---|
[34e7fdb] | 77 | }
|
---|
| 78 |
|
---|
[026bef] | 79 | GLMoleculeObject_molecule::GLMoleculeObject_molecule(
|
---|
| 80 | QGLSceneNode *mesh[],
|
---|
| 81 | QObject *parent,
|
---|
[1b07b1] | 82 | QtObservedMolecule::ptr &_ObservedMolecule) :
|
---|
[bca99d] | 83 | GLMoleculeObject(mesh, parent),
|
---|
[691533] | 84 | TesselationUptodate(false),
|
---|
[8d5fbf1] | 85 | hoverAtomId(-1),
|
---|
[494478] | 86 | ObservedMolecule(_ObservedMolecule)
|
---|
[c67518] | 87 | {
|
---|
[96f14a] | 88 | init();
|
---|
[8d5fbf1] | 89 | }
|
---|
[7c7c4a] | 90 |
|
---|
[96f14a] | 91 | void GLMoleculeObject_molecule::init()
|
---|
[8d5fbf1] | 92 | {
|
---|
[96f14a] | 93 | setObjectId(ObservedMolecule->getMolIndex());
|
---|
[3b229e] | 94 | setMaterial(getMaterial(1));
|
---|
[8c001a] | 95 |
|
---|
[96f14a] | 96 | m_selected = ObservedMolecule->getMolSelected();
|
---|
[a39d72] | 97 |
|
---|
[739ee9] | 98 | // initially, atoms and bonds should be visible
|
---|
| 99 | m_visible = false;
|
---|
| 100 |
|
---|
[691533] | 101 | connect (ObservedMolecule.get(), SIGNAL(tesselationhullChanged()), this, SLOT(setTesselationOutOfDate()));
|
---|
[494478] | 102 | connect (ObservedMolecule.get(), SIGNAL(boundingboxChanged()), this, SLOT(resetBoundingBox()));
|
---|
[522c45] | 103 | connect (ObservedMolecule.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex()));
|
---|
[59f1bc] | 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)) );
|
---|
[5cd3a33] | 109 | connect (ObservedMolecule.get(), SIGNAL(selectedChanged()), this, SLOT(resetSelected()));
|
---|
[c67518] | 110 | }
|
---|
| 111 |
|
---|
| 112 | GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
|
---|
[494478] | 113 | {}
|
---|
[73b13c] | 114 |
|
---|
[6c16a0] | 115 | QGeometryData GLMoleculeObject_molecule::updateTesselationHull() const
|
---|
| 116 | {
|
---|
| 117 | QGeometryData geo;
|
---|
| 118 |
|
---|
[494478] | 119 | const molecule * const molref =
|
---|
| 120 | QtObservedMolecule::getMolecule(ObservedMolecule->getMolIndex());
|
---|
[6c16a0] | 121 | if (molref == NULL) {
|
---|
[494478] | 122 | ELOG(1, "Could not createMoleculeMesh, molecule with id "
|
---|
| 123 | << ObservedMolecule->getMolIndex() << " already gone.");
|
---|
[6c16a0] | 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();
|
---|
[f1b5ca] | 130 | size_t NoAtoms = 0;
|
---|
[6c16a0] | 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 | }
|
---|
[f1b5ca] | 139 | ++NoAtoms;
|
---|
[6c16a0] | 140 | }
|
---|
| 141 | minradius = std::max( std::max(minradius, minlength), 1.);
|
---|
| 142 |
|
---|
| 143 | // we need at least three points for tesselation
|
---|
[f1b5ca] | 144 | if (NoAtoms >= 3) {
|
---|
[6c16a0] | 145 | // Tesselate the points.
|
---|
| 146 | Tesselation T;
|
---|
[494478] | 147 | PointCloudAdaptor<molecule> cloud(
|
---|
| 148 | const_cast<molecule *>(molref),
|
---|
| 149 | ObservedMolecule->getMolName());
|
---|
[6c16a0] | 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 |
|
---|
[691533] | 196 | void GLMoleculeObject_molecule::setTesselationOutOfDate()
|
---|
| 197 | {
|
---|
| 198 | TesselationUptodate = false;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[6c16a0] | 201 | void GLMoleculeObject_molecule::resetTesselationHull()
|
---|
| 202 | {
|
---|
[691533] | 203 | if (!TesselationUptodate) {
|
---|
| 204 | TesselationHull = updateTesselationHull();
|
---|
| 205 | updateMesh(createMoleculeMesh(TesselationHull));
|
---|
| 206 | TesselationUptodate = true;
|
---|
| 207 | }
|
---|
[6c16a0] | 208 | }
|
---|
| 209 |
|
---|
| 210 | void GLMoleculeObject_molecule::resetBoundingBox()
|
---|
| 211 | {
|
---|
[494478] | 212 | molecule::BoundingBoxInfo info = ObservedMolecule->getBoundingBox();
|
---|
[6c16a0] | 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 |
|
---|
[522c45] | 217 | void GLMoleculeObject_molecule::resetIndex()
|
---|
[6c16a0] | 218 | {
|
---|
[494478] | 219 | const atomId_t newId = ObservedMolecule->getMolIndex();
|
---|
[6c16a0] | 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);
|
---|
[d6203a] | 225 | }
|
---|
| 226 |
|
---|
[5cd3a33] | 227 | void GLMoleculeObject_molecule::resetSelected()
|
---|
[015f8c] | 228 | {
|
---|
[5cd3a33] | 229 | const bool new_selected = ObservedMolecule->getMolSelected();
|
---|
| 230 | m_selected = new_selected;
|
---|
[015f8c] | 231 |
|
---|
| 232 | emit changed();
|
---|
| 233 | }
|
---|
| 234 |
|
---|
[8c001a] | 235 | void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
|
---|
| 236 | {
|
---|
| 237 | // Initialize all of the mesh objects that we have as children.
|
---|
[2b596f] | 238 | if (m_visible) {
|
---|
| 239 | GLMoleculeObject::initialize(view, painter);
|
---|
| 240 | } else {
|
---|
[8c001a] | 241 | foreach (QObject *obj, children()) {
|
---|
| 242 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
| 243 | if (meshobj)
|
---|
| 244 | meshobj->initialize(view, painter);
|
---|
| 245 | }
|
---|
[2b596f] | 246 | }
|
---|
[8c001a] | 247 | }
|
---|
| 248 |
|
---|
| 249 | void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
| 250 | {
|
---|
[739ee9] | 251 | // draw either molecule's mesh or all atoms and bonds
|
---|
| 252 | if (m_visible) {
|
---|
[6c16a0] | 253 | resetTesselationHull();
|
---|
[7b5984] | 254 |
|
---|
[34e7fdb] | 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);
|
---|
[739ee9] | 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 | }
|
---|
[2b596f] | 300 |
|
---|
| 301 | // update bounding box prior to selection
|
---|
[6c16a0] | 302 | resetBoundingBox();
|
---|
[2b596f] | 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);
|
---|
[f47efd4] | 308 | if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
|
---|
| 309 | painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
|
---|
[2b596f] | 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();
|
---|
[739ee9] | 317 | }
|
---|
[8c001a] | 318 | }
|
---|
| 319 |
|
---|
[34e7fdb] | 320 | void GLMoleculeObject_molecule::setVisible(bool value)
|
---|
| 321 | {
|
---|
| 322 | // first update the mesh if we are going to be visible now
|
---|
| 323 | if (value)
|
---|
[7b5984] | 324 | updateTesselationHull();
|
---|
[34e7fdb] | 325 | // then emit onward
|
---|
| 326 | GLMoleculeObject::setVisible(value);
|
---|
[8d5fbf1] | 327 |
|
---|
| 328 | emit changed();
|
---|
| 329 | emit changeOccured();
|
---|
[34e7fdb] | 330 | }
|
---|
| 331 |
|
---|
[8c001a] | 332 | std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t)
|
---|
| 333 | {
|
---|
| 334 | ost << t.first << "," << t.second;
|
---|
| 335 | return ost;
|
---|
| 336 | }
|
---|