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