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_bond.cpp
|
---|
25 | *
|
---|
26 | * Created on: Aug 17, 2011
|
---|
27 | * Author: heber
|
---|
28 | */
|
---|
29 |
|
---|
30 | // include config.h
|
---|
31 | #ifdef HAVE_CONFIG_H
|
---|
32 | #include <config.h>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | #include "GLMoleculeObject_bond.hpp"
|
---|
36 |
|
---|
37 | #include <Qt3D/qglmaterial.h>
|
---|
38 | #include <Qt3D/qglscenenode.h>
|
---|
39 |
|
---|
40 | #include "CodePatterns/MemDebug.hpp"
|
---|
41 |
|
---|
42 |
|
---|
43 | #include <cmath>
|
---|
44 |
|
---|
45 | #include "CodePatterns/Assert.hpp"
|
---|
46 | #include "CodePatterns/Log.hpp"
|
---|
47 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
48 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
49 | #include "Atom/atom.hpp"
|
---|
50 | #include "Bond/bond.hpp"
|
---|
51 | #include "Element/element.hpp"
|
---|
52 | #include "Helpers/defs.hpp"
|
---|
53 | #include "LinearAlgebra/Line.hpp"
|
---|
54 | #include "LinearAlgebra/Vector.hpp"
|
---|
55 |
|
---|
56 | GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
|
---|
57 | GLMoleculeObject(mesh, parent),
|
---|
58 | Observer(std::string("GLMoleculeObject_bond")
|
---|
59 | +toString(bondref->leftatom->getId())
|
---|
60 | +std::string("-")
|
---|
61 | +toString(bondref->rightatom->getId())),
|
---|
62 | _bond(*bondref),
|
---|
63 | leftatom(bondref->leftatom),
|
---|
64 | rightatom(bondref->rightatom),
|
---|
65 | leftatomId(bondref->leftatom->getId()),
|
---|
66 | rightatomId(bondref->rightatom->getId()),
|
---|
67 | BondSide(side)
|
---|
68 | {
|
---|
69 | // sign on as observer (obtain non-const instance before)
|
---|
70 | _bond.signOn(this, BondObservable::BondRemoved);
|
---|
71 | leftatom->signOn(this, AtomObservable::PositionChanged);
|
---|
72 | leftatom->signOn(this, AtomObservable::ElementChanged);
|
---|
73 | rightatom->signOn(this, AtomObservable::PositionChanged);
|
---|
74 | rightatom->signOn(this, AtomObservable::ElementChanged);
|
---|
75 |
|
---|
76 | size_t elementno = 0;
|
---|
77 | switch (BondSide) {
|
---|
78 | case left:
|
---|
79 | if (_bond.rightatom->getType() != NULL) {
|
---|
80 | elementno = _bond.rightatom->getType()->getAtomicNumber();
|
---|
81 | } else { // if not element yet set, set to hydrogen
|
---|
82 | elementno = 1;
|
---|
83 | }
|
---|
84 | break;
|
---|
85 | case right:
|
---|
86 | if (_bond.leftatom->getType() != NULL) {
|
---|
87 | elementno = _bond.leftatom->getType()->getAtomicNumber();
|
---|
88 | } else { // if not element yet set, set to hydrogen
|
---|
89 | elementno = 1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | break;
|
---|
93 | default:
|
---|
94 | ASSERT(0,
|
---|
95 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
100 | setMaterial(elementmaterial);
|
---|
101 |
|
---|
102 | resetPosition();
|
---|
103 | }
|
---|
104 |
|
---|
105 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
106 | {
|
---|
107 | // sign off
|
---|
108 | switch (BondSide) {
|
---|
109 | case left:
|
---|
110 | emit BondRemoved(leftatomId, rightatomId);
|
---|
111 | break;
|
---|
112 | case right:
|
---|
113 | emit BondRemoved(rightatomId, leftatomId);
|
---|
114 | break;
|
---|
115 | default:
|
---|
116 | ASSERT(0,
|
---|
117 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
|
---|
118 | +toString(BondSide)+".");
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | _bond.signOff(this, BondObservable::BondRemoved);
|
---|
122 | leftatom->signOff(this, AtomObservable::PositionChanged);
|
---|
123 | leftatom->signOff(this, AtomObservable::ElementChanged);
|
---|
124 | rightatom->signOff(this, AtomObservable::PositionChanged);
|
---|
125 | rightatom->signOff(this, AtomObservable::ElementChanged);
|
---|
126 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
|
---|
127 | }
|
---|
128 |
|
---|
129 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
130 | {
|
---|
131 | #ifdef LOG_OBSERVER
|
---|
132 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
133 | observerLog().addMessage() << "++ Update of Observer "
|
---|
134 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
135 | << " from bond.";
|
---|
136 | } else if (publisher == leftatom) {
|
---|
137 | observerLog().addMessage() << "++ Update of Observer "
|
---|
138 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
139 | << " from leftatom " << _bond.leftatom->getId() << ".";
|
---|
140 | } else if (publisher == rightatom) {
|
---|
141 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
142 | observerLog().getName(static_cast<Observer*>(this))
|
---|
143 | << " from rightatom " << _bond.rightatom->getId() << ".";
|
---|
144 | } else
|
---|
145 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
146 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
147 | #endif
|
---|
148 | }
|
---|
149 |
|
---|
150 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
151 | {
|
---|
152 | delete this;
|
---|
153 | }
|
---|
154 |
|
---|
155 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
156 | {
|
---|
157 | #ifdef LOG_OBSERVER
|
---|
158 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
159 | observerLog().addMessage() << "++ Update of Observer "
|
---|
160 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
161 | << " received notification from bond for channel "
|
---|
162 | << notification->getChannelNo() << ".";
|
---|
163 | } else if (publisher == leftatom) {
|
---|
164 | observerLog().addMessage() << "++ Update of Observer "
|
---|
165 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
166 | << " received notification from leftatom " << _bond.leftatom->getId() << " for channel "
|
---|
167 | << notification->getChannelNo() << ".";
|
---|
168 | } else if (publisher == rightatom) {
|
---|
169 | observerLog().addMessage() << "++ Update of Observer "
|
---|
170 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
171 | << " received notification from rightatom " << _bond.rightatom->getId() << " for channel "
|
---|
172 | << notification->getChannelNo() << ".";
|
---|
173 | } else
|
---|
174 | observerLog().addMessage() << "++ Update of Observer "
|
---|
175 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
176 | << " received notification from unknown source.";
|
---|
177 | #endif
|
---|
178 | if (publisher == static_cast<const Observable *>(&_bond)){
|
---|
179 | delete this;
|
---|
180 | } else {
|
---|
181 | bool DoResetPosition = false;
|
---|
182 | // from an atom
|
---|
183 | switch (notification->getChannelNo()) {
|
---|
184 | case AtomObservable::PositionChanged:
|
---|
185 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
186 | DoResetPosition = true;
|
---|
187 | break;
|
---|
188 | case AtomObservable::ElementChanged:
|
---|
189 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
190 | DoResetPosition = true;
|
---|
191 | break;
|
---|
192 | default:
|
---|
193 | break;
|
---|
194 | }
|
---|
195 | if (DoResetPosition) {
|
---|
196 | resetPosition();
|
---|
197 | emit changed();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void GLMoleculeObject_bond::resetPosition()
|
---|
203 | {
|
---|
204 | Vector Position;
|
---|
205 | Vector OtherPosition;
|
---|
206 | switch (BondSide) {
|
---|
207 | case left:
|
---|
208 | Position = _bond.leftatom->getPosition();
|
---|
209 | OtherPosition = _bond.rightatom->getPosition();
|
---|
210 | break;
|
---|
211 | case right:
|
---|
212 | Position = _bond.rightatom->getPosition();
|
---|
213 | OtherPosition = _bond.leftatom->getPosition();
|
---|
214 | break;
|
---|
215 | default:
|
---|
216 | ASSERT(0,
|
---|
217 | "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | const double distance =
|
---|
221 | Position.distance(OtherPosition)/2.;
|
---|
222 | setScaleZ(distance);
|
---|
223 |
|
---|
224 | // calculate position
|
---|
225 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
226 | Vector zeroVec(0.,0.,0.);
|
---|
227 | Vector a,b;
|
---|
228 | Vector OtherAxis;
|
---|
229 | double alpha;
|
---|
230 | a = Position - OtherPosition;
|
---|
231 | // construct rotation axis
|
---|
232 | b = a;
|
---|
233 | b.VectorProduct(Z);
|
---|
234 | Line axis(zeroVec, b);
|
---|
235 | // calculate rotation angle
|
---|
236 | alpha = a.Angle(Z);
|
---|
237 | // construct other axis to check right-hand rule
|
---|
238 | OtherAxis = b;
|
---|
239 | OtherAxis.VectorProduct(Z);
|
---|
240 | // assure right-hand rule for the rotation
|
---|
241 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
242 | alpha = M_PI-alpha;
|
---|
243 | // check
|
---|
244 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
245 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
246 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
247 |
|
---|
248 | // set position (cylinder offset is in its barymetric center)
|
---|
249 | Vector OneFourth(Position - 0.75 * a);
|
---|
250 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
251 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
252 | setRotationAngle(alpha/M_PI*180.);
|
---|
253 | }
|
---|