source: src/UIElements/Views/Qt4/Qt3D/GLWorldScene.cpp@ 7de208

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 7de208 was 284551, checked in by Frederik Heber <heber@…>, 13 years ago

GLWorldView shows the selected shapes from ShapeRegistry

  • Property mode set to 100644
File size: 13.5 KB
RevLine 
[907636]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[907636]21 */
22
23/*
24 * GLWorldScene.cpp
25 *
26 * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
27 *
28 * Created on: Aug 17, 2011
29 * Author: heber
30 */
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "GLWorldScene.hpp"
[d1196d]38#include <Qt3D/qglview.h>
[bca99d]39#include <Qt3D/qglbuilder.h>
40#include <Qt3D/qglscenenode.h>
41#include <Qt3D/qglsphere.h>
42#include <Qt3D/qglcylinder.h>
[907636]43
44#include "GLMoleculeObject.hpp"
[7188b1]45#include "GLMoleculeObject_atom.hpp"
46#include "GLMoleculeObject_bond.hpp"
[c67518]47#include "GLMoleculeObject_molecule.hpp"
[f75491]48#include "GLMoleculeObject_shape.hpp"
[907636]49
50#include "CodePatterns/MemDebug.hpp"
51
[7188b1]52#include "CodePatterns/Log.hpp"
53
[0e9ffe]54#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
[89643d]55#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
[6966b7]56#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
57#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
[6f0841]58#include "Atom/atom.hpp"
[7188b1]59#include "Bond/bond.hpp"
[89643d]60#include "Descriptors/AtomIdDescriptor.hpp"
[37b2575]61#include "Helpers/helpers.hpp"
[907636]62#include "molecule.hpp"
63#include "World.hpp"
64
[2ad1ec]65#include <iostream>
66
[ce7fdc]67using namespace MoleCuilder;
[907636]68
[2ad1ec]69std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
70{
71 ost << t.first << "," << t.second;
72 return ost;
73}
74
[907636]75GLWorldScene::GLWorldScene(QObject *parent)
[407638e]76 : QObject(parent),
77 hoverAtom(NULL)
[907636]78{
[72a4c1]79 int sphereDetails[] = {5, 3, 2, 0};
80 int cylinderDetails[] = {16, 8, 6, 3};
81 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
82 QGLBuilder emptyBuilder;
83 meshEmpty[i] = emptyBuilder.finalizedSceneNode();
84 QGLBuilder sphereBuilder;
85 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
86 meshSphere[i] = sphereBuilder.finalizedSceneNode();
87 meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
88 QGLBuilder cylinderBuilder;
89 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
90 meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
91 meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
92 }
[0a6ff9]93
[6966b7]94 setSelectionMode(SelectAtom);
95
[907636]96 init();
97}
98
99GLWorldScene::~GLWorldScene()
[7188b1]100{
101 // remove all elements
102 GLMoleculeObject::cleanMaterialMap();
103}
[907636]104
105/** Initialise the WorldScene with molecules and atoms from World.
106 *
107 */
108void GLWorldScene::init()
109{
110 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
111
112 if (molecules.size() > 0) {
113 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
114 Runner != molecules.end();
115 Runner++) {
[c67518]116
[7188b1]117 for (molecule::const_iterator atomiter = (*Runner)->begin();
118 atomiter != (*Runner)->end();
119 ++atomiter) {
120 // create atom objects in scene
121 atomInserted(*atomiter);
122
[9c18e4]123 // create bond objects in scene
[2ad1ec]124 const BondList &bondlist = (*atomiter)->getListOfBonds();
125 for (BondList::const_iterator bonditer = bondlist.begin();
126 bonditer != bondlist.end();
127 ++bonditer) {
128 const bond *_bond = *bonditer;
129 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
130 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
131 bondInserted(_bond, side);
132 }
[7188b1]133 }
[907636]134 }
135 }
136}
137
[7188b1]138/** Adds an atom to the scene.
139 *
140 * @param _atom atom to add
141 */
142void GLWorldScene::atomInserted(const atom *_atom)
[907636]143{
[57a770]144 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
[72a4c1]145 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphere, this, _atom);
[37b2575]146 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
147 ASSERT(iter == AtomsinSceneMap.end(),
[7188b1]148 "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
[37b2575]149 AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
[7188b1]150 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
[5a2a06]151 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
[407638e]152 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
153 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
[5a2a06]154 connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
[2ad1ec]155 connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
[30cd0d]156 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
[2ad1ec]157 //bondsChanged(_atom);
[65487f]158 emit changeOccured();
[7188b1]159}
160
161/** Removes an atom from the scene.
162 *
163 * @param _atom atom to remove
164 */
165void GLWorldScene::atomRemoved(const atom *_atom)
166{
[57a770]167 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
[2ad1ec]168 // bonds are removed by signal coming from ~bond
[7188b1]169 // remove atoms
[37b2575]170 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
171 ASSERT(iter != AtomsinSceneMap.end(),
[7188b1]172 "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
173 GLMoleculeObject_atom *atomObject = iter->second;
174 atomObject->disconnect();
[37b2575]175 AtomsinSceneMap.erase(iter);
[7188b1]176 delete atomObject;
[65487f]177 emit changeOccured();
[907636]178}
179
[3927ef]180/** ....
[c67518]181 *
182 */
[3927ef]183void GLWorldScene::worldSelectionChanged()
[c67518]184{
[3927ef]185 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
186
187 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
188
189 if (molecules.size() > 0) {
190 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
191 Runner != molecules.end();
192 Runner++) {
193
194 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
195 bool isSelected = World::getInstance().isSelected(*Runner);
196
197 // molecule selected but not in scene?
198 if (isSelected && (iter == MoleculesinSceneMap.end())){
199 // -> create new mesh
[bca99d]200 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
[3927ef]201 MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
202 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
203 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
204 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
205 emit changed();
206 emit changeOccured();
207 }
208
209 // molecule not selected but in scene?
210 if (!isSelected && (iter != MoleculesinSceneMap.end())){
211 // -> remove from scene
212 moleculeRemoved(*Runner);
213 }
214
215 }
216 }
[c67518]217}
218
219/** Removes a molecule from the scene.
220 *
221 * @param _molecule molecule to remove
222 */
223void GLWorldScene::moleculeRemoved(const molecule *_molecule)
224{
[3927ef]225 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
[c67518]226 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
[3927ef]227
228 // only remove if the molecule is in the scene
229 // (= is selected)
230 if (iter != MoleculesinSceneMap.end()){
231 GLMoleculeObject_molecule *molObject = iter->second;
232 molObject->disconnect();
233 MoleculesinSceneMap.erase(iter);
234 delete molObject;
235 emit changed();
236 emit changeOccured();
237 }
[c67518]238}
239
[37b2575]240/** Adds a bond to the scene.
[7188b1]241 *
242 * @param _bond bond to add
[2ad1ec]243 * @param side which side of the bond (left or right)
[7188b1]244 */
[2ad1ec]245void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
[7188b1]246{
[57a770]247 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
[2ad1ec]248 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
249
250 BondIds ids;
251 switch (side) {
252 case GLMoleculeObject_bond::left:
253 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
254 break;
255 case GLMoleculeObject_bond::right:
256 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
257 break;
[7188b1]258 }
[2ad1ec]259#ifndef NDEBUG
260 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
261 ASSERT(iter == BondsinSceneMap.end(),
262 "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
263#endif
264 GLMoleculeObject_bond *bondObject =
[72a4c1]265 new GLMoleculeObject_bond(meshCylinder, this, _bond, side);
[2ad1ec]266 connect (
267 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
268 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
[5a2a06]269 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
[2ad1ec]270 BondsinSceneMap.insert( make_pair(ids, bondObject) );
271// BondIdsinSceneMap.insert( Leftids );
[65487f]272 emit changeOccured();
[7188b1]273}
[907636]274
[37b2575]275/** Removes a bond from the scene.
[7188b1]276 *
277 * @param _bond bond to remove
278 */
[37b2575]279void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
[907636]280{
[2ad1ec]281 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
[7188b1]282 {
283 // left bond
[37b2575]284 const BondIds Leftids( make_pair(leftnr, rightnr) );
285 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
286 ASSERT(leftiter != BondsinSceneMap.end(),
287 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
288 +toString(rightnr)+" not on display.");
[2ad1ec]289 //GLMoleculeObject_bond *bondObject = leftiter->second;
[37b2575]290 BondsinSceneMap.erase(leftiter);
[2ad1ec]291 //delete bondObject; // is done by signal from bond itself
292 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
[7188b1]293 }
[2ad1ec]294
[65487f]295 emit changeOccured();
[7188b1]296}
297
[f75491]298/** Adds a shape to the scene.
299 *
300 * @param shape shape to be added
301 */
302void GLWorldScene::addShape(const Shape &shape)
303{
304 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
[284551]305 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
306 ASSERT(iter == ShapesinSceneMap.end(),
307 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
308 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
309}
310
311void GLWorldScene::clearShapes()
312{
313 foreach (QObject *obj, children()) {
314 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
315 if (shapeobj)
316 delete(shapeobj);
317 }
318 ShapesinSceneMap.clear();
[f75491]319}
320
[7188b1]321void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
322{
323 // Initialize all of the mesh objects that we have as children.
324 foreach (QObject *obj, children()) {
325 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
326 if (meshobj)
327 meshobj->initialize(view, painter);
328 }
329}
330
[72a4c1]331void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
[7188b1]332{
333 // Draw all of the mesh objects that we have as children.
334 foreach (QObject *obj, children()) {
335 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
336 if (meshobj)
[72a4c1]337 meshobj->draw(painter, cameraPlane);
[7188b1]338 }
[907636]339}
[06ebf5]340
[7188b1]341void GLWorldScene::atomClicked(atomId_t no)
[907636]342{
[57a770]343 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
[89643d]344 const atom *Walker = World::getInstance().getAtom(AtomById(no));
[6966b7]345 if (selectionMode == SelectAtom){
346 if (!World::getInstance().isSelected(Walker))
[f7c7cf]347 SelectionAtomById(std::vector<atomId_t>(1,no));
[6966b7]348 else
[f7c7cf]349 SelectionNotAtomById(std::vector<atomId_t>(1,no));
[6966b7]350 }else if (selectionMode == SelectMolecule){
351 const molecule *mol = Walker->getMolecule();
352 ASSERT(mol, "Atom without molecule has been clicked.");
353 if (!World::getInstance().isSelected(mol))
354 SelectionMoleculeById(mol->getId());
355 else
356 SelectionNotMoleculeById(mol->getId());
357 }
[7188b1]358 emit clicked(no);
[907636]359}
[029bb4]360
[6966b7]361void GLWorldScene::setSelectionMode(SelectionModeType mode)
362{
363 selectionMode = mode;
364 // TODO send update to toolbar
365}
366
367void GLWorldScene::setSelectionModeAtom()
368{
369 setSelectionMode(SelectAtom);
370}
371
372void GLWorldScene::setSelectionModeMolecule()
373{
374 setSelectionMode(SelectMolecule);
375}
376
[407638e]377void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
378{
379 // Find the atom, ob corresponds to.
380 hoverAtom = NULL;
381 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
382 if (atomObject){
383 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
384 if (iter->second == atomObject)
385 hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
386 }
387 }
388
389 // Propagate signal.
390 emit hoverChanged(hoverAtom);
391}
392
[30cd0d]393void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
394{
395 LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
396 // Remove from map.
397 AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
398 ASSERT(iter != AtomsinSceneMap.end(),
399 "GLWorldScene::objectIdChangedSignalled() - atom with id "+toString(oldId)+" not on display.");
400 GLMoleculeObject_atom *atomObject = iter->second;
401 AtomsinSceneMap.erase(iter);
402
403 // Reinsert with new id.
404 AtomsinSceneMap.insert( make_pair(newId, atomObject) );
405}
406
Note: See TracBrowser for help on using the repository browser.