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

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 Candidate_v1.7.0 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 343a4b was 343a4b, checked in by Frederik Heber <heber@…>, 14 years ago

GL: bond tracks its atoms' position changes

  • Property mode set to 100644
File size: 6.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 * 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/qglbuilder.h>
23#include <Qt3D/qglcylinder.h>
24#include <Qt3D/qglmaterial.h>
25#include <Qt3D/qglscenenode.h>
26
27#include "CodePatterns/MemDebug.hpp"
28
29#include <cmath>
30
31#include "CodePatterns/Assert.hpp"
32#include "CodePatterns/Log.hpp"
33#include "CodePatterns/Observer/Notification.hpp"
34#include "Atom/atom.hpp"
35#include "Bond/bond.hpp"
36#include "Element/element.hpp"
37#include "Helpers/defs.hpp"
38#include "LinearAlgebra/Line.hpp"
39#include "LinearAlgebra/Vector.hpp"
40
41static QGLSceneNode *createBond(QObject *parent, double distance)
42{
43 QGLBuilder builder;
44 QGLCylinder cylinder(.25,.25,.25);
45 cylinder.setHeight(distance);
46 builder << cylinder;
47 QGLSceneNode *n = builder.finalizedSceneNode();
48 n->setParent(parent);
49 return n;
50}
51
52GLMoleculeObject_bond::GLMoleculeObject_bond(QObject *parent, const bond *bondref, const double distance, const enum SideOfBond side) :
53 GLMoleculeObject(createBond(parent, distance), parent),
54 Observer(std::string("GLMoleculeObject_bond")
55 +toString(bondref->leftatom->getId())
56 +std::string("-")
57 +toString(bondref->rightatom->getId())),
58 _bond(bondref),
59 BondSide(side)
60{
61 // sign on as observer (obtain non-const instance before)
62 _bond->signOn(this, BondObservable::BondRemoved);
63 _bond->leftatom->signOn(this, AtomObservable::PositionChanged);
64 _bond->rightatom->signOn(this, AtomObservable::PositionChanged);
65
66 size_t elementno = 0;
67 switch (BondSide) {
68 case left:
69 if (_bond->leftatom->getType() != NULL) {
70 elementno = _bond->leftatom->getType()->getAtomicNumber();
71 } else { // if not element yet set, set to hydrogen
72 elementno = 1;
73 }
74 break;
75 case right:
76 if (_bond->rightatom->getType() != NULL) {
77 elementno = _bond->rightatom->getType()->getAtomicNumber();
78 } else { // if not element yet set, set to hydrogen
79 elementno = 1;
80 }
81
82 break;
83 default:
84 ASSERT(0,
85 "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
86 break;
87 }
88
89 QGLMaterial *elementmaterial = getMaterial(elementno);
90 setMaterial(elementmaterial);
91
92 resetPosition();
93}
94
95GLMoleculeObject_bond::~GLMoleculeObject_bond()
96{
97 // sign on as observer (obtain non-const instance before)
98 _bond->signOff(this, BondObservable::BondRemoved);
99 _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
100 _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
101
102 LOG(2, "INFO: Destroying GLMoleculeObject_bond to bond " << *_bond << " and side " << BondSide << ".");
103}
104
105void GLMoleculeObject_bond::update(Observable *publisher)
106{
107#ifdef LOG_OBSERVER
108 observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from bond " << *_bond << ".";
109#endif
110}
111
112void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
113{
114 LOG(2, "INFO: Received subjectKilled from " << *_bond << ".");
115 switch (BondSide) {
116 case left:
117 emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
118 break;
119 case right:
120 emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
121 break;
122 default:
123 ASSERT(0,
124 "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "+toString(BondSide)+".");
125 break;
126 }
127 delete this;
128}
129
130void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
131{
132#ifdef LOG_OBSERVER
133 observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
134 << " received notification from bond " << *_bond << " for channel "
135 << notification->getChannelNo() << ".";
136#endif
137 if (publisher == dynamic_cast<const Observable*>(_bond)){
138 // from the bond
139 switch (notification->getChannelNo()) {
140 case BondObservable::BondRemoved:
141 LOG(2, "INFO: Received notification of BondRemoved from " << *_bond << ".");
142 switch (BondSide) {
143 case left:
144 emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
145 break;
146 case right:
147 emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
148 break;
149 default:
150 ASSERT(0,
151 "GLMoleculeObject_bond::recieveNotification() - side is not a valid argument: "+toString(BondSide)+".");
152 break;
153 }
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 }
166 }
167}
168
169void GLMoleculeObject_bond::resetPosition()
170{
171 Vector Position;
172 Vector OtherPosition;
173 switch (BondSide) {
174 case left:
175 Position = _bond->leftatom->getPosition();
176 OtherPosition = _bond->rightatom->getPosition();
177 break;
178 case right:
179 Position = _bond->rightatom->getPosition();
180 OtherPosition = _bond->leftatom->getPosition();
181 break;
182 default:
183 ASSERT(0,
184 "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
185 break;
186 }
187
188 // calculate position
189 Vector Z(0.,0.,1.);
190 Vector zeroVec(0.,0.,0.);
191 Vector a,b;
192 Vector OtherAxis;
193 double alpha;
194 a = Position - OtherPosition;
195 // construct rotation axis
196 b = a;
197 b.VectorProduct(Z);
198 Line axis(zeroVec, b);
199 // calculate rotation angle
200 alpha = a.Angle(Z);
201 // construct other axis to check right-hand rule
202 OtherAxis = b;
203 OtherAxis.VectorProduct(Z);
204 // assure right-hand rule for the rotation
205 if (a.ScalarProduct(OtherAxis) < MYEPSILON)
206 alpha = M_PI-alpha;
207 // check
208 Vector a_rotated = axis.rotateVector(a, alpha);
209 LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
210 << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
211
212 // set position
213 setPosition(QVector3D(Position[0], Position[1], Position[2]));
214 setRotationVector(QVector3D(b[0], b[1], b[2]));
215 setRotationAngle(alpha/M_PI*180.);
216}
Note: See TracBrowser for help on using the repository browser.