| [907636] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [907636] | 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 | 
 | 
|---|
| [494478] | 41 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
 | 
|---|
| [907636] | 42 | 
 | 
|---|
| [494478] | 43 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [856d05] | 44 | 
 | 
|---|
| [907636] | 45 | #include <cmath>
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [57a770] | 48 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [96f14a] | 49 | 
 | 
|---|
| [6f0841] | 50 | #include "Atom/atom.hpp"
 | 
|---|
| [907636] | 51 | #include "Bond/bond.hpp"
 | 
|---|
| [3bdb6d] | 52 | #include "Element/element.hpp"
 | 
|---|
| [907636] | 53 | #include "Helpers/defs.hpp"
 | 
|---|
 | 54 | #include "LinearAlgebra/Line.hpp"
 | 
|---|
 | 55 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [c60665] | 56 | #include "World.hpp"
 | 
|---|
| [907636] | 57 | 
 | 
|---|
| [009e2e2] | 58 | GLMoleculeObject_bond::GLMoleculeObject_bond(
 | 
|---|
 | 59 |     QGLSceneNode *mesh[],
 | 
|---|
 | 60 |     QObject *parent,
 | 
|---|
| [96f14a] | 61 |     QtObservedBond::ptr &_ObservedBond,
 | 
|---|
| [009e2e2] | 62 |     const enum SideOfBond side) :
 | 
|---|
| [bca99d] | 63 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| [026bef] | 64 |   BondSide(side),
 | 
|---|
| [96f14a] | 65 |   ObservedBond(_ObservedBond)
 | 
|---|
| [026bef] | 66 | {
 | 
|---|
| [009e2e2] | 67 |   resetElement();
 | 
|---|
| [343a4b] | 68 |   resetPosition();
 | 
|---|
| [4b62d3] | 69 |   resetWidth();
 | 
|---|
| [009e2e2] | 70 | 
 | 
|---|
| [96f14a] | 71 |   // connect to observed bond's signals
 | 
|---|
 | 72 |   connect(_ObservedBond.get(), SIGNAL(degreeChanged()), this, SLOT(resetWidth()));
 | 
|---|
 | 73 |   if (side == left)
 | 
|---|
 | 74 |     connect(_ObservedBond.get(), SIGNAL(leftAtomElementChanged()), this, SLOT(resetElement()));
 | 
|---|
 | 75 |   else
 | 
|---|
 | 76 |     connect(_ObservedBond.get(), SIGNAL(rightAtomElementChanged()), this, SLOT(resetElement()));
 | 
|---|
 | 77 |   connect(_ObservedBond.get(), SIGNAL(leftAtomPositionChanged()), this, SLOT(resetPosition()));
 | 
|---|
 | 78 |   connect(_ObservedBond.get(), SIGNAL(rightAtomPositionChanged()), this, SLOT(resetPosition()));
 | 
|---|
| [4b62d3] | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
| [96f14a] | 81 | static const atomicNumber_t& getElement(
 | 
|---|
 | 82 |     const QtObservedBond::ptr &_ObservedBond,
 | 
|---|
 | 83 |     const enum GLMoleculeObject_bond::SideOfBond _side)
 | 
|---|
| [4b62d3] | 84 | {
 | 
|---|
| [96f14a] | 85 |   if (_side == GLMoleculeObject_bond::left)
 | 
|---|
 | 86 |     return _ObservedBond->getLeftAtomElement();
 | 
|---|
 | 87 |   else
 | 
|---|
 | 88 |     return _ObservedBond->getRightAtomElement();
 | 
|---|
| [099f67] | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
| [96f14a] | 91 | static const Vector& getPosition(
 | 
|---|
 | 92 |     const QtObservedBond::ptr &_ObservedBond,
 | 
|---|
 | 93 |     const enum GLMoleculeObject_bond::SideOfBond _side)
 | 
|---|
| [099f67] | 94 | {
 | 
|---|
| [96f14a] | 95 |   if (_side == GLMoleculeObject_bond::left)
 | 
|---|
 | 96 |     return _ObservedBond->getLeftAtomPosition();
 | 
|---|
 | 97 |   else
 | 
|---|
 | 98 |     return _ObservedBond->getRightAtomPosition();
 | 
|---|
| [343a4b] | 99 | }
 | 
|---|
 | 100 | 
 | 
|---|
| [96f14a] | 101 | static const Vector& getOtherPosition(
 | 
|---|
 | 102 |     const QtObservedBond::ptr &_ObservedBond,
 | 
|---|
 | 103 |     const enum GLMoleculeObject_bond::SideOfBond _side)
 | 
|---|
| [009e2e2] | 104 | {
 | 
|---|
| [96f14a] | 105 |   if (_side == GLMoleculeObject_bond::left)
 | 
|---|
 | 106 |     return _ObservedBond->getRightAtomPosition();
 | 
|---|
 | 107 |   else
 | 
|---|
 | 108 |     return _ObservedBond->getLeftAtomPosition();
 | 
|---|
| [009e2e2] | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
| [96f14a] | 111 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
 | 
|---|
| [343a4b] | 112 | {
 | 
|---|
| [96f14a] | 113 |   LOG(4, "DEBUG: Destroying  GLMoleculeObject_bond to bond [" <<
 | 
|---|
 | 114 |       ObservedBond->getBondIndex() << "] and side " << BondSide << ".");
 | 
|---|
 | 115 |   // signOff() if not already done
 | 
|---|
| [009e2e2] | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | void GLMoleculeObject_bond::resetElement()
 | 
|---|
 | 119 | {
 | 
|---|
| [96f14a] | 120 |   const atomicNumber_t& elementno = getElement(ObservedBond, BondSide);
 | 
|---|
| [009e2e2] | 121 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
 | 122 |   setMaterial(elementmaterial);
 | 
|---|
 | 123 | }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 | void GLMoleculeObject_bond::resetWidth()
 | 
|---|
 | 126 | {
 | 
|---|
| [96f14a] | 127 |   const double factor = 1.0f+.5f*(ObservedBond->getBondDegree()-1);
 | 
|---|
 | 128 |   LOG(4, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
 | 
|---|
| [009e2e2] | 129 |   setScaleX(factor);
 | 
|---|
 | 130 |   setScaleY(factor);
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 |   emit changed();
 | 
|---|
 | 133 | }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | void GLMoleculeObject_bond::resetPosition()
 | 
|---|
 | 136 | {
 | 
|---|
| [96f14a] | 137 |   const Vector& Position = getPosition(ObservedBond, BondSide);
 | 
|---|
 | 138 |   const Vector& OtherPosition = getOtherPosition(ObservedBond, BondSide);
 | 
|---|
| [fbb1f1] | 139 |   const double distance =
 | 
|---|
 | 140 |       Position.distance(OtherPosition)/2.;
 | 
|---|
 | 141 |   setScaleZ(distance);
 | 
|---|
| [343a4b] | 142 | 
 | 
|---|
 | 143 |   // calculate position
 | 
|---|
| [37e910] | 144 |   Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
 | 
|---|
| [343a4b] | 145 |   Vector zeroVec(0.,0.,0.);
 | 
|---|
 | 146 |   Vector a,b;
 | 
|---|
 | 147 |   Vector OtherAxis;
 | 
|---|
 | 148 |   double alpha;
 | 
|---|
| [96f14a] | 149 |   a = OtherPosition - Position;
 | 
|---|
| [343a4b] | 150 |   // construct rotation axis
 | 
|---|
 | 151 |   b = a;
 | 
|---|
 | 152 |   b.VectorProduct(Z);
 | 
|---|
 | 153 |   Line axis(zeroVec, b);
 | 
|---|
 | 154 |   // calculate rotation angle
 | 
|---|
 | 155 |   alpha = a.Angle(Z);
 | 
|---|
 | 156 |   // construct other axis to check right-hand rule
 | 
|---|
 | 157 |   OtherAxis = b;
 | 
|---|
 | 158 |   OtherAxis.VectorProduct(Z);
 | 
|---|
 | 159 |   // assure right-hand rule for the rotation
 | 
|---|
 | 160 |   if (a.ScalarProduct(OtherAxis) < MYEPSILON)
 | 
|---|
 | 161 |     alpha = M_PI-alpha;
 | 
|---|
 | 162 |   // check
 | 
|---|
 | 163 |   Vector a_rotated = axis.rotateVector(a, alpha);
 | 
|---|
| [96f14a] | 164 |   LOG(4, "DEBUG: Created cylinder from "// << Position << " to " << OtherPosition
 | 
|---|
| [343a4b] | 165 |       << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
 | 
|---|
 | 166 | 
 | 
|---|
| [37e910] | 167 |   // set position (cylinder offset is in its barymetric center)
 | 
|---|
| [96f14a] | 168 |   Vector OneFourth(OtherPosition - 0.75 * a);
 | 
|---|
| [37e910] | 169 |   setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
 | 
|---|
| [343a4b] | 170 |   setRotationVector(QVector3D(b[0], b[1], b[2]));
 | 
|---|
 | 171 |   setRotationAngle(alpha/M_PI*180.);
 | 
|---|
| [009e2e2] | 172 | 
 | 
|---|
 | 173 |   emit changed();
 | 
|---|
 | 174 | }
 | 
|---|