source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject.cpp@ f47efd4

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since f47efd4 was f47efd4, checked in by Frederik Heber <heber@…>, 10 years ago

FIX: Bond cylinders were wrongly scaled and too long with degree larger than one.

  • we have to first translate, then rotate, then scale. The last two were interchanged before. This is fixed.
  • Property mode set to 100644
File size: 11.1 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.cpp
26 *
27 * This is based on the Qt3D example "teaservice", specifically meshobject.cpp.
28 *
29 * Created on: Aug 17, 2011
30 * Author: heber
31 */
32
33// include config.h
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include "GLMoleculeObject.hpp"
39
40#include <Qt3D/qglview.h>
41#include <Qt3D/qglscenenode.h>
42#include <Qt3D/qglpainter.h>
43#include <Qt3D/qglmaterial.h>
44
45#include "CodePatterns/MemDebug.hpp"
46
47#include "CodePatterns/Assert.hpp"
48#include "CodePatterns/Log.hpp"
49
50#include "Helpers/defs.hpp"
51#include "Element/element.hpp"
52#include "Element/periodentafel.hpp"
53#include "World.hpp"
54
55GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
56
57
58#include "CodePatterns/MemDebug.hpp"
59
60QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
61QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
62QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
63
64QGLSceneNode *GLMoleculeObject::meshEmpty[GLMoleculeObject::DETAILTYPES_MAX];
65QGLSceneNode *GLMoleculeObject::meshSphere[GLMoleculeObject::DETAILTYPES_MAX];
66QGLSceneNode *GLMoleculeObject::meshCylinder[GLMoleculeObject::DETAILTYPES_MAX];
67
68double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
69
70GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
71 : QObject(parent)
72{
73 //mesh->setParent(this);
74 for (int i=0;i<DETAILTYPES_MAX;i++)
75 m_mesh[i] = mesh[i];
76 MeshFreeType = free_none;
77 m_scaleX = 1.0f;
78 m_scaleY = 1.0f;
79 m_scaleZ = 1.0f;
80 m_rotationAngle = 0.0f;
81 m_effect = 0;
82 m_objectId = -1;
83 m_hovering = false;
84 m_selected = false;
85 m_visible = true;
86 m_material = 0;
87 initStaticMaterials();
88}
89
90GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
91 : QObject(parent)
92{
93 //mesh->setParent(this);
94 for (int i=0;i<DETAILTYPES_MAX;i++)
95 m_mesh[i] = mesh;
96 MeshFreeType = free_none;
97 m_scaleX = 1.0f;
98 m_scaleY = 1.0f;
99 m_scaleZ = 1.0f;
100 m_rotationAngle = 0.0f;
101 m_effect = 0;
102 m_objectId = -1;
103 m_hovering = false;
104 m_selected = false;
105 m_material = 0;
106 initStaticMaterials();
107}
108
109GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
110 : QObject(parent)
111{
112 for (int i=0;i<DETAILTYPES_MAX;i++)
113 m_mesh[i] = scene->mainNode();
114 MeshFreeType = free_none;
115 m_scaleX = 1.0f;
116 m_scaleY = 1.0f;
117 m_scaleZ = 1.0f;
118 m_rotationAngle = 0.0f;
119 m_effect = 0;
120 m_objectId = -1;
121 m_hovering = false;
122 m_selected = false;
123 m_material = 0;
124 initStaticMaterials();
125}
126
127GLMoleculeObject::~GLMoleculeObject()
128{
129}
130
131void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
132{
133 Q_UNUSED(painter);
134 if (m_objectId != -1)
135 view->registerObject(m_objectId, this);
136}
137
138
139/** Draws a box around the mesh.
140 *
141 */
142void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
143{
144 painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
145 QVector3DArray array;
146 qreal radius = 1.0;
147 array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
148 array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
149 array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
150 array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
151
152 array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
153 array.append( radius, -radius, radius); array.append( radius, radius, radius);
154 array.append( radius, radius, radius); array.append(-radius, radius, radius);
155 array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
156
157 array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
158 array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
159 array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
160 array.append( radius, radius, -radius); array.append( radius, radius, radius);
161 painter->clearAttributes();
162 painter->setVertexAttribute(QGL::Position, array);
163 painter->draw(QGL::Lines, 24);
164}
165
166void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
167{
168 // Position the model at its designated position, scale, and orientation.
169 painter->modelViewMatrix().push();
170 painter->modelViewMatrix().translate(m_position);
171 if (m_rotationAngle != 0.0f)
172 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
173 if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
174 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
175
176 // Apply the material and effect to the painter.
177 QGLMaterial *material;
178 if (m_hovering)
179 material = m_hoverMaterial;
180 else if (m_selected)
181 material = m_selectionMaterial;
182 else
183 material = m_material;
184
185 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
186
187 painter->setColor(material->diffuseColor());
188 painter->setFaceMaterial(QGL::AllFaces, material);
189 if (m_effect)
190 painter->setUserEffect(m_effect);
191 else
192 painter->setStandardEffect(QGL::LitMaterial);
193
194 // Mark the object for object picking purposes.
195 int prevObjectId = painter->objectPickId();
196 if (m_objectId != -1)
197 painter->setObjectPickId(m_objectId);
198
199 // Draw the geometry mesh.
200 QVector4D pos4d(m_position, -1);
201 qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
202
203 if (distance > detailMinDistance[DETAIL_LOW])
204 m_mesh[DETAIL_LOW]->draw(painter);
205 else if (distance > detailMinDistance[DETAIL_MEDIUM])
206 m_mesh[DETAIL_MEDIUM]->draw(painter);
207 else if (distance > detailMinDistance[DETAIL_HIGH])
208 m_mesh[DETAIL_HIGH]->draw(painter);
209 else if (distance > detailMinDistance[DETAIL_HIGHEST])
210 m_mesh[DETAIL_HIGHEST]->draw(painter);
211
212 // Draw a box around the mesh, if selected.
213 if (m_selected)
214 drawSelectionBox(painter);
215
216 // Turn off the user effect, if present.
217 if (m_effect)
218 painter->setStandardEffect(QGL::LitMaterial);
219
220 // Revert to the previous object identifier.
221 painter->setObjectPickId(prevObjectId);
222
223 // Restore the modelview matrix.
224 painter->modelViewMatrix().pop();
225}
226
227bool GLMoleculeObject::event(QEvent *e)
228{
229 // Convert the raw event into a signal representing the user's action.
230 if (e->type() == QEvent::MouseButtonPress) {
231 QMouseEvent *me = (QMouseEvent *)e;
232 if (me->button() == Qt::LeftButton)
233 emit pressed();
234 } else if (e->type() == QEvent::MouseButtonRelease) {
235 QMouseEvent *me = (QMouseEvent *)e;
236 if (me->button() == Qt::LeftButton) {
237 emit released();
238 if (me->x() >= 0) // Positive: inside object, Negative: outside.
239 emit clicked();
240 }
241 } else if (e->type() == QEvent::MouseButtonDblClick) {
242 emit doubleClicked();
243 } else if (e->type() == QEvent::Enter) {
244 m_hovering = true;
245 emit hoverChanged(this);
246 } else if (e->type() == QEvent::Leave) {
247 m_hovering = false;
248 emit hoverChanged(NULL);
249 }
250 return QObject::event(e);
251}
252
253/** Returns the ref to the Material for element No \a from the map.
254 *
255 * \note We create a new one if the element is missing.
256 *
257 * @param no element no
258 * @return ref to QGLMaterial
259 */
260QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
261{
262 ASSERT( (no > 0) && (no < MAX_ELEMENTS),
263 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
264 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
265 // get present one
266 return ElementNoMaterialMap[no];
267 } else {
268 // create new one
269 LOG(1, "Creating new material for element "+toString(no)+".");
270 QGLMaterial *newmaterial = new QGLMaterial(NULL);
271
272 // create material for element
273 periodentafel *periode = World::getInstance().getPeriode();
274 const element *desiredelement = periode->FindElement(no);
275 ASSERT(desiredelement != NULL,
276 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
277 const unsigned char* color = desiredelement->getColor();
278 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
279 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
280 newmaterial->setSpecularColor( QColor(60, 60, 60) );
281 newmaterial->setShininess( 128 );
282 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
283
284 return newmaterial;
285 }
286}
287
288/** Create the 3 materials shared by all objects.
289 *
290 */
291void GLMoleculeObject::initStaticMaterials()
292{
293 if (!m_hoverMaterial){
294 m_hoverMaterial = new QGLMaterial(NULL);
295 m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
296 m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
297 m_hoverMaterial->setShininess( 128 );
298 }
299 if (!m_selectionMaterial){
300 m_selectionMaterial = new QGLMaterial(NULL);
301 m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
302 m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
303 m_selectionMaterial->setShininess( 128 );
304 }
305 if (!m_selectionBoxMaterial){
306 m_selectionBoxMaterial = new QGLMaterial(NULL);
307 m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
308 m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
309 m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
310 }
311}
312
313/** Static function to be called when Materials have to be removed.
314 *
315 */
316void GLMoleculeObject::cleanMaterialMap()
317{
318 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
319 !ElementNoMaterialMap.empty();
320 iter = ElementNoMaterialMap.begin()) {
321 delete iter->second;
322 ElementNoMaterialMap.erase(iter);
323 }
324}
325
326
327void GLMoleculeObject::setSelected(bool value)
328{
329 if (value != m_selected){
330 m_selected = value;
331 emit selectionChanged();
332 }
333}
334
335void GLMoleculeObject::setVisible(bool value)
336{
337 if (value != m_visible){
338 m_visible = value;
339 emit changed();
340 }
341}
342
343void GLMoleculeObject::updateMesh(QGLSceneNode *mesh)
344{
345 if (m_mesh[0] != NULL) {
346 switch(MeshFreeType) {
347 case free_all:
348 for (int i=0;i<DETAILTYPES_MAX;i++)
349 delete m_mesh[i];
350 break;
351 case free_single:
352 delete m_mesh[0];
353 break;
354 default:
355 case free_none:
356 break;
357 }
358 }
359 MeshFreeType = free_single;
360 for (int i=0;i<DETAILTYPES_MAX;i++)
361 m_mesh[i] = mesh;
362}
Note: See TracBrowser for help on using the repository browser.