1 | //#include <boost/algorithm/string.hpp>
|
---|
2 |
|
---|
3 | #include <stack>
|
---|
4 | #include "SceneGraph.h"
|
---|
5 | #include "X3dExporter.h"
|
---|
6 | #include "Intersectable.h"
|
---|
7 | #include "IntersectableWrapper.h"
|
---|
8 | //#include "ktball.h"
|
---|
9 |
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | namespace GtpVisibilityPreprocessor {
|
---|
14 |
|
---|
15 |
|
---|
16 | bool
|
---|
17 | SceneGraph::Export( const string filename )
|
---|
18 | {
|
---|
19 | if (strstr(filename.c_str(), ".x3d")) {
|
---|
20 | X3dExporter exporter(filename);
|
---|
21 | exporter.ExportScene(mRoot);
|
---|
22 | return true;
|
---|
23 | } else {
|
---|
24 | cerr<<"Error: Currently unsuported export format, filename "<<filename<<endl;
|
---|
25 |
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | return false;
|
---|
30 |
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | SceneGraphNode::~SceneGraphNode()
|
---|
35 | {
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | SceneGraphLeaf::~SceneGraphLeaf()
|
---|
40 | {
|
---|
41 | CLEAR_CONTAINER(mGeometry);
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | SceneGraphInterior::~SceneGraphInterior()
|
---|
46 | {
|
---|
47 | // recursivly delete all children
|
---|
48 | CLEAR_CONTAINER(mChildren);
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | /************************************************************/
|
---|
54 | /* SceneGraph implementation */
|
---|
55 | /************************************************************/
|
---|
56 |
|
---|
57 |
|
---|
58 | SceneGraph::SceneGraph():
|
---|
59 | mRoot(NULL)
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | SceneGraph::~SceneGraph()
|
---|
65 | {
|
---|
66 | DEL_PTR(mRoot);
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | SceneGraphInterior *SceneGraph::GetRoot()
|
---|
71 | {
|
---|
72 | return mRoot;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | void SceneGraph::SetRoot(SceneGraphInterior *root)
|
---|
77 | {
|
---|
78 | mRoot = root;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | int SceneGraph::CollectObjects(ObjectContainer &instances)
|
---|
83 | {
|
---|
84 | instances.clear();
|
---|
85 | int number = 0;
|
---|
86 |
|
---|
87 | stack<SceneGraphNode *> nodeStack;
|
---|
88 | nodeStack.push(mRoot);
|
---|
89 |
|
---|
90 | while (!nodeStack.empty())
|
---|
91 | {
|
---|
92 | SceneGraphNode *node = nodeStack.top();
|
---|
93 | nodeStack.pop();
|
---|
94 |
|
---|
95 | if (node->IsLeaf())
|
---|
96 | {
|
---|
97 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
98 |
|
---|
99 | ObjectContainer::const_iterator mi = leaf->mGeometry.begin();
|
---|
100 |
|
---|
101 | for (; mi != leaf->mGeometry.end(); mi++)
|
---|
102 | {
|
---|
103 | instances.push_back(*mi);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
109 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
110 |
|
---|
111 | for (; ni != interior->mChildren.end(); ++ ni)
|
---|
112 | {
|
---|
113 | nodeStack.push(*ni);
|
---|
114 | number++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | return number;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | int
|
---|
124 | SceneGraph::AssignObjectIds()
|
---|
125 | {
|
---|
126 | // matt: rather start with id zero
|
---|
127 | int id = 0;
|
---|
128 | stack<SceneGraphNode *> nodeStack;
|
---|
129 |
|
---|
130 | nodeStack.push(mRoot);
|
---|
131 |
|
---|
132 | while (!nodeStack.empty())
|
---|
133 | {
|
---|
134 | SceneGraphNode *node = nodeStack.top();
|
---|
135 | nodeStack.pop();
|
---|
136 |
|
---|
137 | if (node->IsLeaf())
|
---|
138 | {
|
---|
139 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
140 | ObjectContainer::iterator mi = leaf->mGeometry.begin();
|
---|
141 |
|
---|
142 | for (; mi != leaf->mGeometry.end(); mi ++)
|
---|
143 | {
|
---|
144 | (*mi)->SetId(id ++);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
150 |
|
---|
151 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
152 |
|
---|
153 | for (; ni != interior->mChildren.end(); ni ++)
|
---|
154 | {
|
---|
155 | nodeStack.push(*ni);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | // return max id
|
---|
161 | return id;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void
|
---|
165 | SceneGraph::GetStatistics(int &intersectables, int &faces) const
|
---|
166 | {
|
---|
167 | stack<SceneGraphNode *> nodeStack;
|
---|
168 |
|
---|
169 | nodeStack.push(mRoot);
|
---|
170 | faces = 0;
|
---|
171 | intersectables = 0;
|
---|
172 |
|
---|
173 | while (!nodeStack.empty())
|
---|
174 | {
|
---|
175 | SceneGraphNode *node = nodeStack.top();
|
---|
176 | nodeStack.pop();
|
---|
177 |
|
---|
178 | if (node->IsLeaf())
|
---|
179 | {
|
---|
180 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
181 |
|
---|
182 | ObjectContainer::const_iterator mi = leaf->mGeometry.begin();
|
---|
183 | for (; mi != leaf->mGeometry.end(); mi++)
|
---|
184 | {
|
---|
185 | intersectables++;
|
---|
186 | faces += (*mi)->NumberOfFaces();
|
---|
187 | }
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
192 |
|
---|
193 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
194 | for (; ni != interior->mChildren.end(); ni++)
|
---|
195 | {
|
---|
196 | nodeStack.push(*ni);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | void SceneGraphLeaf::UpdateBox()
|
---|
204 | {
|
---|
205 | AxisAlignedBox3 box;
|
---|
206 | box.Initialize();
|
---|
207 |
|
---|
208 | ObjectContainer::const_iterator mi = mGeometry.begin();
|
---|
209 | for (; mi != mGeometry.end(); mi++)
|
---|
210 | box.Include((*mi)->GetBox());
|
---|
211 |
|
---|
212 | mBox = box;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void SceneGraphInterior::UpdateBox()
|
---|
217 | {
|
---|
218 | AxisAlignedBox3 box;
|
---|
219 |
|
---|
220 | box.Initialize();
|
---|
221 |
|
---|
222 | SceneGraphNodeContainer::iterator ni = mChildren.begin();
|
---|
223 |
|
---|
224 | for (; ni != mChildren.end(); ++ ni)
|
---|
225 | {
|
---|
226 | (*ni)->UpdateBox();
|
---|
227 | box.Include((*ni)->GetBox());
|
---|
228 | }
|
---|
229 |
|
---|
230 | mBox = box;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 |
|
---|
235 | void SceneGraph::ExportScene(const string filename)
|
---|
236 | {
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | void SceneGraph::LoadScene(const string filename)
|
---|
241 | {
|
---|
242 | // load binary version of mesh
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | int SceneGraph::GetSize() const
|
---|
247 | {
|
---|
248 | stack<SceneGraphNode *> nodeStack;
|
---|
249 |
|
---|
250 | nodeStack.push(mRoot);
|
---|
251 | int size = 0;
|
---|
252 |
|
---|
253 | while (!nodeStack.empty())
|
---|
254 | {
|
---|
255 | SceneGraphNode *node = nodeStack.top();
|
---|
256 | nodeStack.pop();
|
---|
257 |
|
---|
258 | if (node->IsLeaf())
|
---|
259 | {
|
---|
260 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
261 |
|
---|
262 | size += (int)leaf->mGeometry.size();
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
267 |
|
---|
268 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
269 | for (; ni != interior->mChildren.end(); ni++)
|
---|
270 | {
|
---|
271 | nodeStack.push(*ni);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | return size;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | SceneGraphLeaf::SceneGraphLeaf(): mIsDynamic(false), mHasChanged(true)
|
---|
281 | {
|
---|
282 | mTrafo = IdentityMatrix();
|
---|
283 | mIntersectable = new SceneGraphLeafIntersectable(this, mBox);
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | SceneGraphLeaf::SceneGraphLeaf(bool isDynamic): mIsDynamic(isDynamic), mHasChanged(true)
|
---|
288 | {
|
---|
289 | mTrafo = IdentityMatrix();
|
---|
290 | mIntersectable = new SceneGraphLeafIntersectable(this, mBox);
|
---|
291 | }
|
---|
292 |
|
---|
293 |
|
---|
294 | void SceneGraphLeaf::ApplyTransform(const Matrix4x4 &trafo)
|
---|
295 | {
|
---|
296 | mHasChanged = true;
|
---|
297 | mTrafo = trafo * mTrafo;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | void SceneGraphLeaf::LoadTransform(const Matrix4x4 &m)
|
---|
302 | {
|
---|
303 | mHasChanged = true;
|
---|
304 | mTrafo = m;
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | void SceneGraphLeaf::GetTransform(Matrix4x4 &m) const
|
---|
309 | {
|
---|
310 | m = mTrafo;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | AxisAlignedBox3 SceneGraphLeaf::GetBox() const
|
---|
315 | {
|
---|
316 | return Transform(mBox, mTrafo);
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | AxisAlignedBox3 SceneGraphLeaf::GetOriginalBox() const
|
---|
321 | {
|
---|
322 | return mBox;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | SceneGraphLeaf::SceneGraphLeaf(SceneGraphLeaf const& copy)
|
---|
327 | {
|
---|
328 | // hack: should just pass a IntersectableGroup as a whole
|
---|
329 | // instead we duplicate the object container and create a new
|
---|
330 | // leaf
|
---|
331 | mGeometry = copy.mGeometry;
|
---|
332 | //for (size_t i = 0; i < copy.mGeometry->size(); ++ i)
|
---|
333 | // mGeometry.push_back(copy.mGeometry[i]);
|
---|
334 |
|
---|
335 | mBox = copy.mBox;
|
---|
336 | mTrafo = copy.mTrafo;
|
---|
337 | mIsDynamic = copy.mIsDynamic;
|
---|
338 |
|
---|
339 | mIntersectable = new SceneGraphLeafIntersectable(this, mBox);
|
---|
340 | mHasChanged = true;
|
---|
341 | }
|
---|
342 |
|
---|
343 | } |
---|