[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);
|
---|
[4b62d3] | 71 | _bond.signOn(this, BondObservable::DegreeChanged);
|
---|
[70db8f] | 72 | leftatom->signOn(this, AtomObservable::PositionChanged);
|
---|
| 73 | leftatom->signOn(this, AtomObservable::ElementChanged);
|
---|
| 74 | rightatom->signOn(this, AtomObservable::PositionChanged);
|
---|
| 75 | rightatom->signOn(this, AtomObservable::ElementChanged);
|
---|
[2ad1ec] | 76 |
|
---|
[907636] | 77 | size_t elementno = 0;
|
---|
[2ad1ec] | 78 | switch (BondSide) {
|
---|
[907636] | 79 | case left:
|
---|
[70db8f] | 80 | if (_bond.rightatom->getType() != NULL) {
|
---|
| 81 | elementno = _bond.rightatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 82 | } else { // if not element yet set, set to hydrogen
|
---|
| 83 | elementno = 1;
|
---|
| 84 | }
|
---|
[907636] | 85 | break;
|
---|
| 86 | case right:
|
---|
[70db8f] | 87 | if (_bond.leftatom->getType() != NULL) {
|
---|
| 88 | elementno = _bond.leftatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 89 | } else { // if not element yet set, set to hydrogen
|
---|
| 90 | elementno = 1;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[907636] | 93 | break;
|
---|
| 94 | default:
|
---|
| 95 | ASSERT(0,
|
---|
[2ad1ec] | 96 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[907636] | 97 | break;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 101 | setMaterial(elementmaterial);
|
---|
| 102 |
|
---|
[343a4b] | 103 | resetPosition();
|
---|
[4b62d3] | 104 | resetWidth();
|
---|
[907636] | 105 | }
|
---|
[2ad1ec] | 106 |
|
---|
| 107 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
| 108 | {
|
---|
[70db8f] | 109 | // sign off
|
---|
| 110 | switch (BondSide) {
|
---|
| 111 | case left:
|
---|
| 112 | emit BondRemoved(leftatomId, rightatomId);
|
---|
| 113 | break;
|
---|
| 114 | case right:
|
---|
| 115 | emit BondRemoved(rightatomId, leftatomId);
|
---|
| 116 | break;
|
---|
| 117 | default:
|
---|
| 118 | ASSERT(0,
|
---|
| 119 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
|
---|
| 120 | +toString(BondSide)+".");
|
---|
| 121 | break;
|
---|
[49c965] | 122 | }
|
---|
[70db8f] | 123 | _bond.signOff(this, BondObservable::BondRemoved);
|
---|
[4b62d3] | 124 | _bond.signOff(this, BondObservable::DegreeChanged);
|
---|
[70db8f] | 125 | leftatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 126 | leftatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 127 | rightatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 128 | rightatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 129 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
|
---|
[2ad1ec] | 130 | }
|
---|
| 131 |
|
---|
| 132 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
| 133 | {
|
---|
| 134 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 135 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 136 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 137 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 138 | << " from bond.";
|
---|
| 139 | } else if (publisher == leftatom) {
|
---|
| 140 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 141 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 142 | << " from leftatom " << _bond.leftatom->getId() << ".";
|
---|
| 143 | } else if (publisher == rightatom) {
|
---|
| 144 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 145 | observerLog().getName(static_cast<Observer*>(this))
|
---|
| 146 | << " from rightatom " << _bond.rightatom->getId() << ".";
|
---|
| 147 | } else
|
---|
| 148 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 149 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
[2ad1ec] | 150 | #endif
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
| 154 | {
|
---|
| 155 | delete this;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 159 | {
|
---|
| 160 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 161 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 162 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 163 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 164 | << " received notification from bond for channel "
|
---|
| 165 | << notification->getChannelNo() << ".";
|
---|
| 166 | } else if (publisher == leftatom) {
|
---|
| 167 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 168 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 169 | << " received notification from leftatom " << _bond.leftatom->getId() << " for channel "
|
---|
| 170 | << notification->getChannelNo() << ".";
|
---|
| 171 | } else if (publisher == rightatom) {
|
---|
| 172 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 173 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 174 | << " received notification from rightatom " << _bond.rightatom->getId() << " for channel "
|
---|
| 175 | << notification->getChannelNo() << ".";
|
---|
| 176 | } else
|
---|
| 177 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 178 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 179 | << " received notification from unknown source.";
|
---|
[2ad1ec] | 180 | #endif
|
---|
[4b62d3] | 181 | bool DoResetPosition = false;
|
---|
| 182 | bool DoResetWidth = false;
|
---|
[70db8f] | 183 | if (publisher == static_cast<const Observable *>(&_bond)){
|
---|
[1f693d] | 184 | switch (notification->getChannelNo()) {
|
---|
| 185 | case BondObservable::BondRemoved:
|
---|
| 186 | delete this;
|
---|
| 187 | break;
|
---|
[4b62d3] | 188 | case BondObservable::DegreeChanged:
|
---|
| 189 | DoResetWidth = true;
|
---|
| 190 | break;
|
---|
| 191 | default:
|
---|
| 192 | ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
|
---|
| 193 | break;
|
---|
[1f693d] | 194 | }
|
---|
[70db8f] | 195 | } else {
|
---|
[343a4b] | 196 | // from an atom
|
---|
| 197 | switch (notification->getChannelNo()) {
|
---|
[70db8f] | 198 | case AtomObservable::PositionChanged:
|
---|
| 199 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
| 200 | DoResetPosition = true;
|
---|
| 201 | break;
|
---|
| 202 | case AtomObservable::ElementChanged:
|
---|
| 203 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
| 204 | DoResetPosition = true;
|
---|
| 205 | break;
|
---|
| 206 | default:
|
---|
| 207 | break;
|
---|
| 208 | }
|
---|
[343a4b] | 209 | }
|
---|
[4b62d3] | 210 | if (DoResetPosition) {
|
---|
| 211 | resetPosition();
|
---|
| 212 | emit changed();
|
---|
| 213 | }
|
---|
| 214 | if (DoResetWidth) {
|
---|
| 215 | resetWidth();
|
---|
| 216 | emit changed();
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | void GLMoleculeObject_bond::resetWidth()
|
---|
| 221 | {
|
---|
| 222 | const double factor = 1.0f+.5f*(_bond.getDegree()-1);
|
---|
| 223 | LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
|
---|
| 224 | setScaleX(factor);
|
---|
| 225 | setScaleY(factor);
|
---|
[343a4b] | 226 | }
|
---|
| 227 |
|
---|
| 228 | void GLMoleculeObject_bond::resetPosition()
|
---|
| 229 | {
|
---|
| 230 | Vector Position;
|
---|
| 231 | Vector OtherPosition;
|
---|
| 232 | switch (BondSide) {
|
---|
| 233 | case left:
|
---|
[70db8f] | 234 | Position = _bond.leftatom->getPosition();
|
---|
| 235 | OtherPosition = _bond.rightatom->getPosition();
|
---|
[343a4b] | 236 | break;
|
---|
| 237 | case right:
|
---|
[70db8f] | 238 | Position = _bond.rightatom->getPosition();
|
---|
| 239 | OtherPosition = _bond.leftatom->getPosition();
|
---|
[2ad1ec] | 240 | break;
|
---|
| 241 | default:
|
---|
[343a4b] | 242 | ASSERT(0,
|
---|
| 243 | "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[2ad1ec] | 244 | break;
|
---|
| 245 | }
|
---|
[fbb1f1] | 246 | const double distance =
|
---|
| 247 | Position.distance(OtherPosition)/2.;
|
---|
| 248 | setScaleZ(distance);
|
---|
[343a4b] | 249 |
|
---|
| 250 | // calculate position
|
---|
[37e910] | 251 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
[343a4b] | 252 | Vector zeroVec(0.,0.,0.);
|
---|
| 253 | Vector a,b;
|
---|
| 254 | Vector OtherAxis;
|
---|
| 255 | double alpha;
|
---|
| 256 | a = Position - OtherPosition;
|
---|
| 257 | // construct rotation axis
|
---|
| 258 | b = a;
|
---|
| 259 | b.VectorProduct(Z);
|
---|
| 260 | Line axis(zeroVec, b);
|
---|
| 261 | // calculate rotation angle
|
---|
| 262 | alpha = a.Angle(Z);
|
---|
| 263 | // construct other axis to check right-hand rule
|
---|
| 264 | OtherAxis = b;
|
---|
| 265 | OtherAxis.VectorProduct(Z);
|
---|
| 266 | // assure right-hand rule for the rotation
|
---|
| 267 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
| 268 | alpha = M_PI-alpha;
|
---|
| 269 | // check
|
---|
| 270 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
| 271 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
| 272 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
| 273 |
|
---|
[37e910] | 274 | // set position (cylinder offset is in its barymetric center)
|
---|
| 275 | Vector OneFourth(Position - 0.75 * a);
|
---|
| 276 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
[343a4b] | 277 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
| 278 | setRotationAngle(alpha/M_PI*180.);
|
---|
[2ad1ec] | 279 | }
|
---|