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

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 eb0d77 was 72a4c1, checked in by Michael Ankele <ankele@…>, 13 years ago

GL: using multiple levels of detail for the meshes

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