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

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 25e08a was 284551, checked in by Frederik Heber <heber@…>, 12 years ago

GLWorldView shows the selected shapes from ShapeRegistry

  • Property mode set to 100644
File size: 13.5 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 *
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/>.
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"
38#include <Qt3D/qglview.h>
39#include <Qt3D/qglbuilder.h>
40#include <Qt3D/qglscenenode.h>
41#include <Qt3D/qglsphere.h>
42#include <Qt3D/qglcylinder.h>
43
44#include "GLMoleculeObject.hpp"
45#include "GLMoleculeObject_atom.hpp"
46#include "GLMoleculeObject_bond.hpp"
47#include "GLMoleculeObject_molecule.hpp"
48#include "GLMoleculeObject_shape.hpp"
49
50#include "CodePatterns/MemDebug.hpp"
51
52#include "CodePatterns/Log.hpp"
53
54#include "Actions/SelectionAction/Atoms/AtomByIdAction.hpp"
55#include "Actions/SelectionAction/Atoms/NotAtomByIdAction.hpp"
56#include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
57#include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
58#include "Atom/atom.hpp"
59#include "Bond/bond.hpp"
60#include "Descriptors/AtomIdDescriptor.hpp"
61#include "Helpers/helpers.hpp"
62#include "molecule.hpp"
63#include "World.hpp"
64
65#include <iostream>
66
67using namespace MoleCuilder;
68
69std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
70{
71 ost << t.first << "," << t.second;
72 return ost;
73}
74
75GLWorldScene::GLWorldScene(QObject *parent)
76 : QObject(parent),
77 hoverAtom(NULL)
78{
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 }
93
94 setSelectionMode(SelectAtom);
95
96 init();
97}
98
99GLWorldScene::~GLWorldScene()
100{
101 // remove all elements
102 GLMoleculeObject::cleanMaterialMap();
103}
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++) {
116
117 for (molecule::const_iterator atomiter = (*Runner)->begin();
118 atomiter != (*Runner)->end();
119 ++atomiter) {
120 // create atom objects in scene
121 atomInserted(*atomiter);
122
123 // create bond objects in scene
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 }
133 }
134 }
135 }
136}
137
138/** Adds an atom to the scene.
139 *
140 * @param _atom atom to add
141 */
142void GLWorldScene::atomInserted(const atom *_atom)
143{
144 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
145 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphere, this, _atom);
146 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
147 ASSERT(iter == AtomsinSceneMap.end(),
148 "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
149 AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
150 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
151 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
152 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
153 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
154 connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
155 connect (atomObject, SIGNAL(BondsInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond *, const GLMoleculeObject_bond::SideOfBond)));
156 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
157 //bondsChanged(_atom);
158 emit changeOccured();
159}
160
161/** Removes an atom from the scene.
162 *
163 * @param _atom atom to remove
164 */
165void GLWorldScene::atomRemoved(const atom *_atom)
166{
167 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
168 // bonds are removed by signal coming from ~bond
169 // remove atoms
170 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
171 ASSERT(iter != AtomsinSceneMap.end(),
172 "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
173 GLMoleculeObject_atom *atomObject = iter->second;
174 atomObject->disconnect();
175 AtomsinSceneMap.erase(iter);
176 delete atomObject;
177 emit changeOccured();
178}
179
180/** ....
181 *
182 */
183void GLWorldScene::worldSelectionChanged()
184{
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
200 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
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 }
217}
218
219/** Removes a molecule from the scene.
220 *
221 * @param _molecule molecule to remove
222 */
223void GLWorldScene::moleculeRemoved(const molecule *_molecule)
224{
225 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
226 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
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 }
238}
239
240/** Adds a bond to the scene.
241 *
242 * @param _bond bond to add
243 * @param side which side of the bond (left or right)
244 */
245void GLWorldScene::bondInserted(const bond *_bond, const enum GLMoleculeObject_bond::SideOfBond side)
246{
247 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
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;
258 }
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 =
265 new GLMoleculeObject_bond(meshCylinder, this, _bond, side);
266 connect (
267 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
268 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
269 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
270 BondsinSceneMap.insert( make_pair(ids, bondObject) );
271// BondIdsinSceneMap.insert( Leftids );
272 emit changeOccured();
273}
274
275/** Removes a bond from the scene.
276 *
277 * @param _bond bond to remove
278 */
279void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
280{
281 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
282 {
283 // left bond
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.");
289 //GLMoleculeObject_bond *bondObject = leftiter->second;
290 BondsinSceneMap.erase(leftiter);
291 //delete bondObject; // is done by signal from bond itself
292 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
293 }
294
295 emit changeOccured();
296}
297
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);
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();
319}
320
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
331void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
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)
337 meshobj->draw(painter, cameraPlane);
338 }
339}
340
341void GLWorldScene::atomClicked(atomId_t no)
342{
343 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
344 const atom *Walker = World::getInstance().getAtom(AtomById(no));
345 if (selectionMode == SelectAtom){
346 if (!World::getInstance().isSelected(Walker))
347 SelectionAtomById(std::vector<atomId_t>(1,no));
348 else
349 SelectionNotAtomById(std::vector<atomId_t>(1,no));
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 }
358 emit clicked(no);
359}
360
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
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
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.