[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_atom.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_atom.hpp"
|
---|
| 37 |
|
---|
| 38 | #include <Qt3D/qglscenenode.h>
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 41 |
|
---|
| 42 | #include "CodePatterns/Assert.hpp"
|
---|
[7188b1] | 43 | #include "CodePatterns/Log.hpp"
|
---|
[02ce36] | 44 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
[907636] | 45 |
|
---|
[534374] | 46 | #include <algorithm>
|
---|
| 47 | #include <boost/assign.hpp>
|
---|
| 48 |
|
---|
[2ad1ec] | 49 | #include "Atom/atom.hpp"
|
---|
| 50 | #include "Bond/bond.hpp"
|
---|
[7188b1] | 51 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
[3bdb6d] | 52 | #include "Element/element.hpp"
|
---|
[534374] | 53 | #include "Element/periodentafel.hpp"
|
---|
[907636] | 54 | #include "LinearAlgebra/Vector.hpp"
|
---|
[2ad1ec] | 55 | #include "GLMoleculeObject_bond.hpp"
|
---|
[7188b1] | 56 | #include "World.hpp"
|
---|
[89b992] | 57 | #include "WorldTime.hpp"
|
---|
[907636] | 58 |
|
---|
[534374] | 59 | using namespace boost::assign;
|
---|
| 60 |
|
---|
| 61 | static const Observable::channels_t getAtomBondsChannels()
|
---|
| 62 | {
|
---|
| 63 | Observable::channels_t channels;
|
---|
| 64 | channels += AtomObservable::BondsAdded, AtomObservable::BondsRemoved;
|
---|
| 65 | return channels;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | // static entities
|
---|
| 69 | const Observable::channels_t
|
---|
| 70 | GLMoleculeObject_atom::AtomIndexChannels(1, AtomObservable::IndexChanged);
|
---|
| 71 | const Observable::channels_t
|
---|
| 72 | GLMoleculeObject_atom::AtomPositionChannels(1, AtomObservable::PositionChanged);
|
---|
| 73 | const Observable::channels_t
|
---|
| 74 | GLMoleculeObject_atom::AtomElementChannels(1, AtomObservable::ElementChanged);
|
---|
| 75 | const Observable::channels_t
|
---|
| 76 | GLMoleculeObject_atom::AtomBondsChannels(getAtomBondsChannels());
|
---|
| 77 |
|
---|
[917659] | 78 | GLMoleculeObject_atom::GLMoleculeObject_atom(QGLSceneNode *mesh[], QObject *parent, const atomId_t _id) :
|
---|
[bca99d] | 79 | GLMoleculeObject(mesh, parent),
|
---|
[917659] | 80 | Observer(std::string("GLMoleculeObject_atom")+toString(_id)),
|
---|
[534374] | 81 | atomref(getAtom(_id)),
|
---|
| 82 | AtomIndex(
|
---|
| 83 | atomref,
|
---|
| 84 | boost::bind(&GLMoleculeObject_atom::updateIndex, this),
|
---|
| 85 | "AtomIndex_"+toString(_id),
|
---|
| 86 | _id,
|
---|
| 87 | AtomIndexChannels),
|
---|
| 88 | AtomPosition(
|
---|
| 89 | atomref,
|
---|
| 90 | boost::bind(&GLMoleculeObject_atom::updatePosition, this),
|
---|
| 91 | "AtomPosition_"+toString(_id),
|
---|
| 92 | updatePosition(),
|
---|
| 93 | AtomPositionChannels),
|
---|
| 94 | AtomElement(
|
---|
| 95 | atomref,
|
---|
| 96 | boost::bind(&GLMoleculeObject_atom::updateElement, this),
|
---|
| 97 | "AtomElement"+toString(_id),
|
---|
| 98 | updateElement(),
|
---|
| 99 | AtomElementChannels),
|
---|
| 100 | AtomBonds(
|
---|
| 101 | atomref,
|
---|
| 102 | boost::bind(&GLMoleculeObject_atom::updateBonds, this),
|
---|
| 103 | "AtomBonds_"+toString(_id),
|
---|
| 104 | updateBonds(),
|
---|
| 105 | AtomBondsChannels),
|
---|
| 106 | owner(NULL)
|
---|
[7188b1] | 107 | {
|
---|
[534374] | 108 | setObjectId(_id);
|
---|
| 109 | resetPosition();
|
---|
| 110 | resetElement();
|
---|
[7188b1] | 111 |
|
---|
[a39d72] | 112 | m_selected = const_cast<const World &>(World::getInstance()).isAtomSelected(_id);
|
---|
| 113 |
|
---|
[534374] | 114 | // sign On
|
---|
| 115 | activateObserver();
|
---|
[7188b1] | 116 |
|
---|
[534374] | 117 | // atomref is only used for caching the ref, it must be used elswhere
|
---|
| 118 | const_cast<atom *&>(atomref) = NULL;
|
---|
[7188b1] | 119 |
|
---|
| 120 | connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
|
---|
[534374] | 121 | connect( this, SIGNAL(idChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
|
---|
| 122 | connect( this, SIGNAL(elementChanged()), this, SLOT(resetElement()), Qt::QueuedConnection);
|
---|
| 123 | connect( this, SIGNAL(positionChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
|
---|
| 124 | connect( this, SIGNAL(bondsChanged()), this, SLOT(resetPosition()), Qt::QueuedConnection);
|
---|
[7188b1] | 125 | }
|
---|
| 126 |
|
---|
[534374] | 127 | void GLMoleculeObject_atom::activateObserver()
|
---|
[9c18e4] | 128 | {
|
---|
[534374] | 129 | if (atomref != NULL) {
|
---|
| 130 | owner = static_cast<const Observable *>(atomref);
|
---|
| 131 | owner->signOn(this, AtomObservable::IndexChanged);
|
---|
| 132 | owner->signOn(this, AtomObservable::PositionChanged);
|
---|
| 133 | owner->signOn(this, AtomObservable::ElementChanged);
|
---|
| 134 | owner->signOn(this, AtomObservable::BondsAdded);
|
---|
| 135 | owner->signOn(this, AtomObservable::BondsRemoved);
|
---|
[c736fe] | 136 | }
|
---|
[9c18e4] | 137 | }
|
---|
| 138 |
|
---|
[534374] | 139 |
|
---|
| 140 | void GLMoleculeObject_atom::deactivateObserver()
|
---|
[7188b1] | 141 | {
|
---|
[534374] | 142 | // sign Off
|
---|
| 143 | if (owner != NULL) {
|
---|
| 144 | owner->signOff(this, AtomObservable::IndexChanged);
|
---|
| 145 | owner->signOff(this, AtomObservable::PositionChanged);
|
---|
| 146 | owner->signOff(this, AtomObservable::ElementChanged);
|
---|
| 147 | owner->signOff(this, AtomObservable::BondsAdded);
|
---|
| 148 | owner->signOff(this, AtomObservable::BondsRemoved);
|
---|
| 149 | owner = NULL;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | GLMoleculeObject_atom::~GLMoleculeObject_atom()
|
---|
| 154 | {
|
---|
| 155 | deactivateObserver();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | void GLMoleculeObject_atom::resetIndex()
|
---|
| 159 | {
|
---|
| 160 | const atomId_t newId = AtomIndex.get();
|
---|
| 161 | const size_t oldId = objectId();
|
---|
| 162 | ASSERT( newId != oldId,
|
---|
| 163 | "GLMoleculeObject_atom::updateIndex() - index "+toString(newId)+" did not change.");
|
---|
| 164 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new index is "+toString(newId)+".");
|
---|
| 165 | setObjectId(newId);
|
---|
| 166 |
|
---|
| 167 | emit indexChanged(this, oldId, newId);
|
---|
[7188b1] | 168 | }
|
---|
| 169 |
|
---|
| 170 | void GLMoleculeObject_atom::resetPosition()
|
---|
[907636] | 171 | {
|
---|
[534374] | 172 | const Vector Position = AtomPosition.get();
|
---|
| 173 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new position is "+toString(Position)+".");
|
---|
| 174 | setPosition(QVector3D(Position[0], Position[1], Position[2]));
|
---|
[7188b1] | 175 | }
|
---|
| 176 |
|
---|
| 177 | void GLMoleculeObject_atom::resetElement()
|
---|
| 178 | {
|
---|
| 179 | size_t elementno = 0;
|
---|
[534374] | 180 | const element * const _type = World::getInstance().
|
---|
| 181 | getPeriode()->FindElement(AtomElement.get());
|
---|
[c60665] | 182 | if (_type != NULL) {
|
---|
| 183 | elementno = _type->getAtomicNumber();
|
---|
| 184 | } else { // if no element yet, set to hydrogen
|
---|
| 185 | elementno = 1;
|
---|
| 186 | }
|
---|
| 187 | LOG(4, "INFO: GLMoleculeObject_atom::resetIndex() - new element number is "+toString(elementno)+".");
|
---|
| 188 |
|
---|
| 189 | // set materials
|
---|
| 190 | QGLMaterial *elementmaterial = getMaterial(elementno);
|
---|
| 191 | ASSERT(elementmaterial != NULL,
|
---|
| 192 | "GLMoleculeObject_atom::GLMoleculeObject_atom() - QGLMaterial ref from getter function is NULL.");
|
---|
| 193 | setMaterial(elementmaterial);
|
---|
| 194 |
|
---|
| 195 | // set scale
|
---|
| 196 | double radius = 0.;
|
---|
| 197 | if (_type != NULL) {
|
---|
| 198 | radius = _type->getVanDerWaalsRadius();
|
---|
| 199 | } else {
|
---|
| 200 | radius = 0.5;
|
---|
| 201 | }
|
---|
| 202 | setScale( radius / 4. );
|
---|
[534374] | 203 | }
|
---|
[f115cc] | 204 |
|
---|
[534374] | 205 | void GLMoleculeObject_atom::resetBonds()
|
---|
| 206 | {
|
---|
| 207 | ListOfBonds_t ListOfBonds_new = AtomBonds.get();
|
---|
| 208 | std::sort(ListOfBonds_new.begin(), ListOfBonds_new.end());
|
---|
| 209 | ListOfBonds_t BondsToAdd;
|
---|
| 210 | std::set_difference(
|
---|
| 211 | ListOfBonds_new.begin(), ListOfBonds_new.end(),
|
---|
| 212 | ListOfBonds.begin(), ListOfBonds.end(),
|
---|
| 213 | std::back_inserter(BondsToAdd));
|
---|
| 214 | ListOfBonds_t BondsToRemove;
|
---|
| 215 | std::set_difference(
|
---|
| 216 | ListOfBonds.begin(), ListOfBonds.end(),
|
---|
| 217 | ListOfBonds_new.begin(), ListOfBonds_new.end(),
|
---|
| 218 | std::back_inserter(BondsToRemove));
|
---|
| 219 | for (ListOfBonds_t::const_iterator iter = BondsToAdd.begin();
|
---|
| 220 | iter != BondsToAdd.end();
|
---|
| 221 | ++iter) {
|
---|
| 222 | const GLMoleculeObject_bond::SideOfBond side = (iter->first == AtomIndex.get()) ?
|
---|
| 223 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
---|
| 224 | emit BondsAdded(iter->first, iter->second, side);
|
---|
| 225 | }
|
---|
| 226 | for (ListOfBonds_t::const_iterator iter = BondsToRemove.begin();
|
---|
| 227 | iter != BondsToRemove.end();
|
---|
| 228 | ++iter) {
|
---|
| 229 | emit BondsRemoved(iter->first, iter->second);
|
---|
| 230 | }
|
---|
| 231 | ListOfBonds = ListOfBonds_new;
|
---|
[7188b1] | 232 | }
|
---|
| 233 |
|
---|
[015f8c] | 234 | void GLMoleculeObject_atom::Selected()
|
---|
| 235 | {
|
---|
| 236 | ASSERT( !m_selected,
|
---|
| 237 | "GLMoleculeObject_atom::Selected() - 3D rep of atom is already selected.");
|
---|
| 238 | m_selected = true;
|
---|
| 239 |
|
---|
| 240 | emit changed();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void GLMoleculeObject_atom::Unselected()
|
---|
[7188b1] | 244 | {
|
---|
[015f8c] | 245 | ASSERT( m_selected,
|
---|
| 246 | "GLMoleculeObject_atom::Unselected() - 3D rep of atom is already unselected.");
|
---|
| 247 | m_selected = false;
|
---|
| 248 |
|
---|
| 249 | emit changed();
|
---|
[534374] | 250 | }
|
---|
[30cd0d] | 251 |
|
---|
[015f8c] | 252 |
|
---|
[534374] | 253 | void GLMoleculeObject_atom::draw(QGLPainter *painter, const QVector4D &cameraPlane)
|
---|
| 254 | {
|
---|
| 255 | // call old hook to do the actual paining
|
---|
| 256 | GLMoleculeObject::draw(painter, cameraPlane);
|
---|
[7188b1] | 257 | }
|
---|
| 258 |
|
---|
[534374] | 259 | void GLMoleculeObject_atom::wasClicked()
|
---|
[7188b1] | 260 | {
|
---|
[534374] | 261 | LOG(4, "INFO: GLMoleculeObject_atom: atom " << AtomIndex.get() << " has been clicked");
|
---|
| 262 | emit clicked(AtomIndex.get());
|
---|
| 263 | }
|
---|
[907636] | 264 |
|
---|
[534374] | 265 | const atom * const GLMoleculeObject_atom::getAtomConst(const atomId_t _id)
|
---|
| 266 | {
|
---|
| 267 | const atom * const _atom = const_cast<const World &>(World::getInstance()).
|
---|
| 268 | getAtom(AtomById(_id));
|
---|
| 269 | return _atom;
|
---|
| 270 | }
|
---|
[907636] | 271 |
|
---|
[534374] | 272 | atom * const GLMoleculeObject_atom::getAtom(const atomId_t _id)
|
---|
| 273 | {
|
---|
| 274 | atom * const _atom = World::getInstance().getAtom(AtomById(_id));
|
---|
| 275 | return _atom;
|
---|
| 276 | }
|
---|
[d53902] | 277 |
|
---|
[534374] | 278 | atomId_t GLMoleculeObject_atom::updateIndex() const
|
---|
| 279 | {
|
---|
[54bdaa] | 280 | return const_cast<const World &>(World::getInstance()).lastChangedAtomId();
|
---|
[7188b1] | 281 | }
|
---|
[907636] | 282 |
|
---|
[534374] | 283 | Vector GLMoleculeObject_atom::updatePosition() const
|
---|
[f115cc] | 284 | {
|
---|
[534374] | 285 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
| 286 | if (_atom != NULL) {
|
---|
| 287 | return _atom->getPosition();
|
---|
| 288 | } else {
|
---|
| 289 | return zeroVec;
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
[f115cc] | 292 |
|
---|
[534374] | 293 | atomicNumber_t GLMoleculeObject_atom::updateElement() const
|
---|
| 294 | {
|
---|
| 295 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
| 296 | if (_atom != NULL) {
|
---|
| 297 | return _atom->getElementNo();
|
---|
| 298 | } else {
|
---|
| 299 | return (atomicNumber_t)-1;
|
---|
| 300 | }
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | GLMoleculeObject_atom::ListOfBonds_t GLMoleculeObject_atom::updateBonds() const
|
---|
| 304 | {
|
---|
| 305 | ListOfBonds_t ListOfBonds;
|
---|
| 306 | const atom * const _atom = getAtom(AtomIndex.get());
|
---|
| 307 | if (_atom != NULL) {
|
---|
| 308 | // make sure position is up-to-date
|
---|
| 309 | const BondList ListBonds = _atom->getListOfBonds();
|
---|
| 310 | for (BondList::const_iterator iter = ListBonds.begin();
|
---|
| 311 | iter != ListBonds.end();
|
---|
| 312 | ++iter)
|
---|
| 313 | ListOfBonds.insert( ListOfBonds.end(), std::make_pair(
|
---|
| 314 | (*iter)->leftatom->getId(),
|
---|
| 315 | (*iter)->rightatom->getId()) );
|
---|
| 316 | } else {
|
---|
| 317 | ELOG(2, "Atom with id "+toString(AtomIndex.get())+" is already gone.");
|
---|
| 318 | }
|
---|
| 319 | return ListOfBonds;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | void GLMoleculeObject_atom::update(Observable *publisher)
|
---|
| 323 | {
|
---|
| 324 | ASSERT(0, "GLMoleculeObject_atom::update() - we are not signed on for global updates.");
|
---|
[f115cc] | 325 | }
|
---|
| 326 |
|
---|
[7188b1] | 327 | void GLMoleculeObject_atom::subjectKilled(Observable *publisher)
|
---|
[c736fe] | 328 | {
|
---|
[534374] | 329 | deactivateObserver();
|
---|
[c736fe] | 330 | }
|
---|
[06ebf5] | 331 |
|
---|
[7188b1] | 332 | void GLMoleculeObject_atom::recieveNotification(Observable *publisher, Notification_ptr notification)
|
---|
| 333 | {
|
---|
[534374] | 334 | // ObservedValues have been updated before, hence convert updates to Qt's signals
|
---|
| 335 | atom * const _atom = dynamic_cast<atom *>(publisher);
|
---|
| 336 | if (_atom != NULL) {
|
---|
[d53902] | 337 | switch (notification->getChannelNo()) {
|
---|
| 338 | case AtomObservable::IndexChanged:
|
---|
[534374] | 339 | emit idChanged();
|
---|
[d53902] | 340 | break;
|
---|
| 341 | case AtomObservable::PositionChanged:
|
---|
[534374] | 342 | emit positionChanged();
|
---|
| 343 | break;
|
---|
| 344 | case AtomObservable::ElementChanged:
|
---|
| 345 | emit elementChanged();
|
---|
[d53902] | 346 | break;
|
---|
| 347 | case AtomObservable::BondsAdded:
|
---|
[534374] | 348 | case AtomObservable::BondsRemoved:
|
---|
| 349 | emit bondsChanged();
|
---|
[917659] | 350 | break;
|
---|
[d53902] | 351 | default:
|
---|
[534374] | 352 | ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - we are not signed on to channel "
|
---|
| 353 | +toString(notification->getChannelNo())+" of the atom.");
|
---|
[d53902] | 354 | break;
|
---|
| 355 | }
|
---|
[015f8c] | 356 | } else
|
---|
| 357 | ASSERT(0, "GLMoleculeObject_atom::recieveNotification() - received notification from unexpected source.");
|
---|
[06ebf5] | 358 | }
|
---|