| [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 | 
 | 
|---|
| [fbb1f1] | 41 | static QGLSceneNode *createBond(QObject *parent)
 | 
|---|
| [907636] | 42 | {
 | 
|---|
 | 43 |    QGLBuilder builder;
 | 
|---|
| [fbb1f1] | 44 |    QGLCylinder cylinder(.25,.25,1.0,16);
 | 
|---|
| [907636] | 45 |    builder << cylinder;
 | 
|---|
 | 46 |    QGLSceneNode *n = builder.finalizedSceneNode();
 | 
|---|
 | 47 |    n->setParent(parent);
 | 
|---|
 | 48 |    return n;
 | 
|---|
 | 49 | }
 | 
|---|
 | 50 | 
 | 
|---|
| [fbb1f1] | 51 | GLMoleculeObject_bond::GLMoleculeObject_bond(QObject *parent, const bond *bondref, const enum SideOfBond side) :
 | 
|---|
 | 52 |   GLMoleculeObject(createBond(parent), parent),
 | 
|---|
| [2ad1ec] | 53 |   Observer(std::string("GLMoleculeObject_bond")
 | 
|---|
 | 54 |       +toString(bondref->leftatom->getId())
 | 
|---|
 | 55 |       +std::string("-")
 | 
|---|
 | 56 |       +toString(bondref->rightatom->getId())),
 | 
|---|
 | 57 |   _bond(bondref),
 | 
|---|
 | 58 |   BondSide(side)
 | 
|---|
| [907636] | 59 | {
 | 
|---|
| [2ad1ec] | 60 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
 | 61 |   _bond->signOn(this, BondObservable::BondRemoved);
 | 
|---|
| [343a4b] | 62 |   _bond->leftatom->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
 | 63 |   _bond->rightatom->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
| [2ad1ec] | 64 | 
 | 
|---|
| [907636] | 65 |   size_t elementno = 0;
 | 
|---|
| [2ad1ec] | 66 |   switch (BondSide) {
 | 
|---|
| [907636] | 67 |     case left:
 | 
|---|
| [7188b1] | 68 |       if (_bond->leftatom->getType() != NULL) {
 | 
|---|
| [ed26ae] | 69 |         elementno = _bond->leftatom->getType()->getAtomicNumber();
 | 
|---|
| [7188b1] | 70 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
 | 71 |         elementno = 1;
 | 
|---|
 | 72 |       }
 | 
|---|
| [907636] | 73 |       break;
 | 
|---|
 | 74 |     case right:
 | 
|---|
| [7188b1] | 75 |       if (_bond->rightatom->getType() != NULL) {
 | 
|---|
| [ed26ae] | 76 |         elementno = _bond->rightatom->getType()->getAtomicNumber();
 | 
|---|
| [7188b1] | 77 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
 | 78 |         elementno = 1;
 | 
|---|
 | 79 |       }
 | 
|---|
 | 80 | 
 | 
|---|
| [907636] | 81 |       break;
 | 
|---|
 | 82 |     default:
 | 
|---|
 | 83 |       ASSERT(0,
 | 
|---|
| [2ad1ec] | 84 |           "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| [907636] | 85 |       break;
 | 
|---|
 | 86 |   }
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
 | 89 |   setMaterial(elementmaterial);
 | 
|---|
 | 90 | 
 | 
|---|
| [343a4b] | 91 |   resetPosition();
 | 
|---|
| [907636] | 92 | }
 | 
|---|
| [2ad1ec] | 93 | 
 | 
|---|
 | 94 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
 | 
|---|
 | 95 | {
 | 
|---|
 | 96 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
 | 97 |   _bond->signOff(this, BondObservable::BondRemoved);
 | 
|---|
| [343a4b] | 98 |   _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
 | 99 |   _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| [2ad1ec] | 100 | 
 | 
|---|
 | 101 |   LOG(2, "INFO: Destroying  GLMoleculeObject_bond to bond " << *_bond << " and side " << BondSide << ".");
 | 
|---|
 | 102 | }
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | void GLMoleculeObject_bond::update(Observable *publisher)
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 | #ifdef LOG_OBSERVER
 | 
|---|
 | 107 |   observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from bond " << *_bond << ".";
 | 
|---|
 | 108 | #endif
 | 
|---|
 | 109 | }
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
 | 
|---|
 | 112 | {
 | 
|---|
 | 113 |   LOG(2, "INFO: Received subjectKilled from " << *_bond << ".");
 | 
|---|
 | 114 |   switch (BondSide) {
 | 
|---|
 | 115 |     case left:
 | 
|---|
 | 116 |       emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
 | 
|---|
 | 117 |       break;
 | 
|---|
 | 118 |     case right:
 | 
|---|
 | 119 |       emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
 | 
|---|
 | 120 |       break;
 | 
|---|
 | 121 |     default:
 | 
|---|
 | 122 |       ASSERT(0,
 | 
|---|
 | 123 |           "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
 | 124 |       break;
 | 
|---|
 | 125 |   }
 | 
|---|
 | 126 |   delete this;
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
 | 130 | {
 | 
|---|
 | 131 | #ifdef LOG_OBSERVER
 | 
|---|
 | 132 |   observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
 | 
|---|
 | 133 |       << " received notification from bond " << *_bond << " for channel "
 | 
|---|
 | 134 |       << notification->getChannelNo() << ".";
 | 
|---|
 | 135 | #endif
 | 
|---|
| [343a4b] | 136 |   if (publisher == dynamic_cast<const Observable*>(_bond)){
 | 
|---|
 | 137 |     // from the bond
 | 
|---|
 | 138 |     switch (notification->getChannelNo()) {
 | 
|---|
 | 139 |       case BondObservable::BondRemoved:
 | 
|---|
 | 140 |         LOG(2, "INFO: Received notification of BondRemoved from " << *_bond << ".");
 | 
|---|
 | 141 |         switch (BondSide) {
 | 
|---|
 | 142 |           case left:
 | 
|---|
 | 143 |             emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
 | 
|---|
 | 144 |             break;
 | 
|---|
 | 145 |           case right:
 | 
|---|
 | 146 |             emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
 | 
|---|
 | 147 |             break;
 | 
|---|
 | 148 |           default:
 | 
|---|
 | 149 |             ASSERT(0,
 | 
|---|
 | 150 |                 "GLMoleculeObject_bond::recieveNotification() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
 | 151 |             break;
 | 
|---|
 | 152 |         }
 | 
|---|
 | 153 |         delete this;
 | 
|---|
 | 154 |         break;
 | 
|---|
 | 155 |           default:
 | 
|---|
 | 156 |             break;
 | 
|---|
 | 157 |     }
 | 
|---|
 | 158 |   }else{
 | 
|---|
 | 159 |     // from an atom
 | 
|---|
 | 160 |     switch (notification->getChannelNo()) {
 | 
|---|
 | 161 |       case AtomObservable::PositionChanged:
 | 
|---|
 | 162 |         LOG(2, "INFO: Received notification of PositionChanged.");
 | 
|---|
 | 163 |         resetPosition();
 | 
|---|
| [5a2a06] | 164 |         emit changed();
 | 
|---|
| [343a4b] | 165 |     }
 | 
|---|
 | 166 |   }
 | 
|---|
 | 167 | }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 | void GLMoleculeObject_bond::resetPosition()
 | 
|---|
 | 170 | {
 | 
|---|
 | 171 |   Vector Position;
 | 
|---|
 | 172 |   Vector OtherPosition;
 | 
|---|
 | 173 |   switch (BondSide) {
 | 
|---|
 | 174 |     case left:
 | 
|---|
 | 175 |       Position = _bond->leftatom->getPosition();
 | 
|---|
 | 176 |       OtherPosition = _bond->rightatom->getPosition();
 | 
|---|
 | 177 |       break;
 | 
|---|
 | 178 |     case right:
 | 
|---|
 | 179 |       Position = _bond->rightatom->getPosition();
 | 
|---|
 | 180 |       OtherPosition = _bond->leftatom->getPosition();
 | 
|---|
| [2ad1ec] | 181 |       break;
 | 
|---|
 | 182 |     default:
 | 
|---|
| [343a4b] | 183 |       ASSERT(0,
 | 
|---|
 | 184 |           "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| [2ad1ec] | 185 |       break;
 | 
|---|
 | 186 |   }
 | 
|---|
| [fbb1f1] | 187 |   const double distance =
 | 
|---|
 | 188 |       Position.distance(OtherPosition)/2.;
 | 
|---|
 | 189 |   setScaleZ(distance);
 | 
|---|
| [343a4b] | 190 | 
 | 
|---|
 | 191 |   // calculate position
 | 
|---|
 | 192 |   Vector Z(0.,0.,1.);
 | 
|---|
 | 193 |   Vector zeroVec(0.,0.,0.);
 | 
|---|
 | 194 |   Vector a,b;
 | 
|---|
 | 195 |   Vector OtherAxis;
 | 
|---|
 | 196 |   double alpha;
 | 
|---|
 | 197 |   a = Position - OtherPosition;
 | 
|---|
 | 198 |   // construct rotation axis
 | 
|---|
 | 199 |   b = a;
 | 
|---|
 | 200 |   b.VectorProduct(Z);
 | 
|---|
 | 201 |   Line axis(zeroVec, b);
 | 
|---|
 | 202 |   // calculate rotation angle
 | 
|---|
 | 203 |   alpha = a.Angle(Z);
 | 
|---|
 | 204 |   // construct other axis to check right-hand rule
 | 
|---|
 | 205 |   OtherAxis = b;
 | 
|---|
 | 206 |   OtherAxis.VectorProduct(Z);
 | 
|---|
 | 207 |   // assure right-hand rule for the rotation
 | 
|---|
 | 208 |   if (a.ScalarProduct(OtherAxis) < MYEPSILON)
 | 
|---|
 | 209 |     alpha = M_PI-alpha;
 | 
|---|
 | 210 |   // check
 | 
|---|
 | 211 |   Vector a_rotated = axis.rotateVector(a, alpha);
 | 
|---|
 | 212 |   LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
 | 
|---|
 | 213 |       << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |   // set position
 | 
|---|
 | 216 |   setPosition(QVector3D(Position[0], Position[1], Position[2]));
 | 
|---|
 | 217 |   setRotationVector(QVector3D(b[0], b[1], b[2]));
 | 
|---|
 | 218 |   setRotationAngle(alpha/M_PI*180.);
 | 
|---|
| [2ad1ec] | 219 | }
 | 
|---|