[907636] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[907636] | 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * GLMoleculeObject_bond.cpp
|
---|
| 25 | *
|
---|
| 26 | * Created on: Aug 17, 2011
|
---|
| 27 | * Author: heber
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
| 35 | #include "GLMoleculeObject_bond.hpp"
|
---|
| 36 |
|
---|
| 37 | #include <Qt3D/qglmaterial.h>
|
---|
| 38 | #include <Qt3D/qglscenenode.h>
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 41 |
|
---|
[856d05] | 42 |
|
---|
[907636] | 43 | #include <cmath>
|
---|
| 44 |
|
---|
| 45 | #include "CodePatterns/Assert.hpp"
|
---|
[57a770] | 46 | #include "CodePatterns/Log.hpp"
|
---|
[2ad1ec] | 47 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
[856d05] | 48 | #include "CodePatterns/Observer/ObserverLog.hpp"
|
---|
[6f0841] | 49 | #include "Atom/atom.hpp"
|
---|
[907636] | 50 | #include "Bond/bond.hpp"
|
---|
[3bdb6d] | 51 | #include "Element/element.hpp"
|
---|
[907636] | 52 | #include "Helpers/defs.hpp"
|
---|
| 53 | #include "LinearAlgebra/Line.hpp"
|
---|
| 54 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 55 |
|
---|
[88c8ec] | 56 | GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
|
---|
[bca99d] | 57 | GLMoleculeObject(mesh, parent),
|
---|
[2ad1ec] | 58 | Observer(std::string("GLMoleculeObject_bond")
|
---|
| 59 | +toString(bondref->leftatom->getId())
|
---|
| 60 | +std::string("-")
|
---|
| 61 | +toString(bondref->rightatom->getId())),
|
---|
[70db8f] | 62 | _bond(*bondref),
|
---|
| 63 | leftatom(bondref->leftatom),
|
---|
| 64 | rightatom(bondref->rightatom),
|
---|
| 65 | leftatomId(bondref->leftatom->getId()),
|
---|
| 66 | rightatomId(bondref->rightatom->getId()),
|
---|
[2ad1ec] | 67 | BondSide(side)
|
---|
[907636] | 68 | {
|
---|
[2ad1ec] | 69 | // sign on as observer (obtain non-const instance before)
|
---|
[70db8f] | 70 | _bond.signOn(this, BondObservable::BondRemoved);
|
---|
| 71 | leftatom->signOn(this, AtomObservable::PositionChanged);
|
---|
| 72 | leftatom->signOn(this, AtomObservable::ElementChanged);
|
---|
| 73 | rightatom->signOn(this, AtomObservable::PositionChanged);
|
---|
| 74 | rightatom->signOn(this, AtomObservable::ElementChanged);
|
---|
[2ad1ec] | 75 |
|
---|
[907636] | 76 | size_t elementno = 0;
|
---|
[2ad1ec] | 77 | switch (BondSide) {
|
---|
[907636] | 78 | case left:
|
---|
[70db8f] | 79 | if (_bond.rightatom->getType() != NULL) {
|
---|
| 80 | elementno = _bond.rightatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 81 | } else { // if not element yet set, set to hydrogen
|
---|
| 82 | elementno = 1;
|
---|
| 83 | }
|
---|
[907636] | 84 | break;
|
---|
| 85 | case right:
|
---|
[70db8f] | 86 | if (_bond.leftatom->getType() != NULL) {
|
---|
| 87 | elementno = _bond.leftatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 88 | } else { // if not element yet set, set to hydrogen
|
---|
| 89 | elementno = 1;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[907636] | 92 | break;
|
---|
| 93 | default:
|
---|
| 94 | ASSERT(0,
|
---|
[2ad1ec] | 95 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[907636] | 96 | break;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 100 | setMaterial(elementmaterial);
|
---|
| 101 |
|
---|
[343a4b] | 102 | resetPosition();
|
---|
[907636] | 103 | }
|
---|
[2ad1ec] | 104 |
|
---|
| 105 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
| 106 | {
|
---|
[70db8f] | 107 | // sign off
|
---|
| 108 | switch (BondSide) {
|
---|
| 109 | case left:
|
---|
| 110 | emit BondRemoved(leftatomId, rightatomId);
|
---|
| 111 | break;
|
---|
| 112 | case right:
|
---|
| 113 | emit BondRemoved(rightatomId, leftatomId);
|
---|
| 114 | break;
|
---|
| 115 | default:
|
---|
| 116 | ASSERT(0,
|
---|
| 117 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
|
---|
| 118 | +toString(BondSide)+".");
|
---|
| 119 | break;
|
---|
[49c965] | 120 | }
|
---|
[70db8f] | 121 | _bond.signOff(this, BondObservable::BondRemoved);
|
---|
| 122 | leftatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 123 | leftatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 124 | rightatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 125 | rightatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 126 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
|
---|
[2ad1ec] | 127 | }
|
---|
| 128 |
|
---|
| 129 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
| 130 | {
|
---|
| 131 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 132 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 133 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 134 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 135 | << " from bond.";
|
---|
| 136 | } else if (publisher == leftatom) {
|
---|
| 137 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 138 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 139 | << " from leftatom " << _bond.leftatom->getId() << ".";
|
---|
| 140 | } else if (publisher == rightatom) {
|
---|
| 141 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 142 | observerLog().getName(static_cast<Observer*>(this))
|
---|
| 143 | << " from rightatom " << _bond.rightatom->getId() << ".";
|
---|
| 144 | } else
|
---|
| 145 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 146 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
[2ad1ec] | 147 | #endif
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
| 151 | {
|
---|
| 152 | delete this;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 156 | {
|
---|
| 157 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 158 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 159 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 160 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 161 | << " received notification from bond for channel "
|
---|
| 162 | << notification->getChannelNo() << ".";
|
---|
| 163 | } else if (publisher == leftatom) {
|
---|
| 164 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 165 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 166 | << " received notification from leftatom " << _bond.leftatom->getId() << " for channel "
|
---|
| 167 | << notification->getChannelNo() << ".";
|
---|
| 168 | } else if (publisher == rightatom) {
|
---|
| 169 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 170 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 171 | << " received notification from rightatom " << _bond.rightatom->getId() << " for channel "
|
---|
| 172 | << notification->getChannelNo() << ".";
|
---|
| 173 | } else
|
---|
| 174 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 175 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 176 | << " received notification from unknown source.";
|
---|
[2ad1ec] | 177 | #endif
|
---|
[70db8f] | 178 | if (publisher == static_cast<const Observable *>(&_bond)){
|
---|
| 179 | delete this;
|
---|
| 180 | } else {
|
---|
| 181 | bool DoResetPosition = false;
|
---|
[343a4b] | 182 | // from an atom
|
---|
| 183 | switch (notification->getChannelNo()) {
|
---|
[70db8f] | 184 | case AtomObservable::PositionChanged:
|
---|
| 185 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
| 186 | DoResetPosition = true;
|
---|
| 187 | break;
|
---|
| 188 | case AtomObservable::ElementChanged:
|
---|
| 189 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
| 190 | DoResetPosition = true;
|
---|
| 191 | break;
|
---|
| 192 | default:
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 | if (DoResetPosition) {
|
---|
[343a4b] | 196 | resetPosition();
|
---|
[5a2a06] | 197 | emit changed();
|
---|
[343a4b] | 198 | }
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | void GLMoleculeObject_bond::resetPosition()
|
---|
| 203 | {
|
---|
| 204 | Vector Position;
|
---|
| 205 | Vector OtherPosition;
|
---|
| 206 | switch (BondSide) {
|
---|
| 207 | case left:
|
---|
[70db8f] | 208 | Position = _bond.leftatom->getPosition();
|
---|
| 209 | OtherPosition = _bond.rightatom->getPosition();
|
---|
[343a4b] | 210 | break;
|
---|
| 211 | case right:
|
---|
[70db8f] | 212 | Position = _bond.rightatom->getPosition();
|
---|
| 213 | OtherPosition = _bond.leftatom->getPosition();
|
---|
[2ad1ec] | 214 | break;
|
---|
| 215 | default:
|
---|
[343a4b] | 216 | ASSERT(0,
|
---|
| 217 | "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[2ad1ec] | 218 | break;
|
---|
| 219 | }
|
---|
[fbb1f1] | 220 | const double distance =
|
---|
| 221 | Position.distance(OtherPosition)/2.;
|
---|
| 222 | setScaleZ(distance);
|
---|
[343a4b] | 223 |
|
---|
| 224 | // calculate position
|
---|
[37e910] | 225 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
[343a4b] | 226 | Vector zeroVec(0.,0.,0.);
|
---|
| 227 | Vector a,b;
|
---|
| 228 | Vector OtherAxis;
|
---|
| 229 | double alpha;
|
---|
| 230 | a = Position - OtherPosition;
|
---|
| 231 | // construct rotation axis
|
---|
| 232 | b = a;
|
---|
| 233 | b.VectorProduct(Z);
|
---|
| 234 | Line axis(zeroVec, b);
|
---|
| 235 | // calculate rotation angle
|
---|
| 236 | alpha = a.Angle(Z);
|
---|
| 237 | // construct other axis to check right-hand rule
|
---|
| 238 | OtherAxis = b;
|
---|
| 239 | OtherAxis.VectorProduct(Z);
|
---|
| 240 | // assure right-hand rule for the rotation
|
---|
| 241 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
| 242 | alpha = M_PI-alpha;
|
---|
| 243 | // check
|
---|
| 244 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
| 245 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
| 246 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
| 247 |
|
---|
[37e910] | 248 | // set position (cylinder offset is in its barymetric center)
|
---|
| 249 | Vector OneFourth(Position - 0.75 * a);
|
---|
| 250 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
[343a4b] | 251 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
| 252 | setRotationAngle(alpha/M_PI*180.);
|
---|
[2ad1ec] | 253 | }
|
---|