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