[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 |
|
---|
[009e2e2] | 59 |
|
---|
| 60 | // static entities
|
---|
| 61 | const Observable::channels_t
|
---|
| 62 | GLMoleculeObject_bond::BondPositionChannels(1, AtomObservable::PositionChanged);
|
---|
| 63 | const Observable::channels_t
|
---|
| 64 | GLMoleculeObject_bond::BondDegreeChannels(1, BondObservable::DegreeChanged);
|
---|
| 65 | const Observable::channels_t
|
---|
| 66 | GLMoleculeObject_bond::BondElementChannels(1, AtomObservable::ElementChanged);
|
---|
| 67 |
|
---|
| 68 | GLMoleculeObject_bond::GLMoleculeObject_bond(
|
---|
| 69 | QGLSceneNode *mesh[],
|
---|
| 70 | QObject *parent,
|
---|
| 71 | const bondIds_t bondIds,
|
---|
| 72 | const enum SideOfBond side) :
|
---|
[bca99d] | 73 | GLMoleculeObject(mesh, parent),
|
---|
[2ad1ec] | 74 | Observer(std::string("GLMoleculeObject_bond")
|
---|
[009e2e2] | 75 | +toString(bondIds.first)
|
---|
[2ad1ec] | 76 | +std::string("-")
|
---|
[009e2e2] | 77 | +toString(bondIds.second)),
|
---|
| 78 | leftatomId(bondIds.first),
|
---|
| 79 | rightatomId(bondIds.second),
|
---|
[7e2adc] | 80 | leftowner(getAtom(leftatomId)),
|
---|
| 81 | rightowner(getAtom(rightatomId)),
|
---|
| 82 | bondowner(getAtom(leftatomId)->getBond(getAtom(rightatomId)).get()),
|
---|
[c60665] | 83 | BondSide(side),
|
---|
[009e2e2] | 84 | leftPosition(
|
---|
[7e2adc] | 85 | leftowner,
|
---|
[009e2e2] | 86 | boost::bind(&GLMoleculeObject_bond::updateLeftPosition, this),
|
---|
| 87 | "BondleftPosition_"+toString(leftatomId),
|
---|
| 88 | updateLeftPosition(),
|
---|
| 89 | BondPositionChannels),
|
---|
| 90 | rightPosition(
|
---|
[7e2adc] | 91 | rightowner,
|
---|
[009e2e2] | 92 | boost::bind(&GLMoleculeObject_bond::updateRightPosition, this),
|
---|
| 93 | "BondrightPosition_"+toString(rightatomId),
|
---|
| 94 | updateRightPosition(),
|
---|
| 95 | BondPositionChannels),
|
---|
| 96 | leftElement(
|
---|
[7e2adc] | 97 | leftowner,
|
---|
[009e2e2] | 98 | boost::bind(&GLMoleculeObject_bond::updateLeftElement, this),
|
---|
| 99 | "BondleftElement"+toString(leftatomId),
|
---|
| 100 | updateLeftElement(),
|
---|
| 101 | BondElementChannels),
|
---|
| 102 | rightElement(
|
---|
[7e2adc] | 103 | rightowner,
|
---|
[009e2e2] | 104 | boost::bind(&GLMoleculeObject_bond::updateRightElement, this),
|
---|
| 105 | "BondrightElement"+toString(rightatomId),
|
---|
| 106 | updateRightElement(),
|
---|
| 107 | BondElementChannels),
|
---|
| 108 | Degree(
|
---|
[7e2adc] | 109 | bondowner,
|
---|
[009e2e2] | 110 | boost::bind(&GLMoleculeObject_bond::updateDegree, this),
|
---|
| 111 | "BondDegree"+toString(leftatomId)+"_"+toString(rightatomId),
|
---|
| 112 | updateDegree(),
|
---|
| 113 | BondDegreeChannels),
|
---|
[c60665] | 114 | leftobservable_enabled(false),
|
---|
[009e2e2] | 115 | rightobservable_enabled(false)
|
---|
[907636] | 116 | {
|
---|
[2ad1ec] | 117 | // sign on as observer (obtain non-const instance before)
|
---|
[7e2adc] | 118 | bondowner->signOn(this, BondObservable::BondRemoved);
|
---|
| 119 | bondowner->signOn(this, BondObservable::DegreeChanged);
|
---|
[c60665] | 120 | bond_enabled = true;
|
---|
[7e2adc] | 121 | leftowner->signOn(this, AtomObservable::PositionChanged);
|
---|
| 122 | leftowner->signOn(this, AtomObservable::ElementChanged);
|
---|
[c60665] | 123 | leftobservable_enabled = true;
|
---|
[7e2adc] | 124 | rightowner->signOn(this, AtomObservable::PositionChanged);
|
---|
| 125 | rightowner->signOn(this, AtomObservable::ElementChanged);
|
---|
[c60665] | 126 | rightobservable_enabled = true;
|
---|
[2ad1ec] | 127 |
|
---|
[009e2e2] | 128 | resetElement();
|
---|
[343a4b] | 129 | resetPosition();
|
---|
[4b62d3] | 130 | resetWidth();
|
---|
[009e2e2] | 131 |
|
---|
| 132 | connect(this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
|
---|
| 133 | connect(this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
|
---|
| 134 | connect(this, SIGNAL(degreeChanged()), this, SLOT(resetWidth()), Qt::QueuedConnection);
|
---|
[907636] | 135 | }
|
---|
[2ad1ec] | 136 |
|
---|
| 137 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
|
---|
[c60665] | 138 | {
|
---|
[009e2e2] | 139 | LOG(3, "DEBUG: Destroying GLMoleculeObject_bond to bond [" <<
|
---|
| 140 | leftatomId << "," << rightatomId << "] and side " << BondSide << ".");
|
---|
[c60665] | 141 | // signOff() if not already done
|
---|
| 142 | removeChannels();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[7e2adc] | 145 | void GLMoleculeObject_bond::removeChannels()
|
---|
[c60665] | 146 | {
|
---|
| 147 | // at this point both atoms should still be alive, hence we may safely sign off
|
---|
| 148 | // from the AtomObservable itself
|
---|
[7e2adc] | 149 | if (bond_enabled) {
|
---|
| 150 | if (bondowner != NULL) {
|
---|
| 151 | bondowner->signOff(this, BondObservable::BondRemoved);
|
---|
| 152 | bondowner->signOff(this, BondObservable::DegreeChanged);
|
---|
[c60665] | 153 | }
|
---|
[7e2adc] | 154 | bond_enabled = false;
|
---|
[c60665] | 155 | }
|
---|
| 156 | if (leftobservable_enabled) {
|
---|
[7e2adc] | 157 | if (leftowner != NULL) {
|
---|
| 158 | leftowner->signOff(this, AtomObservable::PositionChanged);
|
---|
| 159 | leftowner->signOff(this, AtomObservable::ElementChanged);
|
---|
[c60665] | 160 | }
|
---|
| 161 | leftobservable_enabled = false;
|
---|
| 162 | }
|
---|
| 163 | if (rightobservable_enabled) {
|
---|
[7e2adc] | 164 | if (rightowner != NULL) {
|
---|
| 165 | rightowner->signOff(this, AtomObservable::PositionChanged);
|
---|
| 166 | rightowner->signOff(this, AtomObservable::ElementChanged);
|
---|
[c60665] | 167 | }
|
---|
| 168 | rightobservable_enabled = false;
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void GLMoleculeObject_bond::removeMe()
|
---|
[2ad1ec] | 173 | {
|
---|
[70db8f] | 174 | // sign off
|
---|
[7e2adc] | 175 | emit BondRemoved(leftatomId, rightatomId);
|
---|
[2ad1ec] | 176 | }
|
---|
| 177 |
|
---|
| 178 | void GLMoleculeObject_bond::update(Observable *publisher)
|
---|
| 179 | {
|
---|
[009e2e2] | 180 | ASSERT(0, "GLMoleculeObject_bond::update() - we are not signed on for any global updates.");
|
---|
[2ad1ec] | 181 | }
|
---|
| 182 |
|
---|
| 183 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
|
---|
| 184 | {
|
---|
[7e2adc] | 185 | // we signOff from all other sources
|
---|
[c60665] | 186 | removeChannels();
|
---|
[7e2adc] | 187 | // then indicate to remove us
|
---|
[c60665] | 188 | removeMe();
|
---|
[2ad1ec] | 189 | }
|
---|
| 190 |
|
---|
| 191 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 192 | {
|
---|
| 193 | #ifdef LOG_OBSERVER
|
---|
[7e2adc] | 194 | if (publisher == static_cast<const Observable *>(bondowner)) {
|
---|
[70db8f] | 195 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 196 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
| 197 | << " received notification from bond for channel "
|
---|
| 198 | << notification->getChannelNo() << ".";
|
---|
[7e2adc] | 199 | } else if (publisher == static_cast<const Observable * const>(leftowner)) {
|
---|
[70db8f] | 200 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 201 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 202 | << " received notification from leftatom " << leftatomId << " for channel "
|
---|
[70db8f] | 203 | << notification->getChannelNo() << ".";
|
---|
[7e2adc] | 204 | } else if (publisher == static_cast<const Observable * const>(rightowner)) {
|
---|
[70db8f] | 205 | observerLog().addMessage() << "++ Update of Observer "
|
---|
| 206 | << observerLog().getName(static_cast<Observer*>(this))
|
---|
[c60665] | 207 | << " received notification from rightatom " << rightatomId << " for channel "
|
---|
[70db8f] | 208 | << notification->getChannelNo() << ".";
|
---|
[7e2adc] | 209 | }
|
---|
[2ad1ec] | 210 | #endif
|
---|
[7e2adc] | 211 | if (publisher == static_cast<const Observable *>(bondowner)){
|
---|
[1f693d] | 212 | switch (notification->getChannelNo()) {
|
---|
| 213 | case BondObservable::BondRemoved:
|
---|
[c60665] | 214 | // removeMe();
|
---|
[1f693d] | 215 | break;
|
---|
[4b62d3] | 216 | case BondObservable::DegreeChanged:
|
---|
[009e2e2] | 217 | emit degreeChanged();
|
---|
[4b62d3] | 218 | break;
|
---|
| 219 | default:
|
---|
| 220 | ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
|
---|
| 221 | break;
|
---|
[1f693d] | 222 | }
|
---|
[70db8f] | 223 | } else {
|
---|
[343a4b] | 224 | // from an atom
|
---|
| 225 | switch (notification->getChannelNo()) {
|
---|
[70db8f] | 226 | case AtomObservable::PositionChanged:
|
---|
| 227 | LOG(2, "INFO: Received notification of PositionChanged.");
|
---|
[009e2e2] | 228 | emit positionChanged();
|
---|
[70db8f] | 229 | break;
|
---|
| 230 | case AtomObservable::ElementChanged:
|
---|
| 231 | LOG(2, "INFO: Received notification of ElementChanged.");
|
---|
[009e2e2] | 232 | emit elementChanged();
|
---|
[70db8f] | 233 | break;
|
---|
| 234 | default:
|
---|
| 235 | break;
|
---|
| 236 | }
|
---|
[343a4b] | 237 | }
|
---|
[4b62d3] | 238 | }
|
---|
| 239 |
|
---|
[009e2e2] | 240 | Vector GLMoleculeObject_bond::updateLeftPosition() const
|
---|
[4b62d3] | 241 | {
|
---|
[009e2e2] | 242 | const atom * const _atom = getAtomConst(leftatomId);
|
---|
| 243 | return _atom->getPosition();
|
---|
[343a4b] | 244 | }
|
---|
| 245 |
|
---|
[009e2e2] | 246 | Vector GLMoleculeObject_bond::updateRightPosition() const
|
---|
| 247 | {
|
---|
| 248 | const atom * const _atom = getAtomConst(rightatomId);
|
---|
| 249 | return _atom->getPosition();
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | atomicNumber_t GLMoleculeObject_bond::updateLeftElement() const
|
---|
| 253 | {
|
---|
| 254 | const atom * const _atom = getAtomConst(leftatomId);
|
---|
| 255 | return _atom->getElementNo();
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | atomicNumber_t GLMoleculeObject_bond::updateRightElement() const
|
---|
| 259 | {
|
---|
| 260 | const atom * const _atom = getAtomConst(rightatomId);
|
---|
| 261 | return _atom->getElementNo();
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | int GLMoleculeObject_bond::updateDegree() const
|
---|
[343a4b] | 265 | {
|
---|
[f01769] | 266 | const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
|
---|
| 267 | getAtom(AtomById(leftatomId));
|
---|
| 268 | const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
|
---|
| 269 | getAtom(AtomById(rightatomId));
|
---|
[009e2e2] | 270 | if ((_leftatom != NULL) && (_rightatom != NULL)) {
|
---|
| 271 | bond::ptr _bond = _leftatom->getBond(_rightatom);
|
---|
| 272 | return _bond->getDegree();
|
---|
| 273 | } else {
|
---|
| 274 | return 1;
|
---|
[2ad1ec] | 275 | }
|
---|
[009e2e2] | 276 | }
|
---|
| 277 |
|
---|
| 278 | void GLMoleculeObject_bond::resetElement()
|
---|
| 279 | {
|
---|
| 280 | size_t elementno = rightElement.get();
|
---|
| 281 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 282 | setMaterial(elementmaterial);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | void GLMoleculeObject_bond::resetWidth()
|
---|
| 286 | {
|
---|
| 287 | const double factor = 1.0f+.5f*(Degree.get()-1);
|
---|
| 288 | LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
|
---|
| 289 | setScaleX(factor);
|
---|
| 290 | setScaleY(factor);
|
---|
| 291 |
|
---|
| 292 | emit changed();
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | void GLMoleculeObject_bond::resetPosition()
|
---|
| 296 | {
|
---|
| 297 | Vector Position = leftPosition.get();
|
---|
| 298 | Vector OtherPosition = rightPosition.get();
|
---|
[fbb1f1] | 299 | const double distance =
|
---|
| 300 | Position.distance(OtherPosition)/2.;
|
---|
| 301 | setScaleZ(distance);
|
---|
[343a4b] | 302 |
|
---|
| 303 | // calculate position
|
---|
[37e910] | 304 | Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
|
---|
[343a4b] | 305 | Vector zeroVec(0.,0.,0.);
|
---|
| 306 | Vector a,b;
|
---|
| 307 | Vector OtherAxis;
|
---|
| 308 | double alpha;
|
---|
| 309 | a = Position - OtherPosition;
|
---|
| 310 | // construct rotation axis
|
---|
| 311 | b = a;
|
---|
| 312 | b.VectorProduct(Z);
|
---|
| 313 | Line axis(zeroVec, b);
|
---|
| 314 | // calculate rotation angle
|
---|
| 315 | alpha = a.Angle(Z);
|
---|
| 316 | // construct other axis to check right-hand rule
|
---|
| 317 | OtherAxis = b;
|
---|
| 318 | OtherAxis.VectorProduct(Z);
|
---|
| 319 | // assure right-hand rule for the rotation
|
---|
| 320 | if (a.ScalarProduct(OtherAxis) < MYEPSILON)
|
---|
| 321 | alpha = M_PI-alpha;
|
---|
| 322 | // check
|
---|
| 323 | Vector a_rotated = axis.rotateVector(a, alpha);
|
---|
| 324 | LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
|
---|
| 325 | << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
|
---|
| 326 |
|
---|
[37e910] | 327 | // set position (cylinder offset is in its barymetric center)
|
---|
| 328 | Vector OneFourth(Position - 0.75 * a);
|
---|
| 329 | setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
|
---|
[343a4b] | 330 | setRotationVector(QVector3D(b[0], b[1], b[2]));
|
---|
| 331 | setRotationAngle(alpha/M_PI*180.);
|
---|
[009e2e2] | 332 |
|
---|
| 333 | emit changed();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | atom * const GLMoleculeObject_bond::getAtom(const atomId_t _id)
|
---|
| 337 | {
|
---|
| 338 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
| 339 | return _atom;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | const atom * const GLMoleculeObject_bond::getAtomConst(const atomId_t _id)
|
---|
| 343 | {
|
---|
| 344 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
| 345 | getAtom(AtomById(_id));
|
---|
| 346 | return _atom;
|
---|
[2ad1ec] | 347 | }
|
---|