| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * GLMoleculeObject_bond.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Aug 17, 2011
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "GLMoleculeObject_bond.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <Qt3D/qglmaterial.h>
 | 
|---|
| 38 | #include <Qt3D/qglscenenode.h>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include <cmath>
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 46 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 47 | #include "CodePatterns/Observer/Notification.hpp"
 | 
|---|
| 48 | #include "CodePatterns/Observer/ObserverLog.hpp"
 | 
|---|
| 49 | #include "Atom/atom.hpp"
 | 
|---|
| 50 | #include "Bond/bond.hpp"
 | 
|---|
| 51 | #include "Element/element.hpp"
 | 
|---|
| 52 | #include "Helpers/defs.hpp"
 | 
|---|
| 53 | #include "LinearAlgebra/Line.hpp"
 | 
|---|
| 54 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | GLMoleculeObject_bond::GLMoleculeObject_bond(QGLSceneNode *mesh[], QObject *parent, const bond::ptr bondref, const enum SideOfBond side) :
 | 
|---|
| 57 |   GLMoleculeObject(mesh, parent),
 | 
|---|
| 58 |   Observer(std::string("GLMoleculeObject_bond")
 | 
|---|
| 59 |       +toString(bondref->leftatom->getId())
 | 
|---|
| 60 |       +std::string("-")
 | 
|---|
| 61 |       +toString(bondref->rightatom->getId())),
 | 
|---|
| 62 |   _bond(bondref),
 | 
|---|
| 63 |   BondSide(side)
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| 66 |   _bond->signOn(this, BondObservable::BondRemoved);
 | 
|---|
| 67 |   _bond->leftatom->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
| 68 |   _bond->rightatom->signOn(this, AtomObservable::PositionChanged);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   size_t elementno = 0;
 | 
|---|
| 71 |   switch (BondSide) {
 | 
|---|
| 72 |     case left:
 | 
|---|
| 73 |       if (_bond->rightatom->getType() != NULL) {
 | 
|---|
| 74 |         elementno = _bond->rightatom->getType()->getAtomicNumber();
 | 
|---|
| 75 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
| 76 |         elementno = 1;
 | 
|---|
| 77 |       }
 | 
|---|
| 78 |       break;
 | 
|---|
| 79 |     case right:
 | 
|---|
| 80 |       if (_bond->leftatom->getType() != NULL) {
 | 
|---|
| 81 |         elementno = _bond->leftatom->getType()->getAtomicNumber();
 | 
|---|
| 82 |       } else { // if not element yet set, set to hydrogen
 | 
|---|
| 83 |         elementno = 1;
 | 
|---|
| 84 |       }
 | 
|---|
| 85 | 
 | 
|---|
| 86 |       break;
 | 
|---|
| 87 |     default:
 | 
|---|
| 88 |       ASSERT(0,
 | 
|---|
| 89 |           "GLMoleculeObject_bond::GLMoleculeObject_bond() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 90 |       break;
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   QGLMaterial *elementmaterial = getMaterial(elementno);
 | 
|---|
| 94 |   setMaterial(elementmaterial);
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   resetPosition();
 | 
|---|
| 97 | }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | GLMoleculeObject_bond::~GLMoleculeObject_bond()
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   // sign on as observer (obtain non-const instance before)
 | 
|---|
| 102 |   if (_bond){
 | 
|---|
| 103 |     _bond->signOff(this, BondObservable::BondRemoved);
 | 
|---|
| 104 |     _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 105 |     _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 106 |     LOG(2, "INFO: Destroying  GLMoleculeObject_bond to bond " << *_bond << " and side " << BondSide << ".");
 | 
|---|
| 107 |   }else{
 | 
|---|
| 108 |     LOG(2, "INFO: Destroying  GLMoleculeObject_bond (bond already killed).");
 | 
|---|
| 109 |   }
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | void GLMoleculeObject_bond::update(Observable *publisher)
 | 
|---|
| 113 | {
 | 
|---|
| 114 | #ifdef LOG_OBSERVER
 | 
|---|
| 115 |   observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(this) << " from bond " << *_bond << ".";
 | 
|---|
| 116 | #endif
 | 
|---|
| 117 | }
 | 
|---|
| 118 | 
 | 
|---|
| 119 | void GLMoleculeObject_bond::subjectKilled(Observable *publisher)
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   LOG(2, "INFO: Received subjectKilled from " << *_bond << ".");
 | 
|---|
| 122 |   switch (BondSide) {
 | 
|---|
| 123 |     case left:
 | 
|---|
| 124 |       emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
 | 
|---|
| 125 |       break;
 | 
|---|
| 126 |     case right:
 | 
|---|
| 127 |       emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
 | 
|---|
| 128 |       break;
 | 
|---|
| 129 |     default:
 | 
|---|
| 130 |       ASSERT(0,
 | 
|---|
| 131 |           "GLMoleculeObject_bond::subjectKilled() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 132 |       break;
 | 
|---|
| 133 |   }
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   // Don't let the destructor automatically sign off from a dead bond!
 | 
|---|
| 136 |   _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 137 |   _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 138 |   _bond.reset();
 | 
|---|
| 139 |   delete this;
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | void GLMoleculeObject_bond::recieveNotification(Observable *publisher, Notification_ptr notification)
 | 
|---|
| 143 | {
 | 
|---|
| 144 | #ifdef LOG_OBSERVER
 | 
|---|
| 145 |   observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(this)
 | 
|---|
| 146 |       << " received notification from bond " << *_bond << " for channel "
 | 
|---|
| 147 |       << notification->getChannelNo() << ".";
 | 
|---|
| 148 | #endif
 | 
|---|
| 149 |   if (publisher == dynamic_cast<const Observable *>(_bond.get())){
 | 
|---|
| 150 |     // from the bond
 | 
|---|
| 151 |     switch (notification->getChannelNo()) {
 | 
|---|
| 152 |       case BondObservable::BondRemoved:
 | 
|---|
| 153 |         LOG(2, "INFO: Received notification of BondRemoved from " << *_bond << ".");
 | 
|---|
| 154 |         switch (BondSide) {
 | 
|---|
| 155 |           case left:
 | 
|---|
| 156 |             emit BondRemoved(_bond->leftatom->getId(), _bond->rightatom->getId());
 | 
|---|
| 157 |             break;
 | 
|---|
| 158 |           case right:
 | 
|---|
| 159 |             emit BondRemoved(_bond->rightatom->getId(), _bond->leftatom->getId());
 | 
|---|
| 160 |             break;
 | 
|---|
| 161 |           default:
 | 
|---|
| 162 |             ASSERT(0,
 | 
|---|
| 163 |                 "GLMoleculeObject_bond::recieveNotification() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 164 |             break;
 | 
|---|
| 165 |         }
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         // Don't let the destructor automatically sign off from a dead bond!
 | 
|---|
| 168 |         _bond->leftatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 169 |         _bond->rightatom->signOff(this, AtomObservable::PositionChanged);
 | 
|---|
| 170 |         _bond.reset();
 | 
|---|
| 171 |         delete this;
 | 
|---|
| 172 |         break;
 | 
|---|
| 173 |           default:
 | 
|---|
| 174 |             break;
 | 
|---|
| 175 |     }
 | 
|---|
| 176 |   }else{
 | 
|---|
| 177 |     // from an atom
 | 
|---|
| 178 |     switch (notification->getChannelNo()) {
 | 
|---|
| 179 |       case AtomObservable::PositionChanged:
 | 
|---|
| 180 |         LOG(2, "INFO: Received notification of PositionChanged.");
 | 
|---|
| 181 |         resetPosition();
 | 
|---|
| 182 |         emit changed();
 | 
|---|
| 183 |     }
 | 
|---|
| 184 |   }
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | void GLMoleculeObject_bond::resetPosition()
 | 
|---|
| 188 | {
 | 
|---|
| 189 |   Vector Position;
 | 
|---|
| 190 |   Vector OtherPosition;
 | 
|---|
| 191 |   switch (BondSide) {
 | 
|---|
| 192 |     case left:
 | 
|---|
| 193 |       Position = _bond->leftatom->getPosition();
 | 
|---|
| 194 |       OtherPosition = _bond->rightatom->getPosition();
 | 
|---|
| 195 |       break;
 | 
|---|
| 196 |     case right:
 | 
|---|
| 197 |       Position = _bond->rightatom->getPosition();
 | 
|---|
| 198 |       OtherPosition = _bond->leftatom->getPosition();
 | 
|---|
| 199 |       break;
 | 
|---|
| 200 |     default:
 | 
|---|
| 201 |       ASSERT(0,
 | 
|---|
| 202 |           "GLMoleculeObject_bond::resetPosition() - side is not a valid argument: "+toString(BondSide)+".");
 | 
|---|
| 203 |       break;
 | 
|---|
| 204 |   }
 | 
|---|
| 205 |   const double distance =
 | 
|---|
| 206 |       Position.distance(OtherPosition)/2.;
 | 
|---|
| 207 |   setScaleZ(distance);
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   // calculate position
 | 
|---|
| 210 |   Vector Z(unitVec[2]); // cylinder are initially aligned along the Z axis
 | 
|---|
| 211 |   Vector zeroVec(0.,0.,0.);
 | 
|---|
| 212 |   Vector a,b;
 | 
|---|
| 213 |   Vector OtherAxis;
 | 
|---|
| 214 |   double alpha;
 | 
|---|
| 215 |   a = Position - OtherPosition;
 | 
|---|
| 216 |   // construct rotation axis
 | 
|---|
| 217 |   b = a;
 | 
|---|
| 218 |   b.VectorProduct(Z);
 | 
|---|
| 219 |   Line axis(zeroVec, b);
 | 
|---|
| 220 |   // calculate rotation angle
 | 
|---|
| 221 |   alpha = a.Angle(Z);
 | 
|---|
| 222 |   // construct other axis to check right-hand rule
 | 
|---|
| 223 |   OtherAxis = b;
 | 
|---|
| 224 |   OtherAxis.VectorProduct(Z);
 | 
|---|
| 225 |   // assure right-hand rule for the rotation
 | 
|---|
| 226 |   if (a.ScalarProduct(OtherAxis) < MYEPSILON)
 | 
|---|
| 227 |     alpha = M_PI-alpha;
 | 
|---|
| 228 |   // check
 | 
|---|
| 229 |   Vector a_rotated = axis.rotateVector(a, alpha);
 | 
|---|
| 230 |   LOG(3, "INFO: Created cylinder from "// << Position << " to " << OtherPosition
 | 
|---|
| 231 |       << a << " to " << a_rotated << " around " << b << " by " << alpha/M_PI*180. << ", respectively.");
 | 
|---|
| 232 | 
 | 
|---|
| 233 |   // set position (cylinder offset is in its barymetric center)
 | 
|---|
| 234 |   Vector OneFourth(Position - 0.75 * a);
 | 
|---|
| 235 |   setPosition(QVector3D(OneFourth[0], OneFourth[1], OneFourth[2]));
 | 
|---|
| 236 |   setRotationVector(QVector3D(b[0], b[1], b[2]));
 | 
|---|
| 237 |   setRotationAngle(alpha/M_PI*180.);
 | 
|---|
| 238 | }
 | 
|---|