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

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 49c965 was 49c965, checked in by Michael Ankele <ankele@…>, 13 years ago

Fix: GLMoleculeObject_bond must not signOff after subjectKilled

  • Property mode set to 100644
File size: 6.8 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 * 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
39GLMoleculeObject_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
82GLMoleculeObject_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
95void 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
102void 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
125void 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
170void 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}
Note: See TracBrowser for help on using the repository browser.