| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 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 "atom.hpp" | 
|---|
| 33 | #include "Bond/bond.hpp" | 
|---|
| 34 | #include "Element/element.hpp" | 
|---|
| 35 | #include "Helpers/defs.hpp" | 
|---|
| 36 | #include "LinearAlgebra/Line.hpp" | 
|---|
| 37 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | static QGLSceneNode *createBond(QObject *parent, double distance) | 
|---|
| 40 | { | 
|---|
| 41 | QGLBuilder builder; | 
|---|
| 42 | QGLCylinder cylinder(.25,.25,.25); | 
|---|
| 43 | cylinder.setHeight(distance); | 
|---|
| 44 | builder << cylinder; | 
|---|
| 45 | QGLSceneNode *n = builder.finalizedSceneNode(); | 
|---|
| 46 | n->setParent(parent); | 
|---|
| 47 | return n; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | GLMoleculeObject_bond::GLMoleculeObject_bond(QObject *parent, const bond *bondref, double distance, enum SideOfBond side) : | 
|---|
| 51 | GLMoleculeObject(createBond(parent, distance), parent), _bond(bondref) | 
|---|
| 52 | { | 
|---|
| 53 | Vector Position; | 
|---|
| 54 | Vector OtherPosition; | 
|---|
| 55 | size_t elementno = 0; | 
|---|
| 56 | switch (side) { | 
|---|
| 57 | case left: | 
|---|
| 58 | Position = _bond->leftatom->getPosition(); | 
|---|
| 59 | OtherPosition = _bond->rightatom->getPosition(); | 
|---|
| 60 | if (_bond->leftatom->getType() != NULL) { | 
|---|
| 61 | elementno = _bond->leftatom->getType()->getNumber(); | 
|---|
| 62 | } else { // if not element yet set, set to hydrogen | 
|---|
| 63 | elementno = 1; | 
|---|
| 64 | } | 
|---|
| 65 | break; | 
|---|
| 66 | case right: | 
|---|
| 67 | Position = _bond->rightatom->getPosition(); | 
|---|
| 68 | OtherPosition = _bond->leftatom->getPosition(); | 
|---|
| 69 | if (_bond->rightatom->getType() != NULL) { | 
|---|
| 70 | elementno = _bond->rightatom->getType()->getNumber(); | 
|---|
| 71 | } else { // if not element yet set, set to hydrogen | 
|---|
| 72 | elementno = 1; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | break; | 
|---|
| 76 | default: | 
|---|
| 77 | ASSERT(0, | 
|---|
| 78 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(side)+"."); | 
|---|
| 79 | break; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | QGLMaterial *elementmaterial = getMaterial(elementno); | 
|---|
| 83 | setMaterial(elementmaterial); | 
|---|
| 84 |  | 
|---|
| 85 | // calculate position | 
|---|
| 86 | Vector Z(0.,0.,1.); | 
|---|
| 87 | Vector zeroVec(0.,0.,0.); | 
|---|
| 88 | Vector a,b; | 
|---|
| 89 | Vector OtherAxis; | 
|---|
| 90 | double alpha; | 
|---|
| 91 | a = Position - OtherPosition; | 
|---|
| 92 | // construct rotation axis | 
|---|
| 93 | b = a; | 
|---|
| 94 | b.VectorProduct(Z); | 
|---|
| 95 | Line axis(zeroVec, b); | 
|---|
| 96 | // calculate rotation angle | 
|---|
| 97 | alpha = a.Angle(Z); | 
|---|
| 98 | // construct other axis to check right-hand rule | 
|---|
| 99 | OtherAxis = b; | 
|---|
| 100 | OtherAxis.VectorProduct(Z); | 
|---|
| 101 | // assure right-hand rule for the rotation | 
|---|
| 102 | if (a.ScalarProduct(OtherAxis) < MYEPSILON) | 
|---|
| 103 | alpha = M_PI-alpha; | 
|---|
| 104 | // check | 
|---|
| 105 | Vector a_rotated = axis.rotateVector(a, alpha); | 
|---|
| 106 | std::cout << "Created cylinder from "// << Position << " to " << OtherPosition | 
|---|
| 107 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively." << endl; | 
|---|
| 108 |  | 
|---|
| 109 | // set position | 
|---|
| 110 | setPosition(QVector3D(Position[0], Position[1], Position[2])); | 
|---|
| 111 | setRotationVector(QVector3D(b[0], b[1], b[2])); | 
|---|
| 112 | setRotationAngle(alpha/M_PI*180.); | 
|---|
| 113 | } | 
|---|