[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"
|
---|
[c60665] | 50 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[6f0841] | 51 | #include "Atom/atom.hpp"
|
---|
[907636] | 52 | #include "Bond/bond.hpp"
|
---|
[3bdb6d] | 53 | #include "Element/element.hpp"
|
---|
[907636] | 54 | #include "Helpers/defs.hpp"
|
---|
| 55 | #include "LinearAlgebra/Line.hpp"
|
---|
| 56 | #include "LinearAlgebra/Vector.hpp"
|
---|
[c60665] | 57 | #include "World.hpp"
|
---|
[907636] | 58 |
|
---|
[88c8ec] | 59 | GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
|
---|
[bca99d] | 60 | GLMoleculeObject(mesh, parent),
|
---|
[2ad1ec] | 61 | Observer(std::string("GLMoleculeObject_bond")
|
---|
| 62 | +toString(bondref->leftatom->getId())
|
---|
| 63 | +std::string("-")
|
---|
| 64 | +toString(bondref->rightatom->getId())),
|
---|
[70db8f] | 65 | _bond(*bondref),
|
---|
[c60665] | 66 | leftobservable(bondref->leftatom),
|
---|
| 67 | rightobservable(bondref->rightatom),
|
---|
[70db8f] | 68 | leftatomId(bondref->leftatom->getId()),
|
---|
| 69 | rightatomId(bondref->rightatom->getId()),
|
---|
[c60665] | 70 | BondSide(side),
|
---|
| 71 | leftobservable_enabled(false),
|
---|
| 72 | rightobservable_enabled(false),
|
---|
| 73 | bond_enabled(false)
|
---|
[907636] | 74 | {
|
---|
[2ad1ec] | 75 | // sign on as observer (obtain non-const instance before)
|
---|
[c60665] | 76 | _bond.signOn(this);
|
---|
[70db8f] | 77 | _bond.signOn(this, BondObservable::BondRemoved);
|
---|
[4b62d3] | 78 | _bond.signOn(this, BondObservable::DegreeChanged);
|
---|
[c60665] | 79 | bond_enabled = true;
|
---|
| 80 | leftobservable->signOn(this);
|
---|
| 81 | leftobservable->signOn(this, AtomObservable::PositionChanged);
|
---|
| 82 | leftobservable->signOn(this, AtomObservable::ElementChanged);
|
---|
| 83 | leftobservable_enabled = true;
|
---|
| 84 | rightobservable->signOn(this);
|
---|
| 85 | rightobservable->signOn(this, AtomObservable::PositionChanged);
|
---|
| 86 | rightobservable->signOn(this, AtomObservable::ElementChanged);
|
---|
| 87 | rightobservable_enabled = true;
|
---|
[2ad1ec] | 88 |
|
---|
[907636] | 89 | size_t elementno = 0;
|
---|
[2ad1ec] | 90 | switch (BondSide) {
|
---|
[907636] | 91 | case left:
|
---|
[c60665] | 92 | {
|
---|
| 93 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
| 94 | if (_rightatom->getType() != NULL) {
|
---|
| 95 | elementno = _rightatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 96 | } else { // if not element yet set, set to hydrogen
|
---|
| 97 | elementno = 1;
|
---|
| 98 | }
|
---|
[907636] | 99 | break;
|
---|
[c60665] | 100 | }
|
---|
[907636] | 101 | case right:
|
---|
[c60665] | 102 | {
|
---|
| 103 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
| 104 | if (_leftatom->getType() != NULL) {
|
---|
| 105 | elementno = _leftatom->getType()->getAtomicNumber();
|
---|
[7188b1] | 106 | } else { // if not element yet set, set to hydrogen
|
---|
| 107 | elementno = 1;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[907636] | 110 | break;
|
---|
[c60665] | 111 | }
|
---|
[907636] | 112 | default:
|
---|
| 113 | ASSERT(0,
|
---|
[2ad1ec] | 114 | "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[907636] | 115 | break;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 119 | setMaterial(elementmaterial);
|
---|
| 120 |
|
---|
[343a4b] | 121 | resetPosition();
|
---|
[4b62d3] | 122 | resetWidth();
|
---|
[907636] | 123 | }
|
---|
[2ad1ec] | 124 |
|
---|
| 125 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
[c60665] | 126 | {
|
---|
| 127 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond " << &_bond << " and side " << BondSide << ".");
|
---|
| 128 | // signOff() if not already done
|
---|
| 129 | removeLeftAtom();
|
---|
| 130 | removeRightAtom();
|
---|
| 131 | removeBond();
|
---|
| 132 | removeChannels();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | void GLMoleculeObject_bond::removeLeftAtom()
|
---|
| 136 | {
|
---|
| 137 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
| 138 | // from the AtomObservable itself
|
---|
| 139 | if (leftobservable_enabled) {
|
---|
| 140 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
| 141 | if (_leftatom != NULL) {
|
---|
| 142 | _leftatom->signOff(this);
|
---|
| 143 | } else {
|
---|
| 144 | ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | void GLMoleculeObject_bond::removeRightAtom()
|
---|
| 150 | {
|
---|
| 151 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
| 152 | // from the AtomObservable itself
|
---|
| 153 | if (rightobservable_enabled) {
|
---|
| 154 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
| 155 | if (_rightatom != NULL) {
|
---|
| 156 | _rightatom->signOff(this);
|
---|
| 157 | } else {
|
---|
| 158 | ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void GLMoleculeObject_bond::removeBond()
|
---|
| 164 | {
|
---|
| 165 | if (bond_enabled)
|
---|
| 166 | _bond.signOff(this);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | void GLMoleculeObject_bond::removeChannels()
|
---|
| 170 | {
|
---|
| 171 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
| 172 | // from the AtomObservable itself
|
---|
| 173 | if (leftobservable_enabled) {
|
---|
| 174 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
| 175 | if (_leftatom != NULL) {
|
---|
| 176 | _leftatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 177 | _leftatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 178 | } else {
|
---|
| 179 | ELOG(2, "Left atom of bond with id "+toString(leftatomId)+" is already gone.");
|
---|
| 180 | }
|
---|
| 181 | leftobservable_enabled = false;
|
---|
| 182 | }
|
---|
| 183 | if (rightobservable_enabled) {
|
---|
| 184 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
| 185 | if (_rightatom != NULL) {
|
---|
| 186 | _rightatom->signOff(this, AtomObservable::PositionChanged);
|
---|
| 187 | _rightatom->signOff(this, AtomObservable::ElementChanged);
|
---|
| 188 | } else {
|
---|
| 189 | ELOG(2, "Right atom of bond with id "+toString(rightatomId)+" is already gone.");
|
---|
| 190 | }
|
---|
| 191 | rightobservable_enabled = false;
|
---|
| 192 | }
|
---|
| 193 | if (bond_enabled) {
|
---|
| 194 | _bond.signOff(this, BondObservable::BondRemoved);
|
---|
| 195 | _bond.signOff(this, BondObservable::DegreeChanged);
|
---|
| 196 | bond_enabled = false;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | void GLMoleculeObject_bond::removeMe()
|
---|
[2ad1ec] | 202 | {
|
---|
[70db8f] | 203 | // sign off
|
---|
| 204 | switch (BondSide) {
|
---|
| 205 | case left:
|
---|
| 206 | emit BondRemoved(leftatomId, rightatomId);
|
---|
| 207 | break;
|
---|
| 208 | case right:
|
---|
| 209 | emit BondRemoved(rightatomId, leftatomId);
|
---|
| 210 | break;
|
---|
| 211 | default:
|
---|
| 212 | ASSERT(0,
|
---|
| 213 | "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "
|
---|
| 214 | +toString(BondSide)+".");
|
---|
| 215 | break;
|
---|
[49c965] | 216 | }
|
---|
[2ad1ec] | 217 | }
|
---|
| 218 |
|
---|
| 219 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
| 220 | {
|
---|
| 221 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 222 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 223 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 224 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 225 | << " from bond.";
|
---|
[c60665] | 226 | } else if (publisher == leftobservable) {
|
---|
[70db8f] | 227 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 228 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 229 | << " from leftatom " << leftatomId << ".";
|
---|
| 230 | } else if (publisher == rightobservable) {
|
---|
[70db8f] | 231 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 232 | observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 233 | << " from rightatom " << rightatomId << ".";
|
---|
[70db8f] | 234 | } else
|
---|
| 235 | observerLog().addMessage() << "++ Update of Observer " <<
|
---|
| 236 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
[2ad1ec] | 237 | #endif
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
| 241 | {
|
---|
[c60665] | 242 | // assume subjectKilled() is from Observable's own subjectKilled(), not notifications
|
---|
| 243 | // but we must always signOff from all other sources!
|
---|
| 244 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 245 | #ifdef LOG_OBSERVER
|
---|
| 246 | observerLog().addMessage() << "++ subjectKilled of Observer "
|
---|
| 247 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 248 | << " from bond.";
|
---|
| 249 | #endif
|
---|
| 250 | removeLeftAtom();
|
---|
| 251 | removeRightAtom();
|
---|
| 252 | } else if (publisher == leftobservable) {
|
---|
| 253 | #ifdef LOG_OBSERVER
|
---|
| 254 | observerLog().addMessage() << "++ subjectKilled of Observer "
|
---|
| 255 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 256 | << " from leftatom " << leftatomId << ".";
|
---|
| 257 | #endif
|
---|
| 258 | removeRightAtom();
|
---|
| 259 | removeBond();
|
---|
| 260 | } else if (publisher == rightobservable) {
|
---|
| 261 | #ifdef LOG_OBSERVER
|
---|
| 262 | observerLog().addMessage() << "++ subjectKilled of Observer " <<
|
---|
| 263 | observerLog().getName(static_cast<Observer*>(this))
|
---|
| 264 | << " from rightatom " << rightatomId << ".";
|
---|
| 265 | #endif
|
---|
| 266 | removeLeftAtom();
|
---|
| 267 | removeBond();
|
---|
| 268 | } else {
|
---|
| 269 | #ifdef LOG_OBSERVER
|
---|
| 270 | observerLog().addMessage() << "++ subjectKilled of Observer " <<
|
---|
| 271 | observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
|
---|
| 272 | #endif
|
---|
| 273 | }
|
---|
| 274 | // then indicate to remove us
|
---|
| 275 | removeChannels();
|
---|
| 276 | removeMe();
|
---|
[2ad1ec] | 277 | }
|
---|
| 278 |
|
---|
| 279 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 280 | {
|
---|
| 281 | #ifdef LOG_OBSERVER
|
---|
[70db8f] | 282 | if (publisher == static_cast<const Observable *>(&_bond)) {
|
---|
| 283 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 284 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 285 | << " received notification from bond for channel "
|
---|
| 286 | << notification->getChannelNo() << ".";
|
---|
[c60665] | 287 | } else if (publisher == leftobservable) {
|
---|
[70db8f] | 288 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 289 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 290 | << " received notification from leftatom " << leftatomId << " for channel "
|
---|
[70db8f] | 291 | << notification->getChannelNo() << ".";
|
---|
[c60665] | 292 | } else if (publisher == rightobservable) {
|
---|
[70db8f] | 293 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 294 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 295 | << " received notification from rightatom " << rightatomId << " for channel "
|
---|
[70db8f] | 296 | << notification->getChannelNo() << ".";
|
---|
| 297 | } else
|
---|
| 298 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 299 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 300 | << " received notification from unknown source.";
|
---|
[2ad1ec] | 301 | #endif
|
---|
[4b62d3] | 302 | bool DoResetPosition = false;
|
---|
| 303 | bool DoResetWidth = false;
|
---|
[70db8f] | 304 | if (publisher == static_cast<const Observable *>(&_bond)){
|
---|
[1f693d] | 305 | switch (notification->getChannelNo()) {
|
---|
| 306 | case BondObservable::BondRemoved:
|
---|
[c60665] | 307 | // removeMe();
|
---|
[1f693d] | 308 | break;
|
---|
[4b62d3] | 309 | case BondObservable::DegreeChanged:
|
---|
| 310 | DoResetWidth = true;
|
---|
| 311 | break;
|
---|
| 312 | default:
|
---|
| 313 | ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
|
---|
| 314 | break;
|
---|
[1f693d] | 315 | }
|
---|
[70db8f] | 316 | } else {
|
---|
[343a4b] | 317 | // from an atom
|
---|
| 318 | switch (notification->getChannelNo()) {
|
---|
[70db8f] | 319 | case AtomObservable::PositionChanged:
|
---|
| 320 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
| 321 | DoResetPosition = true;
|
---|
| 322 | break;
|
---|
| 323 | case AtomObservable::ElementChanged:
|
---|
| 324 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
| 325 | DoResetPosition = true;
|
---|
| 326 | break;
|
---|
| 327 | default:
|
---|
| 328 | break;
|
---|
| 329 | }
|
---|
[343a4b] | 330 | }
|
---|
[4b62d3] | 331 | if (DoResetPosition) {
|
---|
| 332 | resetPosition();
|
---|
| 333 | emit changed();
|
---|
| 334 | }
|
---|
| 335 | if (DoResetWidth) {
|
---|
| 336 | resetWidth();
|
---|
| 337 | emit changed();
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | void GLMoleculeObject_bond::resetWidth()
|
---|
| 342 | {
|
---|
| 343 | const double factor = 1.0f+.5f*(_bond.getDegree()-1);
|
---|
| 344 | LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
|
---|
| 345 | setScaleX(factor);
|
---|
| 346 | setScaleY(factor);
|
---|
[343a4b] | 347 | }
|
---|
| 348 |
|
---|
| 349 | void GLMoleculeObject_bond::resetPosition()
|
---|
| 350 | {
|
---|
| 351 | Vector Position;
|
---|
| 352 | Vector OtherPosition;
|
---|
[c60665] | 353 | const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
|
---|
| 354 | Vector LeftPos = _leftatom->getPosition();
|
---|
| 355 | const atom *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
|
---|
| 356 | Vector RightPos = _rightatom->getPosition();
|
---|
[343a4b] | 357 | switch (BondSide) {
|
---|
| 358 | case left:
|
---|
[c60665] | 359 | Position = LeftPos;
|
---|
| 360 | OtherPosition = RightPos;
|
---|
[343a4b] | 361 | break;
|
---|
| 362 | case right:
|
---|
[c60665] | 363 | Position = RightPos;
|
---|
| 364 | OtherPosition = LeftPos;
|
---|
[2ad1ec] | 365 | break;
|
---|
| 366 | default:
|
---|
[343a4b] | 367 | ASSERT(0,
|
---|
| 368 | "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
|
---|
[2ad1ec] | 369 | break;
|
---|
| 370 | }
|
---|
[fbb1f1] | 371 | const double distance =
|
---|
| 372 | Position.distance(OtherPosition)/2.;
|
---|
| 373 | setScaleZ(distance);
|
---|
[343a4b] | 374 |
|
---|
| 375 | // calculate position
|
---|
[37e910] | 376 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
[343a4b] | 377 | Vector zeroVec(0.,0.,0.);
|
---|
| 378 | Vector a,b;
|
---|
| 379 | Vector OtherAxis;
|
---|
| 380 | double alpha;
|
---|
| 381 | a = Position - OtherPosition;
|
---|
| 382 | // construct rotation axis
|
---|
| 383 | b = a;
|
---|
| 384 | b.VectorProduct(Z);
|
---|
| 385 | Line axis(zeroVec, b);
|
---|
| 386 | // calculate rotation angle
|
---|
| 387 | alpha = a.Angle(Z);
|
---|
| 388 | // construct other axis to check right-hand rule
|
---|
| 389 | OtherAxis = b;
|
---|
| 390 | OtherAxis.VectorProduct(Z);
|
---|
| 391 | // assure right-hand rule for the rotation
|
---|
| 392 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
| 393 | alpha = M_PI-alpha;
|
---|
| 394 | // check
|
---|
| 395 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
| 396 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
| 397 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
| 398 |
|
---|
[37e910] | 399 | // set position (cylinder offset is in its barymetric center)
|
---|
| 400 | Vector OneFourth(Position - 0.75 * a);
|
---|
| 401 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
[343a4b] | 402 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
| 403 | setRotationAngle(alpha/M_PI*180.);
|
---|
[2ad1ec] | 404 | }
|
---|