source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_bond.cpp@ a58c16

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 a58c16 was f01769, checked in by Frederik Heber <heber@…>, 10 years ago

Replaced World::getAtom() wherever possible by const version.

  • some AtomSet member functions now have const atom ptr instead of atom ptr.
  • molecule can return const and non-const AtomSet.
  • added FromIdToConstAtom to allow iterate through atoms in molecule (which are stored by id, not by ptr) in const fashion.
  • in molecule::isInMolecule() is now const, ::CopyMolecule..() is non-const (because copying involves father atom who is stored non-const).
  • Property mode set to 100644
File size: 13.3 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_bond.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_bond.hpp"
37
38#include <Qt3D/qglmaterial.h>
39#include <Qt3D/qglscenenode.h>
40
41#include "CodePatterns/MemDebug.hpp"
42
[856d05]43
[907636]44#include <cmath>
45
46#include "CodePatterns/Assert.hpp"
[57a770]47#include "CodePatterns/Log.hpp"
[2ad1ec]48#include "CodePatterns/Observer/Notification.hpp"
[856d05]49#include "CodePatterns/Observer/ObserverLog.hpp"
[c60665]50#include "Descriptors/AtomIdDescriptor.hpp"
[6f0841]51#include "Atom/atom.hpp"
[907636]52#include "Bond/bond.hpp"
[3bdb6d]53#include "Element/element.hpp"
[907636]54#include "Helpers/defs.hpp"
55#include "LinearAlgebra/Line.hpp"
56#include "LinearAlgebra/Vector.hpp"
[c60665]57#include "World.hpp"
[907636]58
[88c8ec]59GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
[bca99d]60 GLMoleculeObject(mesh, parent),
[2ad1ec]61 Observer(std::string("GLMoleculeObject_bond")
62 +toString(bondref->leftatom->getId())
63 +std::string("-")
64 +toString(bondref->rightatom->getId())),
[70db8f]65 _bond(*bondref),
[c60665]66 leftobservable(bondref->leftatom),
67 rightobservable(bondref->rightatom),
[70db8f]68 leftatomId(bondref->leftatom->getId()),
69 rightatomId(bondref->rightatom->getId()),
[c60665]70 BondSide(side),
71 leftobservable_enabled(false),
72 rightobservable_enabled(false),
73 bond_enabled(false)
[907636]74{
[2ad1ec]75 // sign on as observer (obtain non-const instance before)
[c60665]76 _bond.signOn(this);
[70db8f]77 _bond.signOn(this, BondObservable::BondRemoved);
[4b62d3]78 _bond.signOn(this, BondObservable::DegreeChanged);
[c60665]79 bond_enabled = true;
80 leftobservable->signOn(this);
81 leftobservable->signOn(this, AtomObservable::PositionChanged);
82 leftobservable->signOn(this, AtomObservable::ElementChanged);
83 leftobservable_enabled = true;
84 rightobservable->signOn(this);
85 rightobservable->signOn(this, AtomObservable::PositionChanged);
86 rightobservable->signOn(this, AtomObservable::ElementChanged);
87 rightobservable_enabled = true;
[2ad1ec]88
[907636]89 size_t elementno = 0;
[2ad1ec]90 switch (BondSide) {
[907636]91 case left:
[c60665]92 {
[f01769]93 const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
94 getAtom(AtomById(rightatomId));
[c60665]95 if (_rightatom->getType() != NULL) {
96 elementno = _rightatom->getType()->getAtomicNumber();
[7188b1]97 } else { // if not element yet set, set to hydrogen
98 elementno = 1;
99 }
[907636]100 break;
[c60665]101 }
[907636]102 case right:
[c60665]103 {
[f01769]104 const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
105 getAtom(AtomById(leftatomId));
[c60665]106 if (_leftatom->getType() != NULL) {
107 elementno = _leftatom->getType()->getAtomicNumber();
[7188b1]108 } else { // if not element yet set, set to hydrogen
109 elementno = 1;
110 }
111
[907636]112 break;
[c60665]113 }
[907636]114 default:
115 ASSERT(0,
[2ad1ec]116 "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
[907636]117 break;
118 }
119
120 QGLMaterial *elementmaterial = getMaterial(elementno);
121 setMaterial(elementmaterial);
122
[343a4b]123 resetPosition();
[4b62d3]124 resetWidth();
[907636]125}
[2ad1ec]126
127GLMoleculeObject_bond::~GLMoleculeObject_bond()
[c60665]128{
129 LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
130 // signOff() if not already done
131 removeLeftAtom();
132 removeRightAtom();
133 removeBond();
134 removeChannels();
135}
136
137void GLMoleculeObject_bond::removeLeftAtom()
138{
139 // at this point both atoms should still be alive, hence we may safely sign off
140 // from the AtomObservable itself
141 if (leftobservable_enabled) {
[f01769]142 const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
143 getAtom(AtomById(leftatomId));
[c60665]144 if (_leftatom != NULL) {
145 _leftatom->signOff(this);
146 } else {
147 ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
148 }
149 }
150}
151
152void GLMoleculeObject_bond::removeRightAtom()
153{
154 // at this point both atoms should still be alive, hence we may safely sign off
155 // from the AtomObservable itself
156 if (rightobservable_enabled) {
[f01769]157 const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
158 getAtom(AtomById(rightatomId));
[c60665]159 if (_rightatom != NULL) {
160 _rightatom->signOff(this);
161 } else {
162 ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
163 }
164 }
165}
166
167void GLMoleculeObject_bond::removeBond()
168{
169 if (bond_enabled)
170 _bond.signOff(this);
171}
172
173void GLMoleculeObject_bond::removeChannels()
174{
175 // at this point both atoms should still be alive, hence we may safely sign off
176 // from the AtomObservable itself
177 if (leftobservable_enabled) {
[f01769]178 const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
179 getAtom(AtomById(leftatomId));
[c60665]180 if (_leftatom != NULL) {
181 _leftatom->signOff(this, AtomObservable::PositionChanged);
182 _leftatom->signOff(this, AtomObservable::ElementChanged);
183 } else {
184 ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
185 }
186 leftobservable_enabled = false;
187 }
188 if (rightobservable_enabled) {
[f01769]189 const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
190 getAtom(AtomById(rightatomId));
[c60665]191 if (_rightatom != NULL) {
192 _rightatom->signOff(this, AtomObservable::PositionChanged);
193 _rightatom->signOff(this, AtomObservable::ElementChanged);
194 } else {
195 ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
196 }
197 rightobservable_enabled = false;
198 }
199 if (bond_enabled) {
200 _bond.signOff(this, BondObservable::BondRemoved);
201 _bond.signOff(this, BondObservable::DegreeChanged);
202 bond_enabled = false;
203 }
204}
205
206
207void GLMoleculeObject_bond::removeMe()
[2ad1ec]208{
[70db8f]209 // sign off
210 switch (BondSide) {
211 case left:
212 emit BondRemoved(leftatomId, rightatomId);
213 break;
214 case right:
215 emit BondRemoved(rightatomId, leftatomId);
216 break;
217 default:
218 ASSERT(0,
219 "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
220 +toString(BondSide)+".");
221 break;
[49c965]222 }
[2ad1ec]223}
224
225void GLMoleculeObject_bond::update(Observable *publisher)
226{
227#ifdef LOG_OBSERVER
[70db8f]228 if (publisher == static_cast<const Observable *>(&_bond)) {
229 observerLog().addMessage() << "++ Update of Observer "
230 << observerLog().getName(static_cast<Observer*>(this))
231 << " from bond.";
[c60665]232 } else if (publisher == leftobservable) {
[70db8f]233 observerLog().addMessage() << "++ Update of Observer "
234 << observerLog().getName(static_cast<Observer*>(this))
[c60665]235 << " from leftatom " << leftatomId << ".";
236 } else if (publisher == rightobservable) {
[70db8f]237 observerLog().addMessage() << "++ Update of Observer " <<
238 observerLog().getName(static_cast<Observer*>(this))
[c60665]239 << " from rightatom " << rightatomId << ".";
[70db8f]240 } else
241 observerLog().addMessage() << "++ Update of Observer " <<
242 observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
[2ad1ec]243#endif
244}
245
246void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
247{
[c60665]248 // assume subjectKilled() is from Observable's own subjectKilled(), not notifications
249 // but we must always signOff from all other sources!
250 if (publisher == static_cast<const Observable *>(&_bond)) {
251#ifdef LOG_OBSERVER
252 observerLog().addMessage() << "++ subjectKilled of Observer "
253 << observerLog().getName(static_cast<Observer*>(this))
254 << " from bond.";
255#endif
256 removeLeftAtom();
257 removeRightAtom();
258 } else if (publisher == leftobservable) {
259#ifdef LOG_OBSERVER
260 observerLog().addMessage() << "++ subjectKilled of Observer "
261 << observerLog().getName(static_cast<Observer*>(this))
262 << " from leftatom " << leftatomId << ".";
263#endif
264 removeRightAtom();
265 removeBond();
266 } else if (publisher == rightobservable) {
267#ifdef LOG_OBSERVER
268 observerLog().addMessage() << "++ subjectKilled of Observer " <<
269 observerLog().getName(static_cast<Observer*>(this))
270 << " from rightatom " << rightatomId << ".";
271#endif
272 removeLeftAtom();
273 removeBond();
274 } else {
275#ifdef LOG_OBSERVER
276 observerLog().addMessage() << "++ subjectKilled of Observer " <<
277 observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
278#endif
279 }
280 // then indicate to remove us
281 removeChannels();
282 removeMe();
[2ad1ec]283}
284
285void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
286{
287#ifdef LOG_OBSERVER
[70db8f]288 if (publisher == static_cast<const Observable *>(&_bond)) {
289 observerLog().addMessage() << "++ Update of Observer "
290 << observerLog().getName(static_cast<Observer*>(this))
291 << " received notification from bond for channel "
292 << notification->getChannelNo() << ".";
[c60665]293 } else if (publisher == leftobservable) {
[70db8f]294 observerLog().addMessage() << "++ Update of Observer "
295 << observerLog().getName(static_cast<Observer*>(this))
[c60665]296 << " received notification from leftatom " << leftatomId << " for channel "
[70db8f]297 << notification->getChannelNo() << ".";
[c60665]298 } else if (publisher == rightobservable) {
[70db8f]299 observerLog().addMessage() << "++ Update of Observer "
300 << observerLog().getName(static_cast<Observer*>(this))
[c60665]301 << " received notification from rightatom " << rightatomId << " for channel "
[70db8f]302 << notification->getChannelNo() << ".";
303 } else
304 observerLog().addMessage() << "++ Update of Observer "
305 << observerLog().getName(static_cast<Observer*>(this))
306 << " received notification from unknown source.";
[2ad1ec]307#endif
[4b62d3]308 bool DoResetPosition = false;
309 bool DoResetWidth = false;
[70db8f]310 if (publisher == static_cast<const Observable *>(&_bond)){
[1f693d]311 switch (notification->getChannelNo()) {
312 case BondObservable::BondRemoved:
[c60665]313// removeMe();
[1f693d]314 break;
[4b62d3]315 case BondObservable::DegreeChanged:
316 DoResetWidth = true;
317 break;
318 default:
319 ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
320 break;
[1f693d]321 }
[70db8f]322 } else {
[343a4b]323 // from an atom
324 switch (notification->getChannelNo()) {
[70db8f]325 case AtomObservable::PositionChanged:
326 LOG(2, "INFO: Received notification of PositionChanged.");
327 DoResetPosition = true;
328 break;
329 case AtomObservable::ElementChanged:
330 LOG(2, "INFO: Received notification of ElementChanged.");
331 DoResetPosition = true;
332 break;
333 default:
334 break;
335 }
[343a4b]336 }
[f47efd4]337 if (DoResetPosition)
[4b62d3]338 resetPosition();
[f47efd4]339 if (DoResetWidth)
[4b62d3]340 resetWidth();
[f47efd4]341 if ((DoResetPosition) || (DoResetWidth))
[4b62d3]342 emit changed();
343}
344
345void GLMoleculeObject_bond::resetWidth()
346{
347 const double factor = 1.0f+.5f*(_bond.getDegree()-1);
348 LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
349 setScaleX(factor);
350 setScaleY(factor);
[343a4b]351}
352
353void GLMoleculeObject_bond::resetPosition()
354{
355 Vector Position;
356 Vector OtherPosition;
[f01769]357 const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
358 getAtom(AtomById(leftatomId));
[c60665]359 Vector LeftPos = _leftatom->getPosition();
[f01769]360 const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
361 getAtom(AtomById(rightatomId));
[c60665]362 Vector RightPos = _rightatom->getPosition();
[343a4b]363 switch (BondSide) {
364 case left:
[c60665]365 Position = LeftPos;
366 OtherPosition = RightPos;
[343a4b]367 break;
368 case right:
[c60665]369 Position = RightPos;
370 OtherPosition = LeftPos;
[2ad1ec]371 break;
372 default:
[343a4b]373 ASSERT(0,
374 "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
[2ad1ec]375 break;
376 }
[fbb1f1]377 const double distance =
378 Position.distance(OtherPosition)/2.;
379 setScaleZ(distance);
[343a4b]380
381 // calculate position
[37e910]382 Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
[343a4b]383 Vector zeroVec(0.,0.,0.);
384 Vector a,b;
385 Vector OtherAxis;
386 double alpha;
387 a = Position - OtherPosition;
388 // construct rotation axis
389 b = a;
390 b.VectorProduct(Z);
391 Line axis(zeroVec, b);
392 // calculate rotation angle
393 alpha = a.Angle(Z);
394 // construct other axis to check right-hand rule
395 OtherAxis = b;
396 OtherAxis.VectorProduct(Z);
397 // assure right-hand rule for the rotation
398 if (a.ScalarProduct(OtherAxis) < MYEPSILON)
399 alpha = M_PI-alpha;
400 // check
401 Vector a_rotated = axis.rotateVector(a, alpha);
402 LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
403 << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
404
[37e910]405 // set position (cylinder offset is in its barymetric center)
406 Vector OneFourth(Position - 0.75 * a);
407 setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
[343a4b]408 setRotationVector(QVector3D(b[0], b[1], b[2]));
409 setRotationAngle(alpha/M_PI*180.);
[2ad1ec]410}
Note: See TracBrowser for help on using the repository browser.