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

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 8c001a was 8c001a, checked in by Frederik Heber <heber@…>, 10 years ago

GLMoleculeObject_atoms and .._bonds are now children of .._molecule.

  • GLMoleculeObject::initialize() is now virtual.
  • .._molecule is now draw()ing its children, i.e. initialize() of .._molecule calls in turn those of .._atom and .._bond. Similarly with draw.
  • molecule has a lastchangedatom member variable now.
  • FIX: molecule::getBoundingSphere() did not check for no atoms.
  • Property mode set to 100644
File size: 10.4 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 m_scaleX = 1.0f;
77 m_scaleY = 1.0f;
78 m_scaleZ = 1.0f;
79 m_rotationAngle = 0.0f;
80 m_effect = 0;
81 m_objectId = -1;
82 m_hovering = false;
83 m_selected = false;
84 m_material = 0;
85 initStaticMaterials();
86}
87
88GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
89 : QObject(parent)
90{
91 //mesh->setParent(this);
92 for (int i=0;i<DETAILTYPES_MAX;i++)
93 m_mesh[i] = mesh;
94 m_scaleX = 1.0f;
95 m_scaleY = 1.0f;
96 m_scaleZ = 1.0f;
97 m_rotationAngle = 0.0f;
98 m_effect = 0;
99 m_objectId = -1;
100 m_hovering = false;
101 m_selected = false;
102 m_material = 0;
103 initStaticMaterials();
104}
105
106GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
107 : QObject(parent)
108{
109 scene->setParent(this);
110 for (int i=0;i<DETAILTYPES_MAX;i++)
111 m_mesh[i] = scene->mainNode();
112 m_scaleX = 1.0f;
113 m_scaleY = 1.0f;
114 m_scaleZ = 1.0f;
115 m_rotationAngle = 0.0f;
116 m_effect = 0;
117 m_objectId = -1;
118 m_hovering = false;
119 m_selected = false;
120 m_material = 0;
121 initStaticMaterials();
122}
123
124GLMoleculeObject::~GLMoleculeObject()
125{
126}
127
128void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
129{
130 Q_UNUSED(painter);
131 if (m_objectId != -1)
132 view->registerObject(m_objectId, this);
133}
134
135
136/** Draws a box around the mesh.
137 *
138 */
139void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
140{
141 painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
142 QVector3DArray array;
143 qreal radius = 1.0;
144 array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
145 array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
146 array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
147 array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
148
149 array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
150 array.append( radius, -radius, radius); array.append( radius, radius, radius);
151 array.append( radius, radius, radius); array.append(-radius, radius, radius);
152 array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
153
154 array.append(-radius, -radius, -radius); array.append(-radius, -radius, radius);
155 array.append( radius, -radius, -radius); array.append( radius, -radius, radius);
156 array.append(-radius, radius, -radius); array.append(-radius, radius, radius);
157 array.append( radius, radius, -radius); array.append( radius, radius, radius);
158 painter->clearAttributes();
159 painter->setVertexAttribute(QGL::Position, array);
160 painter->draw(QGL::Lines, 24);
161}
162
163void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
164{
165 // Position the model at its designated position, scale, and orientation.
166 painter->modelViewMatrix().push();
167 painter->modelViewMatrix().translate(m_position);
168 if (m_rotationAngle != 0.0f)
169 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
170 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
171
172 // Apply the material and effect to the painter.
173 QGLMaterial *material;
174 if (m_hovering)
175 material = m_hoverMaterial;
176 else if (m_selected)
177 material = m_selectionMaterial;
178 else
179 material = m_material;
180
181 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
182
183 painter->setColor(material->diffuseColor());
184 painter->setFaceMaterial(QGL::AllFaces, material);
185 if (m_effect)
186 painter->setUserEffect(m_effect);
187 else
188 painter->setStandardEffect(QGL::LitMaterial);
189
190 // Mark the object for object picking purposes.
191 int prevObjectId = painter->objectPickId();
192 if (m_objectId != -1)
193 painter->setObjectPickId(m_objectId);
194
195 // Draw the geometry mesh.
196 QVector4D pos4d(m_position, -1);
197 qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
198
199 if (distance > detailMinDistance[DETAIL_LOW])
200 m_mesh[DETAIL_LOW]->draw(painter);
201 else if (distance > detailMinDistance[DETAIL_MEDIUM])
202 m_mesh[DETAIL_MEDIUM]->draw(painter);
203 else if (distance > detailMinDistance[DETAIL_HIGH])
204 m_mesh[DETAIL_HIGH]->draw(painter);
205 else if (distance > detailMinDistance[DETAIL_HIGHEST])
206 m_mesh[DETAIL_HIGHEST]->draw(painter);
207
208 // Draw a box around the mesh, if selected.
209 if (m_selected)
210 drawSelectionBox(painter);
211
212 // Turn off the user effect, if present.
213 if (m_effect)
214 painter->setStandardEffect(QGL::LitMaterial);
215
216 // Revert to the previous object identifier.
217 painter->setObjectPickId(prevObjectId);
218
219 // Restore the modelview matrix.
220 painter->modelViewMatrix().pop();
221}
222
223bool GLMoleculeObject::event(QEvent *e)
224{
225 // Convert the raw event into a signal representing the user's action.
226 if (e->type() == QEvent::MouseButtonPress) {
227 QMouseEvent *me = (QMouseEvent *)e;
228 if (me->button() == Qt::LeftButton)
229 emit pressed();
230 } else if (e->type() == QEvent::MouseButtonRelease) {
231 QMouseEvent *me = (QMouseEvent *)e;
232 if (me->button() == Qt::LeftButton) {
233 emit released();
234 if (me->x() >= 0) // Positive: inside object, Negative: outside.
235 emit clicked();
236 }
237 } else if (e->type() == QEvent::MouseButtonDblClick) {
238 emit doubleClicked();
239 } else if (e->type() == QEvent::Enter) {
240 m_hovering = true;
241 emit hoverChanged(this);
242 } else if (e->type() == QEvent::Leave) {
243 m_hovering = false;
244 emit hoverChanged(NULL);
245 }
246 return QObject::event(e);
247}
248
249/** Returns the ref to the Material for element No \a from the map.
250 *
251 * \note We create a new one if the element is missing.
252 *
253 * @param no element no
254 * @return ref to QGLMaterial
255 */
256QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
257{
258 ASSERT( (no > 0) && (no < MAX_ELEMENTS),
259 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
260 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
261 // get present one
262 return ElementNoMaterialMap[no];
263 } else {
264 // create new one
265 LOG(1, "Creating new material for element "+toString(no)+".");
266 QGLMaterial *newmaterial = new QGLMaterial(NULL);
267
268 // create material for element
269 periodentafel *periode = World::getInstance().getPeriode();
270 const element *desiredelement = periode->FindElement(no);
271 ASSERT(desiredelement != NULL,
272 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
273 const unsigned char* color = desiredelement->getColor();
274 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
275 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
276 newmaterial->setSpecularColor( QColor(60, 60, 60) );
277 newmaterial->setShininess( 128 );
278 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
279
280 return newmaterial;
281 }
282}
283
284/** Create the 3 materials shared by all objects.
285 *
286 */
287void GLMoleculeObject::initStaticMaterials()
288{
289 if (!m_hoverMaterial){
290 m_hoverMaterial = new QGLMaterial(NULL);
291 m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
292 m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
293 m_hoverMaterial->setShininess( 128 );
294 }
295 if (!m_selectionMaterial){
296 m_selectionMaterial = new QGLMaterial(NULL);
297 m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
298 m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
299 m_selectionMaterial->setShininess( 128 );
300 }
301 if (!m_selectionBoxMaterial){
302 m_selectionBoxMaterial = new QGLMaterial(NULL);
303 m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
304 m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
305 m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
306 }
307}
308
309/** Static function to be called when Materials have to be removed.
310 *
311 */
312void GLMoleculeObject::cleanMaterialMap()
313{
314 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
315 !ElementNoMaterialMap.empty();
316 iter = ElementNoMaterialMap.begin()) {
317 delete iter->second;
318 ElementNoMaterialMap.erase(iter);
319 }
320}
321
322
323void GLMoleculeObject::setSelected(bool value)
324{
325 if (value != m_selected){
326 m_selected = value;
327 emit selectionChanged();
328 }
329}
Note: See TracBrowser for help on using the repository browser.