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