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