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

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 2d5137 was 2d5137, checked in by Frederik Heber <heber@…>, 12 years ago

GLMoleculeObject may set scale independently for each component.

  • this will allow changing of the width of the cylinder representing a bond.
  • Property mode set to 100644
File size: 10.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 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * GLMoleculeObject.cpp
25 *
26 * This is based on the Qt3D example "teaservice", specifically meshobject.cpp.
27 *
28 * Created on: Aug 17, 2011
29 * Author: heber
30 */
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "GLMoleculeObject.hpp"
38
39#include <Qt3D/qglview.h>
40#include <Qt3D/qglscenenode.h>
41#include <Qt3D/qglpainter.h>
42#include <Qt3D/qglmaterial.h>
43
44#include "CodePatterns/MemDebug.hpp"
45
46#include "CodePatterns/Assert.hpp"
47#include "CodePatterns/Log.hpp"
48
49#include "Helpers/defs.hpp"
50#include "Element/element.hpp"
51#include "Element/periodentafel.hpp"
52#include "World.hpp"
53
54GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
55
56
57#include "CodePatterns/MemDebug.hpp"
58
59QGLMaterial *GLMoleculeObject::m_hoverMaterial = NULL;
60QGLMaterial *GLMoleculeObject::m_selectionMaterial = NULL;
61QGLMaterial *GLMoleculeObject::m_selectionBoxMaterial = NULL;
62
63
64double GLMoleculeObject::detailMinDistance[GLMoleculeObject::DETAILTYPES_MAX] = {0, 15, 30, 42};
65
66GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh[], QObject *parent)
67 : QObject(parent)
68{
69 //mesh->setParent(this);
70 for (int i=0;i<DETAILTYPES_MAX;i++)
71 m_mesh[i] = mesh[i];
72 m_scaleX = 1.0f;
73 m_scaleY = 1.0f;
74 m_scaleZ = 1.0f;
75 m_rotationAngle = 0.0f;
76 m_effect = 0;
77 m_objectId = -1;
78 m_hovering = false;
79 m_selected = false;
80 m_material = 0;
81 initStaticMaterials();
82}
83
84GLMoleculeObject::GLMoleculeObject(QGLSceneNode *mesh, QObject *parent)
85 : QObject(parent)
86{
87 //mesh->setParent(this);
88 for (int i=0;i<DETAILTYPES_MAX;i++)
89 m_mesh[i] = mesh;
90 m_scaleX = 1.0f;
91 m_scaleY = 1.0f;
92 m_scaleZ = 1.0f;
93 m_rotationAngle = 0.0f;
94 m_effect = 0;
95 m_objectId = -1;
96 m_hovering = false;
97 m_selected = false;
98 m_material = 0;
99 initStaticMaterials();
100}
101
102GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
103 : QObject(parent)
104{
105 scene->setParent(this);
106 for (int i=0;i<DETAILTYPES_MAX;i++)
107 m_mesh[i] = scene->mainNode();
108 m_scaleX = 1.0f;
109 m_scaleY = 1.0f;
110 m_scaleZ = 1.0f;
111 m_rotationAngle = 0.0f;
112 m_effect = 0;
113 m_objectId = -1;
114 m_hovering = false;
115 m_selected = false;
116 m_material = 0;
117 initStaticMaterials();
118}
119
120GLMoleculeObject::~GLMoleculeObject()
121{
122}
123
124void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
125{
126 Q_UNUSED(painter);
127 if (m_objectId != -1)
128 view->registerObject(m_objectId, this);
129}
130
131
132/** Draws a box around the mesh.
133 *
134 */
135void GLMoleculeObject::drawSelectionBox(QGLPainter *painter)
136{
137 painter->setFaceMaterial(QGL::AllFaces, m_selectionBoxMaterial);
138 QVector3DArray array;
139 qreal radius = 1.0;
140 array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
141 array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
142 array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
143 array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
144
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 array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
149
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 array.append( radius, radius, -radius); array.append( radius, radius, radius);
154 painter->clearAttributes();
155 painter->setVertexAttribute(QGL::Position, array);
156 painter->draw(QGL::Lines, 24);
157}
158
159void GLMoleculeObject::draw(QGLPainter *painter, const QVector4D &cameraPlane)
160{
161 // Position the model at its designated position, scale, and orientation.
162 painter->modelViewMatrix().push();
163 painter->modelViewMatrix().translate(m_position);
164 if (m_rotationAngle != 0.0f)
165 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
166 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
167
168 // Apply the material and effect to the painter.
169 QGLMaterial *material;
170 if (m_hovering)
171 material = m_hoverMaterial;
172 else if (m_selected)
173 material = m_selectionMaterial;
174 else
175 material = m_material;
176
177 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
178
179 painter->setColor(material->diffuseColor());
180 painter->setFaceMaterial(QGL::AllFaces, material);
181 if (m_effect)
182 painter->setUserEffect(m_effect);
183 else
184 painter->setStandardEffect(QGL::LitMaterial);
185
186 // Mark the object for object picking purposes.
187 int prevObjectId = painter->objectPickId();
188 if (m_objectId != -1)
189 painter->setObjectPickId(m_objectId);
190
191 // Draw the geometry mesh.
192 QVector4D pos4d(m_position, -1);
193 qreal distance = QVector4D::dotProduct(cameraPlane, pos4d);
194
195 if (distance > detailMinDistance[DETAIL_LOW])
196 m_mesh[DETAIL_LOW]->draw(painter);
197 else if (distance > detailMinDistance[DETAIL_MEDIUM])
198 m_mesh[DETAIL_MEDIUM]->draw(painter);
199 else if (distance > detailMinDistance[DETAIL_HIGH])
200 m_mesh[DETAIL_HIGH]->draw(painter);
201 else if (distance > detailMinDistance[DETAIL_HIGHEST])
202 m_mesh[DETAIL_HIGHEST]->draw(painter);
203
204 // Draw a box around the mesh, if selected.
205 if (m_selected)
206 drawSelectionBox(painter);
207
208 // Turn off the user effect, if present.
209 if (m_effect)
210 painter->setStandardEffect(QGL::LitMaterial);
211
212 // Revert to the previous object identifier.
213 painter->setObjectPickId(prevObjectId);
214
215 // Restore the modelview matrix.
216 painter->modelViewMatrix().pop();
217}
218
219bool GLMoleculeObject::event(QEvent *e)
220{
221 // Convert the raw event into a signal representing the user's action.
222 if (e->type() == QEvent::MouseButtonPress) {
223 QMouseEvent *me = (QMouseEvent *)e;
224 if (me->button() == Qt::LeftButton)
225 emit pressed();
226 } else if (e->type() == QEvent::MouseButtonRelease) {
227 QMouseEvent *me = (QMouseEvent *)e;
228 if (me->button() == Qt::LeftButton) {
229 emit released();
230 if (me->x() >= 0) // Positive: inside object, Negative: outside.
231 emit clicked();
232 }
233 } else if (e->type() == QEvent::MouseButtonDblClick) {
234 emit doubleClicked();
235 } else if (e->type() == QEvent::Enter) {
236 m_hovering = true;
237 emit hoverChanged(this);
238 } else if (e->type() == QEvent::Leave) {
239 m_hovering = false;
240 emit hoverChanged(NULL);
241 }
242 return QObject::event(e);
243}
244
245/** Returns the ref to the Material for element No \a from the map.
246 *
247 * \note We create a new one if the element is missing.
248 *
249 * @param no element no
250 * @return ref to QGLMaterial
251 */
252QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
253{
254 ASSERT( (no > 0) && (no < MAX_ELEMENTS),
255 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
256 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
257 // get present one
258 return ElementNoMaterialMap[no];
259 } else {
260 // create new one
261 LOG(1, "Creating new material for element "+toString(no)+".");
262 QGLMaterial *newmaterial = new QGLMaterial(NULL);
263
264 // create material for element
265 periodentafel *periode = World::getInstance().getPeriode();
266 const element *desiredelement = periode->FindElement(no);
267 ASSERT(desiredelement != NULL,
268 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
269 const unsigned char* color = desiredelement->getColor();
270 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
271 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
272 newmaterial->setSpecularColor( QColor(60, 60, 60) );
273 newmaterial->setShininess( 128 );
274 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
275
276 return newmaterial;
277 }
278}
279
280/** Create the 3 materials shared by all objects.
281 *
282 */
283void GLMoleculeObject::initStaticMaterials()
284{
285 if (!m_hoverMaterial){
286 m_hoverMaterial = new QGLMaterial(NULL);
287 m_hoverMaterial->setAmbientColor( QColor(0, 128, 128) );
288 m_hoverMaterial->setSpecularColor( QColor(60, 60, 60) );
289 m_hoverMaterial->setShininess( 128 );
290 }
291 if (!m_selectionMaterial){
292 m_selectionMaterial = new QGLMaterial(NULL);
293 m_selectionMaterial->setAmbientColor( QColor(255, 50, 50) );
294 m_selectionMaterial->setSpecularColor( QColor(60, 60, 60) );
295 m_selectionMaterial->setShininess( 128 );
296 }
297 if (!m_selectionBoxMaterial){
298 m_selectionBoxMaterial = new QGLMaterial(NULL);
299 m_selectionBoxMaterial->setAmbientColor( QColor(0, 0, 0) );
300 m_selectionBoxMaterial->setDiffuseColor( QColor(0, 0, 0) );
301 m_selectionBoxMaterial->setEmittedLight( QColor(155, 50, 50) );
302 }
303}
304
305/** Static function to be called when Materials have to be removed.
306 *
307 */
308void GLMoleculeObject::cleanMaterialMap()
309{
310 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
311 !ElementNoMaterialMap.empty();
312 iter = ElementNoMaterialMap.begin()) {
313 delete iter->second;
314 ElementNoMaterialMap.erase(iter);
315 }
316}
317
318
319void GLMoleculeObject::setSelected(bool value)
320{
321 if (value != m_selected){
322 m_selected = value;
323 emit selectionChanged();
324 }
325}
Note: See TracBrowser for help on using the repository browser.