source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.cpp@ 16bd37

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 16bd37 was 16bd37, checked in by Frederik Heber <heber@…>, 9 years ago

GLMoleculeObject_atom's ObservedValues is now all static.

  • this is equivalent to the changes to GLMoleculeObject_molecule.
  • Property mode set to 100644
File size: 14.4 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#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#include "ObservedValue_wCallback.hpp"
60
61using namespace boost::assign;
62
63static const Observable::channels_t getAtomBondsChannels()
64{
65 Observable::channels_t channels;
66 channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
67 return channels;
68}
69
70// static entities
71const Observable::channels_t
72GLMoleculeObject_atom::AtomIndexChannels(1, AtomObservable::IndexChanged);
73const Observable::channels_t
74GLMoleculeObject_atom::AtomPositionChannels(1, AtomObservable::PositionChanged);
75const Observable::channels_t
76GLMoleculeObject_atom::AtomElementChannels(1, AtomObservable::ElementChanged);
77const Observable::channels_t
78GLMoleculeObject_atom::AtomBondsChannels(getAtomBondsChannels());
79
80GLMoleculeObject_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 ObservedValues(MAX_ObservedTypes),
85 subjectKilledCount(0),
86 owner(NULL)
87{
88 boost::function<void()> subjectKilled =
89 boost::bind(&GLMoleculeObject_atom::countsubjectKilled, this);
90 initObservedValues(ObservedValues, _id, atomref, subjectKilled);
91
92 setObjectId(_id);
93 resetPosition();
94 resetElement();
95
96 m_selected = const_cast<const World &>(World::getInstance()).isAtomSelected(_id);
97
98 // sign On
99 activateObserver();
100
101 // atomref is only used for caching the ref, it must be used elswhere
102 const_cast<atom *&>(atomref) = NULL;
103
104 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
105 connect( this, SIGNAL(idChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
106 connect( this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
107 connect( this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
108 connect( this, SIGNAL(bondsChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
109}
110
111void GLMoleculeObject_atom::activateObserver()
112{
113 if (atomref != NULL) {
114 owner = static_cast<const Observable *>(atomref);
115 owner->signOn(this, AtomObservable::IndexChanged);
116 owner->signOn(this, AtomObservable::PositionChanged);
117 owner->signOn(this, AtomObservable::ElementChanged);
118 owner->signOn(this, AtomObservable::BondsAdded);
119 owner->signOn(this, AtomObservable::BondsRemoved);
120 }
121}
122
123
124void GLMoleculeObject_atom::deactivateObserver()
125{
126 // sign Off
127 if (owner != NULL) {
128 owner->signOff(this, AtomObservable::IndexChanged);
129 owner->signOff(this, AtomObservable::PositionChanged);
130 owner->signOff(this, AtomObservable::ElementChanged);
131 owner->signOff(this, AtomObservable::BondsAdded);
132 owner->signOff(this, AtomObservable::BondsRemoved);
133 owner = NULL;
134 }
135}
136
137GLMoleculeObject_atom::~GLMoleculeObject_atom()
138{
139 deactivateObserver();
140 destroyObservedValues(ObservedValues);
141}
142
143void GLMoleculeObject_atom::resetIndex()
144{
145 const atomId_t newId = getAtomIndex();
146 const size_t oldId = objectId();
147 ASSERT( newId != oldId,
148 "GLMoleculeObject_atom::updateIndex() - index "+toString(newId)+" did not change.");
149 LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(newId)+".");
150 setObjectId(newId);
151
152 emit indexChanged(this, oldId, newId);
153}
154
155void GLMoleculeObject_atom::resetPosition()
156{
157 const Vector Position = getAtomPosition();
158 LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
159 setPosition(QVector3D(Position[0], Position[1], Position[2]));
160}
161
162void GLMoleculeObject_atom::resetElement()
163{
164 size_t elementno = 0;
165 const element * const _type = World::getInstance().
166 getPeriode()->FindElement(getAtomElement());
167 if (_type != NULL) {
168 elementno = _type->getAtomicNumber();
169 } else { // if no element yet, set to hydrogen
170 elementno = 1;
171 }
172 LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
173
174 // set materials
175 QGLMaterial *elementmaterial = getMaterial(elementno);
176 ASSERT(elementmaterial != NULL,
177 "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
178 setMaterial(elementmaterial);
179
180 // set scale
181 double radius = 0.;
182 if (_type != NULL) {
183 radius = _type->getVanDerWaalsRadius();
184 } else {
185 radius = 0.5;
186 }
187 setScale( radius / 4. );
188}
189
190void GLMoleculeObject_atom::resetBonds()
191{
192 ListOfBonds_t ListOfBonds_new = getAtomBonds();
193 std::sort(ListOfBonds_new.begin(), ListOfBonds_new.end());
194 ListOfBonds_t BondsToAdd;
195 std::set_difference(
196 ListOfBonds_new.begin(), ListOfBonds_new.end(),
197 ListOfBonds.begin(), ListOfBonds.end(),
198 std::back_inserter(BondsToAdd));
199 ListOfBonds_t BondsToRemove;
200 std::set_difference(
201 ListOfBonds.begin(), ListOfBonds.end(),
202 ListOfBonds_new.begin(), ListOfBonds_new.end(),
203 std::back_inserter(BondsToRemove));
204 for (ListOfBonds_t::const_iterator iter = BondsToAdd.begin();
205 iter != BondsToAdd.end();
206 ++iter) {
207 const GLMoleculeObject_bond::SideOfBond side = (iter->first == getAtomIndex()) ?
208 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
209 emit BondsAdded(iter->first, iter->second, side);
210 }
211 for (ListOfBonds_t::const_iterator iter = BondsToRemove.begin();
212 iter != BondsToRemove.end();
213 ++iter) {
214 emit BondsRemoved(iter->first, iter->second);
215 }
216 ListOfBonds = ListOfBonds_new;
217}
218
219void GLMoleculeObject_atom::Selected()
220{
221 ASSERT( !m_selected,
222 "GLMoleculeObject_atom::Selected() - 3D rep of atom is already selected.");
223 m_selected = true;
224
225 emit changed();
226}
227
228void GLMoleculeObject_atom::Unselected()
229{
230 ASSERT( m_selected,
231 "GLMoleculeObject_atom::Unselected() - 3D rep of atom is already unselected.");
232 m_selected = false;
233
234 emit changed();
235}
236
237
238void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
239{
240 // call old hook to do the actual paining
241 GLMoleculeObject::draw(painter, cameraPlane);
242}
243
244void GLMoleculeObject_atom::wasClicked()
245{
246 LOG(4, "INFO: GLMoleculeObject_atom: atom " << getAtomIndex() << " has been clicked");
247 emit clicked(getAtomIndex());
248}
249
250const atom * const GLMoleculeObject_atom::getAtomConst(const atomId_t _id)
251{
252 const atom * const _atom = const_cast<const World &>(World::getInstance()).
253 getAtom(AtomById(_id));
254 return _atom;
255}
256
257atom * const GLMoleculeObject_atom::getAtom(const atomId_t _id)
258{
259 atom * const _atom = World::getInstance().getAtom(AtomById(_id));
260 return _atom;
261}
262
263atomId_t GLMoleculeObject_atom::updateIndex()
264{
265 return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
266}
267
268Vector GLMoleculeObject_atom::updatePosition(
269 const boost::function<const atomId_t ()> &_getAtomIndex)
270{
271 const atom * const _atom = getAtomConst(_getAtomIndex());
272 if (_atom != NULL) {
273 return _atom->getPosition();
274 } else {
275 return zeroVec;
276 }
277}
278
279atomicNumber_t GLMoleculeObject_atom::updateElement(
280 const boost::function<const atomId_t ()> &_getAtomIndex)
281{
282 const atom * const _atom = getAtomConst(_getAtomIndex());
283 if (_atom != NULL) {
284 return _atom->getElementNo();
285 } else {
286 return (atomicNumber_t)-1;
287 }
288}
289
290GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::updateBonds(
291 const boost::function<const atomId_t ()> &_getAtomIndex)
292{
293 ListOfBonds_t ListOfBonds;
294 const atom * const _atom = getAtomConst(_getAtomIndex());
295 if (_atom != NULL) {
296 // make sure bonds is up-to-date
297 const BondList ListBonds = _atom->getListOfBonds();
298 for (BondList::const_iterator iter = ListBonds.begin();
299 iter != ListBonds.end();
300 ++iter)
301 ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
302 (*iter)->leftatom->getId(),
303 (*iter)->rightatom->getId()) );
304 } else {
305 ELOG(2, "Atom with id "+toString(_getAtomIndex())+" is already gone.");
306 }
307 return ListOfBonds;
308}
309
310void GLMoleculeObject_atom::update(Observable *publisher)
311{
312 ASSERT(0, "GLMoleculeObject_atom::update() - we are not signed on for global updates.");
313}
314
315void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
316{
317 deactivateObserver();
318
319 countsubjectKilled();
320}
321
322void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
323{
324 // ObservedValues have been updated before, hence convert updates to Qt's signals
325 atom * const _atom = dynamic_cast<atom *>(publisher);
326 if (_atom != NULL) {
327 switch (notification->getChannelNo()) {
328 case AtomObservable::IndexChanged:
329 emit idChanged();
330 break;
331 case AtomObservable::PositionChanged:
332 emit positionChanged();
333 break;
334 case AtomObservable::ElementChanged:
335 emit elementChanged();
336 break;
337 case AtomObservable::BondsAdded:
338 case AtomObservable::BondsRemoved:
339 emit bondsChanged();
340 break;
341 default:
342 ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
343 +toString(notification->getChannelNo())+" of the atom.");
344 break;
345 }
346 } else
347 ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - received notification from unexpected source.");
348}
349
350void GLMoleculeObject_atom::countsubjectKilled()
351{
352 ++subjectKilledCount;
353
354 if (subjectKilledCount > ObservedValues.size())
355 emit InstanceRemoved(getAtomIndex());
356}
357
358void GLMoleculeObject_atom::initObservedValues(
359 std::vector<boost::any> &_ObservedValues,
360 const atomId_t _id,
361 const atom * const _atomref,
362 const boost::function<void()> &_subjectKilled)
363{
364 /* This is an old note from when the code was still part of cstor's initializer body.
365 * TODO: Probably does not apply anymore but has not yet been tested.
366 *
367 * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
368 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
369 * the class has not been fully constructed yet. "This" itself seems to be working fine.
370 */
371
372 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
373 "GLMoleculeObject_atom::initObservedValues() - given ObservedValues has not correct size.");
374
375 // fill ObservedValues: index first
376 const boost::function<atomId_t ()> AtomIndexUpdater(
377 boost::bind(&GLMoleculeObject_atom::updateIndex));
378
379 ObservedValue_wCallback<atomId_t> * const IndexObservable =
380 new ObservedValue_wCallback<atomId_t>(
381 _atomref,
382 boost::bind(&GLMoleculeObject_atom::updateIndex),
383 "AtomIndex_"+toString(_id),
384 _id,
385 AtomIndexChannels,
386 _subjectKilled);
387 _ObservedValues[AtomIndex] = IndexObservable;
388
389 const boost::function<const atomId_t ()> AtomIndexGetter =
390 boost::bind(&ObservedValue_wCallback<atomId_t>::get,
391 IndexObservable);
392
393 // fill ObservedValues: then all the other that need index
394 const boost::function<Vector ()> AtomPositionUpdater(
395 boost::bind(&GLMoleculeObject_atom::updatePosition, AtomIndexGetter));
396 const boost::function<atomicNumber_t ()> AtomElementUpdater(
397 boost::bind(&GLMoleculeObject_atom::updateElement, AtomIndexGetter));
398 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
399 boost::bind(&GLMoleculeObject_atom::updateBonds, AtomIndexGetter));
400
401 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector>(
402 _atomref,
403 AtomPositionUpdater,
404 "AtomPosition_"+toString(_id),
405 AtomPositionUpdater(),
406 AtomPositionChannels,
407 _subjectKilled);
408 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t>(
409 _atomref,
410 AtomElementUpdater,
411 "AtomElement"+toString(_id),
412 AtomElementUpdater(),
413 AtomElementChannels,
414 _subjectKilled);
415 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t>(
416 _atomref,
417 AtomBondsUpdater,
418 "AtomBonds_"+toString(_id),
419 AtomBondsUpdater(),
420 AtomBondsChannels,
421 _subjectKilled);
422}
423
424void GLMoleculeObject_atom::destroyObservedValues(
425 std::vector<boost::any> &_ObservedValues)
426{
427 delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[AtomIndex]);
428 delete boost::any_cast<ObservedValue_wCallback<Vector> *>(_ObservedValues[AtomPosition]);
429 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t> *>(_ObservedValues[AtomElement]);
430 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t> *>(_ObservedValues[AtomBonds]);
431 _ObservedValues.clear();
432}
433
434atomId_t GLMoleculeObject_atom::getAtomIndex() const
435{
436 return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
437}
438
439Vector GLMoleculeObject_atom::getAtomPosition() const
440{
441 return boost::any_cast<ObservedValue_wCallback<Vector> *>(ObservedValues[AtomPosition])->get();
442}
443
444atomicNumber_t GLMoleculeObject_atom::getAtomElement() const
445{
446 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t> *>(ObservedValues[AtomElement])->get();
447}
448
449GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::getAtomBonds() const
450{
451 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t> *>(ObservedValues[AtomBonds])->get();
452}
Note: See TracBrowser for help on using the repository browser.