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

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 87d6bd was ba6b5c, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Assert wrong way round in GLWorldScene::removeShape().

  • Property mode set to 100644
File size: 13.9 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 "Shapes/ShapeRegistry.hpp"
63#include "molecule.hpp"
64#include "World.hpp"
65
66#include <iostream>
67
68using namespace MoleCuilder;
69
70std::ostream &operator<<(std::ostream &ost, const GLWorldScene::BondIds &t)
71{
72 ost << t.first << "," << t.second;
73 return ost;
74}
75
76GLWorldScene::GLWorldScene(QObject *parent)
77 : QObject(parent),
78 hoverAtom(NULL)
79{
80 int sphereDetails[] = {5, 3, 2, 0};
81 int cylinderDetails[] = {16, 8, 6, 3};
82 for (int i=0;i<GLMoleculeObject::DETAILTYPES_MAX;i++){
83 QGLBuilder emptyBuilder;
84 meshEmpty[i] = emptyBuilder.finalizedSceneNode();
85 QGLBuilder sphereBuilder;
86 sphereBuilder << QGLSphere(2.0, sphereDetails[i]);
87 meshSphere[i] = sphereBuilder.finalizedSceneNode();
88 meshSphere[i]->setOption(QGLSceneNode::CullBoundingBox, true);
89 QGLBuilder cylinderBuilder;
90 cylinderBuilder << QGLCylinder(.25,.25,1.0,cylinderDetails[i]);
91 meshCylinder[i] = cylinderBuilder.finalizedSceneNode();
92 meshCylinder[i]->setOption(QGLSceneNode::CullBoundingBox, true);
93 }
94
95 setSelectionMode(SelectAtom);
96
97 init();
98}
99
100GLWorldScene::~GLWorldScene()
101{
102 // remove all elements
103 GLMoleculeObject::cleanMaterialMap();
104}
105
106/** Initialise the WorldScene with molecules and atoms from World.
107 *
108 */
109void GLWorldScene::init()
110{
111 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
112
113 if (molecules.size() > 0) {
114 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
115 Runner != molecules.end();
116 Runner++) {
117
118 for (molecule::const_iterator atomiter = (*Runner)->begin();
119 atomiter != (*Runner)->end();
120 ++atomiter) {
121 // create atom objects in scene
122 atomInserted(*atomiter);
123
124 // create bond objects in scene
125 const BondList &bondlist = (*atomiter)->getListOfBonds();
126 for (BondList::const_iterator bonditer = bondlist.begin();
127 bonditer != bondlist.end();
128 ++bonditer) {
129 const bond::ptr _bond = *bonditer;
130 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == *atomiter) ?
131 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
132 bondInserted(_bond, side);
133 }
134 }
135 }
136 }
137}
138
139/** Adds an atom to the scene.
140 *
141 * @param _atom atom to add
142 */
143void GLWorldScene::atomInserted(const atom *_atom)
144{
145 LOG(3, "INFO: GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
146 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(meshSphere, this, _atom);
147 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
148 ASSERT(iter == AtomsinSceneMap.end(),
149 "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
150 AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
151 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
152 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
153 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
154 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
155 connect (atomObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
156 connect (atomObject, SIGNAL(BondsInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const bond::ptr , const GLMoleculeObject_bond::SideOfBond)));
157 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, int, int)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, int, int)));
158 //bondsChanged(_atom);
159 emit changeOccured();
160}
161
162/** Removes an atom from the scene.
163 *
164 * @param _atom atom to remove
165 */
166void GLWorldScene::atomRemoved(const atom *_atom)
167{
168 LOG(3, "INFO: GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
169 // bonds are removed by signal coming from ~bond
170 // remove atoms
171 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
172 ASSERT(iter != AtomsinSceneMap.end(),
173 "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
174 GLMoleculeObject_atom *atomObject = iter->second;
175 atomObject->disconnect();
176 AtomsinSceneMap.erase(iter);
177 delete atomObject;
178 emit changeOccured();
179}
180
181/** ....
182 *
183 */
184void GLWorldScene::worldSelectionChanged()
185{
186 LOG(3, "INFO: GLWorldScene: Received signal selectionChanged.");
187
188 const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
189
190 if (molecules.size() > 0) {
191 for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
192 Runner != molecules.end();
193 Runner++) {
194
195 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find((*Runner)->getId());
196 bool isSelected = World::getInstance().isSelected(*Runner);
197
198 // molecule selected but not in scene?
199 if (isSelected && (iter == MoleculesinSceneMap.end())){
200 // -> create new mesh
201 GLMoleculeObject_molecule *molObject = new GLMoleculeObject_molecule(meshEmpty, this, *Runner);
202 MoleculesinSceneMap.insert( make_pair((*Runner)->getId(), molObject) );
203 connect (molObject, SIGNAL(changed()), this, SIGNAL(changed()));
204 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
205 connect (molObject, SIGNAL(selectionChanged()), this, SIGNAL(changed()));
206 emit changed();
207 emit changeOccured();
208 }
209
210 // molecule not selected but in scene?
211 if (!isSelected && (iter != MoleculesinSceneMap.end())){
212 // -> remove from scene
213 moleculeRemoved(*Runner);
214 }
215
216 }
217 }
218}
219
220/** Removes a molecule from the scene.
221 *
222 * @param _molecule molecule to remove
223 */
224void GLWorldScene::moleculeRemoved(const molecule *_molecule)
225{
226 LOG(3, "INFO: GLWorldScene: Received signal moleculeRemoved for molecule "+toString(_molecule->getId())+".");
227 MoleculeNodeMap::iterator iter = MoleculesinSceneMap.find(_molecule->getId());
228
229 // only remove if the molecule is in the scene
230 // (= is selected)
231 if (iter != MoleculesinSceneMap.end()){
232 GLMoleculeObject_molecule *molObject = iter->second;
233 molObject->disconnect();
234 MoleculesinSceneMap.erase(iter);
235 delete molObject;
236 emit changed();
237 emit changeOccured();
238 }
239}
240
241/** Adds a bond to the scene.
242 *
243 * @param _bond bond to add
244 * @param side which side of the bond (left or right)
245 */
246void GLWorldScene::bondInserted(const bond::ptr _bond, const enum GLMoleculeObject_bond::SideOfBond side)
247{
248 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
249 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
250
251 BondIds ids;
252 switch (side) {
253 case GLMoleculeObject_bond::left:
254 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
255 break;
256 case GLMoleculeObject_bond::right:
257 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
258 break;
259 }
260#ifndef NDEBUG
261 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
262 ASSERT(iter == BondsinSceneMap.end(),
263 "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
264#endif
265 GLMoleculeObject_bond * bondObject =
266 new GLMoleculeObject_bond(meshCylinder, this, _bond, side);
267 connect (
268 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
269 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
270 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
271 BondsinSceneMap.insert( make_pair(ids, bondObject) );
272// BondIdsinSceneMap.insert( Leftids );
273 emit changeOccured();
274}
275
276/** Removes a bond from the scene.
277 *
278 * @param _bond bond to remove
279 */
280void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
281{
282 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
283 {
284 // left bond
285 const BondIds Leftids( make_pair(leftnr, rightnr) );
286 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
287 ASSERT(leftiter != BondsinSceneMap.end(),
288 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
289 +toString(rightnr)+" not on display.");
290 //GLMoleculeObject_bond::ptr bondObject = leftiter->second;
291 BondsinSceneMap.erase(leftiter);
292 //delete bondObject; // is done by signal from bond itself
293 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
294 }
295
296 emit changeOccured();
297}
298
299/** Adds a shape to the scene.
300 *
301 * @param shape shape to be added
302 */
303void GLWorldScene::addShape(Shape &shape)
304{
305 GLMoleculeObject_shape *shapeObject = new GLMoleculeObject_shape(shape, this);
306 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
307 ASSERT(iter == ShapesinSceneMap.end(),
308 "GLWorldScene::addShape() - same shape "+shape.getName()+" added again.");
309 ShapesinSceneMap.insert( make_pair(shape.getName(), shapeObject) );
310}
311
312void GLWorldScene::removeShape(Shape &shape)
313{
314 ShapeNodeMap::iterator iter = ShapesinSceneMap.find(shape.getName());
315 ASSERT(iter != ShapesinSceneMap.end(),
316 "GLWorldScene::removeShape() - shape "+shape.getName()+" not in scene.");
317 ShapesinSceneMap.erase(iter);
318 delete(iter->second);
319}
320
321void GLWorldScene::updateSelectedShapes()
322{
323 foreach (QObject *obj, children()) {
324 GLMoleculeObject_shape *shapeobj = qobject_cast<GLMoleculeObject_shape *>(obj);
325 if (shapeobj){
326 shapeobj->enable(ShapeRegistry::getInstance().isSelected(shapeobj->getShape()));
327 }
328 }
329}
330
331void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
332{
333 // Initialize 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->initialize(view, painter);
338 }
339}
340
341void GLWorldScene::draw(QGLPainter *painter, const QVector4D &cameraPlane) const
342{
343 // Draw all of the mesh objects that we have as children.
344 foreach (QObject *obj, children()) {
345 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
346 if (meshobj)
347 meshobj->draw(painter, cameraPlane);
348 }
349}
350
351void GLWorldScene::atomClicked(atomId_t no)
352{
353 LOG(3, "INFO: GLWorldScene - atom " << no << " has been clicked.");
354 const atom *Walker = World::getInstance().getAtom(AtomById(no));
355 if (selectionMode == SelectAtom){
356 if (!World::getInstance().isSelected(Walker))
357 SelectionAtomById(std::vector<atomId_t>(1,no));
358 else
359 SelectionNotAtomById(std::vector<atomId_t>(1,no));
360 }else if (selectionMode == SelectMolecule){
361 const molecule *mol = Walker->getMolecule();
362 ASSERT(mol, "Atom without molecule has been clicked.");
363 if (!World::getInstance().isSelected(mol))
364 SelectionMoleculeById(mol->getId());
365 else
366 SelectionNotMoleculeById(mol->getId());
367 }
368 emit clicked(no);
369}
370
371void GLWorldScene::setSelectionMode(SelectionModeType mode)
372{
373 selectionMode = mode;
374 // TODO send update to toolbar
375}
376
377void GLWorldScene::setSelectionModeAtom()
378{
379 setSelectionMode(SelectAtom);
380}
381
382void GLWorldScene::setSelectionModeMolecule()
383{
384 setSelectionMode(SelectMolecule);
385}
386
387void GLWorldScene::hoverChangedSignalled(GLMoleculeObject *ob)
388{
389 // Find the atom, ob corresponds to.
390 hoverAtom = NULL;
391 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
392 if (atomObject){
393 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
394 if (iter->second == atomObject)
395 hoverAtom = World::getInstance().getAtom(AtomById(iter->first));
396 }
397 }
398
399 // Propagate signal.
400 emit hoverChanged(hoverAtom);
401}
402
403void GLWorldScene::changeAtomId(GLMoleculeObject_atom *ob, int oldId, int newId)
404{
405 LOG(3, "INFO: GLWorldScene - change atom id " << oldId << " to " << newId << ".");
406 // Remove from map.
407 AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
408 ASSERT(iter != AtomsinSceneMap.end(),
409 "GLWorldScene::objectIdChangedSignalled() - atom with id "+toString(oldId)+" not on display.");
410 GLMoleculeObject_atom *atomObject = iter->second;
411 AtomsinSceneMap.erase(iter);
412
413 // Reinsert with new id.
414 AtomsinSceneMap.insert( make_pair(newId, atomObject) );
415}
416
Note: See TracBrowser for help on using the repository browser.