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

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

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

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