| [907636] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [907636] | 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" | 
|---|
| [57a770] | 32 | #include "CodePatterns/Log.hpp" | 
|---|
| [2ad1ec] | 33 | #include "CodePatterns/Observer/Notification.hpp" | 
|---|
| [6f0841] | 34 | #include "Atom/atom.hpp" | 
|---|
| [907636] | 35 | #include "Bond/bond.hpp" | 
|---|
| [3bdb6d] | 36 | #include "Element/element.hpp" | 
|---|
| [907636] | 37 | #include "Helpers/defs.hpp" | 
|---|
|  | 38 | #include "LinearAlgebra/Line.hpp" | 
|---|
|  | 39 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 40 |  | 
|---|
|  | 41 | static QGLSceneNode *createBond(QObject *parent, double distance) | 
|---|
|  | 42 | { | 
|---|
|  | 43 | QGLBuilder builder; | 
|---|
|  | 44 | QGLCylinder cylinder(.25,.25,.25); | 
|---|
|  | 45 | cylinder.setHeight(distance); | 
|---|
|  | 46 | builder << cylinder; | 
|---|
|  | 47 | QGLSceneNode *n = builder.finalizedSceneNode(); | 
|---|
|  | 48 | n->setParent(parent); | 
|---|
|  | 49 | return n; | 
|---|
|  | 50 | } | 
|---|
|  | 51 |  | 
|---|
| [2ad1ec] | 52 | GLMoleculeObject_bond::GLMoleculeObject_bond(QObject *parent, const bond *bondref, const double distance, const enum SideOfBond side) : | 
|---|
|  | 53 | GLMoleculeObject(createBond(parent, distance), parent), | 
|---|
|  | 54 | Observer(std::string("GLMoleculeObject_bond") | 
|---|
|  | 55 | +toString(bondref->leftatom->getId()) | 
|---|
|  | 56 | +std::string("-") | 
|---|
|  | 57 | +toString(bondref->rightatom->getId())), | 
|---|
|  | 58 | _bond(bondref), | 
|---|
|  | 59 | BondSide(side) | 
|---|
| [907636] | 60 | { | 
|---|
| [2ad1ec] | 61 | // sign on as observer (obtain non-const instance before) | 
|---|
|  | 62 | _bond->signOn(this, BondObservable::BondRemoved); | 
|---|
|  | 63 |  | 
|---|
| [907636] | 64 | Vector Position; | 
|---|
|  | 65 | Vector OtherPosition; | 
|---|
|  | 66 | size_t elementno = 0; | 
|---|
| [2ad1ec] | 67 | switch (BondSide) { | 
|---|
| [907636] | 68 | case left: | 
|---|
|  | 69 | Position = _bond->leftatom->getPosition(); | 
|---|
|  | 70 | OtherPosition = _bond->rightatom->getPosition(); | 
|---|
| [7188b1] | 71 | if (_bond->leftatom->getType() != NULL) { | 
|---|
| [ed26ae] | 72 | elementno = _bond->leftatom->getType()->getAtomicNumber(); | 
|---|
| [7188b1] | 73 | } else { // if not element yet set, set to hydrogen | 
|---|
|  | 74 | elementno = 1; | 
|---|
|  | 75 | } | 
|---|
| [907636] | 76 | break; | 
|---|
|  | 77 | case right: | 
|---|
|  | 78 | Position = _bond->rightatom->getPosition(); | 
|---|
|  | 79 | OtherPosition = _bond->leftatom->getPosition(); | 
|---|
| [7188b1] | 80 | if (_bond->rightatom->getType() != NULL) { | 
|---|
| [ed26ae] | 81 | elementno = _bond->rightatom->getType()->getAtomicNumber(); | 
|---|
| [7188b1] | 82 | } else { // if not element yet set, set to hydrogen | 
|---|
|  | 83 | elementno = 1; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
| [907636] | 86 | break; | 
|---|
|  | 87 | default: | 
|---|
|  | 88 | ASSERT(0, | 
|---|
| [2ad1ec] | 89 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+"."); | 
|---|
| [907636] | 90 | break; | 
|---|
|  | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 | QGLMaterial *elementmaterial = getMaterial(elementno); | 
|---|
|  | 94 | setMaterial(elementmaterial); | 
|---|
|  | 95 |  | 
|---|
|  | 96 | // calculate position | 
|---|
|  | 97 | Vector Z(0.,0.,1.); | 
|---|
|  | 98 | Vector zeroVec(0.,0.,0.); | 
|---|
|  | 99 | Vector a,b; | 
|---|
|  | 100 | Vector OtherAxis; | 
|---|
|  | 101 | double alpha; | 
|---|
|  | 102 | a = Position - OtherPosition; | 
|---|
|  | 103 | // construct rotation axis | 
|---|
|  | 104 | b = a; | 
|---|
|  | 105 | b.VectorProduct(Z); | 
|---|
|  | 106 | Line axis(zeroVec, b); | 
|---|
|  | 107 | // calculate rotation angle | 
|---|
|  | 108 | alpha = a.Angle(Z); | 
|---|
|  | 109 | // construct other axis to check right-hand rule | 
|---|
|  | 110 | OtherAxis = b; | 
|---|
|  | 111 | OtherAxis.VectorProduct(Z); | 
|---|
|  | 112 | // assure right-hand rule for the rotation | 
|---|
|  | 113 | if (a.ScalarProduct(OtherAxis) < MYEPSILON) | 
|---|
|  | 114 | alpha = M_PI-alpha; | 
|---|
|  | 115 | // check | 
|---|
|  | 116 | Vector a_rotated = axis.rotateVector(a, alpha); | 
|---|
| [57a770] | 117 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition | 
|---|
|  | 118 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively."); | 
|---|
| [907636] | 119 |  | 
|---|
|  | 120 | // set position | 
|---|
|  | 121 | setPosition(QVector3D(Position[0], Position[1], Position[2])); | 
|---|
|  | 122 | setRotationVector(QVector3D(b[0], b[1], b[2])); | 
|---|
|  | 123 | setRotationAngle(alpha/M_PI*180.); | 
|---|
|  | 124 | } | 
|---|
| [2ad1ec] | 125 |  | 
|---|
|  | 126 | GLMoleculeObject_bond::~GLMoleculeObject_bond() | 
|---|
|  | 127 | { | 
|---|
|  | 128 | // sign on as observer (obtain non-const instance before) | 
|---|
|  | 129 | _bond->signOff(this, BondObservable::BondRemoved); | 
|---|
|  | 130 |  | 
|---|
|  | 131 | LOG(2, "INFO: Destroying  GLMoleculeObject_bond to bond " << *_bond << " and side " << BondSide << "."); | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | void GLMoleculeObject_bond::update(Observable *publisher) | 
|---|
|  | 135 | { | 
|---|
|  | 136 | #ifdef LOG_OBSERVER | 
|---|
|  | 137 | observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from bond " << *_bond << "."; | 
|---|
|  | 138 | #endif | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
|  | 141 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher) | 
|---|
|  | 142 | { | 
|---|
|  | 143 | LOG(2, "INFO: Received subjectKilled from " << *_bond << "."); | 
|---|
|  | 144 | switch (BondSide) { | 
|---|
|  | 145 | case left: | 
|---|
|  | 146 | emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId()); | 
|---|
|  | 147 | break; | 
|---|
|  | 148 | case right: | 
|---|
|  | 149 | emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId()); | 
|---|
|  | 150 | break; | 
|---|
|  | 151 | default: | 
|---|
|  | 152 | ASSERT(0, | 
|---|
|  | 153 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "+toString(BondSide)+"."); | 
|---|
|  | 154 | break; | 
|---|
|  | 155 | } | 
|---|
|  | 156 | delete this; | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
|  | 159 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification) | 
|---|
|  | 160 | { | 
|---|
|  | 161 | #ifdef LOG_OBSERVER | 
|---|
|  | 162 | observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this) | 
|---|
|  | 163 | << " received notification from bond " << *_bond << " for channel " | 
|---|
|  | 164 | << notification->getChannelNo() << "."; | 
|---|
|  | 165 | #endif | 
|---|
|  | 166 | switch (notification->getChannelNo()) { | 
|---|
|  | 167 | case BondObservable::BondRemoved: | 
|---|
|  | 168 | LOG(2, "INFO: Received notification of BondRemoved from " << *_bond << "."); | 
|---|
|  | 169 | switch (BondSide) { | 
|---|
|  | 170 | case left: | 
|---|
|  | 171 | emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId()); | 
|---|
|  | 172 | break; | 
|---|
|  | 173 | case right: | 
|---|
|  | 174 | emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId()); | 
|---|
|  | 175 | break; | 
|---|
|  | 176 | default: | 
|---|
|  | 177 | ASSERT(0, | 
|---|
|  | 178 | "GLMoleculeObject_bond::recieveNotification() - side is not a valid argument: "+toString(BondSide)+"."); | 
|---|
|  | 179 | break; | 
|---|
|  | 180 | } | 
|---|
|  | 181 | delete this; | 
|---|
|  | 182 | break; | 
|---|
|  | 183 | default: | 
|---|
|  | 184 | break; | 
|---|
|  | 185 | } | 
|---|
|  | 186 | } | 
|---|