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