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

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 Candidate_v1.7.0 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 beadd0 was beadd0, checked in by Frederik Heber <heber@…>, 12 years ago

Changed atom ptr into atomicId_t param for atomInserted of GLWorld... classes.

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