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

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 70db8f was 70db8f, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Significantly simplified observer use in GLMoleculeObject_bond.

  • stored bond is now just reference. As if we store the bond::ptr, we prevent the bond from being free'd when removed.
  • we additionally store leftatom and rightatom as Observable to know when their notifications have arrived. We also store both ids for quick and safe BondRemoved signalling.
  • dstor now always calls signOff()s, elsewhere we just delete ourselves.
  • Property mode set to 100644
File size: 8.3 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 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * GLMoleculeObject_bond.cpp
25 *
26 * Created on: Aug 17, 2011
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35#include "GLMoleculeObject_bond.hpp"
36
37#include <Qt3D/qglmaterial.h>
38#include <Qt3D/qglscenenode.h>
39
40#include "CodePatterns/MemDebug.hpp"
41
42
43#include <cmath>
44
45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Log.hpp"
47#include "CodePatterns/Observer/Notification.hpp"
48#include "CodePatterns/Observer/ObserverLog.hpp"
49#include "Atom/atom.hpp"
50#include "Bond/bond.hpp"
51#include "Element/element.hpp"
52#include "Helpers/defs.hpp"
53#include "LinearAlgebra/Line.hpp"
54#include "LinearAlgebra/Vector.hpp"
55
56GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
57 GLMoleculeObject(mesh, parent),
58 Observer(std::string("GLMoleculeObject_bond")
59 +toString(bondref->leftatom->getId())
60 +std::string("-")
61 +toString(bondref->rightatom->getId())),
62 _bond(*bondref),
63 leftatom(bondref->leftatom),
64 rightatom(bondref->rightatom),
65 leftatomId(bondref->leftatom->getId()),
66 rightatomId(bondref->rightatom->getId()),
67 BondSide(side)
68{
69 // sign on as observer (obtain non-const instance before)
70 _bond.signOn(this, BondObservable::BondRemoved);
71 leftatom->signOn(this, AtomObservable::PositionChanged);
72 leftatom->signOn(this, AtomObservable::ElementChanged);
73 rightatom->signOn(this, AtomObservable::PositionChanged);
74 rightatom->signOn(this, AtomObservable::ElementChanged);
75
76 size_t elementno = 0;
77 switch (BondSide) {
78 case left:
79 if (_bond.rightatom->getType() != NULL) {
80 elementno = _bond.rightatom->getType()->getAtomicNumber();
81 } else { // if not element yet set, set to hydrogen
82 elementno = 1;
83 }
84 break;
85 case right:
86 if (_bond.leftatom->getType() != NULL) {
87 elementno = _bond.leftatom->getType()->getAtomicNumber();
88 } else { // if not element yet set, set to hydrogen
89 elementno = 1;
90 }
91
92 break;
93 default:
94 ASSERT(0,
95 "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
96 break;
97 }
98
99 QGLMaterial *elementmaterial = getMaterial(elementno);
100 setMaterial(elementmaterial);
101
102 resetPosition();
103}
104
105GLMoleculeObject_bond::~GLMoleculeObject_bond()
106{
107 // sign off
108 switch (BondSide) {
109 case left:
110 emit BondRemoved(leftatomId, rightatomId);
111 break;
112 case right:
113 emit BondRemoved(rightatomId, leftatomId);
114 break;
115 default:
116 ASSERT(0,
117 "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
118 +toString(BondSide)+".");
119 break;
120 }
121 _bond.signOff(this, BondObservable::BondRemoved);
122 leftatom->signOff(this, AtomObservable::PositionChanged);
123 leftatom->signOff(this, AtomObservable::ElementChanged);
124 rightatom->signOff(this, AtomObservable::PositionChanged);
125 rightatom->signOff(this, AtomObservable::ElementChanged);
126 LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
127}
128
129void GLMoleculeObject_bond::update(Observable *publisher)
130{
131#ifdef LOG_OBSERVER
132 if (publisher == static_cast<const Observable *>(&_bond)) {
133 observerLog().addMessage() << "++ Update of Observer "
134 << observerLog().getName(static_cast<Observer*>(this))
135 << " from bond.";
136 } else if (publisher == leftatom) {
137 observerLog().addMessage() << "++ Update of Observer "
138 << observerLog().getName(static_cast<Observer*>(this))
139 << " from leftatom " << _bond.leftatom->getId() << ".";
140 } else if (publisher == rightatom) {
141 observerLog().addMessage() << "++ Update of Observer " <<
142 observerLog().getName(static_cast<Observer*>(this))
143 << " from rightatom " << _bond.rightatom->getId() << ".";
144 } else
145 observerLog().addMessage() << "++ Update of Observer " <<
146 observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
147#endif
148}
149
150void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
151{
152 delete this;
153}
154
155void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
156{
157#ifdef LOG_OBSERVER
158 if (publisher == static_cast<const Observable *>(&_bond)) {
159 observerLog().addMessage() << "++ Update of Observer "
160 << observerLog().getName(static_cast<Observer*>(this))
161 << " received notification from bond for channel "
162 << notification->getChannelNo() << ".";
163 } else if (publisher == leftatom) {
164 observerLog().addMessage() << "++ Update of Observer "
165 << observerLog().getName(static_cast<Observer*>(this))
166 << " received notification from leftatom " << _bond.leftatom->getId() << " for channel "
167 << notification->getChannelNo() << ".";
168 } else if (publisher == rightatom) {
169 observerLog().addMessage() << "++ Update of Observer "
170 << observerLog().getName(static_cast<Observer*>(this))
171 << " received notification from rightatom " << _bond.rightatom->getId() << " for channel "
172 << notification->getChannelNo() << ".";
173 } else
174 observerLog().addMessage() << "++ Update of Observer "
175 << observerLog().getName(static_cast<Observer*>(this))
176 << " received notification from unknown source.";
177#endif
178 if (publisher == static_cast<const Observable *>(&_bond)){
179 delete this;
180 } else {
181 bool DoResetPosition = false;
182 // from an atom
183 switch (notification->getChannelNo()) {
184 case AtomObservable::PositionChanged:
185 LOG(2, "INFO: Received notification of PositionChanged.");
186 DoResetPosition = true;
187 break;
188 case AtomObservable::ElementChanged:
189 LOG(2, "INFO: Received notification of ElementChanged.");
190 DoResetPosition = true;
191 break;
192 default:
193 break;
194 }
195 if (DoResetPosition) {
196 resetPosition();
197 emit changed();
198 }
199 }
200}
201
202void GLMoleculeObject_bond::resetPosition()
203{
204 Vector Position;
205 Vector OtherPosition;
206 switch (BondSide) {
207 case left:
208 Position = _bond.leftatom->getPosition();
209 OtherPosition = _bond.rightatom->getPosition();
210 break;
211 case right:
212 Position = _bond.rightatom->getPosition();
213 OtherPosition = _bond.leftatom->getPosition();
214 break;
215 default:
216 ASSERT(0,
217 "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
218 break;
219 }
220 const double distance =
221 Position.distance(OtherPosition)/2.;
222 setScaleZ(distance);
223
224 // calculate position
225 Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
226 Vector zeroVec(0.,0.,0.);
227 Vector a,b;
228 Vector OtherAxis;
229 double alpha;
230 a = Position - OtherPosition;
231 // construct rotation axis
232 b = a;
233 b.VectorProduct(Z);
234 Line axis(zeroVec, b);
235 // calculate rotation angle
236 alpha = a.Angle(Z);
237 // construct other axis to check right-hand rule
238 OtherAxis = b;
239 OtherAxis.VectorProduct(Z);
240 // assure right-hand rule for the rotation
241 if (a.ScalarProduct(OtherAxis) < MYEPSILON)
242 alpha = M_PI-alpha;
243 // check
244 Vector a_rotated = axis.rotateVector(a, alpha);
245 LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
246 << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
247
248 // set position (cylinder offset is in its barymetric center)
249 Vector OneFourth(Position - 0.75 * a);
250 setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
251 setRotationVector(QVector3D(b[0], b[1], b[2]));
252 setRotationAngle(alpha/M_PI*180.);
253}
Note: See TracBrowser for help on using the repository browser.