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

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 c67518 was 5a2a06, checked in by Frederik Heber <heber@…>, 13 years ago

GL: sending more signals to redraw

  • atoms send changed() to scene when element or position changed
  • atoms send selectionChanged() to scene when (un)selected
  • bonds send changed() to scene when an atom changes its position
  • Property mode set to 100644
File size: 6.5 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)
42{
43 QGLBuilder builder;
44 QGLCylinder cylinder(.25,.25,1.0,16);
45 builder << cylinder;
46 QGLSceneNode *n = builder.finalizedSceneNode();
47 n->setParent(parent);
48 return n;
49}
50
51GLMoleculeObject_bond::GLMoleculeObject_bond(QObject *parent, const bond *bondref, const enum SideOfBond side) :
52 GLMoleculeObject(createBond(parent), parent),
53 Observer(std::string("GLMoleculeObject_bond")
54 +toString(bondref->leftatom->getId())
55 +std::string("-")
56 +toString(bondref->rightatom->getId())),
57 _bond(bondref),
58 BondSide(side)
59{
60 // sign on as observer (obtain non-const instance before)
61 _bond->signOn(this, BondObservable::BondRemoved);
62 _bond->leftatom->signOn(this, AtomObservable::PositionChanged);
63 _bond->rightatom->signOn(this, AtomObservable::PositionChanged);
64
65 size_t elementno = 0;
66 switch (BondSide) {
67 case left:
68 if (_bond->leftatom->getType() != NULL) {
69 elementno = _bond->leftatom->getType()->getAtomicNumber();
70 } else { // if not element yet set, set to hydrogen
71 elementno = 1;
72 }
73 break;
74 case right:
75 if (_bond->rightatom->getType() != NULL) {
76 elementno = _bond->rightatom->getType()->getAtomicNumber();
77 } else { // if not element yet set, set to hydrogen
78 elementno = 1;
79 }
80
81 break;
82 default:
83 ASSERT(0,
84 "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
85 break;
86 }
87
88 QGLMaterial *elementmaterial = getMaterial(elementno);
89 setMaterial(elementmaterial);
90
91 resetPosition();
92}
93
94GLMoleculeObject_bond::~GLMoleculeObject_bond()
95{
96 // sign on as observer (obtain non-const instance before)
97 _bond->signOff(this, BondObservable::BondRemoved);
98 _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
99 _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
100
101 LOG(2, "INFO: Destroying GLMoleculeObject_bond to bond " << *_bond << " and side " << BondSide << ".");
102}
103
104void GLMoleculeObject_bond::update(Observable *publisher)
105{
106#ifdef LOG_OBSERVER
107 observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from bond " << *_bond << ".";
108#endif
109}
110
111void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
112{
113 LOG(2, "INFO: Received subjectKilled from " << *_bond << ".");
114 switch (BondSide) {
115 case left:
116 emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
117 break;
118 case right:
119 emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
120 break;
121 default:
122 ASSERT(0,
123 "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "+toString(BondSide)+".");
124 break;
125 }
126 delete this;
127}
128
129void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
130{
131#ifdef LOG_OBSERVER
132 observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
133 << " received notification from bond " << *_bond << " for channel "
134 << notification->getChannelNo() << ".";
135#endif
136 if (publisher == dynamic_cast<const Observable*>(_bond)){
137 // from the bond
138 switch (notification->getChannelNo()) {
139 case BondObservable::BondRemoved:
140 LOG(2, "INFO: Received notification of BondRemoved from " << *_bond << ".");
141 switch (BondSide) {
142 case left:
143 emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
144 break;
145 case right:
146 emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
147 break;
148 default:
149 ASSERT(0,
150 "GLMoleculeObject_bond::recieveNotification() - side is not a valid argument: "+toString(BondSide)+".");
151 break;
152 }
153 delete this;
154 break;
155 default:
156 break;
157 }
158 }else{
159 // from an atom
160 switch (notification->getChannelNo()) {
161 case AtomObservable::PositionChanged:
162 LOG(2, "INFO: Received notification of PositionChanged.");
163 resetPosition();
164 emit changed();
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 const double distance =
188 Position.distance(OtherPosition)/2.;
189 setScaleZ(distance);
190
191 // calculate position
192 Vector Z(0.,0.,1.);
193 Vector zeroVec(0.,0.,0.);
194 Vector a,b;
195 Vector OtherAxis;
196 double alpha;
197 a = Position - OtherPosition;
198 // construct rotation axis
199 b = a;
200 b.VectorProduct(Z);
201 Line axis(zeroVec, b);
202 // calculate rotation angle
203 alpha = a.Angle(Z);
204 // construct other axis to check right-hand rule
205 OtherAxis = b;
206 OtherAxis.VectorProduct(Z);
207 // assure right-hand rule for the rotation
208 if (a.ScalarProduct(OtherAxis) < MYEPSILON)
209 alpha = M_PI-alpha;
210 // check
211 Vector a_rotated = axis.rotateVector(a, alpha);
212 LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
213 << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
214
215 // set position
216 setPosition(QVector3D(Position[0], Position[1], Position[2]));
217 setRotationVector(QVector3D(b[0], b[1], b[2]));
218 setRotationAngle(alpha/M_PI*180.);
219}
Note: See TracBrowser for help on using the repository browser.