source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.cpp

Candidate_v1.7.1 stable v1.7.1
Last change on this file was 57608e, checked in by Frederik Heber <frederik.heber@…>, 3 weeks ago

Attempt to simplify redraw event in Qt View.

  • 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 * 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_atom.cpp
26 *
27 * Created on: Aug 17, 2011
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "GLMoleculeObject_atom.hpp"
37
38#include <Qt3D/qglscenenode.h>
39
40//#include "CodePatterns/MemDebug.hpp"
41
42#include "CodePatterns/Assert.hpp"
43#include "CodePatterns/Log.hpp"
44#include "CodePatterns/Observer/Notification.hpp"
45
46#include <algorithm>
47
48#include "Atom/atom.hpp"
49#include "Bond/bond.hpp"
50#include "Element/element.hpp"
51#include "Element/periodentafel.hpp"
52#include "LinearAlgebra/Line.hpp"
53#include "LinearAlgebra/Vector.hpp"
54#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_bond.hpp"
55#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
56#include "World.hpp"
57
58GLMoleculeObject_atom::GLMoleculeObject_atom(
59 QGLSceneNode *mesh[],
60 QGLSceneNode *mesharrow[],
61 QObject *parent,
62 QtObservedAtom::ptr &_ObservedAtom) :
63 GLMoleculeObject(mesh, parent),
64 GLMoleculeObjectVelocity(mesharrow, this),
65 GLMoleculeObjectForce(mesharrow, this),
66 ObservedAtom(_ObservedAtom)
67{
68 init(ObservedAtom->getAtomIndex());
69}
70
71void GLMoleculeObject_atom::init(const atomId_t _id)
72{
73 setObjectId(_id);
74 resetPosition();
75 resetVelocity();
76 resetForce();
77 resetElement();
78
79 GLMoleculeObjectVelocity.setMaterial(GLMoleculeObject::m_velocityMaterial);
80 GLMoleculeObjectForce.setMaterial(GLMoleculeObject::m_forceMaterial);
81
82 m_selected = ObservedAtom->getAtomSelected();
83
84 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
85 connect( ObservedAtom.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex()));
86 connect( ObservedAtom.get(), SIGNAL(elementChanged()), this, SLOT(resetElement()));
87 connect( ObservedAtom.get(), SIGNAL(positionChanged()), this, SLOT(resetPosition()));
88 connect( ObservedAtom.get(), SIGNAL(velocityChanged()), this, SLOT(resetVelocity()));
89 connect( ObservedAtom.get(), SIGNAL(forceChanged()), this, SLOT(resetForce()));
90 connect( ObservedAtom.get(), SIGNAL(selectedChanged()), this, SLOT(resetSelected()));
91}
92
93GLMoleculeObject_atom::~GLMoleculeObject_atom()
94{}
95
96void GLMoleculeObject_atom::resetIndex()
97{
98 const atomId_t newId = ObservedAtom->getAtomIndex();
99 const size_t oldId = objectId();
100 ASSERT( newId != oldId,
101 "GLMoleculeObject_atom::updateIndex() - index "+toString(newId)+" did not change.");
102 LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(newId)+".");
103 setObjectId(newId);
104
105 emit indexChanged(this, oldId, newId);
106}
107
108static void setArrow(
109 const Vector &_position,
110 const Vector &_arrow,
111 const double _offset,
112 GLMoleculeObject &_obj)
113{
114 // set position (cylinder offset is in its barymetric center)
115 Vector OneFourth(_position + (_offset/_arrow.Norm()+.75) * _arrow);
116 _obj.setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
117}
118
119void GLMoleculeObject_atom::resetPosition()
120{
121 const Vector Position = ObservedAtom->getAtomPosition();
122 LOG(4, "INFO: GLMoleculeObject_atom::resetPosition() - new position is "+toString(Position)+".");
123 setPosition(QVector3D(Position[0], Position[1], Position[2]));
124 setArrow(Position, 10.*ObservedAtom->getAtomVelocity(), scaleX(), GLMoleculeObjectVelocity);
125 setArrow(Position, 10.*ObservedAtom->getAtomForce(), scaleX(), GLMoleculeObjectForce);
126
127 emit changed();
128}
129
130static void alignArrow(
131 const Vector &_arrow,
132 GLMoleculeObject &_obj)
133{
134 if (_arrow.IsZero()) {
135 _obj.setScaleZ(0.);
136 return;
137 }
138 // calculate position
139 Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
140 Vector b;
141 Vector OtherAxis;
142 double alpha;
143 // construct rotation axis
144 b = -1.*_arrow;
145 b.VectorProduct(Z);
146 Line axis(zeroVec, b);
147 // calculate rotation angle
148 alpha = _arrow.Angle(Z);
149 // construct other axis to check right-hand rule
150 OtherAxis = b;
151 OtherAxis.VectorProduct(Z);
152 // assure right-hand rule for the rotation
153 if (_arrow.ScalarProduct(OtherAxis) < MYEPSILON)
154 alpha = M_PI-alpha;
155 // check
156 Vector a_rotated = axis.rotateVector(_arrow, alpha);
157 LOG(5, "DEBUG: Aligning arrow " << _arrow << " to " << a_rotated
158 << " around " << b << " by " << alpha/M_PI*180. << ".");
159
160 _obj.setScaleZ(10.*_arrow.Norm());
161 _obj.setRotationVector(QVector3D(b[0], b[1], b[2]));
162 _obj.setRotationAngle(alpha/M_PI*180.);
163}
164
165void GLMoleculeObject_atom::resetVelocity()
166{
167 const Vector Velocity = ObservedAtom->getAtomVelocity();
168 LOG(4, "INFO: GLMoleculeObject_atom::resetVelocity() - new velocity is "+toString(Velocity)+".");
169 alignArrow(Velocity, GLMoleculeObjectVelocity);
170// GLMoleculeObjectForce.setScaleZ(Velocity.Norm());
171
172 emit changed();
173}
174
175void GLMoleculeObject_atom::resetForce()
176{
177 const Vector Force = ObservedAtom->getAtomForce();
178 LOG(4, "INFO: GLMoleculeObject_atom::resetForce() - new force is "+toString(Force)+".");
179 alignArrow(Force, GLMoleculeObjectForce);
180// GLMoleculeObjectForce.setScaleZ(Force.Norm());
181
182 emit changed();
183}
184
185void GLMoleculeObject_atom::resetElement()
186{
187 size_t elementno = 0;
188 const element * const _type = World::getInstance().
189 getPeriode()->FindElement(ObservedAtom->getAtomElement());
190 if (_type != NULL) {
191 elementno = _type->getAtomicNumber();
192 } else { // if no element yet, set to hydrogen
193 elementno = 1;
194 }
195 LOG(4, "INFO: GLMoleculeObject_atom::resetElement() - new element number is "+toString(elementno)+".");
196
197 // set materials
198 QGLMaterial *elementmaterial = getMaterial(elementno);
199 ASSERT(elementmaterial != NULL,
200 "GLMoleculeObject_atom::resetElement() - QGLMaterial ref from getter function is NULL.");
201 setMaterial(elementmaterial);
202
203 // set scale
204 double radius = 0.;
205 if (_type != NULL) {
206 radius = _type->getVanDerWaalsRadius();
207 } else {
208 radius = 0.5;
209 }
210 setScale( radius / 4. );
211
212 emit changed();
213}
214
215void GLMoleculeObject_atom::resetSelected()
216{
217 // we ascertain check that selection state changed as the Qt signal might be executed
218 // at a later stage when the state has changed yet again
219 const bool new_selected = ObservedAtom->getAtomSelected();
220 m_selected = new_selected;
221
222 emit changed();
223}
224
225void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
226{
227 // call old hook to do the actual paining
228 GLMoleculeObject::draw(painter, cameraPlane);
229
230 // draw all children: such as velocity and force vectors
231 foreach (QObject *obj, children()) {
232 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
233 if (meshobj)
234 meshobj->draw(painter, cameraPlane);
235 }
236}
237
238void GLMoleculeObject_atom::wasClicked()
239{
240 LOG(4, "INFO: GLMoleculeObject_atom: atom "
241 << ObservedAtom->getAtomIndex() << " has been clicked");
242 emit clicked(ObservedAtom->getAtomIndex());
243}
Note: See TracBrowser for help on using the repository browser.