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