| 1 | /*
|
|---|
| 2 | * Project: MoleCuilder
|
|---|
| 3 | * Description: creates and alters molecular systems
|
|---|
| 4 | * Copyright (C) 2010-2012 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 | #include "GLMoleculeObject_molecule.hpp"
|
|---|
| 28 |
|
|---|
| 29 | #include "CodePatterns/MemDebug.hpp"
|
|---|
| 30 |
|
|---|
| 31 | #include "CodePatterns/Log.hpp"
|
|---|
| 32 |
|
|---|
| 33 | #include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
|
|---|
| 34 | #include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
|
|---|
| 35 | #include "Atom/atom.hpp"
|
|---|
| 36 | #include "Bond/bond.hpp"
|
|---|
| 37 | #include "Descriptors/AtomIdDescriptor.hpp"
|
|---|
| 38 | #include "Helpers/helpers.hpp"
|
|---|
| 39 | #include "molecule.hpp"
|
|---|
| 40 | #include "World.hpp"
|
|---|
| 41 |
|
|---|
| 42 | #include <iostream>
|
|---|
| 43 |
|
|---|
| 44 | using namespace MoleCuilder;
|
|---|
| 45 |
|
|---|
| 46 | std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
|
|---|
| 47 | {
|
|---|
| 48 | ost << t.first << "," << t.second;
|
|---|
| 49 | return ost;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | GLWorldScene::GLWorldScene(QObject *parent)
|
|---|
| 53 | : QObject(parent)
|
|---|
| 54 | {
|
|---|
| 55 | init();
|
|---|
| 56 |
|
|---|
| 57 | //changeMaterials(false);
|
|---|
| 58 | domainBoxMaterial = new QGLMaterial;
|
|---|
| 59 | domainBoxMaterial->setAmbientColor(QColor(0,0,0,255));
|
|---|
| 60 | domainBoxMaterial->setDiffuseColor(QColor(0,0,0,255));
|
|---|
| 61 | domainBoxMaterial->setEmittedLight(QColor(150,160,200,255));
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | GLWorldScene::~GLWorldScene()
|
|---|
| 65 | {
|
|---|
| 66 | // remove all elements
|
|---|
| 67 | GLMoleculeObject::cleanMaterialMap();
|
|---|
| 68 |
|
|---|
| 69 | delete(domainBoxMaterial);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /** Initialise the WorldScene with molecules and atoms from World.
|
|---|
| 73 | *
|
|---|
| 74 | */
|
|---|
| 75 | void GLWorldScene::init()
|
|---|
| 76 | {
|
|---|
| 77 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
|---|
| 78 |
|
|---|
| 79 | if (molecules.size() > 0) {
|
|---|
| 80 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
|---|
| 81 | Runner != molecules.end();
|
|---|
| 82 | Runner++) {
|
|---|
| 83 |
|
|---|
| 84 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
|---|
| 85 | atomiter != (*Runner)->end();
|
|---|
| 86 | ++atomiter) {
|
|---|
| 87 | // create atom objects in scene
|
|---|
| 88 | atomInserted(*atomiter);
|
|---|
| 89 |
|
|---|
| 90 | // create bond objects in scene
|
|---|
| 91 | const BondList &bondlist = (*atomiter)->getListOfBonds();
|
|---|
| 92 | for (BondList::const_iterator bonditer = bondlist.begin();
|
|---|
| 93 | bonditer != bondlist.end();
|
|---|
| 94 | ++bonditer) {
|
|---|
| 95 | const bond *_bond = *bonditer;
|
|---|
| 96 | const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
|
|---|
| 97 | GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
|
|---|
| 98 | bondInserted(_bond, side);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /** Adds an atom to the scene.
|
|---|
| 106 | *
|
|---|
| 107 | * @param _atom atom to add
|
|---|
| 108 | */
|
|---|
| 109 | void GLWorldScene::atomInserted(const atom *_atom)
|
|---|
| 110 | {
|
|---|
| 111 | LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
|---|
| 112 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
|---|
| 113 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 114 | ASSERT(iter == AtomsinSceneMap.end(),
|
|---|
| 115 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
|---|
| 116 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
|---|
| 117 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
|---|
| 118 | connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 119 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
|---|
| 120 | connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 121 | connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
|
|---|
| 122 | //bondsChanged(_atom);
|
|---|
| 123 | emit changeOccured();
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Removes an atom from the scene.
|
|---|
| 127 | *
|
|---|
| 128 | * @param _atom atom to remove
|
|---|
| 129 | */
|
|---|
| 130 | void GLWorldScene::atomRemoved(const atom *_atom)
|
|---|
| 131 | {
|
|---|
| 132 | LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
|---|
| 133 | // bonds are removed by signal coming from ~bond
|
|---|
| 134 | // remove atoms
|
|---|
| 135 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
|---|
| 136 | ASSERT(iter != AtomsinSceneMap.end(),
|
|---|
| 137 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
|---|
| 138 | GLMoleculeObject_atom *atomObject = iter->second;
|
|---|
| 139 | atomObject->disconnect();
|
|---|
| 140 | AtomsinSceneMap.erase(iter);
|
|---|
| 141 | delete atomObject;
|
|---|
| 142 | emit changeOccured();
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** ....
|
|---|
| 146 | *
|
|---|
| 147 | */
|
|---|
| 148 | void GLWorldScene::worldSelectionChanged()
|
|---|
| 149 | {
|
|---|
| 150 | LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
|
|---|
| 151 |
|
|---|
| 152 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
|---|
| 153 |
|
|---|
| 154 | if (molecules.size() > 0) {
|
|---|
| 155 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
|---|
| 156 | Runner != molecules.end();
|
|---|
| 157 | Runner++) {
|
|---|
| 158 |
|
|---|
| 159 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
|
|---|
| 160 | bool isSelected = World::getInstance().isSelected(*Runner);
|
|---|
| 161 |
|
|---|
| 162 | // molecule selected but not in scene?
|
|---|
| 163 | if (isSelected && (iter == MoleculesinSceneMap.end())){
|
|---|
| 164 | // -> create new mesh
|
|---|
| 165 | GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(this, *Runner);
|
|---|
| 166 | MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
|
|---|
| 167 | connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 168 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 169 | connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
|
|---|
| 170 | emit changed();
|
|---|
| 171 | emit changeOccured();
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | // molecule not selected but in scene?
|
|---|
| 175 | if (!isSelected && (iter != MoleculesinSceneMap.end())){
|
|---|
| 176 | // -> remove from scene
|
|---|
| 177 | moleculeRemoved(*Runner);
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Removes a molecule from the scene.
|
|---|
| 185 | *
|
|---|
| 186 | * @param _molecule molecule to remove
|
|---|
| 187 | */
|
|---|
| 188 | void GLWorldScene::moleculeRemoved(const molecule *_molecule)
|
|---|
| 189 | {
|
|---|
| 190 | LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
|
|---|
| 191 | MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
|
|---|
| 192 |
|
|---|
| 193 | // only remove if the molecule is in the scene
|
|---|
| 194 | // (= is selected)
|
|---|
| 195 | if (iter != MoleculesinSceneMap.end()){
|
|---|
| 196 | GLMoleculeObject_molecule *molObject = iter->second;
|
|---|
| 197 | molObject->disconnect();
|
|---|
| 198 | MoleculesinSceneMap.erase(iter);
|
|---|
| 199 | delete molObject;
|
|---|
| 200 | emit changed();
|
|---|
| 201 | emit changeOccured();
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Adds a bond to the scene.
|
|---|
| 206 | *
|
|---|
| 207 | * @param _bond bond to add
|
|---|
| 208 | * @param side which side of the bond (left or right)
|
|---|
| 209 | */
|
|---|
| 210 | void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
|
|---|
| 211 | {
|
|---|
| 212 | LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
|---|
| 213 | //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
|
|---|
| 214 |
|
|---|
| 215 | BondIds ids;
|
|---|
| 216 | switch (side) {
|
|---|
| 217 | case GLMoleculeObject_bond::left:
|
|---|
| 218 | ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
|
|---|
| 219 | break;
|
|---|
| 220 | case GLMoleculeObject_bond::right:
|
|---|
| 221 | ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
|
|---|
| 222 | break;
|
|---|
| 223 | }
|
|---|
| 224 | #ifndef NDEBUG
|
|---|
| 225 | BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
|
|---|
| 226 | ASSERT(iter == BondsinSceneMap.end(),
|
|---|
| 227 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
|---|
| 228 | #endif
|
|---|
| 229 | GLMoleculeObject_bond *bondObject =
|
|---|
| 230 | new GLMoleculeObject_bond(this, _bond, side);
|
|---|
| 231 | connect (
|
|---|
| 232 | bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
|
|---|
| 233 | this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
|
|---|
| 234 | connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
|
|---|
| 235 | BondsinSceneMap.insert( make_pair(ids, bondObject) );
|
|---|
| 236 | // BondIdsinSceneMap.insert( Leftids );
|
|---|
| 237 | emit changeOccured();
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | /** Removes a bond from the scene.
|
|---|
| 241 | *
|
|---|
| 242 | * @param _bond bond to remove
|
|---|
| 243 | */
|
|---|
| 244 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
|---|
| 245 | {
|
|---|
| 246 | LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
|
|---|
| 247 | {
|
|---|
| 248 | // left bond
|
|---|
| 249 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
|---|
| 250 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
|---|
| 251 | ASSERT(leftiter != BondsinSceneMap.end(),
|
|---|
| 252 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
|---|
| 253 | +toString(rightnr)+" not on display.");
|
|---|
| 254 | //GLMoleculeObject_bond *bondObject = leftiter->second;
|
|---|
| 255 | BondsinSceneMap.erase(leftiter);
|
|---|
| 256 | //delete bondObject; // is done by signal from bond itself
|
|---|
| 257 | //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | emit changeOccured();
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
|---|
| 264 | {
|
|---|
| 265 | // Initialize all of the mesh objects that we have as children.
|
|---|
| 266 | foreach (QObject *obj, children()) {
|
|---|
| 267 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 268 | if (meshobj)
|
|---|
| 269 | meshobj->initialize(view, painter);
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | void GLWorldScene::drawDomainBox(QGLPainter *painter) const
|
|---|
| 274 | {
|
|---|
| 275 | RealSpaceMatrix m = World::getInstance().getDomain().getM();
|
|---|
| 276 | painter->modelViewMatrix().push();
|
|---|
| 277 | painter->modelViewMatrix() *= QMatrix4x4(m.at(0,0), m.at(0,1), m.at(0,2), 0.0,
|
|---|
| 278 | m.at(1,0), m.at(1,1), m.at(1,2), 0.0,
|
|---|
| 279 | m.at(2,0), m.at(2,1), m.at(2,2), 0.0,
|
|---|
| 280 | 0.0, 0.0, 0.0, 1.0);
|
|---|
| 281 |
|
|---|
| 282 | painter->setFaceMaterial(QGL::AllFaces, domainBoxMaterial);
|
|---|
| 283 | //glEnable(GL_LINE_SMOOTH);
|
|---|
| 284 | QVector3DArray array;
|
|---|
| 285 | array.append(0, 0, 0); array.append(1, 0, 0);
|
|---|
| 286 | array.append(1, 0, 0); array.append(1, 1, 0);
|
|---|
| 287 | array.append(1, 1, 0); array.append(0, 1, 0);
|
|---|
| 288 | array.append(0, 1, 0); array.append(0, 0, 0);
|
|---|
| 289 |
|
|---|
| 290 | array.append(0, 0, 1); array.append(1, 0, 1);
|
|---|
| 291 | array.append(1, 0, 1); array.append(1, 1, 1);
|
|---|
| 292 | array.append(1, 1, 1); array.append(0, 1, 1);
|
|---|
| 293 | array.append(0, 1, 1); array.append(0, 0, 1);
|
|---|
| 294 |
|
|---|
| 295 | array.append(0, 0, 0); array.append(0, 0, 1);
|
|---|
| 296 | array.append(1, 0, 0); array.append(1, 0, 1);
|
|---|
| 297 | array.append(0, 1, 0); array.append(0, 1, 1);
|
|---|
| 298 | array.append(1, 1, 0); array.append(1, 1, 1);
|
|---|
| 299 | painter->clearAttributes();
|
|---|
| 300 | painter->setVertexAttribute(QGL::Position, array);
|
|---|
| 301 | painter->draw(QGL::Lines, 24);
|
|---|
| 302 | painter->modelViewMatrix().pop();
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | void GLWorldScene::draw(QGLPainter *painter) const
|
|---|
| 306 | {
|
|---|
| 307 | // Draw all of the mesh objects that we have as children.
|
|---|
| 308 | foreach (QObject *obj, children()) {
|
|---|
| 309 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
|---|
| 310 | if (meshobj)
|
|---|
| 311 | meshobj->draw(painter);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | drawDomainBox(painter);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | void GLWorldScene::atomClicked(atomId_t no)
|
|---|
| 318 | {
|
|---|
| 319 | LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
|
|---|
| 320 | const atom *Walker = World::getInstance().getAtom(AtomById(no));
|
|---|
| 321 | if (!World::getInstance().isSelected(Walker))
|
|---|
| 322 | SelectionAtomById(no);
|
|---|
| 323 | else
|
|---|
| 324 | SelectionNotAtomById(no);
|
|---|
| 325 | emit clicked(no);
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|