| [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 | 
 | 
|---|
| [494478] | 41 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
 | 
|---|
| [907636] | 42 | 
 | 
|---|
| [494478] | 43 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [856d05] | 44 | 
 | 
|---|
| [907636] | 45 | #include <cmath>
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [57a770] | 48 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [2ad1ec] | 49 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| [856d05] | 50 | #include "CodePatterns/Observer/ObserverLog.hpp"
 | 
|---|
| [c60665] | 51 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [6f0841] | 52 | #include "Atom/atom.hpp"
 | 
|---|
| [907636] | 53 | #include "Bond/bond.hpp"
 | 
|---|
| [3bdb6d] | 54 | #include "Element/element.hpp"
 | 
|---|
| [907636] | 55 | #include "Helpers/defs.hpp"
 | 
|---|
 | 56 | #include "LinearAlgebra/Line.hpp"
 | 
|---|
 | 57 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [494478] | 58 | #include "UIElements/Qt4/InstanceBoard/ObservedValue_wCallback.hpp"
 | 
|---|
| [c60665] | 59 | #include "World.hpp"
 | 
|---|
| [907636] | 60 | 
 | 
|---|
| [009e2e2] | 61 | 
 | 
|---|
 | 62 | // static entities
 | 
|---|
 | 63 | const Observable::channels_t
 | 
|---|
| [099f67] | 64 | GLMoleculeObject_bond::IndexChannels(1, AtomObservable::IndexChanged);
 | 
|---|
 | 65 | const Observable::channels_t
 | 
|---|
| [009e2e2] | 66 | GLMoleculeObject_bond::BondPositionChannels(1, AtomObservable::PositionChanged);
 | 
|---|
 | 67 | const Observable::channels_t
 | 
|---|
 | 68 | GLMoleculeObject_bond::BondDegreeChannels(1, BondObservable::DegreeChanged);
 | 
|---|
 | 69 | const Observable::channels_t
 | 
|---|
 | 70 | GLMoleculeObject_bond::BondElementChannels(1, AtomObservable::ElementChanged);
 | 
|---|
 | 71 | 
 | 
|---|
| [8d5fbf1] | 72 | static GLMoleculeObject_bond::bondIds_t getBondIdsForIds(
 | 
|---|
 | 73 |     const boost::function<const atomId_t()> &_left,
 | 
|---|
 | 74 |     const boost::function<const atomId_t()> &_right)
 | 
|---|
 | 75 | {
 | 
|---|
 | 76 |   return std::make_pair(_left(), _right());
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
| [009e2e2] | 79 | GLMoleculeObject_bond::GLMoleculeObject_bond(
 | 
|---|
 | 80 |     QGLSceneNode *mesh[],
 | 
|---|
 | 81 |     QObject *parent,
 | 
|---|
 | 82 |     const bondIds_t bondIds,
 | 
|---|
 | 83 |     const enum SideOfBond side) :
 | 
|---|
| [bca99d] | 84 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| [2ad1ec] | 85 |   Observer(std::string("GLMoleculeObject_bond")
 | 
|---|
| [009e2e2] | 86 |       +toString(bondIds.first)
 | 
|---|
| [2ad1ec] | 87 |       +std::string("-")
 | 
|---|
| [009e2e2] | 88 |       +toString(bondIds.second)),
 | 
|---|
| [099f67] | 89 |   leftowner(getAtomConst(bondIds.first)),
 | 
|---|
 | 90 |   rightowner(getAtomConst(bondIds.second)),
 | 
|---|
 | 91 |   bondowner(getAtomConst(bondIds.first)->getBond(getAtomConst(bondIds.second)).get()),
 | 
|---|
| [c60665] | 92 |   BondSide(side),
 | 
|---|
| [7c7c4a] | 93 |   ObservedValues(MAX_ObservedTypes),
 | 
|---|
 | 94 |   subjectKilledCount(0),
 | 
|---|
| [c60665] | 95 |   leftobservable_enabled(false),
 | 
|---|
| [8d5fbf1] | 96 |   rightobservable_enabled(false),
 | 
|---|
 | 97 |   board_subjectKilled(
 | 
|---|
 | 98 |       boost::bind(
 | 
|---|
 | 99 |           static_cast<void (GLMoleculeObject_bond::*) ()>(
 | 
|---|
 | 100 |               &GLMoleculeObject_bond::countsubjectKilled),
 | 
|---|
 | 101 |           this))
 | 
|---|
| [907636] | 102 | {
 | 
|---|
| [026bef] | 103 |   boost::function<void(const atomId_t &)> leftsubjectKilled =
 | 
|---|
 | 104 |       boost::bind(
 | 
|---|
 | 105 |           static_cast<void (GLMoleculeObject_bond::*) (const atomId_t &)>(
 | 
|---|
 | 106 |               &GLMoleculeObject_bond::countsubjectKilled),
 | 
|---|
 | 107 |           this, _1);
 | 
|---|
 | 108 |   boost::function<void(const atomId_t &)> rightsubjectKilled =
 | 
|---|
 | 109 |       boost::bind(
 | 
|---|
 | 110 |           static_cast<void (GLMoleculeObject_bond::*) (const atomId_t &)>(
 | 
|---|
 | 111 |               &GLMoleculeObject_bond::countsubjectKilled)
 | 
|---|
 | 112 |               , this, _1);
 | 
|---|
 | 113 |   boost::function<void(const bondIds_t &)> bondsubjectKilled =
 | 
|---|
 | 114 |       boost::bind(
 | 
|---|
 | 115 |           static_cast<void (GLMoleculeObject_bond::*) (const bondIds_t &)>(
 | 
|---|
 | 116 |               &GLMoleculeObject_bond::countsubjectKilled)
 | 
|---|
 | 117 |               , this, _1);
 | 
|---|
 | 118 |   initObservedValues(
 | 
|---|
 | 119 |       ObservedValues,
 | 
|---|
 | 120 |       bondIds.first,
 | 
|---|
 | 121 |       bondIds.second,
 | 
|---|
 | 122 |       leftowner,
 | 
|---|
 | 123 |       rightowner,
 | 
|---|
 | 124 |       bondowner,
 | 
|---|
 | 125 |       leftsubjectKilled,
 | 
|---|
 | 126 |       rightsubjectKilled,
 | 
|---|
 | 127 |       bondsubjectKilled);
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |   init();
 | 
|---|
 | 130 | }
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 | GLMoleculeObject_bond::GLMoleculeObject_bond(
 | 
|---|
 | 133 |     QGLSceneNode *mesh[],
 | 
|---|
 | 134 |     QObject *parent,
 | 
|---|
 | 135 |     const bondIds_t bondIds,
 | 
|---|
 | 136 |     const enum SideOfBond side,
 | 
|---|
| [8d5fbf1] | 137 |     std::vector<boost::any> &_ObservedValues,
 | 
|---|
 | 138 |     const boost::function<void (const bondIds_t)> &_subjectKilled) :
 | 
|---|
| [026bef] | 139 |   GLMoleculeObject(mesh, parent),
 | 
|---|
 | 140 |   Observer(std::string("GLMoleculeObject_bond")
 | 
|---|
 | 141 |       +toString(bondIds.first)
 | 
|---|
 | 142 |       +std::string("-")
 | 
|---|
 | 143 |       +toString(bondIds.second)),
 | 
|---|
 | 144 |   leftowner(getAtomConst(bondIds.first)),
 | 
|---|
 | 145 |   rightowner(getAtomConst(bondIds.second)),
 | 
|---|
 | 146 |   bondowner(getAtomConst(bondIds.first)->getBond(getAtomConst(bondIds.second)).get()),
 | 
|---|
 | 147 |   BondSide(side),
 | 
|---|
 | 148 |   ObservedValues(_ObservedValues),
 | 
|---|
 | 149 |   subjectKilledCount(0),
 | 
|---|
 | 150 |   leftobservable_enabled(false),
 | 
|---|
| [8d5fbf1] | 151 |   rightobservable_enabled(false),
 | 
|---|
 | 152 |   board_subjectKilled(_subjectKilled)
 | 
|---|
| [026bef] | 153 | {
 | 
|---|
 | 154 |   init();
 | 
|---|
 | 155 | }
 | 
|---|
| [7c7c4a] | 156 | 
 | 
|---|
| [026bef] | 157 | void GLMoleculeObject_bond::init()
 | 
|---|
 | 158 | {
 | 
|---|
| [2ad1ec] | 159 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| [7e2adc] | 160 |   bondowner->signOn(this, BondObservable::BondRemoved);
 | 
|---|
 | 161 |   bondowner->signOn(this, BondObservable::DegreeChanged);
 | 
|---|
| [c60665] | 162 |   bond_enabled = true;
 | 
|---|
| [7e2adc] | 163 |   leftowner->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
 | 164 |   leftowner->signOn(this, AtomObservable::ElementChanged);
 | 
|---|
| [c60665] | 165 |   leftobservable_enabled = true;
 | 
|---|
| [7e2adc] | 166 |   rightowner->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
 | 167 |   rightowner->signOn(this, AtomObservable::ElementChanged);
 | 
|---|
| [c60665] | 168 |   rightobservable_enabled = true;
 | 
|---|
| [2ad1ec] | 169 | 
 | 
|---|
| [009e2e2] | 170 |   resetElement();
 | 
|---|
| [343a4b] | 171 |   resetPosition();
 | 
|---|
| [4b62d3] | 172 |   resetWidth();
 | 
|---|
| [009e2e2] | 173 | 
 | 
|---|
 | 174 |   connect(this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
 | 
|---|
 | 175 |   connect(this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
 | 
|---|
 | 176 |   connect(this, SIGNAL(degreeChanged()), this, SLOT(resetWidth()), Qt::QueuedConnection);
 | 
|---|
| [907636] | 177 | }
 | 
|---|
| [2ad1ec] | 178 | 
 | 
|---|
 | 179 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
 | 
|---|
| [c60665] | 180 | {
 | 
|---|
| [009e2e2] | 181 |   LOG(3, "DEBUG: Destroying  GLMoleculeObject_bond to bond [" <<
 | 
|---|
| [099f67] | 182 |       getleftIndex() << "," << getrightIndex() << "] and side " << BondSide << ".");
 | 
|---|
| [c60665] | 183 |   // signOff() if not already done
 | 
|---|
 | 184 |   removeChannels();
 | 
|---|
| [099f67] | 185 |   destroyObservedValues(ObservedValues);
 | 
|---|
| [c60665] | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
| [7e2adc] | 188 | void GLMoleculeObject_bond::removeChannels()
 | 
|---|
| [c60665] | 189 | {
 | 
|---|
 | 190 |   // at this point both atoms should still be alive, hence we may safely sign off
 | 
|---|
 | 191 |   // from the AtomObservable itself
 | 
|---|
| [7e2adc] | 192 |   if (bond_enabled) {
 | 
|---|
 | 193 |     if (bondowner != NULL) {
 | 
|---|
 | 194 |       bondowner->signOff(this, BondObservable::BondRemoved);
 | 
|---|
 | 195 |       bondowner->signOff(this, BondObservable::DegreeChanged);
 | 
|---|
| [c60665] | 196 |     }
 | 
|---|
| [7e2adc] | 197 |     bond_enabled = false;
 | 
|---|
| [c60665] | 198 |   }
 | 
|---|
 | 199 |   if (leftobservable_enabled) {
 | 
|---|
| [7e2adc] | 200 |     if (leftowner != NULL) {
 | 
|---|
 | 201 |       leftowner->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
 | 202 |       leftowner->signOff(this, AtomObservable::ElementChanged);
 | 
|---|
| [c60665] | 203 |     }
 | 
|---|
 | 204 |     leftobservable_enabled = false;
 | 
|---|
 | 205 |   }
 | 
|---|
 | 206 |   if (rightobservable_enabled) {
 | 
|---|
| [7e2adc] | 207 |     if (rightowner != NULL) {
 | 
|---|
 | 208 |       rightowner->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
 | 209 |       rightowner->signOff(this, AtomObservable::ElementChanged);
 | 
|---|
| [c60665] | 210 |     }
 | 
|---|
 | 211 |     rightobservable_enabled = false;
 | 
|---|
 | 212 |   }
 | 
|---|
 | 213 | }
 | 
|---|
 | 214 | 
 | 
|---|
| [2ad1ec] | 215 | void GLMoleculeObject_bond::update(Observable *publisher)
 | 
|---|
 | 216 | {
 | 
|---|
| [009e2e2] | 217 |   ASSERT(0, "GLMoleculeObject_bond::update() - we are not signed on for any global updates.");
 | 
|---|
| [2ad1ec] | 218 | }
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
 | 
|---|
 | 221 | {
 | 
|---|
| [7e2adc] | 222 |   // we signOff from all other sources
 | 
|---|
| [c60665] | 223 |   removeChannels();
 | 
|---|
| [7c7c4a] | 224 |   // check whether we should be removed
 | 
|---|
| [8d5fbf1] | 225 |   board_subjectKilled(std::make_pair(getleftIndex(), getrightIndex()));
 | 
|---|
| [2ad1ec] | 226 | }
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
 | 229 | {
 | 
|---|
 | 230 | #ifdef LOG_OBSERVER
 | 
|---|
| [7e2adc] | 231 |   if (publisher == static_cast<const Observable *>(bondowner)) {
 | 
|---|
| [70db8f] | 232 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
 | 233 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
 | 234 |         << " received notification from bond for channel "
 | 
|---|
 | 235 |         << notification->getChannelNo() << ".";
 | 
|---|
| [7e2adc] | 236 |   } else if (publisher == static_cast<const Observable * const>(leftowner)) {
 | 
|---|
| [70db8f] | 237 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
 | 238 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| [099f67] | 239 |         << " received notification from leftatom " << getleftIndex() << " for channel "
 | 
|---|
| [70db8f] | 240 |         << notification->getChannelNo() << ".";
 | 
|---|
| [7e2adc] | 241 |   } else if (publisher == static_cast<const Observable * const>(rightowner)) {
 | 
|---|
| [70db8f] | 242 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
 | 243 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| [099f67] | 244 |         << " received notification from rightatom " << getrightIndex() << " for channel "
 | 
|---|
| [70db8f] | 245 |         << notification->getChannelNo() << ".";
 | 
|---|
| [7e2adc] | 246 |   }
 | 
|---|
| [2ad1ec] | 247 | #endif
 | 
|---|
| [7e2adc] | 248 |   if (publisher == static_cast<const Observable *>(bondowner)){
 | 
|---|
| [1f693d] | 249 |     switch (notification->getChannelNo()) {
 | 
|---|
 | 250 |     case BondObservable::BondRemoved:
 | 
|---|
| [c60665] | 251 | //      removeMe();
 | 
|---|
| [1f693d] | 252 |       break;
 | 
|---|
| [4b62d3] | 253 |     case BondObservable::DegreeChanged:
 | 
|---|
| [009e2e2] | 254 |       emit degreeChanged();
 | 
|---|
| [4b62d3] | 255 |       break;
 | 
|---|
 | 256 |     default:
 | 
|---|
 | 257 |       ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
 | 
|---|
 | 258 |       break;
 | 
|---|
| [1f693d] | 259 |     }
 | 
|---|
| [70db8f] | 260 |   } else {
 | 
|---|
| [343a4b] | 261 |     // from an atom
 | 
|---|
 | 262 |     switch (notification->getChannelNo()) {
 | 
|---|
| [70db8f] | 263 |     case AtomObservable::PositionChanged:
 | 
|---|
 | 264 |       LOG(2, "INFO: Received notification of PositionChanged.");
 | 
|---|
| [009e2e2] | 265 |       emit positionChanged();
 | 
|---|
| [70db8f] | 266 |       break;
 | 
|---|
 | 267 |     case AtomObservable::ElementChanged:
 | 
|---|
 | 268 |       LOG(2, "INFO: Received notification of ElementChanged.");
 | 
|---|
| [009e2e2] | 269 |       emit elementChanged();
 | 
|---|
| [70db8f] | 270 |       break;
 | 
|---|
 | 271 |     default:
 | 
|---|
 | 272 |       break;
 | 
|---|
 | 273 |     }
 | 
|---|
| [343a4b] | 274 |   }
 | 
|---|
| [4b62d3] | 275 | }
 | 
|---|
 | 276 | 
 | 
|---|
| [099f67] | 277 | atomId_t GLMoleculeObject_bond::updateIndex()
 | 
|---|
| [4b62d3] | 278 | {
 | 
|---|
| [099f67] | 279 |   return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
 | 
|---|
 | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 | Vector GLMoleculeObject_bond::updateLeftPosition(
 | 
|---|
 | 283 |     const boost::function<const atomId_t ()> &_getLeftAtomIndex)
 | 
|---|
 | 284 | {
 | 
|---|
 | 285 |   const atom * const _atom = getAtomConst(_getLeftAtomIndex());
 | 
|---|
| [009e2e2] | 286 |   return _atom->getPosition();
 | 
|---|
| [343a4b] | 287 | }
 | 
|---|
 | 288 | 
 | 
|---|
| [099f67] | 289 | Vector GLMoleculeObject_bond::updateRightPosition(
 | 
|---|
 | 290 |     const boost::function<const atomId_t ()> &_getRightAtomIndex)
 | 
|---|
| [009e2e2] | 291 | {
 | 
|---|
| [099f67] | 292 |   const atom * const _atom = getAtomConst(_getRightAtomIndex());
 | 
|---|
| [009e2e2] | 293 |   return _atom->getPosition();
 | 
|---|
 | 294 | }
 | 
|---|
 | 295 | 
 | 
|---|
| [099f67] | 296 | atomicNumber_t GLMoleculeObject_bond::updateLeftElement(
 | 
|---|
 | 297 |     const boost::function<const atomId_t ()> &_getLeftAtomIndex)
 | 
|---|
| [009e2e2] | 298 | {
 | 
|---|
| [099f67] | 299 |   const atom * const _atom = getAtomConst(_getLeftAtomIndex());
 | 
|---|
| [009e2e2] | 300 |   return _atom->getElementNo();
 | 
|---|
 | 301 | }
 | 
|---|
 | 302 | 
 | 
|---|
| [099f67] | 303 | atomicNumber_t GLMoleculeObject_bond::updateRightElement(
 | 
|---|
 | 304 |     const boost::function<const atomId_t ()> &_getRightAtomIndex)
 | 
|---|
| [009e2e2] | 305 | {
 | 
|---|
| [099f67] | 306 |   const atom * const _atom = getAtomConst(_getRightAtomIndex());
 | 
|---|
| [009e2e2] | 307 |   return _atom->getElementNo();
 | 
|---|
 | 308 | }
 | 
|---|
 | 309 | 
 | 
|---|
| [099f67] | 310 | int GLMoleculeObject_bond::updateDegree(
 | 
|---|
 | 311 |     const boost::function<const atomId_t ()> &_getLeftAtomIndex,
 | 
|---|
 | 312 |     const boost::function<const atomId_t ()> &_getRightAtomIndex)
 | 
|---|
| [343a4b] | 313 | {
 | 
|---|
| [f01769] | 314 |   const atom * const _leftatom = const_cast<const World &>(World::getInstance()).
 | 
|---|
| [099f67] | 315 |       getAtom(AtomById(_getLeftAtomIndex()));
 | 
|---|
| [f01769] | 316 |   const atom * const _rightatom = const_cast<const World &>(World::getInstance()).
 | 
|---|
| [099f67] | 317 |       getAtom(AtomById(_getRightAtomIndex()));
 | 
|---|
| [009e2e2] | 318 |   if ((_leftatom != NULL) && (_rightatom != NULL)) {
 | 
|---|
 | 319 |     bond::ptr _bond = _leftatom->getBond(_rightatom);
 | 
|---|
 | 320 |     return _bond->getDegree();
 | 
|---|
 | 321 |   } else {
 | 
|---|
 | 322 |     return 1;
 | 
|---|
| [2ad1ec] | 323 |   }
 | 
|---|
| [009e2e2] | 324 | }
 | 
|---|
 | 325 | 
 | 
|---|
 | 326 | void GLMoleculeObject_bond::resetElement()
 | 
|---|
 | 327 | {
 | 
|---|
| [7c7c4a] | 328 |   size_t elementno = getrightElement();
 | 
|---|
| [009e2e2] | 329 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
 | 330 |   setMaterial(elementmaterial);
 | 
|---|
 | 331 | }
 | 
|---|
 | 332 | 
 | 
|---|
 | 333 | void GLMoleculeObject_bond::resetWidth()
 | 
|---|
 | 334 | {
 | 
|---|
| [7c7c4a] | 335 |   const double factor = 1.0f+.5f*(getDegree()-1);
 | 
|---|
| [009e2e2] | 336 |   LOG(2, "DEBUG: GLMoleculeObject_bond::resetWidth() - setting bond's width to " << factor << ".");
 | 
|---|
 | 337 |   setScaleX(factor);
 | 
|---|
 | 338 |   setScaleY(factor);
 | 
|---|
 | 339 | 
 | 
|---|
 | 340 |   emit changed();
 | 
|---|
 | 341 | }
 | 
|---|
 | 342 | 
 | 
|---|
 | 343 | void GLMoleculeObject_bond::resetPosition()
 | 
|---|
 | 344 | {
 | 
|---|
| [7c7c4a] | 345 |   Vector Position = getleftPosition();
 | 
|---|
 | 346 |   Vector OtherPosition = getrightPosition();
 | 
|---|
| [fbb1f1] | 347 |   const double distance =
 | 
|---|
 | 348 |       Position.distance(OtherPosition)/2.;
 | 
|---|
 | 349 |   setScaleZ(distance);
 | 
|---|
| [343a4b] | 350 | 
 | 
|---|
 | 351 |   // calculate position
 | 
|---|
| [37e910] | 352 |   Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
 | 
|---|
| [343a4b] | 353 |   Vector zeroVec(0.,0.,0.);
 | 
|---|
 | 354 |   Vector a,b;
 | 
|---|
 | 355 |   Vector OtherAxis;
 | 
|---|
 | 356 |   double alpha;
 | 
|---|
 | 357 |   a = Position - OtherPosition;
 | 
|---|
 | 358 |   // construct rotation axis
 | 
|---|
 | 359 |   b = a;
 | 
|---|
 | 360 |   b.VectorProduct(Z);
 | 
|---|
 | 361 |   Line axis(zeroVec, b);
 | 
|---|
 | 362 |   // calculate rotation angle
 | 
|---|
 | 363 |   alpha = a.Angle(Z);
 | 
|---|
 | 364 |   // construct other axis to check right-hand rule
 | 
|---|
 | 365 |   OtherAxis = b;
 | 
|---|
 | 366 |   OtherAxis.VectorProduct(Z);
 | 
|---|
 | 367 |   // assure right-hand rule for the rotation
 | 
|---|
 | 368 |   if (a.ScalarProduct(OtherAxis) < MYEPSILON)
 | 
|---|
 | 369 |     alpha = M_PI-alpha;
 | 
|---|
 | 370 |   // check
 | 
|---|
 | 371 |   Vector a_rotated = axis.rotateVector(a, alpha);
 | 
|---|
 | 372 |   LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
 | 
|---|
 | 373 |       << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
 | 
|---|
 | 374 | 
 | 
|---|
| [37e910] | 375 |   // set position (cylinder offset is in its barymetric center)
 | 
|---|
 | 376 |   Vector OneFourth(Position - 0.75 * a);
 | 
|---|
 | 377 |   setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
 | 
|---|
| [343a4b] | 378 |   setRotationVector(QVector3D(b[0], b[1], b[2]));
 | 
|---|
 | 379 |   setRotationAngle(alpha/M_PI*180.);
 | 
|---|
| [009e2e2] | 380 | 
 | 
|---|
 | 381 |   emit changed();
 | 
|---|
 | 382 | }
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 | atom * const GLMoleculeObject_bond::getAtom(const atomId_t _id)
 | 
|---|
 | 385 | {
 | 
|---|
 | 386 |   atom * const _atom = World::getInstance().getAtom(AtomById(_id));
 | 
|---|
 | 387 |   return _atom;
 | 
|---|
 | 388 | }
 | 
|---|
 | 389 | 
 | 
|---|
 | 390 | const atom * const GLMoleculeObject_bond::getAtomConst(const atomId_t _id)
 | 
|---|
 | 391 | {
 | 
|---|
 | 392 |   const atom * const _atom = const_cast<const World &>(World::getInstance()).
 | 
|---|
 | 393 |       getAtom(AtomById(_id));
 | 
|---|
 | 394 |   return _atom;
 | 
|---|
| [2ad1ec] | 395 | }
 | 
|---|
| [7c7c4a] | 396 | 
 | 
|---|
 | 397 | void GLMoleculeObject_bond::countsubjectKilled()
 | 
|---|
 | 398 | {
 | 
|---|
 | 399 |   ++subjectKilledCount;
 | 
|---|
 | 400 | 
 | 
|---|
 | 401 |   if (subjectKilledCount > ObservedValues.size())
 | 
|---|
| [099f67] | 402 |     emit BondRemoved(getleftIndex(), getrightIndex());
 | 
|---|
| [7c7c4a] | 403 | }
 | 
|---|
 | 404 | 
 | 
|---|
| [099f67] | 405 | void GLMoleculeObject_bond::initObservedValues(
 | 
|---|
 | 406 |     std::vector<boost::any> &_ObservedValues,
 | 
|---|
 | 407 |     const atomId_t _leftatomId,
 | 
|---|
 | 408 |     const atomId_t _rightatomId,
 | 
|---|
 | 409 |     const Observable * const _leftowner,
 | 
|---|
 | 410 |     const Observable * const _rightowner,
 | 
|---|
 | 411 |     const Observable * const _bondowner,
 | 
|---|
| [026bef] | 412 |     const boost::function<void(const atomId_t &)> &_leftsubjectKilled,
 | 
|---|
 | 413 |     const boost::function<void(const atomId_t &)> &_rightsubjectKilled,
 | 
|---|
 | 414 |     const boost::function<void(const bondIds_t &)> &_bondsubjectKilled)
 | 
|---|
| [7c7c4a] | 415 | {
 | 
|---|
| [099f67] | 416 |   /* This is an old note from when the code was still part of cstor's initializer body.
 | 
|---|
 | 417 |    * TODO: Probably does not apply anymore but has not yet been tested.
 | 
|---|
 | 418 |    *
 | 
|---|
 | 419 |    * We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
 | 
|---|
 | 420 |    * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
 | 
|---|
 | 421 |    * the class has not been fully constructed yet. "This" itself seems to be working fine.
 | 
|---|
 | 422 |    */
 | 
|---|
 | 423 | 
 | 
|---|
 | 424 |   ASSERT( _ObservedValues.size() == MAX_ObservedTypes,
 | 
|---|
 | 425 |       "GLMoleculeObject_bond::initObservedValues() - given ObservedValues has not correct size.");
 | 
|---|
 | 426 | 
 | 
|---|
 | 427 |   // fill ObservedValues: index first
 | 
|---|
 | 428 |   // Note that we only need one as the function just checks on the last changed id
 | 
|---|
 | 429 |   // and ids cannot be changed simultaneously
 | 
|---|
 | 430 |   const boost::function<atomId_t ()> AtomIndexUpdater(
 | 
|---|
 | 431 |       boost::bind(&GLMoleculeObject_bond::updateIndex));
 | 
|---|
 | 432 | 
 | 
|---|
 | 433 |   ObservedValue_wCallback<atomId_t> * const LeftIndexObservable =
 | 
|---|
 | 434 |       new ObservedValue_wCallback<atomId_t>(
 | 
|---|
 | 435 |         _leftowner,
 | 
|---|
 | 436 |         AtomIndexUpdater,
 | 
|---|
 | 437 |         "Bonds_LeftAtomIndex_"+toString(_leftatomId),
 | 
|---|
 | 438 |         _leftatomId,
 | 
|---|
 | 439 |         IndexChannels,
 | 
|---|
| [026bef] | 440 |         _leftsubjectKilled);
 | 
|---|
| [099f67] | 441 |   _ObservedValues[leftIndex] = LeftIndexObservable;
 | 
|---|
 | 442 |   ObservedValue_wCallback<atomId_t> * const RightIndexObservable =
 | 
|---|
 | 443 |       new ObservedValue_wCallback<atomId_t>(
 | 
|---|
 | 444 |         _rightowner,
 | 
|---|
 | 445 |         AtomIndexUpdater,
 | 
|---|
 | 446 |         "Bonds_RightAtomIndex_"+toString(_rightatomId),
 | 
|---|
 | 447 |         _rightatomId,
 | 
|---|
 | 448 |         IndexChannels,
 | 
|---|
| [026bef] | 449 |         _rightsubjectKilled);
 | 
|---|
| [099f67] | 450 |   _ObservedValues[rightIndex] = RightIndexObservable;
 | 
|---|
 | 451 | 
 | 
|---|
 | 452 |   const boost::function<const atomId_t ()> LeftIndexGetter =
 | 
|---|
 | 453 |       boost::bind(&ObservedValue_wCallback<atomId_t>::get,
 | 
|---|
 | 454 |           LeftIndexObservable);
 | 
|---|
 | 455 |   const boost::function<const atomId_t ()> RightIndexGetter =
 | 
|---|
 | 456 |       boost::bind(&ObservedValue_wCallback<atomId_t>::get,
 | 
|---|
 | 457 |           RightIndexObservable);
 | 
|---|
 | 458 | 
 | 
|---|
 | 459 |   // fill ObservedValues: then all the other that need index
 | 
|---|
 | 460 |   const boost::function<Vector ()> LeftPositionUpdater(
 | 
|---|
 | 461 |       boost::bind(&GLMoleculeObject_bond::updateLeftPosition, LeftIndexGetter));
 | 
|---|
 | 462 |   const boost::function<Vector ()> RightPositionUpdater(
 | 
|---|
 | 463 |       boost::bind(&GLMoleculeObject_bond::updateRightPosition, RightIndexGetter));
 | 
|---|
 | 464 |   const boost::function<atomicNumber_t ()> LeftElementUpdater(
 | 
|---|
 | 465 |       boost::bind(&GLMoleculeObject_bond::updateLeftElement, LeftIndexGetter));
 | 
|---|
 | 466 |   const boost::function<atomicNumber_t ()> RightElementUpdater(
 | 
|---|
 | 467 |       boost::bind(&GLMoleculeObject_bond::updateRightElement, RightIndexGetter));
 | 
|---|
 | 468 |   const boost::function<int ()> DegreeUpdater(
 | 
|---|
 | 469 |       boost::bind(&GLMoleculeObject_bond::updateDegree, LeftIndexGetter, RightIndexGetter));
 | 
|---|
| [026bef] | 470 |   const boost::function<bondIds_t ()> BondIdGetter(
 | 
|---|
 | 471 |       boost::bind(&getBondIdsForIds, LeftIndexGetter, RightIndexGetter));
 | 
|---|
| [099f67] | 472 | 
 | 
|---|
| [026bef] | 473 |   _ObservedValues[leftPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
 | 
|---|
| [099f67] | 474 |       _leftowner,
 | 
|---|
 | 475 |       LeftPositionUpdater,
 | 
|---|
 | 476 |       "BondleftPosition_"+toString(_leftatomId),
 | 
|---|
 | 477 |       LeftPositionUpdater(),
 | 
|---|
| [7c7c4a] | 478 |       BondPositionChannels,
 | 
|---|
| [026bef] | 479 |       _leftsubjectKilled,
 | 
|---|
 | 480 |       LeftIndexGetter);
 | 
|---|
 | 481 |   _ObservedValues[rightPosition] = new ObservedValue_wCallback<Vector, atomId_t>(
 | 
|---|
| [099f67] | 482 |       _rightowner,
 | 
|---|
 | 483 |       RightPositionUpdater,
 | 
|---|
 | 484 |       "BondrightPosition_"+toString(_rightatomId),
 | 
|---|
 | 485 |       RightPositionUpdater(),
 | 
|---|
| [7c7c4a] | 486 |       BondPositionChannels,
 | 
|---|
| [026bef] | 487 |       _rightsubjectKilled,
 | 
|---|
 | 488 |       RightIndexGetter);
 | 
|---|
 | 489 |   _ObservedValues[leftElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
 | 
|---|
| [099f67] | 490 |       _leftowner,
 | 
|---|
 | 491 |       LeftElementUpdater,
 | 
|---|
 | 492 |       "BondleftElement"+toString(_leftatomId),
 | 
|---|
 | 493 |       LeftElementUpdater(),
 | 
|---|
| [7c7c4a] | 494 |       BondElementChannels,
 | 
|---|
| [026bef] | 495 |       _leftsubjectKilled,
 | 
|---|
 | 496 |       LeftIndexGetter);
 | 
|---|
 | 497 |   _ObservedValues[rightElement] = new ObservedValue_wCallback<atomicNumber_t, atomId_t>(
 | 
|---|
| [099f67] | 498 |       _rightowner,
 | 
|---|
 | 499 |       RightElementUpdater,
 | 
|---|
 | 500 |       "BondrightElement"+toString(_rightatomId),
 | 
|---|
 | 501 |       RightElementUpdater(),
 | 
|---|
| [7c7c4a] | 502 |       BondElementChannels,
 | 
|---|
| [026bef] | 503 |       _rightsubjectKilled,
 | 
|---|
 | 504 |       RightIndexGetter);
 | 
|---|
 | 505 |   _ObservedValues[Degree] = new ObservedValue_wCallback<int, bondIds_t>(
 | 
|---|
| [099f67] | 506 |       _bondowner,
 | 
|---|
 | 507 |       DegreeUpdater,
 | 
|---|
 | 508 |       "BondDegree"+toString(_leftatomId)+"_"+toString(_rightatomId),
 | 
|---|
 | 509 |       DegreeUpdater(),
 | 
|---|
| [7c7c4a] | 510 |       BondDegreeChannels,
 | 
|---|
| [026bef] | 511 |       _bondsubjectKilled,
 | 
|---|
 | 512 |       BondIdGetter);
 | 
|---|
| [099f67] | 513 | }
 | 
|---|
 | 514 | 
 | 
|---|
 | 515 | void GLMoleculeObject_bond::destroyObservedValues(
 | 
|---|
 | 516 |     std::vector<boost::any> &_ObservedValues)
 | 
|---|
 | 517 | {
 | 
|---|
 | 518 |   delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[leftIndex]);
 | 
|---|
 | 519 |   delete boost::any_cast<ObservedValue_wCallback<atomId_t> *>(_ObservedValues[rightIndex]);
 | 
|---|
| [026bef] | 520 |   delete boost::any_cast<ObservedValue_wCallback<Vector,atomId_t> *>(_ObservedValues[leftPosition]);
 | 
|---|
 | 521 |   delete boost::any_cast<ObservedValue_wCallback<Vector,atomId_t> *>(_ObservedValues[rightPosition]);
 | 
|---|
 | 522 |   delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t,atomId_t> *>(_ObservedValues[leftElement]);
 | 
|---|
 | 523 |   delete boost::any_cast<ObservedValue_wCallback<atomicNumber_t,atomId_t> *>(_ObservedValues[rightElement]);
 | 
|---|
 | 524 |   delete boost::any_cast<ObservedValue_wCallback<int, bondIds_t> *>(_ObservedValues[Degree]);
 | 
|---|
| [099f67] | 525 |   _ObservedValues.clear();
 | 
|---|
 | 526 | }
 | 
|---|
 | 527 | 
 | 
|---|
| [3b9aa1] | 528 | const atomId_t& GLMoleculeObject_bond::getleftIndex() const
 | 
|---|
| [099f67] | 529 | {
 | 
|---|
 | 530 |   return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[leftIndex])->get();
 | 
|---|
| [7c7c4a] | 531 | }
 | 
|---|
 | 532 | 
 | 
|---|
| [3b9aa1] | 533 | const atomId_t& GLMoleculeObject_bond::getrightIndex() const
 | 
|---|
| [7c7c4a] | 534 | {
 | 
|---|
| [099f67] | 535 |   return boost::any_cast<ObservedValue_wCallback<atomId_t> *>(ObservedValues[rightIndex])->get();
 | 
|---|
| [7c7c4a] | 536 | }
 | 
|---|
 | 537 | 
 | 
|---|
| [3b9aa1] | 538 | const Vector& GLMoleculeObject_bond::getleftPosition() const
 | 
|---|
| [7c7c4a] | 539 | {
 | 
|---|
| [026bef] | 540 |   return boost::any_cast<ObservedValue_wCallback<Vector,atomId_t> *>(ObservedValues[leftPosition])->get();
 | 
|---|
| [7c7c4a] | 541 | }
 | 
|---|
 | 542 | 
 | 
|---|
| [3b9aa1] | 543 | const Vector& GLMoleculeObject_bond::getrightPosition() const
 | 
|---|
| [7c7c4a] | 544 | {
 | 
|---|
| [026bef] | 545 |   return boost::any_cast<ObservedValue_wCallback<Vector, atomId_t> *>(ObservedValues[rightPosition])->get();
 | 
|---|
| [7c7c4a] | 546 | }
 | 
|---|
 | 547 | 
 | 
|---|
| [3b9aa1] | 548 | const atomicNumber_t& GLMoleculeObject_bond::getleftElement() const
 | 
|---|
| [7c7c4a] | 549 | {
 | 
|---|
| [026bef] | 550 |   return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[leftElement])->get();
 | 
|---|
| [7c7c4a] | 551 | }
 | 
|---|
 | 552 | 
 | 
|---|
| [3b9aa1] | 553 | const atomicNumber_t& GLMoleculeObject_bond::getrightElement() const
 | 
|---|
| [7c7c4a] | 554 | {
 | 
|---|
| [026bef] | 555 |   return boost::any_cast<ObservedValue_wCallback<atomicNumber_t, atomId_t> *>(ObservedValues[rightElement])->get();
 | 
|---|
| [7c7c4a] | 556 | }
 | 
|---|
 | 557 | 
 | 
|---|
| [3b9aa1] | 558 | const int& GLMoleculeObject_bond::getDegree() const
 | 
|---|
| [7c7c4a] | 559 | {
 | 
|---|
| [026bef] | 560 |   return boost::any_cast<ObservedValue_wCallback<int, bondIds_t> *>(ObservedValues[Degree])->get();
 | 
|---|
| [7c7c4a] | 561 | }
 | 
|---|