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

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 378fc6b was 4b62d3, checked in by Frederik Heber <heber@…>, 12 years ago

GLMoleculeObject_bond sets width of cylinder according to BondDegree.

  • Property mode set to 100644
File size: 9.0 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 _bond.signOn(this, BondObservable::DegreeChanged);
72 leftatom->signOn(this, AtomObservable::PositionChanged);
73 leftatom->signOn(this, AtomObservable::ElementChanged);
74 rightatom->signOn(this, AtomObservable::PositionChanged);
75 rightatom->signOn(this, AtomObservable::ElementChanged);
76
77 size_t elementno = 0;
78 switch (BondSide) {
79 case left:
80 if (_bond.rightatom->getType() != NULL) {
81 elementno = _bond.rightatom->getType()->getAtomicNumber();
82 } else { // if not element yet set, set to hydrogen
83 elementno = 1;
84 }
85 break;
86 case right:
87 if (_bond.leftatom->getType() != NULL) {
88 elementno = _bond.leftatom->getType()->getAtomicNumber();
89 } else { // if not element yet set, set to hydrogen
90 elementno = 1;
91 }
92
93 break;
94 default:
95 ASSERT(0,
96 "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
97 break;
98 }
99
100 QGLMaterial *elementmaterial = getMaterial(elementno);
101 setMaterial(elementmaterial);
102
103 resetPosition();
104 resetWidth();
105}
106
107GLMoleculeObject_bond::~GLMoleculeObject_bond()
108{
109 // sign off
110 switch (BondSide) {
111 case left:
112 emit BondRemoved(leftatomId, rightatomId);
113 break;
114 case right:
115 emit BondRemoved(rightatomId, leftatomId);
116 break;
117 default:
118 ASSERT(0,
119 "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
120 +toString(BondSide)+".");
121 break;
122 }
123 _bond.signOff(this, BondObservable::BondRemoved);
124 _bond.signOff(this, BondObservable::DegreeChanged);
125 leftatom->signOff(this, AtomObservable::PositionChanged);
126 leftatom->signOff(this, AtomObservable::ElementChanged);
127 rightatom->signOff(this, AtomObservable::PositionChanged);
128 rightatom->signOff(this, AtomObservable::ElementChanged);
129 LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
130}
131
132void GLMoleculeObject_bond::update(Observable *publisher)
133{
134#ifdef LOG_OBSERVER
135 if (publisher == static_cast<const Observable *>(&_bond)) {
136 observerLog().addMessage() << "++ Update of Observer "
137 << observerLog().getName(static_cast<Observer*>(this))
138 << " from bond.";
139 } else if (publisher == leftatom) {
140 observerLog().addMessage() << "++ Update of Observer "
141 << observerLog().getName(static_cast<Observer*>(this))
142 << " from leftatom " << _bond.leftatom->getId() << ".";
143 } else if (publisher == rightatom) {
144 observerLog().addMessage() << "++ Update of Observer " <<
145 observerLog().getName(static_cast<Observer*>(this))
146 << " from rightatom " << _bond.rightatom->getId() << ".";
147 } else
148 observerLog().addMessage() << "++ Update of Observer " <<
149 observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
150#endif
151}
152
153void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
154{
155 delete this;
156}
157
158void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
159{
160#ifdef LOG_OBSERVER
161 if (publisher == static_cast<const Observable *>(&_bond)) {
162 observerLog().addMessage() << "++ Update of Observer "
163 << observerLog().getName(static_cast<Observer*>(this))
164 << " received notification from bond for channel "
165 << notification->getChannelNo() << ".";
166 } else if (publisher == leftatom) {
167 observerLog().addMessage() << "++ Update of Observer "
168 << observerLog().getName(static_cast<Observer*>(this))
169 << " received notification from leftatom " << _bond.leftatom->getId() << " for channel "
170 << notification->getChannelNo() << ".";
171 } else if (publisher == rightatom) {
172 observerLog().addMessage() << "++ Update of Observer "
173 << observerLog().getName(static_cast<Observer*>(this))
174 << " received notification from rightatom " << _bond.rightatom->getId() << " for channel "
175 << notification->getChannelNo() << ".";
176 } else
177 observerLog().addMessage() << "++ Update of Observer "
178 << observerLog().getName(static_cast<Observer*>(this))
179 << " received notification from unknown source.";
180#endif
181 bool DoResetPosition = false;
182 bool DoResetWidth = false;
183 if (publisher == static_cast<const Observable *>(&_bond)){
184 switch (notification->getChannelNo()) {
185 case BondObservable::BondRemoved:
186 delete this;
187 break;
188 case BondObservable::DegreeChanged:
189 DoResetWidth = true;
190 break;
191 default:
192 ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
193 break;
194 }
195 } else {
196 // from an atom
197 switch (notification->getChannelNo()) {
198 case AtomObservable::PositionChanged:
199 LOG(2, "INFO: Received notification of PositionChanged.");
200 DoResetPosition = true;
201 break;
202 case AtomObservable::ElementChanged:
203 LOG(2, "INFO: Received notification of ElementChanged.");
204 DoResetPosition = true;
205 break;
206 default:
207 break;
208 }
209 }
210 if (DoResetPosition) {
211 resetPosition();
212 emit changed();
213 }
214 if (DoResetWidth) {
215 resetWidth();
216 emit changed();
217 }
218}
219
220void GLMoleculeObject_bond::resetWidth()
221{
222 const double factor = 1.0f+.5f*(_bond.getDegree()-1);
223 LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
224 setScaleX(factor);
225 setScaleY(factor);
226}
227
228void GLMoleculeObject_bond::resetPosition()
229{
230 Vector Position;
231 Vector OtherPosition;
232 switch (BondSide) {
233 case left:
234 Position = _bond.leftatom->getPosition();
235 OtherPosition = _bond.rightatom->getPosition();
236 break;
237 case right:
238 Position = _bond.rightatom->getPosition();
239 OtherPosition = _bond.leftatom->getPosition();
240 break;
241 default:
242 ASSERT(0,
243 "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
244 break;
245 }
246 const double distance =
247 Position.distance(OtherPosition)/2.;
248 setScaleZ(distance);
249
250 // calculate position
251 Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
252 Vector zeroVec(0.,0.,0.);
253 Vector a,b;
254 Vector OtherAxis;
255 double alpha;
256 a = Position - OtherPosition;
257 // construct rotation axis
258 b = a;
259 b.VectorProduct(Z);
260 Line axis(zeroVec, b);
261 // calculate rotation angle
262 alpha = a.Angle(Z);
263 // construct other axis to check right-hand rule
264 OtherAxis = b;
265 OtherAxis.VectorProduct(Z);
266 // assure right-hand rule for the rotation
267 if (a.ScalarProduct(OtherAxis) < MYEPSILON)
268 alpha = M_PI-alpha;
269 // check
270 Vector a_rotated = axis.rotateVector(a, alpha);
271 LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
272 << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
273
274 // set position (cylinder offset is in its barymetric center)
275 Vector OneFourth(Position - 0.75 * a);
276 setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
277 setRotationVector(QVector3D(b[0], b[1], b[2]));
278 setRotationAngle(alpha/M_PI*180.);
279}
Note: See TracBrowser for help on using the repository browser.