| 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 *_rightatom = World::getInstance().getAtom(AtomById(rightatomId));
 | 
|---|
| 94 |       if (_rightatom->getType() != NULL) {
 | 
|---|
| 95 |         elementno = _rightatom->getType()->getAtomicNumber();
 | 
|---|
| 96 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
| 97 |         elementno = 1;
 | 
|---|
| 98 |       }
 | 
|---|
| 99 |       break;
 | 
|---|
| 100 |     }
 | 
|---|
| 101 |     case right:
 | 
|---|
| 102 |     {
 | 
|---|
| 103 |       const atom *_leftatom = World::getInstance().getAtom(AtomById(leftatomId));
 | 
|---|
| 104 |       if (_leftatom->getType() != NULL) {
 | 
|---|
| 105 |         elementno = _leftatom->getType()->getAtomicNumber();
 | 
|---|
| 106 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
| 107 |         elementno = 1;
 | 
|---|
| 108 |       }
 | 
|---|
| 109 | 
 | 
|---|
| 110 |       break;
 | 
|---|
| 111 |     }
 | 
|---|
| 112 |     default:
 | 
|---|
| 113 |       ASSERT(0,
 | 
|---|
| 114 |           "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 115 |       break;
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
| 119 |   setMaterial(elementmaterial);
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   resetPosition();
 | 
|---|
| 122 |   resetWidth();
 | 
|---|
| 123 | }
 | 
|---|
| 124 | 
 | 
|---|
| 125 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
 | 
|---|
| 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()
 | 
|---|
| 202 | {
 | 
|---|
| 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;
 | 
|---|
| 216 |   }
 | 
|---|
| 217 | }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | void GLMoleculeObject_bond::update(Observable *publisher)
 | 
|---|
| 220 | {
 | 
|---|
| 221 | #ifdef LOG_OBSERVER
 | 
|---|
| 222 |   if (publisher == static_cast<const Observable *>(&_bond)) {
 | 
|---|
| 223 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
| 224 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 225 |         << " from bond.";
 | 
|---|
| 226 |   } else if (publisher == leftobservable) {
 | 
|---|
| 227 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
| 228 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 229 |         << " from leftatom " << leftatomId << ".";
 | 
|---|
| 230 |   } else if (publisher == rightobservable) {
 | 
|---|
| 231 |     observerLog().addMessage() << "++ Update of Observer " <<
 | 
|---|
| 232 |         observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 233 |         << " from rightatom " << rightatomId << ".";
 | 
|---|
| 234 |   } else
 | 
|---|
| 235 |     observerLog().addMessage() << "++ Update of Observer " <<
 | 
|---|
| 236 |     observerLog().getName(static_cast<Observer*>(this)) << " from unknown source.";
 | 
|---|
| 237 | #endif
 | 
|---|
| 238 | }
 | 
|---|
| 239 | 
 | 
|---|
| 240 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
 | 
|---|
| 241 | {
 | 
|---|
| 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();
 | 
|---|
| 277 | }
 | 
|---|
| 278 | 
 | 
|---|
| 279 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| 280 | {
 | 
|---|
| 281 | #ifdef LOG_OBSERVER
 | 
|---|
| 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() << ".";
 | 
|---|
| 287 |   } else if (publisher == leftobservable) {
 | 
|---|
| 288 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
| 289 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 290 |         << " received notification from leftatom " << leftatomId << " for channel "
 | 
|---|
| 291 |         << notification->getChannelNo() << ".";
 | 
|---|
| 292 |   } else if (publisher == rightobservable) {
 | 
|---|
| 293 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
| 294 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 295 |         << " received notification from rightatom " << rightatomId << " for channel "
 | 
|---|
| 296 |         << notification->getChannelNo() << ".";
 | 
|---|
| 297 |   } else
 | 
|---|
| 298 |     observerLog().addMessage() << "++ Update of Observer "
 | 
|---|
| 299 |         << observerLog().getName(static_cast<Observer*>(this))
 | 
|---|
| 300 |         << " received notification from unknown source.";
 | 
|---|
| 301 | #endif
 | 
|---|
| 302 |   bool DoResetPosition = false;
 | 
|---|
| 303 |   bool DoResetWidth = false;
 | 
|---|
| 304 |   if (publisher == static_cast<const Observable *>(&_bond)){
 | 
|---|
| 305 |     switch (notification->getChannelNo()) {
 | 
|---|
| 306 |     case BondObservable::BondRemoved:
 | 
|---|
| 307 | //      removeMe();
 | 
|---|
| 308 |       break;
 | 
|---|
| 309 |     case BondObservable::DegreeChanged:
 | 
|---|
| 310 |       DoResetWidth = true;
 | 
|---|
| 311 |       break;
 | 
|---|
| 312 |     default:
 | 
|---|
| 313 |       ASSERT(0, "GLMoleculeObject_bond::recieveNotification() - unknown signal.");
 | 
|---|
| 314 |       break;
 | 
|---|
| 315 |     }
 | 
|---|
| 316 |   } else {
 | 
|---|
| 317 |     // from an atom
 | 
|---|
| 318 |     switch (notification->getChannelNo()) {
 | 
|---|
| 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 |     }
 | 
|---|
| 330 |   }
 | 
|---|
| 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);
 | 
|---|
| 347 | }
 | 
|---|
| 348 | 
 | 
|---|
| 349 | void GLMoleculeObject_bond::resetPosition()
 | 
|---|
| 350 | {
 | 
|---|
| 351 |   Vector Position;
 | 
|---|
| 352 |   Vector OtherPosition;
 | 
|---|
| 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();
 | 
|---|
| 357 |   switch (BondSide) {
 | 
|---|
| 358 |     case left:
 | 
|---|
| 359 |       Position = LeftPos;
 | 
|---|
| 360 |       OtherPosition = RightPos;
 | 
|---|
| 361 |       break;
 | 
|---|
| 362 |     case right:
 | 
|---|
| 363 |       Position = RightPos; 
 | 
|---|
| 364 |       OtherPosition = LeftPos;
 | 
|---|
| 365 |       break;
 | 
|---|
| 366 |     default:
 | 
|---|
| 367 |       ASSERT(0,
 | 
|---|
| 368 |           "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 369 |       break;
 | 
|---|
| 370 |   }
 | 
|---|
| 371 |   const double distance =
 | 
|---|
| 372 |       Position.distance(OtherPosition)/2.;
 | 
|---|
| 373 |   setScaleZ(distance);
 | 
|---|
| 374 | 
 | 
|---|
| 375 |   // calculate position
 | 
|---|
| 376 |   Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
 | 
|---|
| 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 | 
 | 
|---|
| 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]));
 | 
|---|
| 402 |   setRotationVector(QVector3D(b[0], b[1], b[2]));
 | 
|---|
| 403 |   setRotationAngle(alpha/M_PI*180.);
 | 
|---|
| 404 | }
 | 
|---|