source: src/UIElements/Views/Qt4/Qt3D/GLMoleculeObject_molecule.cpp@ 4e6ffe

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 4e6ffe was 494478, checked in by Frederik Heber <heber@…>, 9 years ago

Extracted all ObservedValue functionality out of GLMoleculeObject_molecule.

  • see previous commit for explanations.
  • Property mode set to 100644
File size: 22.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 "UIElements/Views/Qt4/Qt3D/GLMoleculeObject_atom.hpp"
43
44#include "CodePatterns/MemDebug.hpp"
45
46#include <boost/assign.hpp>
47
48#include "CodePatterns/Assert.hpp"
49#include "CodePatterns/IteratorAdaptors.hpp"
50#include "CodePatterns/Log.hpp"
51
52#include "LinearAlgebra/Vector.hpp"
53#include "LinkedCell/PointCloudAdaptor.hpp"
54#include "LinkedCell/linkedcell.hpp"
55#include "Tesselation/tesselation.hpp"
56#include "Tesselation/BoundaryLineSet.hpp"
57#include "Tesselation/BoundaryTriangleSet.hpp"
58#include "Tesselation/CandidateForTesselation.hpp"
59#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
60#include "Atom/TesselPoint.hpp"
61#include "World.hpp"
62
63using namespace boost::assign;
64
65static Observable::channels_t getAllAtomicChangesChannels()
66{
67 Observable::channels_t channels;
68 channels += molecule::AtomInserted, molecule::AtomRemoved, molecule::AtomMoved;
69 return channels;
70}
71
72const Observable::channels_t GLMoleculeObject_molecule::HullChannels(getAllAtomicChangesChannels());
73
74static QGLSceneNode *createMoleculeMesh(const QGeometryData &_geo)
75{
76 // Build a mesh from the geometry.
77 QGLBuilder builder;
78 builder.addTriangles(_geo);
79 QGLSceneNode *mesh = builder.finalizedSceneNode();
80 return mesh;
81}
82
83GLMoleculeObject_molecule::GLMoleculeObject_molecule(
84 QObject *parent,
85 QtObservedInstanceBoard &_board,
86 QtObservedMolecule::ptr _ObservedMolecule) :
87 GLMoleculeObject((QGLSceneNode *)NULL, parent),
88 owner(NULL),
89 molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())),
90 /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
91 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
92 * the class has not been fully constructed yet. "This" itself seems to be working fine.
93 */
94 TesselationHullUpdater(
95 boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
96 ),
97 TesselationHull(
98 molref,
99 TesselationHullUpdater,
100 "MoleculeTesselationHull_"+toString(_ObservedMolecule->getMolIndex()),
101 HullChannels),
102 hoverAtomId(-1),
103 board(_board),
104 ObservedMolecule(_ObservedMolecule)
105{
106 init(ObservedMolecule->getMolIndex());
107}
108
109GLMoleculeObject_molecule::GLMoleculeObject_molecule(
110 QGLSceneNode *mesh[],
111 QObject *parent,
112 QtObservedInstanceBoard &_board,
113 QtObservedMolecule::ptr _ObservedMolecule) :
114 GLMoleculeObject(mesh, parent),
115 owner(NULL),
116 molref(QtObservedMolecule::getMolecule(_ObservedMolecule->getMolIndex())),
117 /* We must not use boost::cref(this) as "this" has not been properly constructed and seemingly
118 * boost::cref tries to do some magic to grasp the inheritance hierarchy which fails because
119 * the class has not been fully constructed yet. "This" itself seems to be working fine.
120 */
121 TesselationHullUpdater(
122 boost::bind(&GLMoleculeObject_molecule::updateTesselationHull, this)
123 ),
124 TesselationHull(
125 molref,
126 TesselationHullUpdater,
127 "MoleculeTesselationHull_"+toString(_ObservedMolecule->getMolIndex()),
128 HullChannels),
129 hoverAtomId(-1),
130 board(_board),
131 ObservedMolecule(_ObservedMolecule)
132{
133 init(ObservedMolecule->getMolIndex());
134}
135
136void GLMoleculeObject_molecule::init(const moleculeId_t _molid)
137{
138 setObjectId(_molid);
139 setMaterial(getMaterial(1));
140
141 m_selected = const_cast<const World &>(World::getInstance()).isMoleculeSelected(_molid);
142
143 // initially, atoms and bonds should be visible
144 m_visible = false;
145
146 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
147 connect (this, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
148 connect (ObservedMolecule.get(), SIGNAL(tesselationhullChanged()), this, SLOT(resetTesselationHull()));
149 connect (ObservedMolecule.get(), SIGNAL(boundingboxChanged()), this, SLOT(resetBoundingBox()));
150 connect (ObservedMolecule.get(), SIGNAL(indexChanged()), this, SLOT(resetIndex()));
151 connect (ObservedMolecule.get(), SIGNAL(atomInserted(const atomId_t)), this, SLOT(atomInserted(const atomId_t)));
152 connect (ObservedMolecule.get(), SIGNAL(atomRemoved(const atomId_t)), this, SLOT(atomRemoved(const atomId_t)));
153
154 connect( this, SIGNAL(clicked()), this, SLOT(wasClicked()));
155}
156
157GLMoleculeObject_molecule::~GLMoleculeObject_molecule()
158{}
159
160void GLMoleculeObject_molecule::addAtomBonds(
161 const bond::ptr &_bond,
162 const GLMoleculeObject_bond::SideOfBond _side
163 )
164{
165 bool bond_present = false;
166 const BondIds ids = getBondIds(_bond, _side);
167 // check whether bond is not present already
168 bond_present = BondsinSceneMap.count(ids);
169 if (!bond_present)
170 bondInserted(ids.first, ids.second, _side);
171 else {
172 BondsinSceneMap[ids]->resetPosition();
173 BondsinSceneMap[ids]->resetWidth();
174 }
175}
176
177QGeometryData GLMoleculeObject_molecule::updateTesselationHull() const
178{
179 QGeometryData geo;
180
181 const molecule * const molref =
182 QtObservedMolecule::getMolecule(ObservedMolecule->getMolIndex());
183 if (molref == NULL) {
184 ELOG(1, "Could not createMoleculeMesh, molecule with id "
185 << ObservedMolecule->getMolIndex() << " already gone.");
186 return geo;
187 }
188 double minradius = 2.; // TODO: set to maximum bond length value
189 LOG(3, "DEBUG: Molecule fits into sphere of radius " << minradius);
190 // check minimum bond radius in molecule
191 double minlength = std::numeric_limits<double>::max();
192 for (molecule::const_iterator iter = molref->begin();
193 iter != molref->end(); ++iter) {
194 const BondList &ListOfBonds = (*iter)->getListOfBonds();
195 for (BondList::const_iterator bonditer = ListOfBonds.begin();
196 bonditer != ListOfBonds.end(); ++bonditer) {
197 const double bond_distance = (*bonditer)->GetDistance();
198 minlength = std::min(bond_distance, minlength);
199 }
200 }
201 minradius = std::max( std::max(minradius, minlength), 1.);
202
203 // we need at least three points for tesselation
204 if (getPresentAtoms().size() >= 3) {
205 // Tesselate the points.
206 Tesselation T;
207 PointCloudAdaptor<molecule> cloud(
208 const_cast<molecule *>(molref),
209 ObservedMolecule->getMolName());
210 T(cloud, minradius);
211
212 // Fill the points into a Qt geometry.
213 LinkedCell_deprecated LinkedList(cloud, minradius);
214 std::map<int, int> indices;
215 std::map<int, Vector> normals;
216 int index = 0;
217 for (PointMap::const_iterator piter = T.PointsOnBoundary.begin();
218 piter != T.PointsOnBoundary.end(); ++piter) {
219 const Vector &point = piter->second->getPosition();
220 // add data to the primitive
221 geo.appendVertex(QVector3D(point[0], point[1], point[2]));
222 Vector normalvector;
223 for (LineMap::const_iterator lineiter = piter->second->lines.begin();
224 lineiter != piter->second->lines.end(); ++lineiter)
225 for (TriangleMap::const_iterator triangleiter = lineiter->second->triangles.begin();
226 triangleiter != lineiter->second->triangles.end(); ++triangleiter)
227 normalvector +=
228 triangleiter->second->NormalVector;
229 normalvector.Normalize();
230 geo.appendNormal(QVector3D(normalvector[0], normalvector[1], normalvector[2]));
231 geo.appendColor(QColor(1, 1, 1, 1));
232 geo.appendTexCoord(QVector2D(0, 0));
233 indices.insert( std::make_pair( piter->second->getNr(), index++));
234 }
235
236 // Fill the tesselated triangles into the geometry.
237 for (TriangleMap::const_iterator runner = T.TrianglesOnBoundary.begin();
238 runner != T.TrianglesOnBoundary.end(); runner++) {
239 int v[3];
240 for (size_t i=0; i<3; ++i)
241 v[i] = runner->second->endpoints[i]->getNr();
242
243 // Sort the vertices so the triangle is clockwise (relative to the normal vector).
244 Vector cross = T.PointsOnBoundary[v[1]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition();
245 cross.VectorProduct(T.PointsOnBoundary[v[2]]->getPosition() - T.PointsOnBoundary[v[0]]->getPosition());
246 if (cross.ScalarProduct(runner->second->NormalVector) > 0)
247 geo.appendIndices(indices[v[0]], indices[v[1]], indices[v[2]]);
248 else
249 geo.appendIndices(indices[v[0]], indices[v[2]], indices[v[1]]);
250 }
251 }
252
253 return geo;
254}
255
256void GLMoleculeObject_molecule::resetTesselationHull()
257{
258 // only when Observable still lives so far, we may check on Cacheable::isValid
259 if ((owner != NULL) && (!TesselationHull.isValid()))
260 updateMesh(createMoleculeMesh(*TesselationHull));
261}
262
263void GLMoleculeObject_molecule::resetBoundingBox()
264{
265 molecule::BoundingBoxInfo info = ObservedMolecule->getBoundingBox();
266 setPosition(QVector3D(info.position[0], info.position[1], info.position[2]));
267 setScale(info.radius + 0.3); // getBoundingSphere() only sees atoms as points, so make the box a bit bigger
268}
269
270void GLMoleculeObject_molecule::resetIndex()
271{
272 const atomId_t newId = ObservedMolecule->getMolIndex();
273 const size_t oldId = objectId();
274 ASSERT( newId != oldId,
275 "GLMoleculeObject_molecule::resetIndex() - index "+toString(newId)+" did not change.");
276 LOG(4, "INFO: GLMoleculeObject_molecule: new index is "+toString(newId)+".");
277 setObjectId(newId);
278
279 emit indexChanged(this, oldId, newId);
280}
281
282void GLMoleculeObject_molecule::AtomSelected(const atomId_t _id)
283{
284 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
285 if (iter != AtomsinSceneMap.end())
286 QMetaObject::invokeMethod(iter->second, // pointer to a QObject
287 "Selected", // member name (no parameters here)
288 Qt::QueuedConnection); // connection type
289 else
290 ELOG(2, "GLMoleculeObject_molecule::AtomSelected() - atom "
291 << _id << " unknown to GLMoleculeObject_molecule.");
292}
293
294void GLMoleculeObject_molecule::AtomUnselected(const atomId_t _id)
295{
296 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
297 if (iter != AtomsinSceneMap.end())
298 QMetaObject::invokeMethod(iter->second, // pointer to a QObject
299 "Unselected", // member name (no parameters here)
300 Qt::QueuedConnection); // connection type
301 else ELOG(2, "GLMoleculeObject_molecule::AtomUnselected() - atom "
302 << _id << " unknown to GLMoleculeObject_molecule.");
303}
304
305void GLMoleculeObject_molecule::Selected()
306{
307 ASSERT( !m_selected,
308 "GLMoleculeObject_molecule::Selected() - 3D rep of molecule is already selected.");
309 m_selected = true;
310
311 emit changed();
312}
313
314void GLMoleculeObject_molecule::Unselected()
315{
316 ASSERT( m_selected,
317 "GLMoleculeObject_molecule::Unselected() - 3D rep of molecule is already unselected.");
318 m_selected = false;
319
320 emit changed();
321}
322
323
324void GLMoleculeObject_molecule::initialize(QGLView *view, QGLPainter *painter)
325{
326 // Initialize all of the mesh objects that we have as children.
327 if (m_visible) {
328 GLMoleculeObject::initialize(view, painter);
329 } else {
330 foreach (QObject *obj, children()) {
331 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
332 if (meshobj)
333 meshobj->initialize(view, painter);
334 }
335 }
336}
337
338void GLMoleculeObject_molecule::draw(QGLPainter *painter, const QVector4D &cameraPlane)
339{
340 // draw either molecule's mesh or all atoms and bonds
341 if (m_visible) {
342 resetTesselationHull();
343
344 painter->modelViewMatrix().push();
345
346 // Apply the material and effect to the painter.
347 QGLMaterial *material;
348 if (m_hovering)
349 material = m_hoverMaterial;
350 else if (m_selected)
351 material = m_selectionMaterial;
352 else
353 material = m_material;
354
355 ASSERT(material, "GLMoleculeObject::draw: chosen material is NULL");
356
357 painter->setColor(material->diffuseColor());
358 painter->setFaceMaterial(QGL::AllFaces, material);
359 if (m_effect)
360 painter->setUserEffect(m_effect);
361 else
362 painter->setStandardEffect(QGL::LitMaterial);
363
364 // Mark the object for object picking purposes.
365 int prevObjectId = painter->objectPickId();
366 if (m_objectId != -1)
367 painter->setObjectPickId(m_objectId);
368
369 m_mesh[0]->draw(painter);
370
371 // Turn off the user effect, if present.
372 if (m_effect)
373 painter->setStandardEffect(QGL::LitMaterial);
374
375 // Revert to the previous object identifier.
376 painter->setObjectPickId(prevObjectId);
377
378 // Restore the modelview matrix.
379 painter->modelViewMatrix().pop();
380
381 // GLMoleculeObject::draw(painter, cameraPlane);
382 } else {
383 // Draw all of the mesh objects that we have as children.
384 foreach (QObject *obj, children()) {
385 GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
386 if (meshobj)
387 meshobj->draw(painter, cameraPlane);
388 }
389
390 // update bounding box prior to selection
391 resetBoundingBox();
392
393 painter->modelViewMatrix().push();
394 painter->modelViewMatrix().translate(m_position);
395 if (m_rotationAngle != 0.0f)
396 painter->modelViewMatrix().rotate(m_rotationAngle, m_rotationVector);
397 if ((m_scaleX != 1.0f) || (m_scaleY != 1.0f) || (m_scaleZ != 1.0f))
398 painter->modelViewMatrix().scale(m_scaleX, m_scaleY, m_scaleZ);
399
400 // Draw a box around the mesh, if selected.
401 if (m_selected)
402 drawSelectionBox(painter);
403
404 // Restore the modelview matrix.
405 painter->modelViewMatrix().pop();
406 }
407}
408
409/** Adds an atom of this molecule to the scene.
410 *
411 * @param _atom atom to add
412 */
413void GLMoleculeObject_molecule::atomInserted(const atomId_t _id)
414{
415 LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomInserted for atom "+toString(_id)+".");
416
417 GLMoleculeObject_atom *atomObject =
418 new GLMoleculeObject_atom(
419 GLMoleculeObject::meshSphere,
420 this,
421 board.getObservedAtom(_id));
422 ASSERT( atomObject != NULL,
423 "GLMoleculeObject_molecule::atomInserted - could not create atom object for "+toString(_id));
424 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
425 ASSERT(iter == AtomsinSceneMap.end(),
426 "GLMoleculeObject_molecule::atomInserted - same atom with id "+toString(_id)+" added again.");
427 AtomsinSceneMap.insert( make_pair(_id, atomObject) );
428
429 qRegisterMetaType<atomId_t>("atomId_t");
430 qRegisterMetaType<GLMoleculeObject_bond::SideOfBond>("GLMoleculeObject_bond::SideOfBond");
431 connect (atomObject, SIGNAL(clicked(atomId_t)), this, SIGNAL(atomClicked(atomId_t)));
432 connect (atomObject, SIGNAL(changed()), this, SIGNAL(changed()));
433 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SIGNAL(changed()));
434 connect (atomObject, SIGNAL(hoverChanged(GLMoleculeObject *)), this, SLOT(hoverChangedSignalled(GLMoleculeObject *)));
435// connect (atomObject, SIGNAL(bondsChanged()), this, SLOT(bondInserted(const atomId_t, const atomId_t, const GLMoleculeObject_bond::SideOfBond)));
436 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)));
437 connect (atomObject, SIGNAL(BondsRemoved(const atomId_t, const atomId_t)), this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
438 connect (atomObject, SIGNAL(indexChanged(GLMoleculeObject_atom*, const atomId_t, const atomId_t)), this, SLOT(changeAtomId(GLMoleculeObject_atom*, const atomId_t, const atomId_t)));
439
440 if (m_objectId == -1)
441 setObjectId(_id);
442
443 emit changed();
444 emit changeOccured();
445}
446
447/** Removes an atom of this molecule from the scene.
448 *
449 * We just the id as the atom might have already been destroyed.
450 *
451 * @param _id id of atom to remove
452 */
453void GLMoleculeObject_molecule::atomRemoved(const atomId_t _id)
454{
455 LOG(3, "INFO: GLMoleculeObject_molecule: Received signal atomRemoved for atom "+toString(_id)+".");
456 // bonds are removed by signal coming from ~bond
457
458 if ((unsigned int)m_objectId == _id)
459 setObjectId(-1);
460
461 // remove atoms
462 AtomNodeMap::iterator iter = AtomsinSceneMap.find(_id);
463 ASSERT(iter != AtomsinSceneMap.end(),
464 "GLMoleculeObject_molecule::atomRemoved() - atom "+toString(_id)+" not on display.");
465 GLMoleculeObject_atom *atomObject = iter->second;
466 AtomsinSceneMap.erase(iter);
467 atomObject->disconnect();
468 delete atomObject;
469
470 emit changed();
471 emit changeOccured();
472}
473
474void GLMoleculeObject_molecule::hoverChangedSignalled(GLMoleculeObject *ob)
475{
476 // Find the atom, ob corresponds to.
477 hoverAtomId = -1;
478 GLMoleculeObject_atom *atomObject = dynamic_cast<GLMoleculeObject_atom *>(ob);
479 if (atomObject){
480 for (AtomNodeMap::iterator iter = AtomsinSceneMap.begin();iter != AtomsinSceneMap.end(); ++ iter){
481 if (iter->second == atomObject)
482 hoverAtomId = iter->first;
483 }
484
485 // Propagate signal.
486 emit hoverChanged(hoverAtomId);
487 } else {
488 // Find the atom, ob corresponds to.
489 GLMoleculeObject_molecule *moleculeObject = dynamic_cast<GLMoleculeObject_molecule *>(ob);
490 if (moleculeObject == this){
491 // Propagate signal.
492 emit hoverChanged(ObservedMolecule->getMolIndex(), 0);
493 }
494 }
495}
496
497
498/** Helper function to get bond ids in the correct order for BondNodeMap.
499 *
500 * \return pair of ids in correct order.
501 */
502GLMoleculeObject_molecule::BondIds GLMoleculeObject_molecule::getBondIds(
503 const bond::ptr _bond,
504 const enum GLMoleculeObject_bond::SideOfBond _side)
505{
506 BondIds ids;
507 switch (_side) {
508 case GLMoleculeObject_bond::left:
509 ids = std::make_pair(_bond->leftatom->getId(), _bond->rightatom->getId());
510 break;
511 case GLMoleculeObject_bond::right:
512 ids = std::make_pair(_bond->rightatom->getId(), _bond->leftatom->getId());
513 break;
514 }
515 return ids;
516}
517
518/** Adds a bond to the scene.
519 *
520 * @param _bond bond to add
521 * @param side which side of the bond (left or right)
522 */
523void GLMoleculeObject_molecule::bondInserted(
524 const atomId_t _left, const atomId_t _right,
525 const enum GLMoleculeObject_bond::SideOfBond _side)
526{
527 LOG(3, "INFO: GLWorldScene::bondInserted() - Adding bond "+toString(_left)
528 +toString(_right)+".");
529 //LOG(4, "INFO: Currently present bonds " << BondsinSceneMap << ".");
530
531 const BondIds ids( std::make_pair(_left, _right) );
532 BondNodeMap::iterator iter = BondsinSceneMap.find(ids);
533 if (iter == BondsinSceneMap.end()) {
534 GLMoleculeObject_bond * bondObject =
535 new GLMoleculeObject_bond(GLMoleculeObject::meshCylinder, this, ids, _side);
536 connect (
537 bondObject, SIGNAL(BondRemoved(const atomId_t, const atomId_t)),
538 this, SLOT(bondRemoved(const atomId_t, const atomId_t)));
539 connect (bondObject, SIGNAL(changed()), this, SIGNAL(changed()));
540 BondsinSceneMap.insert( make_pair(ids, bondObject) );
541 // BondIdsinSceneMap.insert( Leftids );
542 } else {
543 iter->second->resetPosition();
544 iter->second->resetWidth();
545 }
546 emit changed();
547 emit changeOccured();
548}
549
550/** Removes a bond from the scene.
551 *
552 * @param _bond bond to remove
553 */
554void GLMoleculeObject_molecule::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
555{
556 LOG(3, "INFO: GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(rightnr)+".");
557 {
558 // left bond
559 const BondIds Leftids( make_pair(leftnr, rightnr) );
560 BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
561 ASSERT(leftiter != BondsinSceneMap.end(),
562 "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
563 +toString(rightnr)+" not on display.");
564 GLMoleculeObject_bond *bondObject = leftiter->second;
565 bondObject->disconnect();
566 BondsinSceneMap.erase(leftiter);
567 delete bondObject; // is done by signal from bond itself
568 //LOG(4, "INFO: Still present bonds " << BondsinSceneMap << ".");
569 }
570
571 emit changed();
572 emit changeOccured();
573}
574
575void GLMoleculeObject_molecule::setVisible(bool value)
576{
577 // first update the mesh if we are going to be visible now
578 if (value)
579 updateTesselationHull();
580 // then emit onward
581 GLMoleculeObject::setVisible(value);
582
583 emit changed();
584 emit changeOccured();
585}
586
587std::ostream &operator<<(std::ostream &ost, const GLMoleculeObject_molecule::BondIds &t)
588{
589 ost << t.first << "," << t.second;
590 return ost;
591}
592
593void GLMoleculeObject_molecule::wasClicked()
594{
595 LOG(4, "INFO: GLMoleculeObject_molecule: atom "
596 << ObservedMolecule->getMolIndex() << " has been clicked");
597 emit moleculeClicked(ObservedMolecule->getMolIndex());
598}
599
600void GLMoleculeObject_molecule::changeAtomId(
601 GLMoleculeObject_atom *ob,
602 const atomId_t oldId,
603 const atomId_t newId)
604{
605 LOG(3, "INFO: GLMoleculeObject_molecule - change atom id " << oldId << " to " << newId << ".");
606
607 // Remove from map.
608 AtomNodeMap::iterator iter = AtomsinSceneMap.find(oldId);
609 ASSERT(iter != AtomsinSceneMap.end(),
610 "GLMoleculeObject_molecule::changeAtomId() - atom with old id "+toString(oldId)+" not on display.");
611 ASSERT(iter->second == ob,
612 "GLMoleculeObject_molecule::changeAtomId() - atom with id "
613 +toString(oldId)+" does not match with object in AtomsinSceneMap.");
614 AtomsinSceneMap.erase(iter);
615
616 // Reinsert with new id.
617 {
618 AtomNodeMap::iterator iter = AtomsinSceneMap.find(newId);
619 ASSERT(iter == AtomsinSceneMap.end(),
620 "GLMoleculeObject_molecule::changeAtomId() - atom with new id "+toString(newId)+" already known.");
621 }
622 AtomsinSceneMap.insert( make_pair(newId, ob) );
623}
624
625
626GLMoleculeObject_molecule::atoms_t GLMoleculeObject_molecule::getPresentAtoms() const
627{
628 atoms_t returnAtomIds;
629 returnAtomIds.insert(
630 MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).begin()),
631 MapKeyConstIterator<AtomNodeMap::const_iterator>(const_cast<const AtomNodeMap &>(AtomsinSceneMap).end()));
632 return returnAtomIds;
633}
Note: See TracBrowser for help on using the repository browser.