| 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 "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | //#include "CodePatterns/MemDebug.hpp" | 
|---|
| 44 |  | 
|---|
| 45 | #include <cmath> | 
|---|
| 46 |  | 
|---|
| 47 | #include "CodePatterns/Assert.hpp" | 
|---|
| 48 | #include "CodePatterns/Log.hpp" | 
|---|
| 49 |  | 
|---|
| 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 | #include "World.hpp" | 
|---|
| 57 |  | 
|---|
| 58 | GLMoleculeObject_bond::GLMoleculeObject_bond( | 
|---|
| 59 | QGLSceneNode *mesh[], | 
|---|
| 60 | QObject *parent, | 
|---|
| 61 | QtObservedBond::ptr &_ObservedBond, | 
|---|
| 62 | const enum SideOfBond side) : | 
|---|
| 63 | GLMoleculeObject(mesh, parent), | 
|---|
| 64 | BondSide(side), | 
|---|
| 65 | ObservedBond(_ObservedBond) | 
|---|
| 66 | { | 
|---|
| 67 | resetElement(); | 
|---|
| 68 | resetPosition(); | 
|---|
| 69 | resetWidth(); | 
|---|
| 70 |  | 
|---|
| 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())); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | static const atomicNumber_t& getElement( | 
|---|
| 82 | const QtObservedBond::ptr &_ObservedBond, | 
|---|
| 83 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 84 | { | 
|---|
| 85 | if (_side == GLMoleculeObject_bond::left) | 
|---|
| 86 | return _ObservedBond->getLeftAtomElement(); | 
|---|
| 87 | else | 
|---|
| 88 | return _ObservedBond->getRightAtomElement(); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | static const Vector& getPosition( | 
|---|
| 92 | const QtObservedBond::ptr &_ObservedBond, | 
|---|
| 93 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 94 | { | 
|---|
| 95 | if (_side == GLMoleculeObject_bond::left) | 
|---|
| 96 | return _ObservedBond->getLeftAtomPosition(); | 
|---|
| 97 | else | 
|---|
| 98 | return _ObservedBond->getRightAtomPosition(); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | static const Vector& getOtherPosition( | 
|---|
| 102 | const QtObservedBond::ptr &_ObservedBond, | 
|---|
| 103 | const enum GLMoleculeObject_bond::SideOfBond _side) | 
|---|
| 104 | { | 
|---|
| 105 | if (_side == GLMoleculeObject_bond::left) | 
|---|
| 106 | return _ObservedBond->getRightAtomPosition(); | 
|---|
| 107 | else | 
|---|
| 108 | return _ObservedBond->getLeftAtomPosition(); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | GLMoleculeObject_bond::~GLMoleculeObject_bond() | 
|---|
| 112 | { | 
|---|
| 113 | LOG(4, "DEBUG: Destroying  GLMoleculeObject_bond to bond [" << | 
|---|
| 114 | ObservedBond->getBondIndex() << "] and side " << BondSide << "."); | 
|---|
| 115 | // signOff() if not already done | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | void GLMoleculeObject_bond::resetElement() | 
|---|
| 119 | { | 
|---|
| 120 | const atomicNumber_t& elementno = getElement(ObservedBond, BondSide); | 
|---|
| 121 | QGLMaterial *elementmaterial = getMaterial(elementno); | 
|---|
| 122 | setMaterial(elementmaterial); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void GLMoleculeObject_bond::resetWidth() | 
|---|
| 126 | { | 
|---|
| 127 | const double factor = 1.0f+.5f*(ObservedBond->getBondDegree()-1); | 
|---|
| 128 | LOG(4, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << "."); | 
|---|
| 129 | setScaleX(factor); | 
|---|
| 130 | setScaleY(factor); | 
|---|
| 131 |  | 
|---|
| 132 | emit changed(); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | void GLMoleculeObject_bond::resetPosition() | 
|---|
| 136 | { | 
|---|
| 137 | const Vector& Position = getPosition(ObservedBond, BondSide); | 
|---|
| 138 | const Vector& OtherPosition = getOtherPosition(ObservedBond, BondSide); | 
|---|
| 139 | const double distance = | 
|---|
| 140 | Position.distance(OtherPosition)/2.; | 
|---|
| 141 | setScaleZ(distance); | 
|---|
| 142 |  | 
|---|
| 143 | // calculate position | 
|---|
| 144 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis | 
|---|
| 145 | Vector zeroVec(0.,0.,0.); | 
|---|
| 146 | Vector a,b; | 
|---|
| 147 | Vector OtherAxis; | 
|---|
| 148 | double alpha; | 
|---|
| 149 | a = OtherPosition - Position; | 
|---|
| 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); | 
|---|
| 164 | LOG(4, "DEBUG: Created cylinder from "// << Position << " to " << OtherPosition | 
|---|
| 165 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively."); | 
|---|
| 166 |  | 
|---|
| 167 | // set position (cylinder offset is in its barymetric center) | 
|---|
| 168 | Vector OneFourth(OtherPosition - 0.75 * a); | 
|---|
| 169 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2])); | 
|---|
| 170 | setRotationVector(QVector3D(b[0], b[1], b[2])); | 
|---|
| 171 | setRotationAngle(alpha/M_PI*180.); | 
|---|
| 172 |  | 
|---|
| 173 | emit changed(); | 
|---|
| 174 | } | 
|---|