source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.cpp@ 98c35c

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 98c35c was 2f7988, checked in by Frederik Heber <heber@…>, 9 years ago

Moved and renamed QtInstanceInformationBoard -> QtObservedInstanceBoard.

  • Property mode set to 100644
File size: 15.0 KB
RevLine 
[907636]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]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/>.
[907636]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"
[7188b1]43#include "CodePatterns/Log.hpp"
[02ce36]44#include "CodePatterns/Observer/Notification.hpp"
[907636]45
[534374]46#include <algorithm>
47#include <boost/assign.hpp>
48
[2ad1ec]49#include "Atom/atom.hpp"
50#include "Bond/bond.hpp"
[7188b1]51#include "Descriptors/AtomIdDescriptor.hpp"
[3bdb6d]52#include "Element/element.hpp"
[534374]53#include "Element/periodentafel.hpp"
[907636]54#include "LinearAlgebra/Vector.hpp"
[8d5fbf1]55#include "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_bond.hpp"
[2f7988]56#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
[7188b1]57#include "World.hpp"
[89b992]58#include "WorldTime.hpp"
[907636]59
[7c7c4a]60#include "ObservedValue_wCallback.hpp"
61
[534374]62using namespace boost::assign;
63
64static const Observable::channels_t getAtomBondsChannels()
65{
66 Observable::channels_t channels;
67 channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
68 return channels;
69}
70
[8d5fbf1]71static const Observable::channels_t getAllObservedChannels()
72{
73 Observable::channels_t channels;
74 channels +=
75 AtomObservable::IndexChanged,
76 AtomObservable::PositionChanged,
77 AtomObservable::ElementChanged,
78 AtomObservable::BondsAdded,
79 AtomObservable::BondsRemoved;
80 return channels;
81}
82
[534374]83// static entities
84const Observable::channels_t
85GLMoleculeObject_atom::AtomIndexChannels(1, AtomObservable::IndexChanged);
86const Observable::channels_t
87GLMoleculeObject_atom::AtomPositionChannels(1, AtomObservable::PositionChanged);
88const Observable::channels_t
89GLMoleculeObject_atom::AtomElementChannels(1, AtomObservable::ElementChanged);
90const Observable::channels_t
91GLMoleculeObject_atom::AtomBondsChannels(getAtomBondsChannels());
92
[026bef]93GLMoleculeObject_atom::GLMoleculeObject_atom(
94 QGLSceneNode *mesh[],
95 QObject *parent,
96 const atomId_t _id,
[2f7988]97 QtObservedInstanceBoard * _board,
[026bef]98 std::vector<boost::any> _ObservedValues) :
99 GLMoleculeObject(mesh, parent),
100 Observer(std::string("GLMoleculeObject_atom")+toString(_id)),
101 atomref(getAtom(_id)),
102 ObservedValues(_ObservedValues),
103 subjectKilledCount(0),
[8d5fbf1]104 owner(NULL),
105 AllsignedOnChannels(getAllObservedChannels().size()),
106 signedOnChannels(0),
107 board(_board)
[026bef]108{
109 init(_id);
110}
111
112void GLMoleculeObject_atom::init(const atomId_t _id)
113{
[534374]114 setObjectId(_id);
115 resetPosition();
116 resetElement();
[7188b1]117
[a39d72]118 m_selected = const_cast<const World &>(World::getInstance()).isAtomSelected(_id);
119
[534374]120 // sign On
121 activateObserver();
[7188b1]122
[534374]123 // atomref is only used for caching the ref, it must be used elswhere
124 const_cast<atom *&>(atomref) = NULL;
[7188b1]125
126 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
[534374]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);
[8d5fbf1]131
132 // use that ObservedValues::AtomBonds is always up-to-date
133 resetBonds();
[7188b1]134}
135
[534374]136void GLMoleculeObject_atom::activateObserver()
[9c18e4]137{
[534374]138 if (atomref != NULL) {
[8d5fbf1]139 Observable::channels_t channels = getAllObservedChannels();
[534374]140 owner = static_cast<const Observable *>(atomref);
[8d5fbf1]141 for (Observable::channels_t::const_iterator iter = channels.begin();
142 iter != channels.end(); ++iter)
143 owner->signOn(this, *iter);
[c736fe]144 }
[9c18e4]145}
146
[534374]147
148void GLMoleculeObject_atom::deactivateObserver()
[7188b1]149{
[534374]150 // sign Off
151 if (owner != NULL) {
[8d5fbf1]152 Observable::channels_t channels = getAllObservedChannels();
153 for (Observable::channels_t::const_iterator iter = channels.begin();
154 iter != channels.end(); ++iter)
155 owner->signOff(this, *iter);
[534374]156 owner = NULL;
[8d5fbf1]157 signedOnChannels = 0;
[534374]158 }
159}
160
161GLMoleculeObject_atom::~GLMoleculeObject_atom()
162{
163 deactivateObserver();
[8d5fbf1]164 board->returnAtomObservedValues(getAtomIndex(), ObservedValues);
[534374]165}
166
167void GLMoleculeObject_atom::resetIndex()
168{
[7c7c4a]169 const atomId_t newId = getAtomIndex();
[534374]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);
[7188b1]177}
178
179void GLMoleculeObject_atom::resetPosition()
[907636]180{
[7c7c4a]181 const Vector Position = getAtomPosition();
[534374]182 LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
183 setPosition(QVector3D(Position[0], Position[1], Position[2]));
[7188b1]184}
185
186void GLMoleculeObject_atom::resetElement()
187{
188 size_t elementno = 0;
[534374]189 const element * const _type = World::getInstance().
[7c7c4a]190 getPeriode()->FindElement(getAtomElement());
[c60665]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. );
[534374]212}
[f115cc]213
[534374]214void GLMoleculeObject_atom::resetBonds()
215{
[7c7c4a]216 ListOfBonds_t ListOfBonds_new = getAtomBonds();
[534374]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) {
[7c7c4a]231 const GLMoleculeObject_bond::SideOfBond side = (iter->first == getAtomIndex()) ?
[534374]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;
[7188b1]241}
242
[015f8c]243void GLMoleculeObject_atom::Selected()
244{
245 ASSERT( !m_selected,
246 "GLMoleculeObject_atom::Selected() - 3D rep of atom is already selected.");
247 m_selected = true;
248
249 emit changed();
250}
251
252void GLMoleculeObject_atom::Unselected()
[7188b1]253{
[015f8c]254 ASSERT( m_selected,
255 "GLMoleculeObject_atom::Unselected() - 3D rep of atom is already unselected.");
256 m_selected = false;
257
258 emit changed();
[534374]259}
[30cd0d]260
[015f8c]261
[534374]262void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
263{
264 // call old hook to do the actual paining
265 GLMoleculeObject::draw(painter, cameraPlane);
[7188b1]266}
267
[534374]268void GLMoleculeObject_atom::wasClicked()
[7188b1]269{
[7c7c4a]270 LOG(4, "INFO: GLMoleculeObject_atom: atom " << getAtomIndex() << " has been clicked");
271 emit clicked(getAtomIndex());
[534374]272}
[907636]273
[534374]274const atom * const GLMoleculeObject_atom::getAtomConst(const atomId_t _id)
275{
276 const atom * const _atom = const_cast<const World &>(World::getInstance()).
277 getAtom(AtomById(_id));
278 return _atom;
279}
[907636]280
[534374]281atom * const GLMoleculeObject_atom::getAtom(const atomId_t _id)
282{
283 atom * const _atom = World::getInstance().getAtom(AtomById(_id));
284 return _atom;
285}
[d53902]286
[16bd37]287atomId_t GLMoleculeObject_atom::updateIndex()
[534374]288{
[54bdaa]289 return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
[7188b1]290}
[907636]291
[16bd37]292Vector GLMoleculeObject_atom::updatePosition(
293 const boost::function<const atomId_t ()> &_getAtomIndex)
[f115cc]294{
[16bd37]295 const atom * const _atom = getAtomConst(_getAtomIndex());
[534374]296 if (_atom != NULL) {
297 return _atom->getPosition();
298 } else {
299 return zeroVec;
300 }
301}
[f115cc]302
[16bd37]303atomicNumber_t GLMoleculeObject_atom::updateElement(
304 const boost::function<const atomId_t ()> &_getAtomIndex)
[534374]305{
[16bd37]306 const atom * const _atom = getAtomConst(_getAtomIndex());
[534374]307 if (_atom != NULL) {
308 return _atom->getElementNo();
309 } else {
310 return (atomicNumber_t)-1;
311 }
312}
313
[16bd37]314GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::updateBonds(
315 const boost::function<const atomId_t ()> &_getAtomIndex)
[534374]316{
317 ListOfBonds_t ListOfBonds;
[16bd37]318 const atom * const _atom = getAtomConst(_getAtomIndex());
[534374]319 if (_atom != NULL) {
[16bd37]320 // make sure bonds is up-to-date
[534374]321 const BondList ListBonds = _atom->getListOfBonds();
322 for (BondList::const_iterator iter = ListBonds.begin();
323 iter != ListBonds.end();
324 ++iter)
325 ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
326 (*iter)->leftatom->getId(),
327 (*iter)->rightatom->getId()) );
328 } else {
[16bd37]329 ELOG(2, "Atom with id "+toString(_getAtomIndex())+" is already gone.");
[534374]330 }
331 return ListOfBonds;
332}
333
334void GLMoleculeObject_atom::update(Observable *publisher)
335{
336 ASSERT(0, "GLMoleculeObject_atom::update() - we are not signed on for global updates.");
[f115cc]337}
338
[7188b1]339void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
[c736fe]340{
[8d5fbf1]341 ++signedOnChannels;
[7c7c4a]342
[8d5fbf1]343 if (signedOnChannels == AllsignedOnChannels) {
344 // remove owner: no more signOff needed
345 owner = NULL;
346
347 board->atomcountsubjectKilled(getAtomIndex());
348 }
[c736fe]349}
[06ebf5]350
[7188b1]351void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
352{
[534374]353 // ObservedValues have been updated before, hence convert updates to Qt's signals
354 atom * const _atom = dynamic_cast<atom *>(publisher);
355 if (_atom != NULL) {
[d53902]356 switch (notification->getChannelNo()) {
357 case AtomObservable::IndexChanged:
[534374]358 emit idChanged();
[d53902]359 break;
360 case AtomObservable::PositionChanged:
[534374]361 emit positionChanged();
362 break;
363 case AtomObservable::ElementChanged:
364 emit elementChanged();
[d53902]365 break;
366 case AtomObservable::BondsAdded:
[534374]367 case AtomObservable::BondsRemoved:
368 emit bondsChanged();
[917659]369 break;
[d53902]370 default:
[534374]371 ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
372 +toString(notification->getChannelNo())+" of the atom.");
[d53902]373 break;
374 }
[015f8c]375 } else
376 ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - received notification from unexpected source.");
[06ebf5]377}
[7c7c4a]378
[16bd37]379void GLMoleculeObject_atom::initObservedValues(
380 std::vector<boost::any> &_ObservedValues,
381 const atomId_t _id,
382 const atom * const _atomref,
[026bef]383 const boost::function<void(const atomId_t)> &_subjectKilled)
[7c7c4a]384{
[16bd37]385 /* This is an old note from when the code was still part of cstor's initializer body.
386 * TODO: Probably does not apply anymore but has not yet been tested.
387 *
388 * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
389 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
390 * the class has not been fully constructed yet. "This" itself seems to be working fine.
391 */
392
393 ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
394 "GLMoleculeObject_atom::initObservedValues() - given ObservedValues has not correct size.");
395
396 // fill ObservedValues: index first
397 const boost::function<atomId_t ()> AtomIndexUpdater(
398 boost::bind(&GLMoleculeObject_atom::updateIndex));
399
400 ObservedValue_wCallback<atomId_t> * const IndexObservable =
401 new ObservedValue_wCallback<atomId_t>(
402 _atomref,
403 boost::bind(&GLMoleculeObject_atom::updateIndex),
404 "AtomIndex_"+toString(_id),
405 _id,
406 AtomIndexChannels,
407 _subjectKilled);
408 _ObservedValues[AtomIndex] = IndexObservable;
409
410 const boost::function<const atomId_t ()> AtomIndexGetter =
411 boost::bind(&ObservedValue_wCallback<atomId_t>::get,
412 IndexObservable);
413
414 // fill ObservedValues: then all the other that need index
415 const boost::function<Vector ()> AtomPositionUpdater(
416 boost::bind(&GLMoleculeObject_atom::updatePosition, AtomIndexGetter));
417 const boost::function<atomicNumber_t ()> AtomElementUpdater(
418 boost::bind(&GLMoleculeObject_atom::updateElement, AtomIndexGetter));
419 const boost::function<ListOfBonds_t ()> AtomBondsUpdater(
420 boost::bind(&GLMoleculeObject_atom::updateBonds, AtomIndexGetter));
421
[026bef]422 _ObservedValues[AtomPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
[16bd37]423 _atomref,
424 AtomPositionUpdater,
[7c7c4a]425 "AtomPosition_"+toString(_id),
[16bd37]426 AtomPositionUpdater(),
[7c7c4a]427 AtomPositionChannels,
[026bef]428 _subjectKilled,
429 AtomIndexGetter);
430 _ObservedValues[AtomElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
[16bd37]431 _atomref,
432 AtomElementUpdater,
[7c7c4a]433 "AtomElement"+toString(_id),
[16bd37]434 AtomElementUpdater(),
[7c7c4a]435 AtomElementChannels,
[026bef]436 _subjectKilled,
437 AtomIndexGetter);
438 _ObservedValues[AtomBonds] = new ObservedValue_wCallback<ListOfBonds_t, atomId_t>(
[16bd37]439 _atomref,
440 AtomBondsUpdater,
[7c7c4a]441 "AtomBonds_"+toString(_id),
[16bd37]442 AtomBondsUpdater(),
[7c7c4a]443 AtomBondsChannels,
[026bef]444 _subjectKilled,
445 AtomIndexGetter);
[7c7c4a]446}
447
[16bd37]448void GLMoleculeObject_atom::destroyObservedValues(
449 std::vector<boost::any> &_ObservedValues)
[7c7c4a]450{
[16bd37]451 delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[AtomIndex]);
[026bef]452 delete boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(_ObservedValues[AtomPosition]);
453 delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(_ObservedValues[AtomElement]);
454 delete boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(_ObservedValues[AtomBonds]);
[16bd37]455 _ObservedValues.clear();
[7c7c4a]456}
457
458atomId_t GLMoleculeObject_atom::getAtomIndex() const
459{
460 return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[AtomIndex])->get();
461}
462
463Vector GLMoleculeObject_atom::getAtomPosition() const
464{
[026bef]465 return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[AtomPosition])->get();
[7c7c4a]466}
467
468atomicNumber_t GLMoleculeObject_atom::getAtomElement() const
469{
[026bef]470 return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[AtomElement])->get();
[7c7c4a]471}
472
473GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::getAtomBonds() const
474{
[026bef]475 return boost::any_cast<ObservedValue_wCallback<ListOfBonds_t, atomId_t> *>(ObservedValues[AtomBonds])->get();
[7c7c4a]476}
Note: See TracBrowser for help on using the repository browser.