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

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 34e7fdb was 739ee9, checked in by Frederik Heber <heber@…>, 10 years ago

GLMoleculeObject have m_visible, settable in QtMoleculeList.

  • is connected via QtMainWindow an GLWorldView, GLWorldScene.
  • i.e. molecule's atoms and bond display can now be switched on and off.
  • Property mode set to 100644
File size: 15.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
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/>.
22 */
23
24/*
25 * GLWorldScene.cpp
26 *
27 * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
28 *
29 * Created on: Aug 17, 2011
30 * Author: heber
31 */
32
33// include config.h
34#ifdef HAVE_CONFIG_H
35#include <config.h>
36#endif
37
38#include "GLWorldScene.hpp"
39#include <Qt3D/qglview.h>
40#include <Qt3D/qglbuilder.h>
41#include <Qt3D/qglscenenode.h>
42#include <Qt3D/qglsphere.h>
43#include <Qt3D/qglcylinder.h>
44
45#include "GLMoleculeObject.hpp"
46#include "GLMoleculeObject_atom.hpp"
47#include "GLMoleculeObject_bond.hpp"
48#include "GLMoleculeObject_molecule.hpp"
49#include "GLMoleculeObject_shape.hpp"
50
51#include "CodePatterns/MemDebug.hpp"
52
53#include "CodePatterns/Log.hpp"
54
55#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
56#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
57#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
58#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
59#include "Atom/atom.hpp"
60#include "Bond/bond.hpp"
61#include "Descriptors/AtomIdDescriptor.hpp"
62#include "Descriptors/MoleculeIdDescriptor.hpp"
63#include "Helpers/helpers.hpp"
64#include "Shapes/ShapeRegistry.hpp"
65#include "molecule.hpp"
66#include "World.hpp"
67
68#include <iostream>
69
70using namespace MoleCuilder;
71
72GLWorldScene::GLWorldScene(QObject *parent)
73 : QObject(parent)
74{
75 int sphereDetails[] = {5, 3, 2, 0};
76 int cylinderDetails[] = {16, 8, 6, 3};
77 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
78 QGLBuilder emptyBuilder;
79 GLMoleculeObject::meshEmpty[i] = emptyBuilder.finalizedSceneNode();
80 QGLBuilder sphereBuilder;
81 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
82 GLMoleculeObject::meshSphere[i] = sphereBuilder.finalizedSceneNode();
83 GLMoleculeObject::meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
84 QGLBuilder cylinderBuilder;
85 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
86 GLMoleculeObject::meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
87 GLMoleculeObject::meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
88 }
89
90 connect(this, SIGNAL(updated()), this, SLOT(update()));
91
92
93 setSelectionMode(SelectAtom);
94
95 init();
96}
97
98GLWorldScene::~GLWorldScene()
99{
100 // remove all elements
101 GLMoleculeObject::cleanMaterialMap();
102}
103
104/** Initialise the WorldScene with molecules and atoms from World.
105 *
106 */
107void GLWorldScene::init()
108{
109 const std::vector<molecule *> &molecules = World::getInstance().getAllMolecules();
110
111 for (std::vector<molecule*>::const_iterator moliter = molecules.begin();
112 moliter != molecules.end();
113 moliter++) {
114 // create molecule objects in scene
115 moleculeInserted(*moliter);
116 }
117}
118
119/** Update the WorldScene with molecules and atoms from World.
120 *
121 * This function should be called after e.g. WorldTime::TimeChanged was
122 * received or after another molecule has been loaded.
123 *
124 */
125void GLWorldScene::update()
126{
127 const std::vector<molecule *> &molecules = World::getInstance().getAllMolecules();
128
129 for (std::vector<molecule*>::const_iterator moliter = molecules.begin();
130 moliter != molecules.end();
131 moliter++) {
132 // check whether molecule already exists
133 const moleculeId_t molid = (*moliter)->getId();
134 const bool mol_present = MoleculesinSceneMap.count(molid);
135 if (!mol_present)
136 moleculeInserted(*moliter);
137 }
138}
139
140void GLWorldScene::atomClicked(atomId_t no)
141{
142 LOG(3, "INFO: GLMoleculeObject_molecule - atom " << no << " has been clicked.");
143 const atom *Walker = World::getInstance().getAtom(AtomById(no));
144 if (selectionMode == SelectAtom){
145 if (!World::getInstance().isSelected(Walker))
146 SelectionAtomById(std::vector<atomId_t>(1,no));
147 else
148 SelectionNotAtomById(std::vector<atomId_t>(1,no));
149 }else if (selectionMode == SelectMolecule){
150 const molecule *mol = Walker->getMolecule();
151 ASSERT(mol, "Atom without molecule has been clicked.");
152 if (!World::getInstance().isSelected(mol))
153 SelectionMoleculeById(mol->getId());
154 else
155 SelectionNotMoleculeById(mol->getId());
156 }
157 emit clicked(no);
158}
159
160
161/** Adds an atom to the scene.
162 *
163 * @param _atom atom to add
164 */
165void GLWorldScene::atomInserted(const atomId_t _id)
166{
167 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_id)+".");
168 // find associated molecule
169 const moleculeId_t molid = World::getInstance().getAtom(AtomById(_id))->getMolecule()->getId();
170 MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid );
171 ASSERT(moliter != MoleculesinSceneMap.end(),
172 "GLWorldScene::atomAdded() - molecule with id of "+toString(molid)
173 +" atom with id "+toString(_id)+" is unknown.");
174 GLMoleculeObject_molecule *molObject = moliter->second;
175
176 // add atom to internal list
177#ifndef NDEBUG
178 AtomMoleculeMap::const_iterator atomiter = AtomsinSceneMap.find(_id);
179 ASSERT(atomiter == AtomsinSceneMap.end(),
180 "GLWorldScene::atomRemoved() - atom "+toString(_id)+" already known.");
181#endif
182 AtomsinSceneMap.insert( std::make_pair( _id, molObject ));
183
184 // inform its GlMoleculeObject_molecule.
185 molObject->atomInserted(_id);
186
187 // emit change
188 emit changeOccured();
189}
190
191/** Removes an atom from the scene.
192 *
193 * We just the id as the atom might have already been destroyed.
194 *
195 * @param _id id of atom to remove
196 */
197void GLWorldScene::atomRemoved(const atomId_t _id)
198{
199 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_id)+".");
200 // find associated molecule
201 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(_id);
202 ASSERT(iter != AtomsinSceneMap.end(),
203 "GLWorldScene::atomRemoved() - atom "+toString(_id)+" not on display.");
204 GLMoleculeObject_molecule *molObject = iter->second;
205
206 // remove from internal list
207 AtomsinSceneMap.erase(iter);
208
209 // inform its GlMoleculeObject_molecule.
210 molObject->atomRemoved(_id);
211
212 // emit change
213 emit changeOccured();
214}
215
216/** ....
217 *
218 */
219void GLWorldScene::worldSelectionChanged()
220{
221 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
222
223 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
224
225 if (molecules.size() > 0) {
226 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
227 Runner != molecules.end();
228 Runner++) {
229
230 // molecule selected but not in scene?
231 const bool isSelected = World::getInstance().isSelected(*Runner);
232 if (isSelected){
233 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
234 ASSERT( iter != MoleculesinSceneMap.end(),
235 "GLWorldScene::worldSelectionChanged() - selected molecule is unknown.");
236 GLMoleculeObject_molecule *molObject = iter->second;
237 // inform molecule object
238 molObject->selected(isSelected);
239 }
240 }
241 }
242}
243
244/** Inserts a molecule into the scene.
245 *
246 * @param _mol molecule to insert
247 */
248void GLWorldScene::moleculeInserted(const molecule * _mol)
249{
250 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_mol->getId())+".");
251 MoleculeNodeMap::const_iterator iter = MoleculesinSceneMap.find(_mol->getId());
252 ASSERT( iter == MoleculesinSceneMap.end(),
253 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_mol->getId())+" already present.");
254
255 // add new object
256 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(GLMoleculeObject::meshEmpty, this, _mol);
257 MoleculesinSceneMap.insert( make_pair(_mol->getId(), molObject) );
258 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
259 connect (molObject, SIGNAL(changeOccured()), this, SIGNAL(changeOccured()));
260 connect (molObject, SIGNAL(atomClicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
261 connect (molObject, SIGNAL(changeAtomId(GLMoleculeObject_atom *, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom *, int, int)));
262 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
263 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
264 connect (molObject, SIGNAL(hoverChanged(const atom *)), this, SIGNAL(hoverChanged(const atom *)));
265 emit changed();
266 emit changeOccured();
267}
268
269/** Removes a molecule from the scene.
270 *
271 * @param _id id of molecule to remove
272 */
273void GLWorldScene::moleculeRemoved(const moleculeId_t _id)
274{
275 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_id)+".");
276 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
277 ASSERT( iter != MoleculesinSceneMap.end(),
278 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
279
280 GLMoleculeObject_molecule *molObject = iter->second;
281 molObject->disconnect();
282 MoleculesinSceneMap.erase(iter);
283 delete molObject;
284 emit changed();
285 emit changeOccured();
286}
287
288void GLWorldScene::moleculesVisibilityChanged(const moleculeId_t _id, bool _visible)
289{
290 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_id);
291 ASSERT( iter != MoleculesinSceneMap.end(),
292 "GLWorldScene::moleculeInserted() - molecule's id "+toString(_id)+" is unknown.");
293
294 GLMoleculeObject_molecule *molObject = iter->second;
295 molObject->setVisible(_visible);
296
297 emit changed();
298 emit changeOccured();
299}
300
301/** Adds a bond to the scene.
302 *
303 * @param _bond bond to add
304 * @param side which side of the bond (left or right)
305 */
306void GLWorldScene::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond _side)
307{
308 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
309 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
310 // bond partners must both belong to same atom
311 ASSERT( _bond->leftatom->getMolecule() == _bond->rightatom->getMolecule(),
312 "GLWorldScene::bondInserted() - bond partners do not belong to same molecule.");
313
314 // find associated molecule object
315 const moleculeId_t molid = _bond->leftatom->getMolecule()->getId();
316 MoleculeNodeMap::const_iterator moliter = MoleculesinSceneMap.find(molid );
317 ASSERT(moliter != MoleculesinSceneMap.end(),
318 "GLWorldScene::bondInserted() - molecule with id of "+toString(molid)
319 +" atom with id "+toString(_bond->leftatom->getId())+" is unknown.");
320 GLMoleculeObject_molecule *molObject = moliter->second;
321
322 // add to internal list
323 const GLMoleculeObject_molecule::BondIds ids =
324 GLMoleculeObject_molecule::getBondIds(_bond, _side);
325 BondMoleculeMap::const_iterator iter = BondsinSceneMap.find(ids);
326 ASSERT(iter == BondsinSceneMap.end(),
327 "GLWorldScene::bondInserted() - bond with ids "+toString(ids.first)
328 +","+toString(ids.second)+" already present.");
329 BondsinSceneMap.insert( std::make_pair( ids, molObject ));
330
331 // inform its GlMoleculeObject_molecule.
332 molObject->bondInserted(_bond, _side);
333
334 // emit change
335 emit changeOccured();
336}
337
338/** Removes a bond from the scene.
339 *
340 * @param _bond bond to remove
341 */
342void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
343{
344 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
345 {
346 // left bond
347 const GLMoleculeObject_molecule::BondIds Leftids( make_pair(leftnr, rightnr) );
348 BondMoleculeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
349 ASSERT(leftiter != BondsinSceneMap.end(),
350 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
351 +toString(rightnr)+" not on display.");
352 GLMoleculeObject_molecule *molObject = leftiter->second;
353
354 // remove from internal list
355 BondsinSceneMap.erase(leftiter);
356
357 // inform its GlMoleculeObject_molecule.
358 molObject->bondRemoved( leftnr, rightnr );
359 }
360
361 // emit change
362 emit changeOccured();
363}
364
365/** Adds a shape to the scene.
366 *
367 * uses ShapeRegistry::lastChanged()
368 *
369 */
370void GLWorldScene::addShape()
371{
372 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
373 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
374 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
375 ASSERT(iter == ShapesinSceneMap.end(),
376 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
377 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
378}
379
380void GLWorldScene::removeShape()
381{
382 Shape &shape = *ShapeRegistry::getInstance().lastChanged();
383 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
384 ASSERT(iter != ShapesinSceneMap.end(),
385 "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
386 ShapesinSceneMap.erase(iter);
387 delete(iter->second);
388}
389
390void GLWorldScene::updateSelectedShapes()
391{
392 foreach (QObject *obj, children()) {
393 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
394 if (shapeobj){
395 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
396 }
397 }
398}
399
400void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
401{
402 // Initialize all of the mesh objects that we have as children.
403 foreach (QObject *obj, children()) {
404 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
405 if (meshobj)
406 meshobj->initialize(view, painter);
407 }
408}
409
410void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
411{
412 // Draw all of the mesh objects that we have as children.
413 foreach (QObject *obj, children()) {
414 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
415 if (meshobj)
416 meshobj->draw(painter, cameraPlane);
417 }
418}
419
420void GLWorldScene::setSelectionMode(SelectionModeType mode)
421{
422 selectionMode = mode;
423 // TODO send update to toolbar
424}
425
426void GLWorldScene::setSelectionModeAtom()
427{
428 setSelectionMode(SelectAtom);
429}
430
431void GLWorldScene::setSelectionModeMolecule()
432{
433 setSelectionMode(SelectMolecule);
434}
435
436void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
437{
438 LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
439 // Remove from map.
440 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(oldId);
441 ASSERT(iter != AtomsinSceneMap.end(),
442 "GLWorldScene::changeAtomId() - atom with old id "+toString(oldId)+" not on display.");
443 GLMoleculeObject_molecule *molObject = iter->second;
444
445 // erase by signalling removal
446 molObject->atomRemoved(oldId);
447
448 // remove from internal list
449 AtomsinSceneMap.erase(iter);
450
451 // Reinsert with new id.
452 {
453 AtomMoleculeMap::iterator iter = AtomsinSceneMap.find(newId);
454 ASSERT(iter == AtomsinSceneMap.end(),
455 "GLWorldScene::changeAtomId() - atom with new id "+toString(newId)+" already known.");
456 }
457 AtomsinSceneMap.insert( make_pair(newId, molObject) );
458
459 // inform molecule object
460 molObject->atomInserted(oldId);
461}
462
Note: See TracBrowser for help on using the repository browser.