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