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

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 1e1098 was 5aaa43, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

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