1 | #include <stack>
|
---|
2 | #include "common.h"
|
---|
3 | #include "SceneGraph.h"
|
---|
4 | #include "X3dExporter.h"
|
---|
5 | #include "Mesh.h"
|
---|
6 | #include "KdTree.h"
|
---|
7 | #include "ViewCellBsp.h"
|
---|
8 | #include "ViewCell.h"
|
---|
9 | #include "Polygon3.h"
|
---|
10 | ViewCellContainer X3dExporter::foundViewCells; // TODO: delete later
|
---|
11 |
|
---|
12 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
13 | {
|
---|
14 | stream.open(mFilename.c_str());
|
---|
15 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
16 | stream<<"<X3D>"<<endl;
|
---|
17 | stream<<"<Scene>"<<endl;
|
---|
18 |
|
---|
19 | }
|
---|
20 |
|
---|
21 | X3dExporter::~X3dExporter()
|
---|
22 | {
|
---|
23 | stream<<"</Scene>"<<endl;
|
---|
24 | stream<<"</X3D>"<<endl;
|
---|
25 | stream.close();
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | bool
|
---|
30 | X3dExporter::ExportRays(const vector<Ray> &rays,
|
---|
31 | const float length,
|
---|
32 | const RgbColor &color)
|
---|
33 | {
|
---|
34 | vector<Ray>::const_iterator ri = rays.begin();
|
---|
35 | stream<<"<Shape>"<<endl;
|
---|
36 | stream<<"<Appearance>"<<endl;
|
---|
37 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
38 | "\" />"<<endl;
|
---|
39 | stream<<"</Appearance>"<<endl;
|
---|
40 |
|
---|
41 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
42 |
|
---|
43 | int index = 0;
|
---|
44 | for (; ri != rays.end(); ri++) {
|
---|
45 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
46 | index+=2;
|
---|
47 | }
|
---|
48 |
|
---|
49 | stream<<"\" >"<<endl;
|
---|
50 |
|
---|
51 | stream<<"<Coordinate point=\""<<endl;
|
---|
52 |
|
---|
53 | ri = rays.begin();
|
---|
54 | for (; ri != rays.end(); ri++) {
|
---|
55 | Vector3 a = (*ri).GetLoc();
|
---|
56 |
|
---|
57 | Vector3 b;
|
---|
58 | if (length < 0)
|
---|
59 | b = (*ri).GetLoc() - length*(*ri).GetDir();
|
---|
60 | else
|
---|
61 | if ((*ri).intersections.size()==0)
|
---|
62 | b = (*ri).GetLoc() + length*(*ri).GetDir();
|
---|
63 | else
|
---|
64 | b = (*ri).Extrap((*ri).intersections[0].mT);
|
---|
65 |
|
---|
66 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
67 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
68 | }
|
---|
69 |
|
---|
70 | stream<<"\" >"<<endl;
|
---|
71 | stream<<"</Coordinate>"<<endl;
|
---|
72 | stream<<"</IndexedLineSet>"<<endl;
|
---|
73 | stream<<"</Shape>"<<endl;
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | bool
|
---|
78 | X3dExporter::ExportRays(const RayContainer &rays,
|
---|
79 | const float length,
|
---|
80 | const RgbColor &color)
|
---|
81 | {
|
---|
82 | RayContainer::const_iterator ri = rays.begin();
|
---|
83 |
|
---|
84 | stream<<"<Shape>"<<endl;
|
---|
85 | stream<<"<Appearance>"<<endl;
|
---|
86 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
87 | "\" />"<<endl;
|
---|
88 | stream<<"</Appearance>"<<endl;
|
---|
89 |
|
---|
90 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
91 |
|
---|
92 | int index = 0;
|
---|
93 | for (; ri != rays.end(); ri++) {
|
---|
94 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
95 | index+=2;
|
---|
96 | }
|
---|
97 |
|
---|
98 | stream<<"\" >"<<endl;
|
---|
99 |
|
---|
100 | stream<<"<Coordinate point=\""<<endl;
|
---|
101 |
|
---|
102 | ri = rays.begin();
|
---|
103 | for (; ri != rays.end(); ri++) {
|
---|
104 | Vector3 a = (*ri)->GetLoc();
|
---|
105 |
|
---|
106 | Vector3 b;
|
---|
107 | if (length < 0)
|
---|
108 | b = (*ri)->GetLoc() - length*(*ri)->GetDir();
|
---|
109 | else
|
---|
110 | if ((*ri)->intersections.size()==0)
|
---|
111 | b = (*ri)->GetLoc() + length*(*ri)->GetDir();
|
---|
112 | else
|
---|
113 | b = (*ri)->Extrap((*ri)->intersections[0].mT);
|
---|
114 |
|
---|
115 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
116 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
117 | }
|
---|
118 |
|
---|
119 | stream<<"\" >"<<endl;
|
---|
120 | stream<<"</Coordinate>"<<endl;
|
---|
121 | stream<<"</IndexedLineSet>"<<endl;
|
---|
122 | stream<<"</Shape>"<<endl;
|
---|
123 |
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 |
|
---|
127 | void
|
---|
128 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
129 | {
|
---|
130 | stream<<"<Group>"<<endl;
|
---|
131 |
|
---|
132 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
133 | for (; ni != node->mChildren.end(); ni++)
|
---|
134 | ExportSceneNode(*ni);
|
---|
135 |
|
---|
136 |
|
---|
137 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
138 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
139 | // export the transform...
|
---|
140 | ExportIntersectable(*mi);
|
---|
141 | }
|
---|
142 |
|
---|
143 | stream<<"</Group>"<<endl;
|
---|
144 |
|
---|
145 | }
|
---|
146 | void
|
---|
147 | X3dExporter::ExportIntersectable(Intersectable *object)
|
---|
148 | {
|
---|
149 | switch (object->Type()) {
|
---|
150 | case Intersectable::MESH_INSTANCE:
|
---|
151 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
152 | ExportMeshInstance((MeshInstance *)object);
|
---|
153 | break;
|
---|
154 | case Intersectable::VIEW_CELL:
|
---|
155 | ExportViewCell((ViewCell *)object);
|
---|
156 | break;
|
---|
157 | default:
|
---|
158 | cerr<<"Sorry the export for object not yet defined"<<endl;
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | void
|
---|
164 | X3dExporter::ExportMeshInstance(MeshInstance *object)
|
---|
165 | {
|
---|
166 | // $$JB$$
|
---|
167 | // in the future check whether the mesh was not already exported
|
---|
168 | // and use a reference to the that mesh instead
|
---|
169 | ExportMesh(object->GetMesh());
|
---|
170 | }
|
---|
171 |
|
---|
172 | void
|
---|
173 | X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
174 | {
|
---|
175 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
176 |
|
---|
177 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
178 | ExportViewCell(*it);
|
---|
179 | }
|
---|
180 |
|
---|
181 | void
|
---|
182 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
183 | {
|
---|
184 | if (viewCell->GetMesh())
|
---|
185 | ExportMesh(viewCell->GetMesh());
|
---|
186 | }
|
---|
187 |
|
---|
188 | void
|
---|
189 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
190 | {
|
---|
191 |
|
---|
192 | stream<<"<Shape>"<<endl;
|
---|
193 | stream<<"<Appearance>"<<endl;
|
---|
194 |
|
---|
195 | // $$ tmp -> random material
|
---|
196 |
|
---|
197 | float r, g, b;
|
---|
198 |
|
---|
199 | if (mUseForcedMaterial) {
|
---|
200 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
201 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
202 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
203 |
|
---|
204 | } else
|
---|
205 | if (mesh->mMaterial) {
|
---|
206 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
207 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
208 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
209 | } else {
|
---|
210 | r = RandomValue(0.5, 1.0);
|
---|
211 | g = RandomValue(0.5, 1.0);
|
---|
212 | b = RandomValue(0.5, 1.0);
|
---|
213 | }
|
---|
214 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
215 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
216 | stream<<"</Appearance>"<<endl;
|
---|
217 |
|
---|
218 |
|
---|
219 | if (mWireframe)
|
---|
220 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
221 | else
|
---|
222 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
223 |
|
---|
224 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
225 |
|
---|
226 | int index = 0;
|
---|
227 |
|
---|
228 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
229 | Face *face = *fi;
|
---|
230 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
231 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
232 | stream<<*vi<<" ";
|
---|
233 | stream<<"-1"<<endl;
|
---|
234 | }
|
---|
235 | stream<<"\" >"<<endl;
|
---|
236 |
|
---|
237 | stream<<"<Coordinate point=\""<<endl;
|
---|
238 |
|
---|
239 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
240 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
241 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
242 | stream<<","<<endl;
|
---|
243 | }
|
---|
244 |
|
---|
245 | stream<<"\" >"<<endl;
|
---|
246 | stream<<"</Coordinate>"<<endl;
|
---|
247 |
|
---|
248 | if (mWireframe)
|
---|
249 | stream<<"</IndexedLineSet>"<<endl;
|
---|
250 | else
|
---|
251 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
252 |
|
---|
253 | stream<<"</Shape>"<<endl;
|
---|
254 |
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
259 | {
|
---|
260 | stream << "<Shape>" << endl;
|
---|
261 | stream << "<Appearance>" << endl;
|
---|
262 |
|
---|
263 | // $$ tmp -> random material
|
---|
264 |
|
---|
265 | float r, g, b;
|
---|
266 |
|
---|
267 | if (mUseForcedMaterial)
|
---|
268 | {
|
---|
269 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
270 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
271 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
272 | }
|
---|
273 | else if (poly->mMaterial)
|
---|
274 | {
|
---|
275 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
276 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
277 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
278 | } else
|
---|
279 | {
|
---|
280 | r = RandomValue(0.5, 1.0);
|
---|
281 | g = RandomValue(0.5, 1.0);
|
---|
282 | b = RandomValue(0.5, 1.0);
|
---|
283 | }
|
---|
284 |
|
---|
285 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
286 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
287 |
|
---|
288 | stream << "</Appearance>" << endl;
|
---|
289 |
|
---|
290 |
|
---|
291 | //-- create and write indices
|
---|
292 | if (mWireframe)
|
---|
293 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
294 | else
|
---|
295 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
296 |
|
---|
297 | int index = 0;
|
---|
298 |
|
---|
299 | VertexContainer::const_iterator vi;
|
---|
300 |
|
---|
301 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
302 | stream << index << " ";
|
---|
303 |
|
---|
304 | if (mWireframe) // final line to finish polygon
|
---|
305 | stream << "0 ";
|
---|
306 |
|
---|
307 | stream << "-1" << endl;
|
---|
308 | stream << "\" >" << endl;
|
---|
309 |
|
---|
310 | stream << "<Coordinate point=\"" << endl;
|
---|
311 |
|
---|
312 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
313 | {
|
---|
314 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
315 | stream << "," << endl;
|
---|
316 | }
|
---|
317 |
|
---|
318 | stream << "\" >" << endl;
|
---|
319 | stream << "</Coordinate>" << endl;
|
---|
320 |
|
---|
321 | if (mWireframe)
|
---|
322 | stream << "</IndexedLineSet>" << endl;
|
---|
323 | else
|
---|
324 | stream << "</IndexedFaceSet>" << endl;
|
---|
325 |
|
---|
326 | stream << "</Shape>" << endl;
|
---|
327 | }
|
---|
328 |
|
---|
329 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
330 | {
|
---|
331 | stream << "<Shape>" << endl;
|
---|
332 | stream << "<Appearance>" << endl;
|
---|
333 |
|
---|
334 | // $$ tmp -> random material
|
---|
335 |
|
---|
336 | float r, g, b;
|
---|
337 |
|
---|
338 | if (mUseForcedMaterial)
|
---|
339 | {
|
---|
340 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
341 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
342 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | r = RandomValue(0.5, 1.0);
|
---|
347 | g = RandomValue(0.5, 1.0);
|
---|
348 | b = RandomValue(0.5, 1.0);
|
---|
349 | }
|
---|
350 |
|
---|
351 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
352 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
353 |
|
---|
354 | stream << "</Appearance>" << endl;
|
---|
355 |
|
---|
356 |
|
---|
357 | //-- create and write indices
|
---|
358 | if (mWireframe)
|
---|
359 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
360 | else
|
---|
361 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
362 |
|
---|
363 | int index = 0;
|
---|
364 |
|
---|
365 | PolygonContainer::const_iterator pit;
|
---|
366 |
|
---|
367 | VertexContainer::const_iterator vi;
|
---|
368 |
|
---|
369 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
370 | {
|
---|
371 | Polygon3 *poly = *pit;
|
---|
372 | int startIdx = index;
|
---|
373 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
374 | {
|
---|
375 | stream << index ++ << " ";
|
---|
376 | }
|
---|
377 |
|
---|
378 | stream << startIdx << " ";// finish line
|
---|
379 | stream << "-1" << endl;
|
---|
380 | }
|
---|
381 |
|
---|
382 | stream << "\" >" << endl;
|
---|
383 |
|
---|
384 | stream << "<Coordinate point=\"" << endl;
|
---|
385 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
386 | {
|
---|
387 | Polygon3 *poly = *pit;
|
---|
388 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
389 | {
|
---|
390 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
391 | stream << "," << endl;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | stream << "\" >" << endl;
|
---|
395 | stream << "</Coordinate>" << endl;
|
---|
396 |
|
---|
397 | if (mWireframe)
|
---|
398 | stream << "</IndexedLineSet>" << endl;
|
---|
399 | else
|
---|
400 | stream << "</IndexedFaceSet>" << endl;
|
---|
401 |
|
---|
402 | stream << "</Shape>" << endl;
|
---|
403 | }
|
---|
404 |
|
---|
405 | bool
|
---|
406 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
407 | {
|
---|
408 | Mesh *mesh = new Mesh;
|
---|
409 | // add 6 vertices of the box
|
---|
410 | int index = (int)mesh->mVertices.size();
|
---|
411 | for (int i=0; i < 8; i++) {
|
---|
412 | Vector3 v;
|
---|
413 | box.GetVertex(i, v);
|
---|
414 | mesh->mVertices.push_back(v);
|
---|
415 | }
|
---|
416 |
|
---|
417 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
418 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
419 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
420 |
|
---|
421 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
422 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
423 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
424 |
|
---|
425 | ExportMesh(mesh);
|
---|
426 | delete mesh;
|
---|
427 | return true;
|
---|
428 | }
|
---|
429 |
|
---|
430 | bool
|
---|
431 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
432 | {
|
---|
433 | if (mExportRayDensity)
|
---|
434 | {
|
---|
435 | return ExportBspTreeRayDensity(tree);
|
---|
436 | }
|
---|
437 |
|
---|
438 | stack<BspNode *> tStack;
|
---|
439 |
|
---|
440 | tStack.push(tree.GetRoot());
|
---|
441 |
|
---|
442 | Mesh *mesh = new Mesh;
|
---|
443 |
|
---|
444 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
445 | bool savedWireframe = mWireframe;
|
---|
446 |
|
---|
447 | SetWireframe();
|
---|
448 | ExportBox(box);
|
---|
449 |
|
---|
450 | if (!savedWireframe)
|
---|
451 | SetFilled();
|
---|
452 |
|
---|
453 | //ViewCellContainer foundViewCells;
|
---|
454 |
|
---|
455 | if (BspTree::sStoreSplitPolys)
|
---|
456 | {
|
---|
457 | while (!tStack.empty())
|
---|
458 | {
|
---|
459 | BspNode *node = tStack.top();
|
---|
460 |
|
---|
461 | tStack.pop();
|
---|
462 |
|
---|
463 | PolygonContainer::const_iterator it;
|
---|
464 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
465 |
|
---|
466 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
467 | ExportPolygon(*it);
|
---|
468 |
|
---|
469 | if (!node->IsLeaf())
|
---|
470 | {
|
---|
471 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
472 |
|
---|
473 | tStack.push(interior->GetFront());
|
---|
474 | tStack.push(interior->GetBack());
|
---|
475 |
|
---|
476 | }
|
---|
477 | }
|
---|
478 | }
|
---|
479 | else // export view cells
|
---|
480 | {
|
---|
481 | while (!tStack.empty())
|
---|
482 | {
|
---|
483 | BspNode *node = tStack.top();
|
---|
484 |
|
---|
485 | tStack.pop();
|
---|
486 |
|
---|
487 | if (node->IsLeaf())
|
---|
488 | {
|
---|
489 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
490 | if (viewCell)
|
---|
491 | foundViewCells.push_back(viewCell);
|
---|
492 | }
|
---|
493 | else
|
---|
494 | {
|
---|
495 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
496 |
|
---|
497 | tStack.push(interior->GetFront());
|
---|
498 | tStack.push(interior->GetBack());
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | Debug << "Number of view cells with dublicates: " << (int)foundViewCells.size() << endl;
|
---|
503 |
|
---|
504 | //-- erase dublicates
|
---|
505 | sort(foundViewCells.begin(), foundViewCells.end());
|
---|
506 | ViewCellContainer::iterator new_end = unique(foundViewCells.begin(), foundViewCells.end());
|
---|
507 | foundViewCells.erase(new_end, foundViewCells.end());
|
---|
508 | ExportViewCells(foundViewCells);
|
---|
509 |
|
---|
510 | Debug << "Number of view cells after erasing dublicates: " << (int)foundViewCells.size() << endl;
|
---|
511 | }
|
---|
512 |
|
---|
513 | return true;
|
---|
514 | }
|
---|
515 |
|
---|
516 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
517 | {
|
---|
518 | if (mExportRayDensity) {
|
---|
519 | return ExportKdTreeRayDensity(tree);
|
---|
520 | }
|
---|
521 |
|
---|
522 | stack<KdNode *> tStack;
|
---|
523 |
|
---|
524 | tStack.push(tree.GetRoot());
|
---|
525 |
|
---|
526 | Mesh *mesh = new Mesh;
|
---|
527 |
|
---|
528 | while (!tStack.empty()) {
|
---|
529 | KdNode *node = tStack.top();
|
---|
530 | tStack.pop();
|
---|
531 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
532 | // add 6 vertices of the box
|
---|
533 | int index = (int)mesh->mVertices.size();
|
---|
534 | for (int i=0; i < 8; i++) {
|
---|
535 | Vector3 v;
|
---|
536 | box.GetVertex(i, v);
|
---|
537 | mesh->mVertices.push_back(v);
|
---|
538 | }
|
---|
539 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
540 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
541 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
542 |
|
---|
543 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
544 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
545 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
546 |
|
---|
547 | if (!node->IsLeaf()) {
|
---|
548 | KdInterior *interior = (KdInterior *)node;
|
---|
549 | tStack.push(interior->mFront);
|
---|
550 | tStack.push(interior->mBack);
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | ExportMesh(mesh);
|
---|
555 | delete mesh;
|
---|
556 | return true;
|
---|
557 | // TODO
|
---|
558 | return true;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | bool
|
---|
563 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
564 | {
|
---|
565 | stack<BspNode *> tStack;
|
---|
566 |
|
---|
567 | tStack.push(tree.GetRoot());
|
---|
568 |
|
---|
569 | bool fm = mUseForcedMaterial;
|
---|
570 |
|
---|
571 | mUseForcedMaterial = true;
|
---|
572 |
|
---|
573 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
574 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
575 |
|
---|
576 | while (!tStack.empty())
|
---|
577 | {
|
---|
578 | BspNode *node = tStack.top();
|
---|
579 | tStack.pop();
|
---|
580 |
|
---|
581 | if (node->IsLeaf())
|
---|
582 | {
|
---|
583 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
584 |
|
---|
585 | // set the mesh material according to the ray density
|
---|
586 | if (vc->mPassingRays.mRays)
|
---|
587 | {
|
---|
588 | float importance =
|
---|
589 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
590 |
|
---|
591 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
592 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
593 | ExportViewCell(vc);
|
---|
594 | }
|
---|
595 | } else
|
---|
596 | {
|
---|
597 | BspInterior *interior = (BspInterior *)node;
|
---|
598 | tStack.push(interior->GetFront());
|
---|
599 | tStack.push(interior->GetBack());
|
---|
600 | }
|
---|
601 | }
|
---|
602 |
|
---|
603 | // restore the state of forced material
|
---|
604 | mUseForcedMaterial = fm;
|
---|
605 |
|
---|
606 | return true;
|
---|
607 | }
|
---|
608 |
|
---|
609 | bool
|
---|
610 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
611 | {
|
---|
612 | stack<KdNode *> tStack;
|
---|
613 |
|
---|
614 | tStack.push(tree.GetRoot());
|
---|
615 |
|
---|
616 | bool fm = mUseForcedMaterial;
|
---|
617 | mUseForcedMaterial = true;
|
---|
618 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
619 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
620 | while (!tStack.empty()) {
|
---|
621 | KdNode *node = tStack.top();
|
---|
622 | tStack.pop();
|
---|
623 | if (node->IsLeaf()) {
|
---|
624 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
625 | Mesh *mesh = new Mesh;
|
---|
626 |
|
---|
627 | // add 6 vertices of the box
|
---|
628 | int index = (int)mesh->mVertices.size();
|
---|
629 | for (int i=0; i < 8; i++) {
|
---|
630 | Vector3 v;
|
---|
631 | box.GetVertex(i, v);
|
---|
632 | mesh->mVertices.push_back(v);
|
---|
633 | }
|
---|
634 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
635 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
636 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
637 |
|
---|
638 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
639 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
640 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
641 |
|
---|
642 |
|
---|
643 | // set the mesh material according to the ray density
|
---|
644 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
645 | if (leaf->mPassingRays.mRays) {
|
---|
646 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
647 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
648 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
649 | ///(float)leaf->mPassingRays.mRays;
|
---|
650 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
651 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
652 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
653 | ExportMesh(mesh);
|
---|
654 | }
|
---|
655 | delete mesh;
|
---|
656 | } else {
|
---|
657 | KdInterior *interior = (KdInterior *)node;
|
---|
658 | tStack.push(interior->mFront);
|
---|
659 | tStack.push(interior->mBack);
|
---|
660 | }
|
---|
661 | }
|
---|
662 | // restore the state of forced material
|
---|
663 | mUseForcedMaterial = fm;
|
---|
664 | return true;
|
---|
665 | }
|
---|
666 |
|
---|
667 |
|
---|
668 | struct BspSplitData
|
---|
669 | {
|
---|
670 | /// the current node |
---|
671 | BspNode *mNode; |
---|
672 | |
---|
673 | vector<Plane3 *> mPlanes; |
---|
674 | vector<bool> mSides; |
---|
675 | bool mIsFront; |
---|
676 | |
---|
677 | BspSplitData(BspNode *node):
|
---|
678 | mNode(node), mIsFront(false)
|
---|
679 | {}; |
---|
680 | BspSplitData(BspNode *node,
|
---|
681 | vector<Plane3 *> planes,
|
---|
682 | vector<bool> sides,
|
---|
683 | bool isFront):
|
---|
684 | mNode(node), mPlanes(planes),
|
---|
685 | mSides(sides), mIsFront(isFront)
|
---|
686 | {};
|
---|
687 | };
|
---|
688 |
|
---|
689 | void X3dExporter::ExportLeavesGeometry(const BspTree &tree, const vector<BspLeaf *> &leaves)
|
---|
690 | {
|
---|
691 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
692 |
|
---|
693 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
694 | {
|
---|
695 | PolygonContainer cell;
|
---|
696 | tree.ConstructGeometry(*it, cell);
|
---|
697 |
|
---|
698 | ExportPolygons(cell);
|
---|
699 |
|
---|
700 | CLEAR_CONTAINER(cell);
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | void X3dExporter::ExportBspSplits(const BspTree &tree) |
---|
705 | { |
---|
706 | std::stack<BspSplitData> tStack; |
---|
707 | |
---|
708 | BspSplitData tData(tree.GetRoot()); |
---|
709 | tStack.push(tData); |
---|
710 | |
---|
711 | PolygonContainer polys; |
---|
712 | |
---|
713 | while (!tStack.empty()) |
---|
714 | { |
---|
715 | // filter polygons donw the tree |
---|
716 | BspSplitData tData = tStack.top(); |
---|
717 | tStack.pop(); |
---|
718 | |
---|
719 | if (!tData.mNode->IsLeaf()) |
---|
720 | { |
---|
721 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode); |
---|
722 | |
---|
723 | if (tData.mNode != tree.GetRoot()) |
---|
724 | tData.mSides.push_back(tData.mIsFront); // add current side of split plane |
---|
725 | |
---|
726 | // bounded plane is added to the polygons |
---|
727 | Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(*interior->GetPlane()); |
---|
728 | |
---|
729 | // do all the splits with the previous planes |
---|
730 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i) |
---|
731 | { |
---|
732 | VertexContainer splitPts; |
---|
733 | |
---|
734 | if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT) |
---|
735 | { |
---|
736 | Polygon3 *frontPoly = new Polygon3(); |
---|
737 | Polygon3 *backPoly = new Polygon3(); |
---|
738 | |
---|
739 | planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts); |
---|
740 | DEL_PTR(planePoly); |
---|
741 | |
---|
742 | if(tData.mSides[i] == true) |
---|
743 | { |
---|
744 | planePoly = frontPoly; |
---|
745 | DEL_PTR(backPoly); |
---|
746 | } |
---|
747 | else |
---|
748 | { |
---|
749 | planePoly = backPoly; |
---|
750 | DEL_PTR(frontPoly); |
---|
751 | } |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes |
---|
756 | |
---|
757 | if (planePoly->Valid()) |
---|
758 | polys.push_back(planePoly); |
---|
759 | else |
---|
760 | DEL_PTR(planePoly); |
---|
761 | |
---|
762 | // push the children on the stack |
---|
763 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true)); |
---|
764 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false)); |
---|
765 | } |
---|
766 | } |
---|
767 | ExportPolygons(polys); |
---|
768 | CLEAR_CONTAINER(polys); |
---|
769 | } |
---|
770 | |
---|
771 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree) |
---|
772 | { |
---|
773 | std::stack<BspNode *> tStack; |
---|
774 | |
---|
775 | tStack.push(tree.GetRoot()); |
---|
776 | |
---|
777 | PolygonContainer polys; |
---|
778 | |
---|
779 | while (!tStack.empty()) |
---|
780 | { |
---|
781 | // filter polygons donw the tree |
---|
782 | BspNode *node = tStack.top(); |
---|
783 | tStack.pop(); |
---|
784 | |
---|
785 | if (!node->IsLeaf()) |
---|
786 | { |
---|
787 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
788 | |
---|
789 | // bounded plane is added to the polygons |
---|
790 | polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane())); |
---|
791 | |
---|
792 | // push the children on the stack |
---|
793 | tStack.push(interior->GetBack()); |
---|
794 | tStack.push(interior->GetFront()); |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | ExportPolygons(polys); |
---|
799 | CLEAR_CONTAINER(polys); |
---|
800 | } |
---|