| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /*
|
|---|
| 9 | * GLWorldScene.cpp
|
|---|
| 10 | *
|
|---|
| 11 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
|---|
| 12 | *
|
|---|
| 13 | * Created on: Aug 17, 2011
|
|---|
| 14 | * Author: heber
|
|---|
| 15 | */
|
|---|
| 16 |
|
|---|
| 17 | // include config.h
|
|---|
| 18 | #ifdef HAVE_CONFIG_H
|
|---|
| 19 | #include <config.h>
|
|---|
| 20 | #endif
|
|---|
| 21 |
|
|---|
| 22 | #include "GLWorldScene.hpp"
|
|---|
| 23 |
|
|---|
| 24 | #include "GLMoleculeObject.hpp"
|
|---|
| 25 | #include "GLMoleculeObject_atom.hpp"
|
|---|
| 26 | #include "GLMoleculeObject_bond.hpp"
|
|---|
| 27 |
|
|---|
| 28 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 29 |
|
|---|
| 30 | #include "CodePatterns/Log.hpp"
|
|---|
| 31 |
|
|---|
| 32 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
|---|
| 33 | #include "atom.hpp"
|
|---|
| 34 | #include "Bond/bond.hpp"
|
|---|
| 35 | #include "Helpers/helpers.hpp"
|
|---|
| 36 | #include "molecule.hpp"
|
|---|
| 37 | #include "World.hpp"
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | GLWorldScene::GLWorldScene(QObject *parent)
|
|---|
| 41 | : QObject(parent)
|
|---|
| 42 | {
|
|---|
| 43 | init();
|
|---|
| 44 |
|
|---|
| 45 | //changeMaterials(false);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | GLWorldScene::~GLWorldScene()
|
|---|
| 49 | {
|
|---|
| 50 | // remove all elements
|
|---|
| 51 | GLMoleculeObject::cleanMaterialMap();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /** Initialise the WorldScene with molecules and atoms from World.
|
|---|
| 55 | *
|
|---|
| 56 | */
|
|---|
| 57 | void GLWorldScene::init()
|
|---|
| 58 | {
|
|---|
| 59 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
|---|
| 60 |
|
|---|
| 61 | if (molecules.size() > 0) {
|
|---|
| 62 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
|---|
| 63 | Runner != molecules.end();
|
|---|
| 64 | Runner++) {
|
|---|
| 65 | // create molecule
|
|---|
| 66 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
|---|
| 67 | atomiter != (*Runner)->end();
|
|---|
| 68 | ++atomiter) {
|
|---|
| 69 | // create atom objects in scene
|
|---|
| 70 | atomInserted(*atomiter);
|
|---|
| 71 |
|
|---|
| 72 | // create bond objects in scene
|
|---|
| 73 | bondsChanged(*atomiter);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Adds an atom to the scene.
|
|---|
| 80 | *
|
|---|
| 81 | * @param _atom atom to add
|
|---|
| 82 | */
|
|---|
| 83 | void GLWorldScene::atomInserted(const atom *_atom)
|
|---|
| 84 | {
|
|---|
| 85 | LOG(0, "GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
|---|
| 86 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
|---|
| 87 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 88 | ASSERT(iter == AtomsinSceneMap.end(),
|
|---|
| 89 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
|---|
| 90 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
|---|
| 91 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
|---|
| 92 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
|---|
| 93 | connect (atomObject, SIGNAL(BondsChanged(const atom *)), this, SLOT(bondsChanged(const atom *)));
|
|---|
| 94 | bondsChanged(_atom);
|
|---|
| 95 | emit changeOccured();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /** Removes an atom from the scene.
|
|---|
| 99 | *
|
|---|
| 100 | * @param _atom atom to remove
|
|---|
| 101 | */
|
|---|
| 102 | void GLWorldScene::atomRemoved(const atom *_atom)
|
|---|
| 103 | {
|
|---|
| 104 | LOG(0, "GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
|---|
| 105 | // remove all its bonds
|
|---|
| 106 | const BondList& bondlist = _atom->getListOfBonds();
|
|---|
| 107 | for (BondList::const_iterator iter = bondlist.begin(); iter != bondlist.end(); ++iter) {
|
|---|
| 108 | bondRemoved((*iter)->leftatom->getId(), (*iter)->rightatom->getId());
|
|---|
| 109 | bondRemoved((*iter)->rightatom->getId(), (*iter)->leftatom->getId());
|
|---|
| 110 | }
|
|---|
| 111 | // remove atoms
|
|---|
| 112 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 113 | ASSERT(iter != AtomsinSceneMap.end(),
|
|---|
| 114 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
|---|
| 115 | GLMoleculeObject_atom *atomObject = iter->second;
|
|---|
| 116 | atomObject->disconnect();
|
|---|
| 117 | AtomsinSceneMap.erase(iter);
|
|---|
| 118 | delete atomObject;
|
|---|
| 119 | emit changeOccured();
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /** Updates the bond structure of the signaled \a _atom.
|
|---|
| 123 | *
|
|---|
| 124 | * @param _atom atom whose bonds changed.
|
|---|
| 125 | */
|
|---|
| 126 | void GLWorldScene::bondsChanged(const atom *_atom)
|
|---|
| 127 | {
|
|---|
| 128 | const atomId_t id = _atom->getId();
|
|---|
| 129 |
|
|---|
| 130 | // create list with all present bonds
|
|---|
| 131 | std::set< atomId_t > presentBonds;
|
|---|
| 132 | std::pair< BondIdsMap::const_iterator, BondIdsMap::const_iterator> range =
|
|---|
| 133 | BondIdsinSceneMap.equal_range( id );
|
|---|
| 134 | for (BondIdsMap::const_iterator iter = range.first; iter != range.second; ++iter) {
|
|---|
| 135 | const atomId_t otherid = iter->second;
|
|---|
| 136 | #ifndef NDEBUG
|
|---|
| 137 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
|---|
| 138 | ASSERT(iter == presentBonds.end(),
|
|---|
| 139 | "GLWorldScene::BondsChanged() - bond id "+toString(otherid)+" for atom "
|
|---|
| 140 | +toString(id)+" present twice.");
|
|---|
| 141 | #endif
|
|---|
| 142 | presentBonds.insert( otherid );
|
|---|
| 143 | }
|
|---|
| 144 | LOG(0, "We have the following bonds: "+toString(presentBonds)+".");
|
|---|
| 145 |
|
|---|
| 146 | // search for added bonds
|
|---|
| 147 | const BondList &bondlist = _atom->getListOfBonds();
|
|---|
| 148 | for (BondList::const_iterator bonditer = bondlist.begin();
|
|---|
| 149 | bonditer != bondlist.end();
|
|---|
| 150 | ++bonditer) {
|
|---|
| 151 | const bond *_bond = *bonditer;
|
|---|
| 152 | const atomId_t otherid = _bond->GetOtherAtom(_atom)->getId();
|
|---|
| 153 | const BondIds ids = std::make_pair( id, otherid );
|
|---|
| 154 | BondNodeMap::const_iterator iter = BondsinSceneMap.find(ids);
|
|---|
| 155 | if (iter != BondsinSceneMap.end()) {
|
|---|
| 156 | // bond is already present
|
|---|
| 157 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
|---|
| 158 | ASSERT(iter != presentBonds.end(),
|
|---|
| 159 | "GLWorldScene::BondsChanged() - other id "+toString(otherid)+" for atom "
|
|---|
| 160 | +toString(_atom->getId())+" not present in BondIdsinSceneMap.");
|
|---|
| 161 | presentBonds.erase(otherid);
|
|---|
| 162 | LOG(0, "Removing "+toString(otherid)+" from presentBonds.");
|
|---|
| 163 | } else {
|
|---|
| 164 | // insert new bond
|
|---|
| 165 | bondInserted(_bond);
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | LOG(0, "The following bonds should not be present: "+toString(presentBonds)+".");
|
|---|
| 169 |
|
|---|
| 170 | // remove all still presentBonds
|
|---|
| 171 | for (std::set< atomId_t >::iterator iter = presentBonds.begin();
|
|---|
| 172 | !presentBonds.empty(); iter = presentBonds.begin()) {
|
|---|
| 173 | bondRemoved( id, *iter );
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Adds a bond to the scene.
|
|---|
| 178 | *
|
|---|
| 179 | * @param _bond bond to add
|
|---|
| 180 | */
|
|---|
| 181 | void GLWorldScene::bondInserted(const bond *_bond)
|
|---|
| 182 | {
|
|---|
| 183 | LOG(0, "GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
|---|
| 184 | const double distance =
|
|---|
| 185 | _bond->leftatom->getPosition().distance(_bond->rightatom->getPosition())/2.;
|
|---|
| 186 | {
|
|---|
| 187 | // left bond
|
|---|
| 188 | const BondIds Leftids( make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()) );
|
|---|
| 189 | BondNodeMap::iterator iter = BondsinSceneMap.find(Leftids);
|
|---|
| 190 | ASSERT(iter == BondsinSceneMap.end(),
|
|---|
| 191 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
|---|
| 192 | GLMoleculeObject_bond *bondObject =
|
|---|
| 193 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::left);
|
|---|
| 194 | BondsinSceneMap.insert( make_pair(Leftids, bondObject) );
|
|---|
| 195 | BondIdsinSceneMap.insert( Leftids );
|
|---|
| 196 | }
|
|---|
| 197 | {
|
|---|
| 198 | // right bond
|
|---|
| 199 | const BondIds Rightids( make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()) );
|
|---|
| 200 | BondNodeMap::iterator iter = BondsinSceneMap.find(Rightids);
|
|---|
| 201 | ASSERT(iter == BondsinSceneMap.end(),
|
|---|
| 202 | "GLWorldScene::bondAdded() - same right-sided bond "+toString(*_bond)+" added again.");
|
|---|
| 203 | GLMoleculeObject_bond *bondObject =
|
|---|
| 204 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::right);
|
|---|
| 205 | BondsinSceneMap.insert( make_pair(Rightids, bondObject) );
|
|---|
| 206 | BondIdsinSceneMap.insert( Rightids );
|
|---|
| 207 | }
|
|---|
| 208 | emit changeOccured();
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /** Removes a bond from the scene.
|
|---|
| 212 | *
|
|---|
| 213 | * @param _bond bond to remove
|
|---|
| 214 | */
|
|---|
| 215 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
|---|
| 216 | {
|
|---|
| 217 | LOG(0, "GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(leftnr)+".");
|
|---|
| 218 | {
|
|---|
| 219 | // left bond
|
|---|
| 220 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
|---|
| 221 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
|---|
| 222 | ASSERT(leftiter != BondsinSceneMap.end(),
|
|---|
| 223 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
|---|
| 224 | +toString(rightnr)+" not on display.");
|
|---|
| 225 | GLMoleculeObject_bond *bondObject = leftiter->second;
|
|---|
| 226 | BondsinSceneMap.erase(leftiter);
|
|---|
| 227 | delete bondObject;
|
|---|
| 228 | }
|
|---|
| 229 | // remove from bond ids
|
|---|
| 230 | std::pair<BondIdsMap::iterator, BondIdsMap::iterator> leftrange =
|
|---|
| 231 | BondIdsinSceneMap.equal_range(leftnr);
|
|---|
| 232 | BondIdsMap::iterator iter;
|
|---|
| 233 | for (iter = leftrange.first; iter != leftrange.second; ++iter) {
|
|---|
| 234 | if (iter->second == rightnr) {
|
|---|
| 235 | BondIdsinSceneMap.erase(iter);
|
|---|
| 236 | break;
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 | ASSERT(iter != leftrange.second,
|
|---|
| 240 | "GLWorldScene::bondRemoved() - could not find ("
|
|---|
| 241 | +toString(leftnr)+"-"+toString(rightnr)+" in BondIdsinSceneMap.");
|
|---|
| 242 | emit changeOccured();
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
|---|
| 246 | {
|
|---|
| 247 | // Initialize all of the mesh objects that we have as children.
|
|---|
| 248 | foreach (QObject *obj, children()) {
|
|---|
| 249 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 250 | if (meshobj)
|
|---|
| 251 | meshobj->initialize(view, painter);
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | void GLWorldScene::draw(QGLPainter *painter) const
|
|---|
| 256 | {
|
|---|
| 257 | // Draw all of the mesh objects that we have as children.
|
|---|
| 258 | foreach (QObject *obj, children()) {
|
|---|
| 259 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 260 | if (meshobj)
|
|---|
| 261 | meshobj->draw(painter);
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | void GLWorldScene::atomClicked(atomId_t no)
|
|---|
| 266 | {
|
|---|
| 267 | qDebug("GLWorldScene: atom %d has been clicked.", no);
|
|---|
| 268 | SelectionAtomById(no);
|
|---|
| 269 | emit clicked(no);
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|