source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp

Candidate_v1.7.1 stable v1.7.1
Last change on this file was 37d1c3, checked in by Frederik Heber <frederik.heber@…>, 4 months ago

Molecules in tesselated view can now be clicked and selected.

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