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_bond.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_bond.hpp"
|
---|
37 |
|
---|
38 | #include <Qt3D/qglmaterial.h>
|
---|
39 | #include <Qt3D/qglscenenode.h>
|
---|
40 |
|
---|
41 | #include "CodePatterns/MemDebug.hpp"
|
---|
42 |
|
---|
43 |
|
---|
44 | #include <cmath>
|
---|
45 |
|
---|
46 | #include "CodePatterns/Assert.hpp"
|
---|
47 | #include "CodePatterns/Log.hpp"
|
---|
48 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
49 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
50 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
51 | #include "Atom/atom.hpp"
|
---|
52 | #include "Bond/bond.hpp"
|
---|
53 | #include "Element/element.hpp"
|
---|
54 | #include "Helpers/defs.hpp"
|
---|
55 | #include "LinearAlgebra/Line.hpp"
|
---|
56 | #include "LinearAlgebra/Vector.hpp"
|
---|
57 | #include "World.hpp"
|
---|
58 |
|
---|
59 | GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
|
---|
60 | GLMoleculeObject(mesh, parent),
|
---|
61 | Observer(std::string("GLMoleculeObject_bond")
|
---|
62 | +toString(bondref->leftatom->getId())
|
---|
63 | +std::string("-")
|
---|
64 | +toString(bondref->rightatom->getId())),
|
---|
65 | _bond(*bondref),
|
---|
66 | leftobservable(bondref->leftatom),
|
---|
67 | rightobservable(bondref->rightatom),
|
---|
68 | leftatomId(bondref->leftatom->getId()),
|
---|
69 | rightatomId(bondref->rightatom->getId()),
|
---|
70 | BondSide(side),
|
---|
71 | leftobservable_enabled(false),
|
---|
72 | rightobservable_enabled(false),
|
---|
73 | bond_enabled(false)
|
---|
74 | {
|
---|
75 | // sign on as observer (obtain non-const instance before)
|
---|
76 | _bond.signOn(this);
|
---|
77 | _bond.signOn(this, BondObservable::BondRemoved);
|
---|
78 | _bond.signOn(this, BondObservable::DegreeChanged);
|
---|
79 | bond_enabled = true;
|
---|
80 | leftobservable->signOn(this);
|
---|
81 | leftobservable->signOn(this, AtomObservable::PositionChanged);
|
---|
82 | leftobservable->signOn(this, AtomObservable::ElementChanged);
|
---|
83 | leftobservable_enabled = true;
|
---|
84 | rightobservable->signOn(this);
|
---|
85 | rightobservable->signOn(this, AtomObservable::PositionChanged);
|
---|
86 | rightobservable->signOn(this, AtomObservable::ElementChanged);
|
---|
87 | rightobservable_enabled = true;
|
---|
88 |
|
---|
89 | size_t elementno = 0;
|
---|
90 | switch (BondSide) {
|
---|
91 | case left:
|
---|
92 | {
|
---|
93 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
94 | if (_rightatom->getType() != NULL) {
|
---|
95 | elementno = _rightatom->getType()->getAtomicNumber();
|
---|
96 | } else { // if not element yet set, set to hydrogen
|
---|
97 | elementno = 1;
|
---|
98 | }
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | case right:
|
---|
102 | {
|
---|
103 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
104 | if (_leftatom->getType() != NULL) {
|
---|
105 | elementno = _leftatom->getType()->getAtomicNumber();
|
---|
106 | } else { // if not element yet set, set to hydrogen
|
---|
107 | elementno = 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | default:
|
---|
113 | ASSERT(0,
|
---|
114 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
115 | break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
119 | setMaterial(elementmaterial);
|
---|
120 |
|
---|
121 | resetPosition();
|
---|
122 | resetWidth();
|
---|
123 | }
|
---|
124 |
|
---|
125 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
126 | {
|
---|
127 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
|
---|
128 | // signOff() if not already done
|
---|
129 | removeLeftAtom();
|
---|
130 | removeRightAtom();
|
---|
131 | removeBond();
|
---|
132 | removeChannels();
|
---|
133 | }
|
---|
134 |
|
---|
135 | void GLMoleculeObject_bond::removeLeftAtom()
|
---|
136 | {
|
---|
137 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
138 | // from the AtomObservable itself
|
---|
139 | if (leftobservable_enabled) {
|
---|
140 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
141 | if (_leftatom != NULL) {
|
---|
142 | _leftatom->signOff(this);
|
---|
143 | } else {
|
---|
144 | ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void GLMoleculeObject_bond::removeRightAtom()
|
---|
150 | {
|
---|
151 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
152 | // from the AtomObservable itself
|
---|
153 | if (rightobservable_enabled) {
|
---|
154 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
155 | if (_rightatom != NULL) {
|
---|
156 | _rightatom->signOff(this);
|
---|
157 | } else {
|
---|
158 | ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | void GLMoleculeObject_bond::removeBond()
|
---|
164 | {
|
---|
165 | if (bond_enabled)
|
---|
166 | _bond.signOff(this);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void GLMoleculeObject_bond::removeChannels()
|
---|
170 | {
|
---|
171 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
172 | // from the AtomObservable itself
|
---|
173 | if (leftobservable_enabled) {
|
---|
174 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
175 | if (_leftatom != NULL) {
|
---|
176 | _leftatom->signOff(this, AtomObservable::PositionChanged);
|
---|
177 | _leftatom->signOff(this, AtomObservable::ElementChanged);
|
---|
178 | } else {
|
---|
179 | ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
|
---|
180 | }
|
---|
181 | leftobservable_enabled = false;
|
---|
182 | }
|
---|
183 | if (rightobservable_enabled) {
|
---|
184 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
185 | if (_rightatom != NULL) {
|
---|
186 | _rightatom->signOff(this, AtomObservable::PositionChanged);
|
---|
187 | _rightatom->signOff(this, AtomObservable::ElementChanged);
|
---|
188 | } else {
|
---|
189 | ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
|
---|
190 | }
|
---|
191 | rightobservable_enabled = false;
|
---|
192 | }
|
---|
193 | if (bond_enabled) {
|
---|
194 | _bond.signOff(this, BondObservable::BondRemoved);
|
---|
195 | _bond.signOff(this, BondObservable::DegreeChanged);
|
---|
196 | bond_enabled = false;
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | void GLMoleculeObject_bond::removeMe()
|
---|
202 | {
|
---|
203 | // sign off
|
---|
204 | switch (BondSide) {
|
---|
205 | case left:
|
---|
206 | emit BondRemoved(leftatomId, rightatomId);
|
---|
207 | break;
|
---|
208 | case right:
|
---|
209 | emit BondRemoved(rightatomId, leftatomId);
|
---|
210 | break;
|
---|
211 | default:
|
---|
212 | ASSERT(0,
|
---|
213 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
|
---|
214 | +toString(BondSide)+".");
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
220 | {
|
---|
221 | #ifdef LOG_OBSERVER
|
---|
222 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
223 | observerLog().addMessage() << "++ Update of Observer "
|
---|
224 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
225 | << " from bond.";
|
---|
226 | } else if (publisher == leftobservable) {
|
---|
227 | observerLog().addMessage() << "++ Update of Observer "
|
---|
228 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
229 | << " from leftatom " << leftatomId << ".";
|
---|
230 | } else if (publisher == rightobservable) {
|
---|
231 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
232 | observerLog().getName(static_cast<Observer*>(this))
|
---|
233 | << " from rightatom " << rightatomId << ".";
|
---|
234 | } else
|
---|
235 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
236 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
237 | #endif
|
---|
238 | }
|
---|
239 |
|
---|
240 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
241 | {
|
---|
242 | // assume subjectKilled() is from Observable's own subjectKilled(), not notifications
|
---|
243 | // but we must always signOff from all other sources!
|
---|
244 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
245 | #ifdef LOG_OBSERVER
|
---|
246 | observerLog().addMessage() << "++ subjectKilled of Observer "
|
---|
247 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
248 | << " from bond.";
|
---|
249 | #endif
|
---|
250 | removeLeftAtom();
|
---|
251 | removeRightAtom();
|
---|
252 | } else if (publisher == leftobservable) {
|
---|
253 | #ifdef LOG_OBSERVER
|
---|
254 | observerLog().addMessage() << "++ subjectKilled of Observer "
|
---|
255 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
256 | << " from leftatom " << leftatomId << ".";
|
---|
257 | #endif
|
---|
258 | removeRightAtom();
|
---|
259 | removeBond();
|
---|
260 | } else if (publisher == rightobservable) {
|
---|
261 | #ifdef LOG_OBSERVER
|
---|
262 | observerLog().addMessage() << "++ subjectKilled of Observer " <<
|
---|
263 | observerLog().getName(static_cast<Observer*>(this))
|
---|
264 | << " from rightatom " << rightatomId << ".";
|
---|
265 | #endif
|
---|
266 | removeLeftAtom();
|
---|
267 | removeBond();
|
---|
268 | } else {
|
---|
269 | #ifdef LOG_OBSERVER
|
---|
270 | observerLog().addMessage() << "++ subjectKilled of Observer " <<
|
---|
271 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 | // then indicate to remove us
|
---|
275 | removeChannels();
|
---|
276 | removeMe();
|
---|
277 | }
|
---|
278 |
|
---|
279 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
280 | {
|
---|
281 | #ifdef LOG_OBSERVER
|
---|
282 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
283 | observerLog().addMessage() << "++ Update of Observer "
|
---|
284 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
285 | << " received notification from bond for channel "
|
---|
286 | << notification->getChannelNo() << ".";
|
---|
287 | } else if (publisher == leftobservable) {
|
---|
288 | observerLog().addMessage() << "++ Update of Observer "
|
---|
289 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
290 | << " received notification from leftatom " << leftatomId << " for channel "
|
---|
291 | << notification->getChannelNo() << ".";
|
---|
292 | } else if (publisher == rightobservable) {
|
---|
293 | observerLog().addMessage() << "++ Update of Observer "
|
---|
294 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
295 | << " received notification from rightatom " << rightatomId << " for channel "
|
---|
296 | << notification->getChannelNo() << ".";
|
---|
297 | } else
|
---|
298 | observerLog().addMessage() << "++ Update of Observer "
|
---|
299 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
300 | << " received notification from unknown source.";
|
---|
301 | #endif
|
---|
302 | bool DoResetPosition = false;
|
---|
303 | bool DoResetWidth = false;
|
---|
304 | if (publisher == static_cast<const Observable *>(&_bond)){
|
---|
305 | switch (notification->getChannelNo()) {
|
---|
306 | case BondObservable::BondRemoved:
|
---|
307 | // removeMe();
|
---|
308 | break;
|
---|
309 | case BondObservable::DegreeChanged:
|
---|
310 | DoResetWidth = true;
|
---|
311 | break;
|
---|
312 | default:
|
---|
313 | ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | } else {
|
---|
317 | // from an atom
|
---|
318 | switch (notification->getChannelNo()) {
|
---|
319 | case AtomObservable::PositionChanged:
|
---|
320 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
321 | DoResetPosition = true;
|
---|
322 | break;
|
---|
323 | case AtomObservable::ElementChanged:
|
---|
324 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
325 | DoResetPosition = true;
|
---|
326 | break;
|
---|
327 | default:
|
---|
328 | break;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | if (DoResetPosition)
|
---|
332 | resetPosition();
|
---|
333 | if (DoResetWidth)
|
---|
334 | resetWidth();
|
---|
335 | if ((DoResetPosition) || (DoResetWidth))
|
---|
336 | emit changed();
|
---|
337 | }
|
---|
338 |
|
---|
339 | void GLMoleculeObject_bond::resetWidth()
|
---|
340 | {
|
---|
341 | const double factor = 1.0f+.5f*(_bond.getDegree()-1);
|
---|
342 | LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
|
---|
343 | setScaleX(factor);
|
---|
344 | setScaleY(factor);
|
---|
345 | }
|
---|
346 |
|
---|
347 | void GLMoleculeObject_bond::resetPosition()
|
---|
348 | {
|
---|
349 | Vector Position;
|
---|
350 | Vector OtherPosition;
|
---|
351 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
352 | Vector LeftPos = _leftatom->getPosition();
|
---|
353 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
354 | Vector RightPos = _rightatom->getPosition();
|
---|
355 | switch (BondSide) {
|
---|
356 | case left:
|
---|
357 | Position = LeftPos;
|
---|
358 | OtherPosition = RightPos;
|
---|
359 | break;
|
---|
360 | case right:
|
---|
361 | Position = RightPos;
|
---|
362 | OtherPosition = LeftPos;
|
---|
363 | break;
|
---|
364 | default:
|
---|
365 | ASSERT(0,
|
---|
366 | "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
367 | break;
|
---|
368 | }
|
---|
369 | const double distance =
|
---|
370 | Position.distance(OtherPosition)/2.;
|
---|
371 | setScaleZ(distance);
|
---|
372 |
|
---|
373 | // calculate position
|
---|
374 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
375 | Vector zeroVec(0.,0.,0.);
|
---|
376 | Vector a,b;
|
---|
377 | Vector OtherAxis;
|
---|
378 | double alpha;
|
---|
379 | a = Position - OtherPosition;
|
---|
380 | // construct rotation axis
|
---|
381 | b = a;
|
---|
382 | b.VectorProduct(Z);
|
---|
383 | Line axis(zeroVec, b);
|
---|
384 | // calculate rotation angle
|
---|
385 | alpha = a.Angle(Z);
|
---|
386 | // construct other axis to check right-hand rule
|
---|
387 | OtherAxis = b;
|
---|
388 | OtherAxis.VectorProduct(Z);
|
---|
389 | // assure right-hand rule for the rotation
|
---|
390 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
391 | alpha = M_PI-alpha;
|
---|
392 | // check
|
---|
393 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
394 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
395 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
396 |
|
---|
397 | // set position (cylinder offset is in its barymetric center)
|
---|
398 | Vector OneFourth(Position - 0.75 * a);
|
---|
399 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
400 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
401 | setRotationAngle(alpha/M_PI*180.);
|
---|
402 | }
|
---|