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

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

GLMoleculeObject_bond: dynamic bond distance via matrices

  • Property mode set to 100644
File size: 7.5 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 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * GLMoleculeObject.cpp
10 *
11 * This is based on the Qt3D example "teaservice", specifically meshobject.cpp.
12 *
13 * Created on: Aug 17, 2011
14 * Author: heber
15 */
16
17// include config.h
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22#include "GLMoleculeObject.hpp"
23
24#include <Qt3D/qglview.h>
25#include <Qt3D/qglscenenode.h>
26#include <Qt3D/qglpainter.h>
27#include <Qt3D/qglmaterial.h>
28
29#include "CodePatterns/MemDebug.hpp"
30
31#include "CodePatterns/Assert.hpp"
32#include "CodePatterns/Log.hpp"
33
34#include "Helpers/defs.hpp"
35#include "Element/element.hpp"
36#include "Element/periodentafel.hpp"
37#include "World.hpp"
38
39GLMoleculeObject::ElementMaterialMap GLMoleculeObject::ElementNoMaterialMap;
40
41
42#include "CodePatterns/MemDebug.hpp"
43
44
45GLMoleculeObject::GLMoleculeObject(QGLSceneNode *GLMoleculeObject, QObject *parent)
46 : QObject(parent)
47{
48 m_mesh = 0;
49 m_GLMoleculeObject = GLMoleculeObject;
50 m_scale = 1.0f;
51 m_scaleZ = 1.0f;
52 m_rotationAngle = 0.0f;
53 m_effect = 0;
54 m_objectId = -1;
55 m_hovering = false;
56 m_selected = false;
57 m_material = 0;
58 m_hoverMaterial = 0;
59 m_selectionMaterial = 0;
60}
61
62GLMoleculeObject::GLMoleculeObject(QGLAbstractScene *scene, QObject *parent)
63 : QObject(parent)
64{
65 scene->setParent(this);
66 m_mesh = 0;
67 m_GLMoleculeObject = scene->mainNode();
68 m_scale = 1.0f;
69 m_scaleZ = 1.0f;
70 m_rotationAngle = 0.0f;
71 m_effect = 0;
72 m_objectId = -1;
73 m_hovering = false;
74 m_selected = false;
75 m_material = 0;
76 m_hoverMaterial = 0;
77 m_selectionMaterial = 0;
78}
79
80GLMoleculeObject::~GLMoleculeObject()
81{
82 delete m_mesh;
83}
84
85void GLMoleculeObject::initialize(QGLView *view, QGLPainter *painter)
86{
87 Q_UNUSED(painter);
88 if (m_objectId != -1)
89 view->registerObject(m_objectId, this);
90}
91
92void GLMoleculeObject::draw(QGLPainter *painter)
93{
94 // Position the model at its designated position, scale, and orientation.
95 painter->modelViewMatrix().push();
96 painter->modelViewMatrix().translate(m_position);
97 if (m_scale != 1.0f)
98 painter->modelViewMatrix().scale(m_scale);
99 if (m_rotationAngle != 0.0f)
100 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
101 if (m_scaleZ != 1.0f)
102 painter->modelViewMatrix().scale(1.0f, 1.0f, m_scaleZ);
103
104 // Apply the material and effect to the painter.
105 QGLMaterial *material;
106 if (m_hovering)
107 material = m_hoverMaterial;
108 else if (m_selected)
109 material = m_selectionMaterial;
110 else
111 material = m_material;
112 painter->setColor(material->diffuseColor());
113 painter->setFaceMaterial(QGL::AllFaces, material);
114 if (m_effect)
115 painter->setUserEffect(m_effect);
116 else
117 painter->setStandardEffect(QGL::LitMaterial);
118
119 // Mark the object for object picking purposes.
120 int prevObjectId = painter->objectPickId();
121 if (m_objectId != -1)
122 painter->setObjectPickId(m_objectId);
123
124 // Draw the geometry mesh.
125 if (m_GLMoleculeObject)
126 m_GLMoleculeObject->draw(painter);
127 else
128 m_mesh->draw(painter);
129
130 QVector3DArray array;
131 qreal radius = 0.3f;
132 array.append(-radius, -radius, -radius); array.append( radius, -radius, -radius);
133 array.append( radius, -radius, -radius); array.append( radius, radius, -radius);
134 array.append( radius, radius, -radius); array.append(-radius, radius, -radius);
135 array.append(-radius, radius, -radius); array.append(-radius, -radius, -radius);
136
137 array.append(-radius, -radius, radius); array.append( radius, -radius, radius);
138 array.append( radius, -radius, radius); array.append( radius, radius, radius);
139 array.append( radius, radius, radius); array.append(-radius, radius, radius);
140 array.append(-radius, radius, radius); array.append(-radius, -radius, radius);
141
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 array.append( radius, radius, -radius); array.append( radius, radius, radius);
146 painter->clearAttributes();
147 painter->setVertexAttribute(QGL::Position, array);
148 painter->draw(QGL::Lines, 24);
149
150 // Turn off the user effect, if present.
151 if (m_effect)
152 painter->setStandardEffect(QGL::LitMaterial);
153
154 // Revert to the previous object identifier.
155 painter->setObjectPickId(prevObjectId);
156
157 // Restore the modelview matrix.
158 painter->modelViewMatrix().pop();
159}
160
161bool GLMoleculeObject::event(QEvent *e)
162{
163 // Convert the raw event into a signal representing the user's action.
164 if (e->type() == QEvent::MouseButtonPress) {
165 QMouseEvent *me = (QMouseEvent *)e;
166 if (me->button() == Qt::LeftButton)
167 emit pressed();
168 } else if (e->type() == QEvent::MouseButtonRelease) {
169 QMouseEvent *me = (QMouseEvent *)e;
170 if (me->button() == Qt::LeftButton) {
171 emit released();
172 if (me->x() >= 0) // Positive: inside object, Negative: outside.
173 emit clicked();
174 }
175 } else if (e->type() == QEvent::MouseButtonDblClick) {
176 emit doubleClicked();
177 } else if (e->type() == QEvent::Enter) {
178 m_hovering = true;
179 emit hoverChanged();
180 } else if (e->type() == QEvent::Leave) {
181 m_hovering = false;
182 emit hoverChanged();
183 }
184 return QObject::event(e);
185}
186
187/** Returns the ref to the Material for element No \a from the map.
188 *
189 * \note We create a new one if the element is missing.
190 *
191 * @param no element no
192 * @return ref to QGLMaterial
193 */
194QGLMaterial* GLMoleculeObject::getMaterial(size_t no)
195{
196 ASSERT( (no >= 0) && (no < MAX_ELEMENTS) || (no == 65536),
197 "GLMoleculeView::getMaterial() - Element no "+toString(no)+" is invalid.");
198 if (ElementNoMaterialMap.find(no) != ElementNoMaterialMap.end()){
199 // get present one
200 return ElementNoMaterialMap[no];
201 } else {
202 // create new one
203 LOG(1, "Creating new material for element "+toString(no)+".");
204 QGLMaterial *newmaterial = new QGLMaterial(NULL);
205
206 if (no == 0) { // create hover material
207 newmaterial->setAmbientColor( QColor(0, 128, 128) );
208 } else if (no == 65536) { // create selection material
209 newmaterial->setAmbientColor( QColor(200, 200, 200) );
210 } else { // create material for element
211 periodentafel *periode = World::getInstance().getPeriode();
212 const element *desiredelement = periode->FindElement(no);
213 ASSERT(desiredelement != NULL,
214 "GLMoleculeView::getMaterial() - desired element "+toString(no)+" not present in periodentafel.");
215 const unsigned char* color = desiredelement->getColor();
216 LOG(1, "Creating new material with color " << (int)color[0] << "," << (int)color[1] << "," << (int)color[2] << ".");
217 newmaterial->setAmbientColor( QColor((int)color[0], (int)color[1], (int)color[2]) );
218 }
219 newmaterial->setSpecularColor( QColor(60, 60, 60) );
220 newmaterial->setShininess( 128 );
221 ElementNoMaterialMap.insert( make_pair(no, newmaterial) );
222
223 return newmaterial;
224 }
225}
226
227/** Static function to be called when Materials have to be removed.
228 *
229 */
230void GLMoleculeObject::cleanMaterialMap()
231{
232 for (ElementMaterialMap::iterator iter = ElementNoMaterialMap.begin();
233 !ElementNoMaterialMap.empty();
234 iter = ElementNoMaterialMap.begin()) {
235 delete iter->second;
236 ElementNoMaterialMap.erase(iter);
237 }
238}
Note: See TracBrowser for help on using the repository browser.