1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * GLWorldScene.cpp
|
---|
10 | *
|
---|
11 | * This is based on the Qt3D example "teaservice", specifically parts of teaservice.cpp.
|
---|
12 | *
|
---|
13 | * Created on: Aug 17, 2011
|
---|
14 | * Author: heber
|
---|
15 | */
|
---|
16 |
|
---|
17 | // include config.h
|
---|
18 | #ifdef HAVE_CONFIG_H
|
---|
19 | #include <config.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include "GLWorldScene.hpp"
|
---|
23 |
|
---|
24 | #include "GLMoleculeObject.hpp"
|
---|
25 | #include "GLMoleculeObject_atom.hpp"
|
---|
26 | #include "GLMoleculeObject_bond.hpp"
|
---|
27 |
|
---|
28 | #include "CodePatterns/MemDebug.hpp"
|
---|
29 |
|
---|
30 | #include "CodePatterns/Log.hpp"
|
---|
31 |
|
---|
32 | #include "atom.hpp"
|
---|
33 | #include "Bond/bond.hpp"
|
---|
34 | #include "Helpers/helpers.hpp"
|
---|
35 | #include "molecule.hpp"
|
---|
36 | #include "World.hpp"
|
---|
37 |
|
---|
38 |
|
---|
39 | GLWorldScene::GLWorldScene(QObject *parent)
|
---|
40 | : QObject(parent)
|
---|
41 | {
|
---|
42 | init();
|
---|
43 |
|
---|
44 | //changeMaterials(false);
|
---|
45 | }
|
---|
46 |
|
---|
47 | GLWorldScene::~GLWorldScene()
|
---|
48 | {
|
---|
49 | // remove all elements
|
---|
50 | GLMoleculeObject::cleanMaterialMap();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Initialise the WorldScene with molecules and atoms from World.
|
---|
54 | *
|
---|
55 | */
|
---|
56 | void GLWorldScene::init()
|
---|
57 | {
|
---|
58 | const std::vector<molecule*> &molecules = World::getInstance().getAllMolecules();
|
---|
59 |
|
---|
60 | if (molecules.size() > 0) {
|
---|
61 | for (std::vector<molecule*>::const_iterator Runner = molecules.begin();
|
---|
62 | Runner != molecules.end();
|
---|
63 | Runner++) {
|
---|
64 | // create molecule
|
---|
65 | for (molecule::const_iterator atomiter = (*Runner)->begin();
|
---|
66 | atomiter != (*Runner)->end();
|
---|
67 | ++atomiter) {
|
---|
68 | // create atom objects in scene
|
---|
69 | atomInserted(*atomiter);
|
---|
70 |
|
---|
71 | // create bond objects in scene
|
---|
72 | bondsChanged(*atomiter);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Adds an atom to the scene.
|
---|
79 | *
|
---|
80 | * @param _atom atom to add
|
---|
81 | */
|
---|
82 | void GLWorldScene::atomInserted(const atom *_atom)
|
---|
83 | {
|
---|
84 | LOG(0, "GLWorldScene: Received signal atomInserted for atom "+toString(_atom->getId())+".");
|
---|
85 | GLMoleculeObject_atom *atomObject = new GLMoleculeObject_atom(this, _atom);
|
---|
86 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
87 | ASSERT(iter == AtomsinSceneMap.end(),
|
---|
88 | "GLWorldScene::atomAdded() - same atom "+_atom->getName()+" added again.");
|
---|
89 | AtomsinSceneMap.insert( make_pair(_atom->getId(), atomObject) );
|
---|
90 | connect (atomObject, SIGNAL(clicked(atomId_t)), this, SLOT(atomClicked(atomId_t)));
|
---|
91 | connect (atomObject, SIGNAL(hoverChanged()), this, SIGNAL(changed()));
|
---|
92 | connect (atomObject, SIGNAL(BondsChanged(const atom *)), this, SLOT(bondsChanged(const atom *)));
|
---|
93 | bondsChanged(_atom);
|
---|
94 | emit changed();
|
---|
95 | }
|
---|
96 |
|
---|
97 | /** Removes an atom from the scene.
|
---|
98 | *
|
---|
99 | * @param _atom atom to remove
|
---|
100 | */
|
---|
101 | void GLWorldScene::atomRemoved(const atom *_atom)
|
---|
102 | {
|
---|
103 | LOG(0, "GLWorldScene: Received signal atomRemoved for atom "+toString(_atom->getId())+".");
|
---|
104 | // remove all its bonds
|
---|
105 | const BondList& bondlist = _atom->getListOfBonds();
|
---|
106 | for (BondList::const_iterator iter = bondlist.begin(); iter != bondlist.end(); ++iter) {
|
---|
107 | bondRemoved((*iter)->leftatom->getId(), (*iter)->rightatom->getId());
|
---|
108 | bondRemoved((*iter)->rightatom->getId(), (*iter)->leftatom->getId());
|
---|
109 | }
|
---|
110 | // remove atoms
|
---|
111 | AtomNodeMap::iterator iter = AtomsinSceneMap.find(_atom->getId());
|
---|
112 | ASSERT(iter != AtomsinSceneMap.end(),
|
---|
113 | "GLWorldScene::atomRemoved() - atom "+_atom->getName()+" not on display.");
|
---|
114 | GLMoleculeObject_atom *atomObject = iter->second;
|
---|
115 | atomObject->disconnect();
|
---|
116 | AtomsinSceneMap.erase(iter);
|
---|
117 | delete atomObject;
|
---|
118 | emit changed();
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Updates the bond structure of the signaled \a _atom.
|
---|
122 | *
|
---|
123 | * @param _atom atom whose bonds changed.
|
---|
124 | */
|
---|
125 | void GLWorldScene::bondsChanged(const atom *_atom)
|
---|
126 | {
|
---|
127 | const atomId_t id = _atom->getId();
|
---|
128 |
|
---|
129 | // create list with all present bonds
|
---|
130 | std::set< atomId_t > presentBonds;
|
---|
131 | std::pair< BondIdsMap::const_iterator, BondIdsMap::const_iterator> range =
|
---|
132 | BondIdsinSceneMap.equal_range( id );
|
---|
133 | for (BondIdsMap::const_iterator iter = range.first; iter != range.second; ++iter) {
|
---|
134 | const atomId_t otherid = iter->second;
|
---|
135 | #ifndef NDEBUG
|
---|
136 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
---|
137 | ASSERT(iter == presentBonds.end(),
|
---|
138 | "GLWorldScene::BondsChanged() - bond id "+toString(otherid)+" for atom "
|
---|
139 | +toString(id)+" present twice.");
|
---|
140 | #endif
|
---|
141 | presentBonds.insert( otherid );
|
---|
142 | }
|
---|
143 | LOG(0, "We have the following bonds: "+toString(presentBonds)+".");
|
---|
144 |
|
---|
145 | // search for added bonds
|
---|
146 | const BondList &bondlist = _atom->getListOfBonds();
|
---|
147 | for (BondList::const_iterator bonditer = bondlist.begin();
|
---|
148 | bonditer != bondlist.end();
|
---|
149 | ++bonditer) {
|
---|
150 | const bond *_bond = *bonditer;
|
---|
151 | const atomId_t otherid = _bond->GetOtherAtom(_atom)->getId();
|
---|
152 | const BondIds ids = std::make_pair( id, otherid );
|
---|
153 | BondNodeMap::const_iterator iter = BondsinSceneMap.find(ids);
|
---|
154 | if (iter != BondsinSceneMap.end()) {
|
---|
155 | // bond is already present
|
---|
156 | std::set< atomId_t >::const_iterator iter = presentBonds.find(otherid);
|
---|
157 | ASSERT(iter != presentBonds.end(),
|
---|
158 | "GLWorldScene::BondsChanged() - other id "+toString(otherid)+" for atom "
|
---|
159 | +toString(_atom->getId())+" not present in BondIdsinSceneMap.");
|
---|
160 | presentBonds.erase(otherid);
|
---|
161 | LOG(0, "Removing "+toString(otherid)+" from presentBonds.");
|
---|
162 | } else {
|
---|
163 | // insert new bond
|
---|
164 | bondInserted(_bond);
|
---|
165 | }
|
---|
166 | }
|
---|
167 | LOG(0, "The following bonds should not be present: "+toString(presentBonds)+".");
|
---|
168 |
|
---|
169 | // remove all still presentBonds
|
---|
170 | for (std::set< atomId_t >::iterator iter = presentBonds.begin();
|
---|
171 | !presentBonds.empty(); iter = presentBonds.begin()) {
|
---|
172 | bondRemoved( id, *iter );
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /** Adds a bond to the scene.
|
---|
177 | *
|
---|
178 | * @param _bond bond to add
|
---|
179 | */
|
---|
180 | void GLWorldScene::bondInserted(const bond *_bond)
|
---|
181 | {
|
---|
182 | LOG(0, "GLWorldScene::bondInserted() - Adding bond "+toString(*_bond)+".");
|
---|
183 | const double distance =
|
---|
184 | _bond->leftatom->getPosition().distance(_bond->rightatom->getPosition())/2.;
|
---|
185 | {
|
---|
186 | // left bond
|
---|
187 | const BondIds Leftids( make_pair(_bond->leftatom->getId(), _bond->rightatom->getId()) );
|
---|
188 | BondNodeMap::iterator iter = BondsinSceneMap.find(Leftids);
|
---|
189 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
190 | "GLWorldScene::bondAdded() - same left-sided bond "+toString(*_bond)+" added again.");
|
---|
191 | GLMoleculeObject_bond *bondObject =
|
---|
192 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::left);
|
---|
193 | BondsinSceneMap.insert( make_pair(Leftids, bondObject) );
|
---|
194 | BondIdsinSceneMap.insert( Leftids );
|
---|
195 | }
|
---|
196 | {
|
---|
197 | // right bond
|
---|
198 | const BondIds Rightids( make_pair(_bond->rightatom->getId(), _bond->leftatom->getId()) );
|
---|
199 | BondNodeMap::iterator iter = BondsinSceneMap.find(Rightids);
|
---|
200 | ASSERT(iter == BondsinSceneMap.end(),
|
---|
201 | "GLWorldScene::bondAdded() - same right-sided bond "+toString(*_bond)+" added again.");
|
---|
202 | GLMoleculeObject_bond *bondObject =
|
---|
203 | new GLMoleculeObject_bond(this, _bond, distance, GLMoleculeObject_bond::right);
|
---|
204 | BondsinSceneMap.insert( make_pair(Rightids, bondObject) );
|
---|
205 | BondIdsinSceneMap.insert( Rightids );
|
---|
206 | }
|
---|
207 | emit changed();
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Removes a bond from the scene.
|
---|
211 | *
|
---|
212 | * @param _bond bond to remove
|
---|
213 | */
|
---|
214 | void GLWorldScene::bondRemoved(const atomId_t leftnr, const atomId_t rightnr)
|
---|
215 | {
|
---|
216 | LOG(0, "GLWorldScene::bondRemoved() - Removing bond between "+toString(leftnr)+" and "+toString(leftnr)+".");
|
---|
217 | {
|
---|
218 | // left bond
|
---|
219 | const BondIds Leftids( make_pair(leftnr, rightnr) );
|
---|
220 | BondNodeMap::iterator leftiter = BondsinSceneMap.find( Leftids );
|
---|
221 | ASSERT(leftiter != BondsinSceneMap.end(),
|
---|
222 | "GLWorldScene::bondRemoved() - bond "+toString(leftnr)+"-"
|
---|
223 | +toString(rightnr)+" not on display.");
|
---|
224 | GLMoleculeObject_bond *bondObject = leftiter->second;
|
---|
225 | BondsinSceneMap.erase(leftiter);
|
---|
226 | delete bondObject;
|
---|
227 | }
|
---|
228 | // remove from bond ids
|
---|
229 | std::pair<BondIdsMap::iterator, BondIdsMap::iterator> leftrange =
|
---|
230 | BondIdsinSceneMap.equal_range(leftnr);
|
---|
231 | BondIdsMap::iterator iter;
|
---|
232 | for (iter = leftrange.first; iter != leftrange.second; ++iter) {
|
---|
233 | if (iter->second == rightnr) {
|
---|
234 | BondIdsinSceneMap.erase(iter);
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | ASSERT(iter != leftrange.second,
|
---|
239 | "GLWorldScene::bondRemoved() - could not find ("
|
---|
240 | +toString(leftnr)+"-"+toString(rightnr)+" in BondIdsinSceneMap.");
|
---|
241 | emit changed();
|
---|
242 | }
|
---|
243 |
|
---|
244 | void GLWorldScene::initialize(QGLView *view, QGLPainter *painter) const
|
---|
245 | {
|
---|
246 | // Initialize all of the mesh objects that we have as children.
|
---|
247 | foreach (QObject *obj, children()) {
|
---|
248 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
249 | if (meshobj)
|
---|
250 | meshobj->initialize(view, painter);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void GLWorldScene::draw(QGLPainter *painter) const
|
---|
255 | {
|
---|
256 | // Draw all of the mesh objects that we have as children.
|
---|
257 | foreach (QObject *obj, children()) {
|
---|
258 | GLMoleculeObject *meshobj = qobject_cast<GLMoleculeObject *>(obj);
|
---|
259 | if (meshobj)
|
---|
260 | meshobj->draw(painter);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | void GLWorldScene::atomClicked(atomId_t no)
|
---|
265 | {
|
---|
266 | qDebug("GLWorldScene: atom %d has been clicked.", no);
|
---|
267 | emit clicked(no);
|
---|
268 | }
|
---|
269 |
|
---|