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