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 | #include <boost/assign.hpp>
|
---|
48 |
|
---|
49 | #include "Atom/atom.hpp"
|
---|
50 | #include "Bond/bond.hpp"
|
---|
51 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
52 | #include "Element/element.hpp"
|
---|
53 | #include "Element/periodentafel.hpp"
|
---|
54 | #include "LinearAlgebra/Vector.hpp"
|
---|
55 | #include "GLMoleculeObject_bond.hpp"
|
---|
56 | #include "World.hpp"
|
---|
57 | #include "WorldTime.hpp"
|
---|
58 |
|
---|
59 | using namespace boost::assign;
|
---|
60 |
|
---|
61 | static const Observable::channels_t getAtomBondsChannels()
|
---|
62 | {
|
---|
63 | Observable::channels_t channels;
|
---|
64 | channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
|
---|
65 | return channels;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // static entities
|
---|
69 | const Observable::channels_t
|
---|
70 | GLMoleculeObject_atom::AtomIndexChannels(1, AtomObservable::IndexChanged);
|
---|
71 | const Observable::channels_t
|
---|
72 | GLMoleculeObject_atom::AtomPositionChannels(1, AtomObservable::PositionChanged);
|
---|
73 | const Observable::channels_t
|
---|
74 | GLMoleculeObject_atom::AtomElementChannels(1, AtomObservable::ElementChanged);
|
---|
75 | const Observable::channels_t
|
---|
76 | GLMoleculeObject_atom::AtomBondsChannels(getAtomBondsChannels());
|
---|
77 | const Observable::channels_t
|
---|
78 | GLMoleculeObject_atom::AtomSelectionStatusChannels(World::SelectionChanged);
|
---|
79 |
|
---|
80 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atomId_t _id) :
|
---|
81 | GLMoleculeObject(mesh, parent),
|
---|
82 | Observer(std::string("GLMoleculeObject_atom")+toString(_id)),
|
---|
83 | atomref(getAtom(_id)),
|
---|
84 | AtomIndex(
|
---|
85 | atomref,
|
---|
86 | boost::bind(&GLMoleculeObject_atom::updateIndex, this),
|
---|
87 | "AtomIndex_"+toString(_id),
|
---|
88 | _id,
|
---|
89 | AtomIndexChannels),
|
---|
90 | AtomPosition(
|
---|
91 | atomref,
|
---|
92 | boost::bind(&GLMoleculeObject_atom::updatePosition, this),
|
---|
93 | "AtomPosition_"+toString(_id),
|
---|
94 | updatePosition(),
|
---|
95 | AtomPositionChannels),
|
---|
96 | AtomElement(
|
---|
97 | atomref,
|
---|
98 | boost::bind(&GLMoleculeObject_atom::updateElement, this),
|
---|
99 | "AtomElement"+toString(_id),
|
---|
100 | updateElement(),
|
---|
101 | AtomElementChannels),
|
---|
102 | AtomBonds(
|
---|
103 | atomref,
|
---|
104 | boost::bind(&GLMoleculeObject_atom::updateBonds, this),
|
---|
105 | "AtomBonds_"+toString(_id),
|
---|
106 | updateBonds(),
|
---|
107 | AtomBondsChannels),
|
---|
108 | AtomSelectionStatus(
|
---|
109 | World::getPointer(),
|
---|
110 | boost::bind(&GLMoleculeObject_atom::updateSelectionStatus, this),
|
---|
111 | "AtomSelectionStatus_"+toString(_id),
|
---|
112 | updateSelectionStatus(),
|
---|
113 | AtomSelectionStatusChannels),
|
---|
114 | owner(NULL)
|
---|
115 | {
|
---|
116 | setObjectId(_id);
|
---|
117 | resetPosition();
|
---|
118 | resetElement();
|
---|
119 |
|
---|
120 | // sign On
|
---|
121 | activateObserver();
|
---|
122 |
|
---|
123 | // atomref is only used for caching the ref, it must be used elswhere
|
---|
124 | const_cast<atom *&>(atomref) = NULL;
|
---|
125 |
|
---|
126 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
127 | connect( this, SIGNAL(idChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
|
---|
128 | connect( this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
|
---|
129 | connect( this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
|
---|
130 | connect( this, SIGNAL(bondsChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
|
---|
131 | connect( this, SIGNAL(selectionstatusChanged()), this, SLOT(resetSelectionStatus()), Qt::QueuedConnection);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void GLMoleculeObject_atom::activateObserver()
|
---|
135 | {
|
---|
136 | if (atomref != NULL) {
|
---|
137 | owner = static_cast<const Observable *>(atomref);
|
---|
138 | owner->signOn(this, AtomObservable::IndexChanged);
|
---|
139 | owner->signOn(this, AtomObservable::PositionChanged);
|
---|
140 | owner->signOn(this, AtomObservable::ElementChanged);
|
---|
141 | owner->signOn(this, AtomObservable::BondsAdded);
|
---|
142 | owner->signOn(this, AtomObservable::BondsRemoved);
|
---|
143 | World::getInstance().signOn(this, World::SelectionChanged);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | void GLMoleculeObject_atom::deactivateObserver()
|
---|
149 | {
|
---|
150 | // sign Off
|
---|
151 | if (owner != NULL) {
|
---|
152 | owner->signOff(this, AtomObservable::IndexChanged);
|
---|
153 | owner->signOff(this, AtomObservable::PositionChanged);
|
---|
154 | owner->signOff(this, AtomObservable::ElementChanged);
|
---|
155 | owner->signOff(this, AtomObservable::BondsAdded);
|
---|
156 | owner->signOff(this, AtomObservable::BondsRemoved);
|
---|
157 | World::getInstance().signOff(this, World::SelectionChanged);
|
---|
158 | owner = NULL;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
163 | {
|
---|
164 | deactivateObserver();
|
---|
165 | }
|
---|
166 |
|
---|
167 | void GLMoleculeObject_atom::resetIndex()
|
---|
168 | {
|
---|
169 | const atomId_t newId = AtomIndex.get();
|
---|
170 | const size_t oldId = objectId();
|
---|
171 | ASSERT( newId != oldId,
|
---|
172 | "GLMoleculeObject_atom::updateIndex() - index "+toString(newId)+" did not change.");
|
---|
173 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(newId)+".");
|
---|
174 | setObjectId(newId);
|
---|
175 |
|
---|
176 | emit indexChanged(this, oldId, newId);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void GLMoleculeObject_atom::resetPosition()
|
---|
180 | {
|
---|
181 | const Vector Position = AtomPosition.get();
|
---|
182 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
183 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
184 | }
|
---|
185 |
|
---|
186 | void GLMoleculeObject_atom::resetElement()
|
---|
187 | {
|
---|
188 | size_t elementno = 0;
|
---|
189 | const element * const _type = World::getInstance().
|
---|
190 | getPeriode()->FindElement(AtomElement.get());
|
---|
191 | if (_type != NULL) {
|
---|
192 | elementno = _type->getAtomicNumber();
|
---|
193 | } else { // if no element yet, set to hydrogen
|
---|
194 | elementno = 1;
|
---|
195 | }
|
---|
196 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
197 |
|
---|
198 | // set materials
|
---|
199 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
200 | ASSERT(elementmaterial != NULL,
|
---|
201 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
202 | setMaterial(elementmaterial);
|
---|
203 |
|
---|
204 | // set scale
|
---|
205 | double radius = 0.;
|
---|
206 | if (_type != NULL) {
|
---|
207 | radius = _type->getVanDerWaalsRadius();
|
---|
208 | } else {
|
---|
209 | radius = 0.5;
|
---|
210 | }
|
---|
211 | setScale( radius / 4. );
|
---|
212 | }
|
---|
213 |
|
---|
214 | void GLMoleculeObject_atom::resetBonds()
|
---|
215 | {
|
---|
216 | ListOfBonds_t ListOfBonds_new = AtomBonds.get();
|
---|
217 | std::sort(ListOfBonds_new.begin(), ListOfBonds_new.end());
|
---|
218 | ListOfBonds_t BondsToAdd;
|
---|
219 | std::set_difference(
|
---|
220 | ListOfBonds_new.begin(), ListOfBonds_new.end(),
|
---|
221 | ListOfBonds.begin(), ListOfBonds.end(),
|
---|
222 | std::back_inserter(BondsToAdd));
|
---|
223 | ListOfBonds_t BondsToRemove;
|
---|
224 | std::set_difference(
|
---|
225 | ListOfBonds.begin(), ListOfBonds.end(),
|
---|
226 | ListOfBonds_new.begin(), ListOfBonds_new.end(),
|
---|
227 | std::back_inserter(BondsToRemove));
|
---|
228 | for (ListOfBonds_t::const_iterator iter = BondsToAdd.begin();
|
---|
229 | iter != BondsToAdd.end();
|
---|
230 | ++iter) {
|
---|
231 | const GLMoleculeObject_bond::SideOfBond side = (iter->first == AtomIndex.get()) ?
|
---|
232 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
233 | emit BondsAdded(iter->first, iter->second, side);
|
---|
234 | }
|
---|
235 | for (ListOfBonds_t::const_iterator iter = BondsToRemove.begin();
|
---|
236 | iter != BondsToRemove.end();
|
---|
237 | ++iter) {
|
---|
238 | emit BondsRemoved(iter->first, iter->second);
|
---|
239 | }
|
---|
240 | ListOfBonds = ListOfBonds_new;
|
---|
241 | }
|
---|
242 |
|
---|
243 | void GLMoleculeObject_atom::resetSelectionStatus()
|
---|
244 | {
|
---|
245 | setSelected(AtomSelectionStatus.get());
|
---|
246 | }
|
---|
247 |
|
---|
248 | void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
249 | {
|
---|
250 | // call old hook to do the actual paining
|
---|
251 | GLMoleculeObject::draw(painter, cameraPlane);
|
---|
252 | }
|
---|
253 |
|
---|
254 | void GLMoleculeObject_atom::wasClicked()
|
---|
255 | {
|
---|
256 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << AtomIndex.get() << " has been clicked");
|
---|
257 | emit clicked(AtomIndex.get());
|
---|
258 | }
|
---|
259 |
|
---|
260 | const atom * const GLMoleculeObject_atom::getAtomConst(const atomId_t _id)
|
---|
261 | {
|
---|
262 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
263 | getAtom(AtomById(_id));
|
---|
264 | return _atom;
|
---|
265 | }
|
---|
266 |
|
---|
267 | atom * const GLMoleculeObject_atom::getAtom(const atomId_t _id)
|
---|
268 | {
|
---|
269 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
270 | return _atom;
|
---|
271 | }
|
---|
272 |
|
---|
273 | atomId_t GLMoleculeObject_atom::updateIndex() const
|
---|
274 | {
|
---|
275 | const atom * const _atom = World::getInstance().lastChanged<atom>();
|
---|
276 | if (_atom != NULL) {
|
---|
277 | return _atom->getId();
|
---|
278 | }
|
---|
279 | return (atomId_t)-1;
|
---|
280 | }
|
---|
281 |
|
---|
282 | Vector GLMoleculeObject_atom::updatePosition() const
|
---|
283 | {
|
---|
284 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
285 | if (_atom != NULL) {
|
---|
286 | return _atom->getPosition();
|
---|
287 | } else {
|
---|
288 | return zeroVec;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | atomicNumber_t GLMoleculeObject_atom::updateElement() const
|
---|
293 | {
|
---|
294 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
295 | if (_atom != NULL) {
|
---|
296 | return _atom->getElementNo();
|
---|
297 | } else {
|
---|
298 | return (atomicNumber_t)-1;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::updateBonds() const
|
---|
303 | {
|
---|
304 | ListOfBonds_t ListOfBonds;
|
---|
305 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
306 | if (_atom != NULL) {
|
---|
307 | // make sure position is up-to-date
|
---|
308 | const BondList ListBonds = _atom->getListOfBonds();
|
---|
309 | for (BondList::const_iterator iter = ListBonds.begin();
|
---|
310 | iter != ListBonds.end();
|
---|
311 | ++iter)
|
---|
312 | ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
|
---|
313 | (*iter)->leftatom->getId(),
|
---|
314 | (*iter)->rightatom->getId()) );
|
---|
315 | } else {
|
---|
316 | ELOG(2, "Atom with id "+toString(AtomIndex.get())+" is already gone.");
|
---|
317 | }
|
---|
318 | return ListOfBonds;
|
---|
319 | }
|
---|
320 |
|
---|
321 | bool GLMoleculeObject_atom::updateSelectionStatus() const
|
---|
322 | {
|
---|
323 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
324 | if (_atom != NULL) {
|
---|
325 | return const_cast<const World &>(World::getInstance()).isSelected(_atom);
|
---|
326 | }
|
---|
327 | return false;
|
---|
328 | }
|
---|
329 |
|
---|
330 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
331 | {
|
---|
332 | ASSERT(0, "GLMoleculeObject_atom::update() - we are not signed on for global updates.");
|
---|
333 | }
|
---|
334 |
|
---|
335 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
336 | {
|
---|
337 | deactivateObserver();
|
---|
338 | }
|
---|
339 |
|
---|
340 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
341 | {
|
---|
342 | // ObservedValues have been updated before, hence convert updates to Qt's signals
|
---|
343 | atom * const _atom = dynamic_cast<atom *>(publisher);
|
---|
344 | if (_atom != NULL) {
|
---|
345 | switch (notification->getChannelNo()) {
|
---|
346 | case AtomObservable::IndexChanged:
|
---|
347 | emit idChanged();
|
---|
348 | break;
|
---|
349 | case AtomObservable::PositionChanged:
|
---|
350 | emit positionChanged();
|
---|
351 | break;
|
---|
352 | case AtomObservable::ElementChanged:
|
---|
353 | emit elementChanged();
|
---|
354 | break;
|
---|
355 | case AtomObservable::BondsAdded:
|
---|
356 | case AtomObservable::BondsRemoved:
|
---|
357 | emit bondsChanged();
|
---|
358 | break;
|
---|
359 | default:
|
---|
360 | ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
|
---|
361 | +toString(notification->getChannelNo())+" of the atom.");
|
---|
362 | break;
|
---|
363 | }
|
---|
364 | } else if (publisher == static_cast<World *>(World::getPointer())) {
|
---|
365 | switch (notification->getChannelNo()) {
|
---|
366 | case World::SelectionChanged:
|
---|
367 | emit selectionstatusChanged();
|
---|
368 | break;
|
---|
369 | default:
|
---|
370 | ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
|
---|
371 | +toString(notification->getChannelNo())+" of the World.");
|
---|
372 | break;
|
---|
373 | }
|
---|
374 | }
|
---|
375 | }
|
---|