source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp@ 5b61e5

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 5b61e5 was 5b61e5, checked in by Frederik Heber <heber@…>, 10 years ago

GLMoleculeObject_molecule also has ObservedValue for name.

  • this removes any (uncached) molecule::getName() from Qt code part.
  • Property mode set to 100644
File size: 32.2 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 * GLMoleculeObject_molecule.cpp
26 *
27 * Created on: Mar 30, 2012
28 * Author: ankele
29 */
30
31
32// include config.h
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
37#include "GLMoleculeObject_molecule.hpp"
38
39#include <Qt3D/qglscenenode.h>
40#include <Qt3D/qglbuilder.h>
41
42#include "CodePatterns/MemDebug.hpp"
43
44#include <boost/assign.hpp>
45
46#include "CodePatterns/Assert.hpp"
47#include "CodePatterns/Log.hpp"
48#include "CodePatterns/Observer/Notification.hpp"
49#include "CodePatterns/Observer/ObserverLog.hpp"
50
51#include "Atom/atom.hpp"
52#include "molecule.hpp"
53#include "Descriptors/AtomIdDescriptor.hpp"
54#include "Descriptors/MoleculeIdDescriptor.hpp"
55#include "Element/element.hpp"
56#include "LinearAlgebra/Vector.hpp"
57#include "LinkedCell/PointCloudAdaptor.hpp"
58#include "LinkedCell/linkedcell.hpp"
59#include "Tesselation/tesselation.hpp"
60#include "Tesselation/BoundaryLineSet.hpp"
61#include "Tesselation/BoundaryTriangleSet.hpp"
62#include "Tesselation/CandidateForTesselation.hpp"
63#include "Atom/TesselPoint.hpp"
64#include "World.hpp"
65
66using namespace boost::assign;
67
68#include "GLMoleculeObject_atom.hpp"
69
70static Observable::channels_t getAtomsChannels()
71{
72 Observable::channels_t channels;
73 channels += molecule::AtomInserted, molecule::AtomRemoved;
74 return channels;
75}
76
77static Observable::channels_t getAllAtomicChangesChannels()
78{
79 Observable::channels_t channels;
80 channels += molecule::AtomInserted, molecule::AtomRemoved, molecule::AtomMoved;
81 return channels;
82}
83
84// static instances
85const Observable::channels_t GLMoleculeObject_molecule::AtomsChannels(getAtomsChannels());
86const Observable::channels_t GLMoleculeObject_molecule::HullChannels(getAllAtomicChangesChannels());
87const Observable::channels_t GLMoleculeObject_molecule::BoundingBoxChannels(getAllAtomicChangesChannels());
88const Observable::channels_t GLMoleculeObject_molecule::IndexChannels(1, molecule::IndexChanged);
89const Observable::channels_t GLMoleculeObject_molecule::NameChannels(1, molecule::MoleculeNameChanged);
90
91static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo)
92{
93 // Build a mesh from the geometry.
94 QGLBuilder builder;
95 builder.addTriangles(_geo);
96 QGLSceneNode *mesh = builder.finalizedSceneNode();
97 return mesh;
98}
99
100GLMoleculeObject_molecule::GLMoleculeObject_molecule(QObject *parent, const moleculeId_t _molid) :
101 GLMoleculeObject((QGLSceneNode *)NULL, parent),
102 Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
103 owner(NULL),
104 molref(getMolecule(_molid)),
105 /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
106 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
107 * the class has not been fully constructed yet. "This" itself seems to be working fine.
108 */
109 MolIndexUpdater(
110 boost::bind(&GLMoleculeObject_molecule::updateIndex, this)
111 ),
112 MolNameUpdater(
113 boost::bind(&GLMoleculeObject_molecule::updateName, this)
114 ),
115 TesselationHullUpdater(
116 boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
117 ),
118 BoundingBoxUpdater(
119 boost::bind(&GLMoleculeObject_molecule::updateBoundingBox, this)
120 ),
121 PresentAtomsUpdater(
122 boost::bind(&GLMoleculeObject_molecule::updateAtoms, this)
123 ),
124 MolIndex(
125 molref,
126 MolIndexUpdater,
127 "MoleculeIndex_"+toString(_molid),
128 _molid,
129 IndexChannels),
130 MolName(
131 molref,
132 MolNameUpdater,
133 "MoleculeName_"+toString(_molid),
134 updateName(),
135 NameChannels),
136 TesselationHull(
137 molref,
138 TesselationHullUpdater,
139 "MoleculeTesselationHull_"+toString(_molid),
140 HullChannels),
141 BoundingBox(
142 molref,
143 BoundingBoxUpdater,
144 "MoleculeBoundingBox_"+toString(_molid),
145 updateBoundingBox(),
146 BoundingBoxChannels),
147 PresentAtoms(
148 molref,
149 PresentAtomsUpdater,
150 "MoleculeAtoms_"+toString(_molid),
151 updateAtoms(),
152 AtomsChannels),
153 hoverAtomId(-1)
154{
155 setObjectId(_molid);
156 setMaterial(getMaterial(1));
157
158 m_selected = const_cast<const World &>(World::getInstance()).isMoleculeSelected(_molid);
159
160 // initially, atoms and bonds should be visible
161 m_visible = false;
162
163 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
164 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
165 connect (this, SIGNAL(TesselationHullChanged()), this, SLOT(resetTesselationHull()), Qt::QueuedConnection);
166 connect (this, SIGNAL(BoundingBoxChanged()), this, SLOT(resetBoundingBox()), Qt::QueuedConnection);
167 connect (this, SIGNAL(IsSelectedChanged()), this, SLOT(resetIsSelected()), Qt::QueuedConnection);
168 connect (this, SIGNAL(IdChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
169 connect (this, SIGNAL(AtomInserted(const atomId_t)), this, SLOT(atomInserted(const atomId_t)), Qt::QueuedConnection);
170 connect (this, SIGNAL(AtomInserted(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
171 connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
172 connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)), Qt::QueuedConnection);
173
174 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
175}
176
177GLMoleculeObject_molecule::GLMoleculeObject_molecule(QGLSceneNode *mesh[], QObject *parent, const moleculeId_t _molid) :
178 GLMoleculeObject(mesh, parent),
179 Observer(std::string("GLMoleculeObject_molecule")+toString(_molid)),
180 owner(NULL),
181 molref(getMolecule(_molid)),
182 /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
183 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
184 * the class has not been fully constructed yet. "This" itself seems to be working fine.
185 */
186 MolIndexUpdater(
187 boost::bind(&GLMoleculeObject_molecule::updateIndex, this)
188 ),
189 MolNameUpdater(
190 boost::bind(&GLMoleculeObject_molecule::updateName, this)
191 ),
192 TesselationHullUpdater(
193 boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
194 ),
195 BoundingBoxUpdater(
196 boost::bind(&GLMoleculeObject_molecule::updateBoundingBox, this)
197 ),
198 PresentAtomsUpdater(
199 boost::bind(&GLMoleculeObject_molecule::updateAtoms, this)
200 ),
201 MolIndex(
202 molref,
203 MolIndexUpdater,
204 "MoleculeIndex_"+toString(_molid),
205 _molid,
206 IndexChannels),
207 MolName(
208 molref,
209 MolNameUpdater,
210 "MoleculeName_"+toString(_molid),
211 updateName(),
212 NameChannels),
213 TesselationHull(
214 molref,
215 TesselationHullUpdater,
216 "MoleculeTesselationHull_"+toString(_molid),
217 HullChannels),
218 BoundingBox(
219 molref,
220 BoundingBoxUpdater,
221 "MoleculeBoundingBox_"+toString(_molid),
222 updateBoundingBox(),
223 BoundingBoxChannels),
224 PresentAtoms(
225 molref,
226 PresentAtomsUpdater,
227 "MoleculeAtoms_"+toString(_molid),
228 atoms_t(),
229 AtomsChannels),
230 hoverAtomId(-1)
231{
232 setObjectId(_molid);
233 setMaterial(getMaterial(1));
234
235 m_selected = const_cast<const World &>(World::getInstance()).isMoleculeSelected(_molid);
236
237 // initially, atoms and bonds should be visible
238 m_visible = false;
239
240 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
241 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
242 connect (this, SIGNAL(TesselationHullChanged()), this, SLOT(resetTesselationHull()), Qt::QueuedConnection);
243 connect (this, SIGNAL(BoundingBoxChanged()), this, SLOT(resetBoundingBox()), Qt::QueuedConnection);
244 connect (this, SIGNAL(IdChanged()), this, SLOT(resetIndex()), Qt::QueuedConnection);
245 connect (this, SIGNAL(AtomInserted(const atomId_t)), this, SLOT(atomInserted(const atomId_t)), Qt::QueuedConnection);
246 connect (this, SIGNAL(AtomInserted(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
247 connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(resetAtoms()), Qt::QueuedConnection);
248 connect (this, SIGNAL(AtomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)), Qt::QueuedConnection);
249
250 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
251}
252
253GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
254{
255 deactivateObserver();
256}
257
258void GLMoleculeObject_molecule::deactivateObserver()
259{
260 if (owner != NULL) {
261 owner->signOff(this, molecule::AtomInserted);
262 owner->signOff(this, molecule::AtomRemoved);
263 owner->signOff(this, molecule::AtomMoved);
264 owner->signOff(this, molecule::IndexChanged);
265 owner = NULL;
266 }
267}
268
269void GLMoleculeObject_molecule::activateObserver()
270{
271 // sign on as observer (obtain non-const instance before)
272 const molecule * const _molecule = getMolecule(MolIndex.get());
273 if (_molecule != NULL) {
274 owner = static_cast<const Observable *>(_molecule);
275 owner->signOn(this, molecule::AtomInserted);
276 owner->signOn(this, molecule::AtomRemoved);
277 owner->signOn(this, molecule::AtomMoved);
278 owner->signOn(this, molecule::IndexChanged);
279 } else {
280 ELOG(1, "GLMoleculeObject_molecule() - added null object for not present mol id " << MolIndex.get());
281 }
282
283}
284
285void GLMoleculeObject_molecule::addAtomBonds(
286 const bond::ptr &_bond,
287 const GLMoleculeObject_bond::SideOfBond _side
288 )
289{
290 bool bond_present = false;
291 const BondIds ids = getBondIds(_bond, _side);
292 // check whether bond is not present already
293 bond_present = BondsinSceneMap.count(ids);
294 if (!bond_present)
295 bondInserted(ids.first, ids.second, _side);
296 else {
297 BondsinSceneMap[ids]->resetPosition();
298 BondsinSceneMap[ids]->resetWidth();
299 }
300}
301
302void GLMoleculeObject_molecule::addAtomBonds(
303 const atom *_atom)
304{
305 const bool atom_present = AtomsinSceneMap.count(_atom->getId());
306 const BondList &bondlist = _atom->getListOfBonds();
307 for (BondList::const_iterator bonditer = bondlist.begin();
308 (bonditer != bondlist.end()) && atom_present;
309 ++bonditer) {
310 const bond::ptr _bond = *bonditer;
311 // check if OtherAtom's sphere is already present
312 const atom *OtherAtom = _bond->GetOtherAtom(_atom);
313 const bool otheratom_present = AtomsinSceneMap.count(OtherAtom->getId());
314 if (otheratom_present && atom_present) {
315 const GLMoleculeObject_bond::SideOfBond side = (_bond->leftatom == _atom) ?
316 GLMoleculeObject_bond::left : GLMoleculeObject_bond::right;
317 const GLMoleculeObject_bond::SideOfBond otherside = (_bond->leftatom == _atom) ?
318 GLMoleculeObject_bond::right : GLMoleculeObject_bond::left;
319 addAtomBonds(_bond, side);
320 addAtomBonds(_bond, otherside);
321 }
322 }
323}
324
325QGeometryData GLMoleculeObject_molecule::updateTesselationHull() const
326{
327 QGeometryData geo;
328
329 const molecule * const molref = getMolecule(MolIndex.get());
330 if (molref == NULL) {
331 ELOG(1, "Could not createMoleculeMesh, molecule with id " << MolIndex.get() << " already gone.");
332 return geo;
333 }
334 double minradius = 2.; // TODO: set to maximum bond length value
335 LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius);
336 // check minimum bond radius in molecule
337 double minlength = std::numeric_limits<double>::max();
338 for (molecule::const_iterator iter = molref->begin();
339 iter != molref->end(); ++iter) {
340 const BondList &ListOfBonds = (*iter)->getListOfBonds();
341 for (BondList::const_iterator bonditer = ListOfBonds.begin();
342 bonditer != ListOfBonds.end(); ++bonditer) {
343 const double bond_distance = (*bonditer)->GetDistance();
344 minlength = std::min(bond_distance, minlength);
345 }
346 }
347 minradius = std::max( std::max(minradius, minlength), 1.);
348
349 // we need at least three points for tesselation
350 if (molref->getAtomCount() >= 3) {
351 // Tesselate the points.
352 Tesselation T;
353 PointCloudAdaptor<molecule> cloud(const_cast<molecule *>(molref), MolName.get());
354 T(cloud, minradius);
355
356 // Fill the points into a Qt geometry.
357 LinkedCell_deprecated LinkedList(cloud, minradius);
358 std::map<int, int> indices;
359 std::map<int, Vector> normals;
360 int index = 0;
361 for (PointMap::const_iterator piter = T.PointsOnBoundary.begin();
362 piter != T.PointsOnBoundary.end(); ++piter) {
363 const Vector &point = piter->second->getPosition();
364 // add data to the primitive
365 geo.appendVertex(QVector3D(point[0], point[1], point[2]));
366 Vector normalvector;
367 for (LineMap::const_iterator lineiter = piter->second->lines.begin();
368 lineiter != piter->second->lines.end(); ++lineiter)
369 for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin();
370 triangleiter != lineiter->second->triangles.end(); ++triangleiter)
371 normalvector +=
372 triangleiter->second->NormalVector;
373 normalvector.Normalize();
374 geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2]));
375 geo.appendColor(QColor(1, 1, 1, 1));
376 geo.appendTexCoord(QVector2D(0, 0));
377 indices.insert( std::make_pair( piter->second->getNr(), index++));
378 }
379
380 // Fill the tesselated triangles into the geometry.
381 for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin();
382 runner != T.TrianglesOnBoundary.end(); runner++) {
383 int v[3];
384 for (size_t i=0; i<3; ++i)
385 v[i] = runner->second->endpoints[i]->getNr();
386
387 // Sort the vertices so the triangle is clockwise (relative to the normal vector).
388 Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition();
389 cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition());
390 if (cross.ScalarProduct(runner->second->NormalVector) > 0)
391 geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]);
392 else
393 geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]);
394 }
395 }
396
397 return geo;
398}
399
400GLMoleculeObject_molecule::BoundingBoxInfo GLMoleculeObject_molecule::updateBoundingBox() const
401{
402 BoundingBoxInfo info;
403 const molecule * const _molecule = getMolecule(MolIndex.get());
404 if (_molecule != NULL) {
405 Shape shape = _molecule->getBoundingSphere();
406 info.position = shape.getCenter();
407 info.radius = shape.getRadius();
408 } else
409 ELOG(2, "GLMoleculeObject_molecule cannot updateBoundingBox, molecule with "
410 << MolIndex.get() << " has disappeared.");
411 return info;
412}
413
414GLMoleculeObject_molecule::atoms_t GLMoleculeObject_molecule::updateAtoms()
415{
416 const molecule * const mol = getMolecule(MolIndex.get());
417 if (mol != NULL) {
418 const atomId_t id = mol->lastChangedAtomId();
419 if (mol->containsAtom(id))
420 DisplayedAtoms.insert(id);
421 else
422 DisplayedAtoms.erase(id);
423 }
424 return DisplayedAtoms;
425}
426
427moleculeId_t GLMoleculeObject_molecule::updateIndex() const
428{
429 return const_cast<const World &>(World::getInstance()).lastChangedMolId();
430}
431
432std::string GLMoleculeObject_molecule::updateName() const
433{
434 const molecule * const mol = getMolecule(MolIndex.get());
435 return mol->getName();
436}
437
438void GLMoleculeObject_molecule::resetTesselationHull()
439{
440 if (!TesselationHull.isValid())
441 updateMesh(createMoleculeMesh(*TesselationHull));
442}
443
444void GLMoleculeObject_molecule::resetBoundingBox()
445{
446 BoundingBoxInfo info = BoundingBox.get();
447 setPosition(QVector3D(info.position[0], info.position[1], info.position[2]));
448 setScale(info.radius + 0.3); // getBoundingSphere() only sees atoms as points, so make the box a bit bigger
449}
450
451void GLMoleculeObject_molecule::resetAtoms()
452{
453 const atoms_t atoms = PresentAtoms.get();
454 std::vector<atomId_t> InsertedAtoms;
455 std::vector<atomId_t> RemovedAtoms;
456 // obtain all newly inserted and removed atoms
457 std::set_difference(
458 atoms.begin(), atoms.end(),
459 DisplayedAtoms.begin(), DisplayedAtoms.end(),
460 std::back_inserter(InsertedAtoms));
461 std::set_difference(
462 DisplayedAtoms.begin(), DisplayedAtoms.end(),
463 atoms.begin(), atoms.end(),
464 std::back_inserter(RemovedAtoms));
465 // remove the atoms
466 std::for_each(RemovedAtoms.begin(), RemovedAtoms.end(),
467 boost::bind(&GLMoleculeObject_molecule::atomRemoved, this, _1));
468 // insert the atoms
469 std::for_each(InsertedAtoms.begin(), InsertedAtoms.end(),
470 boost::bind(&GLMoleculeObject_molecule::atomInserted, this, _1));
471
472 emit changed();
473}
474
475void GLMoleculeObject_molecule::resetIndex()
476{
477 const atomId_t newId = MolIndex.get();
478 const size_t oldId = objectId();
479 ASSERT( newId != oldId,
480 "GLMoleculeObject_molecule::resetIndex() - index "+toString(newId)+" did not change.");
481 LOG(4, "INFO: GLMoleculeObject_molecule: new index is "+toString(newId)+".");
482 setObjectId(newId);
483
484 emit indexChanged(this, oldId, newId);
485}
486
487void GLMoleculeObject_molecule::resetName()
488{
489}
490
491void GLMoleculeObject_molecule::AtomSelected(const atomId_t _id)
492{
493 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
494 if (iter != AtomsinSceneMap.end())
495 QMetaObject::invokeMethod(iter->second, // pointer to a QObject
496 "Selected", // member name (no parameters here)
497 Qt::QueuedConnection); // connection type
498 else
499 ELOG(2, "GLMoleculeObject_molecule::AtomSelected() - atom "
500 << _id << " unknown to GLMoleculeObject_molecule.");
501}
502
503void GLMoleculeObject_molecule::AtomUnselected(const atomId_t _id)
504{
505 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
506 if (iter != AtomsinSceneMap.end())
507 QMetaObject::invokeMethod(iter->second, // pointer to a QObject
508 "Unselected", // member name (no parameters here)
509 Qt::QueuedConnection); // connection type
510 else ELOG(2, "GLMoleculeObject_molecule::AtomUnselected() - atom "
511 << _id << " unknown to GLMoleculeObject_molecule.");
512}
513
514void GLMoleculeObject_molecule::Selected()
515{
516 ASSERT( !m_selected,
517 "GLMoleculeObject_molecule::Selected() - 3D rep of molecule is already selected.");
518 m_selected = true;
519
520 emit changed();
521}
522
523void GLMoleculeObject_molecule::Unselected()
524{
525 ASSERT( m_selected,
526 "GLMoleculeObject_molecule::Unselected() - 3D rep of molecule is already unselected.");
527 m_selected = false;
528
529 emit changed();
530}
531
532void GLMoleculeObject_molecule::update(Observable *publisher)
533{
534#ifdef LOG_OBSERVER
535 const molecule *_mol = static_cast<molecule *>(publisher);
536 observerLog().addMessage() << "++ Update of Observer " << observerLog().getName(static_cast<Observer *>(this)) << " from molecule "+toString(_mol->getId())+".";
537#endif
538}
539
540void GLMoleculeObject_molecule::subjectKilled(Observable *publisher)
541{
542 deactivateObserver();
543}
544
545void GLMoleculeObject_molecule::recieveNotification(Observable *publisher, Notification_ptr notification)
546{
547 const molecule * const _molecule = getMolecule(MolIndex.get());
548 // when molecule is NULL we will soon get destroyed anyway
549 if (_molecule == NULL)
550 return;
551 if (publisher == dynamic_cast<const Observable*>(_molecule)){
552 // notofication from atom
553#ifdef LOG_OBSERVER
554 observerLog().addMessage() << "++ Update of Observer "<< observerLog().getName(static_cast<Observer *>(this))
555 << " received notification from molecule " << _molecule->getId() << " for channel "
556 << notification->getChannelNo() << ".";
557#endif
558 switch (notification->getChannelNo()) {
559 case molecule::AtomInserted:
560 {
561 const atomId_t _id = _molecule->lastChangedAtomId();
562 #ifdef LOG_OBSERVER
563 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
564 #endif
565 emit AtomInserted(_id);
566 emit TesselationHullChanged();
567 emit BoundingBoxChanged();
568 break;
569 }
570 case World::AtomRemoved:
571 {
572 const atomId_t _id = _molecule->lastChangedAtomId();
573 #ifdef LOG_OBSERVER
574 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been removed.";
575 #endif
576 emit AtomRemoved(_id);
577 emit TesselationHullChanged();
578 emit BoundingBoxChanged();
579 break;
580 }
581 case molecule::AtomMoved:
582 {
583 #ifdef LOG_OBSERVER
584 const atomId_t _id = _molecule->lastChangedAtomId();
585 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+" has been inserted.";
586 #endif
587 emit TesselationHullChanged();
588 emit BoundingBoxChanged();
589 break;
590 }
591 case molecule::IndexChanged:
592 {
593 #ifdef LOG_OBSERVER
594 const atomId_t _id = _molecule->lastChangedAtomId();
595 observerLog().addMessage() << "++ Observer " << observerLog().getName(static_cast<Observer *>(this)) << " received notification that atom "+toString(_id)+"'s index has changed.";
596 #endif
597 emit IdChanged();
598 break;
599 }
600 default:
601 break;
602 }
603 }
604}
605
606void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
607{
608 // Initialize all of the mesh objects that we have as children.
609 if (m_visible) {
610 GLMoleculeObject::initialize(view, painter);
611 } else {
612 foreach (QObject *obj, children()) {
613 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
614 if (meshobj)
615 meshobj->initialize(view, painter);
616 }
617 }
618}
619
620void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane)
621{
622 // draw either molecule's mesh or all atoms and bonds
623 if (m_visible) {
624 resetTesselationHull();
625
626 painter->modelViewMatrix().push();
627
628 // Apply the material and effect to the painter.
629 QGLMaterial *material;
630 if (m_hovering)
631 material = m_hoverMaterial;
632 else if (m_selected)
633 material = m_selectionMaterial;
634 else
635 material = m_material;
636
637 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
638
639 painter->setColor(material->diffuseColor());
640 painter->setFaceMaterial(QGL::AllFaces, material);
641 if (m_effect)
642 painter->setUserEffect(m_effect);
643 else
644 painter->setStandardEffect(QGL::LitMaterial);
645
646 // Mark the object for object picking purposes.
647 int prevObjectId = painter->objectPickId();
648 if (m_objectId != -1)
649 painter->setObjectPickId(m_objectId);
650
651 m_mesh[0]->draw(painter);
652
653 // Turn off the user effect, if present.
654 if (m_effect)
655 painter->setStandardEffect(QGL::LitMaterial);
656
657 // Revert to the previous object identifier.
658 painter->setObjectPickId(prevObjectId);
659
660 // Restore the modelview matrix.
661 painter->modelViewMatrix().pop();
662
663 // GLMoleculeObject::draw(painter, cameraPlane);
664 } else {
665 // Draw all of the mesh objects that we have as children.
666 foreach (QObject *obj, children()) {
667 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
668 if (meshobj)
669 meshobj->draw(painter, cameraPlane);
670 }
671
672 // update bounding box prior to selection
673 resetBoundingBox();
674
675 painter->modelViewMatrix().push();
676 painter->modelViewMatrix().translate(m_position);
677 if (m_rotationAngle != 0.0f)
678 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
679 if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
680 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
681
682 // Draw a box around the mesh, if selected.
683 if (m_selected)
684 drawSelectionBox(painter);
685
686 // Restore the modelview matrix.
687 painter->modelViewMatrix().pop();
688 }
689}
690
691/** Adds an atom of this molecule to the scene.
692 *
693 * @param _atom atom to add
694 */
695void GLMoleculeObject_molecule::atomInserted(const atomId_t _id)
696{
697 LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomInserted for atom "+toString(_id)+".");
698
699 GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(GLMoleculeObject::meshSphere, this, _id);
700 ASSERT( atomObject != NULL,
701 "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id));
702 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
703 ASSERT(iter == AtomsinSceneMap.end(),
704 "GLMoleculeObject_molecule::atomInserted - same atom with id "+toString(_id)+" added again.");
705 AtomsinSceneMap.insert( make_pair(_id, atomObject) );
706
707 qRegisterMetaType<atomId_t>("atomId_t");
708 qRegisterMetaType<bond::ptr>("bond::ptr");
709 qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
710 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t)));
711 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
712 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
713 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
714// connect (atomObject, SIGNAL(bondsChanged()), this, SLOT(bondInserted(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond)));
715 connect (atomObject, SIGNAL(BondsAdded(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond)), this, SLOT(bondInserted(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond)));
716 connect (atomObject, SIGNAL(BondsRemoved(const atomId_t, const atomId_t)), this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
717 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, const atomId_t, const atomId_t)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, const atomId_t, const atomId_t)));
718
719 if (m_objectId == -1)
720 setObjectId(_id);
721
722 // add all bonds
723 const atom * const Walker = const_cast<const World &>(World::getInstance()).
724 getAtom(AtomById(_id));
725 if (Walker != NULL)
726 addAtomBonds(Walker);
727 else
728 ELOG(1, "GLMoleculeObject_atom disappeared while about to add bonds.");
729
730 emit changeOccured();
731}
732
733/** Removes an atom of this molecule from the scene.
734 *
735 * We just the id as the atom might have already been destroyed.
736 *
737 * @param _id id of atom to remove
738 */
739void GLMoleculeObject_molecule::atomRemoved(const atomId_t _id)
740{
741 LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomRemoved for atom "+toString(_id)+".");
742 // bonds are removed by signal coming from ~bond
743
744 if ((unsigned int)m_objectId == _id)
745 setObjectId(-1);
746
747 // remove atoms
748 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
749 ASSERT(iter != AtomsinSceneMap.end(),
750 "GLMoleculeObject_molecule::atomRemoved() - atom "+toString(_id)+" not on display.");
751 GLMoleculeObject_atom *atomObject = iter->second;
752 AtomsinSceneMap.erase(iter);
753 atomObject->disconnect();
754 delete atomObject;
755
756 emit changeOccured();
757}
758
759void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob)
760{
761 // Find the atom, ob corresponds to.
762 hoverAtomId = -1;
763 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
764 if (atomObject){
765 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
766 if (iter->second == atomObject)
767 hoverAtomId = iter->first;
768 }
769
770 // Propagate signal.
771 emit hoverChanged(hoverAtomId);
772 } else {
773 // Find the atom, ob corresponds to.
774 GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob);
775 if (moleculeObject == this){
776 // Propagate signal.
777 emit hoverChanged(MolIndex.get(), 0);
778 }
779 }
780}
781
782
783/** Helper function to get bond ids in the correct order for BondNodeMap.
784 *
785 * \return pair of ids in correct order.
786 */
787GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds(
788 const bond::ptr _bond,
789 const enum GLMoleculeObject_bond::SideOfBond _side)
790{
791 BondIds ids;
792 switch (_side) {
793 case GLMoleculeObject_bond::left:
794 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
795 break;
796 case GLMoleculeObject_bond::right:
797 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
798 break;
799 }
800 return ids;
801}
802
803/** Adds a bond to the scene.
804 *
805 * @param _bond bond to add
806 * @param side which side of the bond (left or right)
807 */
808void GLMoleculeObject_molecule::bondInserted(
809 const atomId_t _left, const atomId_t _right,
810 const enum GLMoleculeObject_bond::SideOfBond _side)
811{
812 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(_left)
813 +toString(_right)+".");
814 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
815
816 const BondIds ids( std::make_pair(_left, _right) );
817 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
818 if (iter == BondsinSceneMap.end()) {
819 GLMoleculeObject_bond * bondObject =
820 new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, ids, _side);
821 connect (
822 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
823 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
824 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
825 BondsinSceneMap.insert( make_pair(ids, bondObject) );
826 // BondIdsinSceneMap.insert( Leftids );
827 } else {
828 iter->second->resetPosition();
829 iter->second->resetWidth();
830 }
831 emit changeOccured();
832}
833
834/** Removes a bond from the scene.
835 *
836 * @param _bond bond to remove
837 */
838void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
839{
840 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
841 {
842 // left bond
843 const BondIds Leftids( make_pair(leftnr, rightnr) );
844 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
845 ASSERT(leftiter != BondsinSceneMap.end(),
846 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
847 +toString(rightnr)+" not on display.");
848 GLMoleculeObject_bond *bondObject = leftiter->second;
849 bondObject->disconnect();
850 BondsinSceneMap.erase(leftiter);
851 delete bondObject; // is done by signal from bond itself
852 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
853 }
854
855 emit changeOccured();
856}
857
858void GLMoleculeObject_molecule::setVisible(bool value)
859{
860 // first update the mesh if we are going to be visible now
861 if (value)
862 updateTesselationHull();
863 // then emit onward
864 GLMoleculeObject::setVisible(value);
865}
866
867std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t)
868{
869 ost << t.first << "," << t.second;
870 return ost;
871}
872
873void GLMoleculeObject_molecule::wasClicked()
874{
875 LOG(4, "INFO: GLMoleculeObject_molecule: atom " << MolIndex.get() << " has been clicked");
876 emit moleculeClicked(MolIndex.get());
877}
878
879void GLMoleculeObject_molecule::changeAtomId(
880 GLMoleculeObject_atom *ob,
881 const atomId_t oldId,
882 const atomId_t newId)
883{
884 LOG(3, "INFO: GLMoleculeObject_molecule - change atom id " << oldId << " to " << newId << ".");
885
886 // Remove from map.
887 AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
888 ASSERT(iter != AtomsinSceneMap.end(),
889 "GLMoleculeObject_molecule::changeAtomId() - atom with old id "+toString(oldId)+" not on display.");
890 ASSERT(iter->second == ob,
891 "GLMoleculeObject_molecule::changeAtomId() - atom with id "
892 +toString(oldId)+" does not match with object in AtomsinSceneMap.");
893 AtomsinSceneMap.erase(iter);
894
895 // Reinsert with new id.
896 {
897 AtomNodeMap::iterator iter = AtomsinSceneMap.find(newId);
898 ASSERT(iter == AtomsinSceneMap.end(),
899 "GLMoleculeObject_molecule::changeAtomId() - atom with new id "+toString(newId)+" already known.");
900 }
901 AtomsinSceneMap.insert( make_pair(newId, ob) );
902}
903
904const molecule * const GLMoleculeObject_molecule::getMolecule(const moleculeId_t _id)
905{
906 const molecule * const mol = const_cast<const World &>(World::getInstance()).
907 getMolecule(MoleculeById(_id));
908 return mol;
909}
Note: See TracBrowser for help on using the repository browser.