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 | #include "HierarchyManager.h"
|
---|
12 | #include "VssTree.h"
|
---|
13 | #include "VspBspTree.h"
|
---|
14 | #include "RssTree.h"
|
---|
15 | #include "Beam.h"
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
18 |
|
---|
19 |
|
---|
20 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
21 | {
|
---|
22 | stream.open(mFilename.c_str());
|
---|
23 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
24 | stream<<"<X3D>"<<endl;
|
---|
25 | stream<<"<Scene>"<<endl;
|
---|
26 |
|
---|
27 | }
|
---|
28 |
|
---|
29 | X3dExporter::~X3dExporter()
|
---|
30 | {
|
---|
31 | stream<<"</Scene>"<<endl;
|
---|
32 | stream<<"</X3D>"<<endl;
|
---|
33 | stream.close();
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 |
|
---|
38 | bool
|
---|
39 | X3dExporter::ExportRays(const RayContainer &rays,
|
---|
40 | const float length,
|
---|
41 | const RgbColor &color)
|
---|
42 | {
|
---|
43 | RayContainer::const_iterator ri = rays.begin();
|
---|
44 |
|
---|
45 | stream<<"<Shape>"<<endl;
|
---|
46 | stream<<"<Appearance>"<<endl;
|
---|
47 | stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
48 | "\" />"<<endl;
|
---|
49 | stream<<"</Appearance>"<<endl;
|
---|
50 |
|
---|
51 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
52 |
|
---|
53 | int index = 0;
|
---|
54 | for (; ri != rays.end(); ri++) {
|
---|
55 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
56 | index+=2;
|
---|
57 | }
|
---|
58 |
|
---|
59 | stream<<"\" >"<<endl;
|
---|
60 |
|
---|
61 | stream<<"<Coordinate point=\""<<endl;
|
---|
62 |
|
---|
63 | ri = rays.begin();
|
---|
64 | for (; ri != rays.end(); ri++) {
|
---|
65 | Vector3 a = (*ri)->GetLoc();
|
---|
66 |
|
---|
67 | Vector3 b;
|
---|
68 | if (length < 0)
|
---|
69 | b = (*ri)->GetLoc() - length*(*ri)->GetDir();
|
---|
70 | else
|
---|
71 | if ((*ri)->intersections.size()==0)
|
---|
72 | b = (*ri)->GetLoc() + length*(*ri)->GetDir();
|
---|
73 | else
|
---|
74 | b = (*ri)->Extrap((*ri)->intersections[0].mT);
|
---|
75 |
|
---|
76 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
77 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
78 | }
|
---|
79 |
|
---|
80 | stream<<"\" >"<<endl;
|
---|
81 | stream<<"</Coordinate>"<<endl;
|
---|
82 | stream<<"</IndexedLineSet>"<<endl;
|
---|
83 | stream<<"</Shape>"<<endl;
|
---|
84 |
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | bool
|
---|
90 | X3dExporter::ExportRaySets(const vector<VssRayContainer> &rays,
|
---|
91 | const RgbColor &color)
|
---|
92 | {
|
---|
93 | vector<VssRayContainer>::const_iterator ri = rays.begin();
|
---|
94 |
|
---|
95 | stream<<"<Switch DEF=\"RAYS\" whichChoice=\"0\" >"<<endl;
|
---|
96 |
|
---|
97 | bool result = false;
|
---|
98 |
|
---|
99 | for (; ri != rays.end(); ri++) {
|
---|
100 | result = ExportRays(*ri, color);
|
---|
101 | if (!result) {
|
---|
102 | cerr<<"Error while exporting rays!\n";
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | stream<<"</Switch>"<<endl;
|
---|
108 |
|
---|
109 | stream<<"<TimeSensor DEF='TIMER' loop='true' startTime='0' stopTime='0' cycleInterval='5'>"
|
---|
110 | <<endl;
|
---|
111 | stream<<"</TimeSensor>"<<endl;
|
---|
112 |
|
---|
113 | stream<<"<IntegerSequencer DEF='SEQ'"<<endl;
|
---|
114 | stream<<"key='0.0 ";
|
---|
115 | int i;
|
---|
116 | int size = (int)rays.size() + 2;
|
---|
117 | for (i=1; i < size; i++) {
|
---|
118 | stream<<i/(float)(size-1)<<" ";
|
---|
119 | }
|
---|
120 | stream<<"'\n";
|
---|
121 |
|
---|
122 | stream<<"keyValue='";
|
---|
123 | for (i=0; i < size; i++) {
|
---|
124 | stream<<i<<" ";
|
---|
125 | }
|
---|
126 | stream<<"'>\n";
|
---|
127 |
|
---|
128 | stream<<"</IntegerSequencer>"<<endl;
|
---|
129 |
|
---|
130 | stream<<
|
---|
131 | "<ROUTE fromNode='TIMER' fromField='fraction_changed' toNode='SEQ' toField='set_fraction'>";
|
---|
132 | stream<<"</ROUTE>"<<endl;
|
---|
133 |
|
---|
134 | stream<<"<ROUTE fromNode='SEQ' fromField='value_changed' toNode='RAYS' toField='set_whichChoice'>";
|
---|
135 |
|
---|
136 | stream<<"</ROUTE>"<<endl;
|
---|
137 |
|
---|
138 | return result;
|
---|
139 | }
|
---|
140 |
|
---|
141 | bool
|
---|
142 | X3dExporter::ExportRays(const VssRayContainer &rays,
|
---|
143 | const RgbColor &color)
|
---|
144 | {
|
---|
145 | VssRayContainer::const_iterator ri = rays.begin();
|
---|
146 |
|
---|
147 | stream<<"<Shape>"<<endl;
|
---|
148 | stream<<"<Appearance>"<<endl;
|
---|
149 | stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
150 | "\" />"<<endl;
|
---|
151 | stream<<"</Appearance>"<<endl;
|
---|
152 |
|
---|
153 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
154 |
|
---|
155 | int index = 0;
|
---|
156 | for (; ri != rays.end(); ri++) {
|
---|
157 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
158 | index+=2;
|
---|
159 | }
|
---|
160 |
|
---|
161 | stream<<"\" >"<<endl;
|
---|
162 |
|
---|
163 | stream<<"<Coordinate point=\""<<endl;
|
---|
164 |
|
---|
165 | ri = rays.begin();
|
---|
166 | for (; ri != rays.end(); ri++) {
|
---|
167 | const Vector3 a = (*ri)->GetOrigin();
|
---|
168 | //const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
|
---|
169 | const Vector3 b = (*ri)->GetTermination(); // matt: change again!!
|
---|
170 |
|
---|
171 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
172 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
173 | }
|
---|
174 |
|
---|
175 | stream<<"\" >"<<endl;
|
---|
176 | stream<<"</Coordinate>"<<endl;
|
---|
177 | stream<<"</IndexedLineSet>"<<endl;
|
---|
178 | stream<<"</Shape>"<<endl;
|
---|
179 |
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void
|
---|
184 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
185 | {
|
---|
186 | stream<<"<Group>"<<endl;
|
---|
187 |
|
---|
188 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
189 | for (; ni != node->mChildren.end(); ni++)
|
---|
190 | ExportSceneNode(*ni);
|
---|
191 |
|
---|
192 |
|
---|
193 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
194 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
195 | // export the transform...
|
---|
196 | ExportIntersectable(*mi);
|
---|
197 | }
|
---|
198 |
|
---|
199 | stream<<"</Group>"<<endl;
|
---|
200 |
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | void
|
---|
205 | X3dExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
|
---|
206 | {
|
---|
207 | stack<pair<BspNode *, BspNodeGeometry *> > tStack;
|
---|
208 | ViewCell::NewMail();
|
---|
209 |
|
---|
210 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
211 | tree.ConstructGeometry(tree.GetRoot(), *geom);
|
---|
212 |
|
---|
213 | tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
|
---|
214 |
|
---|
215 | if (maxPvs)
|
---|
216 | mUseForcedMaterial = true;
|
---|
217 |
|
---|
218 | while (!tStack.empty())
|
---|
219 | {
|
---|
220 | BspNode *node = tStack.top().first;
|
---|
221 | BspNodeGeometry *cell = tStack.top().second;
|
---|
222 | tStack.pop();
|
---|
223 |
|
---|
224 | if (!node->IsLeaf())
|
---|
225 | {
|
---|
226 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
227 |
|
---|
228 | BspNodeGeometry *front = new BspNodeGeometry();
|
---|
229 | BspNodeGeometry *back = new BspNodeGeometry();
|
---|
230 |
|
---|
231 | cell->SplitGeometry(*front,
|
---|
232 | *back,
|
---|
233 | interior->GetPlane(),
|
---|
234 | tree.GetBoundingBox(),
|
---|
235 | tree.GetEpsilon());
|
---|
236 |
|
---|
237 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
|
---|
238 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetBack(), back));
|
---|
239 | }
|
---|
240 | else
|
---|
241 | {
|
---|
242 | if (maxPvs)
|
---|
243 | {
|
---|
244 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
245 |
|
---|
246 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
247 | const float importance =
|
---|
248 | (float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
|
---|
249 |
|
---|
250 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
251 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
252 | }
|
---|
253 | ExportPolygons(cell->GetPolys());
|
---|
254 | }
|
---|
255 | DEL_PTR(cell);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | void
|
---|
260 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
261 | {
|
---|
262 | if (viewCell->GetMesh())
|
---|
263 | {
|
---|
264 | ExportMesh(viewCell->GetMesh());
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | void
|
---|
269 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
270 | {
|
---|
271 |
|
---|
272 | stream<<"<Shape>"<<endl;
|
---|
273 | stream<<"<Appearance>"<<endl;
|
---|
274 |
|
---|
275 | // $$ tmp -> random material
|
---|
276 |
|
---|
277 | float r, g, b;
|
---|
278 |
|
---|
279 | if (mUseForcedMaterial) {
|
---|
280 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
281 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
282 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
283 |
|
---|
284 | } else
|
---|
285 | if (mesh->mMaterial) {
|
---|
286 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
287 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
288 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
289 | } else {
|
---|
290 | r = RandomValue(0.5, 1.0);
|
---|
291 | g = RandomValue(0.5, 1.0);
|
---|
292 | b = RandomValue(0.5, 1.0);
|
---|
293 | }
|
---|
294 |
|
---|
295 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
296 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
297 | stream<<"</Appearance>"<<endl;
|
---|
298 |
|
---|
299 |
|
---|
300 | if (mWireframe)
|
---|
301 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
302 | else
|
---|
303 | //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
304 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
305 |
|
---|
306 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
307 |
|
---|
308 | int index = 0;
|
---|
309 |
|
---|
310 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
311 | Face *face = *fi;
|
---|
312 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
313 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
314 | stream<<*vi<<" ";
|
---|
315 | if (mWireframe) // final line to finish polygon
|
---|
316 | stream << (*face->mVertexIndices.begin()) << " ";
|
---|
317 |
|
---|
318 | stream<<"-1"<<endl;
|
---|
319 | }
|
---|
320 | stream<<"\" >"<<endl;
|
---|
321 |
|
---|
322 | stream<<"<Coordinate point=\""<<endl;
|
---|
323 |
|
---|
324 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
325 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
326 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
327 | stream<<","<<endl;
|
---|
328 | }
|
---|
329 |
|
---|
330 | stream<<"\" >"<<endl;
|
---|
331 | stream<<"</Coordinate>"<<endl;
|
---|
332 |
|
---|
333 | if (mWireframe)
|
---|
334 | stream<<"</IndexedLineSet>"<<endl;
|
---|
335 | else
|
---|
336 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
337 |
|
---|
338 | stream<<"</Shape>"<<endl;
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
344 | {
|
---|
345 | stream << "<Shape>" << endl;
|
---|
346 | stream << "<Appearance>" << endl;
|
---|
347 |
|
---|
348 | // $$ tmp -> random material
|
---|
349 |
|
---|
350 | float r, g, b;
|
---|
351 |
|
---|
352 | if (mUseForcedMaterial)
|
---|
353 | {
|
---|
354 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
355 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
356 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
357 | }
|
---|
358 | else if (poly->mMaterial)
|
---|
359 | {
|
---|
360 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
361 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
362 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
363 | } else
|
---|
364 | {
|
---|
365 | r = RandomValue(0.5, 1.0);
|
---|
366 | g = RandomValue(0.5, 1.0);
|
---|
367 | b = RandomValue(0.5, 1.0);
|
---|
368 | }
|
---|
369 |
|
---|
370 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
371 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
372 |
|
---|
373 | stream << "</Appearance>" << endl;
|
---|
374 |
|
---|
375 |
|
---|
376 | //-- create and write indices
|
---|
377 | if (mWireframe)
|
---|
378 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
379 | else
|
---|
380 | //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
381 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
382 | int index = 0;
|
---|
383 |
|
---|
384 | VertexContainer::const_iterator vi;
|
---|
385 |
|
---|
386 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
387 | stream << index << " ";
|
---|
388 |
|
---|
389 | if (mWireframe) // final line to finish polygon
|
---|
390 | stream << "0 ";
|
---|
391 |
|
---|
392 | stream << "-1" << endl;
|
---|
393 | stream << "\" >" << endl;
|
---|
394 |
|
---|
395 | stream << "<Coordinate point=\"" << endl;
|
---|
396 |
|
---|
397 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
398 | {
|
---|
399 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
400 | stream << "," << endl;
|
---|
401 | }
|
---|
402 |
|
---|
403 | stream << "\" >" << endl;
|
---|
404 | stream << "</Coordinate>" << endl;
|
---|
405 |
|
---|
406 | if (mWireframe)
|
---|
407 | stream << "</IndexedLineSet>" << endl;
|
---|
408 | else
|
---|
409 | stream << "</IndexedFaceSet>" << endl;
|
---|
410 |
|
---|
411 | stream << "</Shape>" << endl;
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 |
|
---|
416 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
417 | {
|
---|
418 | stream << "<Shape>" << endl;
|
---|
419 | stream << "<Appearance>" << endl;
|
---|
420 |
|
---|
421 | // $$ tmp -> random material
|
---|
422 |
|
---|
423 | float r, g, b;
|
---|
424 |
|
---|
425 | if (mUseForcedMaterial)
|
---|
426 | {
|
---|
427 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
428 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
429 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | r = RandomValue(0.5, 1.0);
|
---|
434 | g = RandomValue(0.5, 1.0);
|
---|
435 | b = RandomValue(0.5, 1.0);
|
---|
436 | }
|
---|
437 |
|
---|
438 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
439 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
440 |
|
---|
441 | stream << "</Appearance>" << endl;
|
---|
442 |
|
---|
443 |
|
---|
444 | //-- create and write indices
|
---|
445 | if (mWireframe)
|
---|
446 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
447 | else
|
---|
448 | //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
449 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
450 |
|
---|
451 | int index = 0;
|
---|
452 |
|
---|
453 | PolygonContainer::const_iterator pit;
|
---|
454 |
|
---|
455 | VertexContainer::const_iterator vi;
|
---|
456 |
|
---|
457 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
458 | {
|
---|
459 | Polygon3 *poly = *pit;
|
---|
460 | int startIdx = index;
|
---|
461 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
462 | {
|
---|
463 | stream << index ++ << " ";
|
---|
464 | }
|
---|
465 |
|
---|
466 | stream << startIdx << " ";// finish line
|
---|
467 | stream << "-1" << endl;
|
---|
468 | }
|
---|
469 |
|
---|
470 | stream << "\" >" << endl;
|
---|
471 |
|
---|
472 | stream << "<Coordinate point=\"" << endl;
|
---|
473 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
474 | {
|
---|
475 | Polygon3 *poly = *pit;
|
---|
476 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
477 | {
|
---|
478 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
479 | stream << "," << endl;
|
---|
480 | }
|
---|
481 | }
|
---|
482 | stream << "\" >" << endl;
|
---|
483 | stream << "</Coordinate>" << endl;
|
---|
484 |
|
---|
485 | if (mWireframe)
|
---|
486 | stream << "</IndexedLineSet>" << endl;
|
---|
487 | else
|
---|
488 | stream << "</IndexedFaceSet>" << endl;
|
---|
489 |
|
---|
490 | stream << "</Shape>" << endl;
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | bool
|
---|
495 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
496 | {
|
---|
497 | Mesh *mesh = new Mesh;
|
---|
498 | // add 8 vertices of the box
|
---|
499 | int index = (int)mesh->mVertices.size();
|
---|
500 | for (int i=0; i < 8; i++) {
|
---|
501 | Vector3 v;
|
---|
502 | box.GetVertex(i, v);
|
---|
503 | mesh->mVertices.push_back(v);
|
---|
504 | }
|
---|
505 |
|
---|
506 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
507 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
508 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
509 |
|
---|
510 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
511 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
512 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
513 |
|
---|
514 | ExportMesh(mesh);
|
---|
515 | delete mesh;
|
---|
516 | return true;
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | bool
|
---|
521 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
522 | {
|
---|
523 | if (mExportRayDensity)
|
---|
524 | {
|
---|
525 | return ExportBspTreeRayDensity(tree);
|
---|
526 | }
|
---|
527 |
|
---|
528 | bool savedWireframe = mWireframe;
|
---|
529 |
|
---|
530 | SetWireframe();
|
---|
531 |
|
---|
532 | ExportBox(tree.GetBoundingBox());
|
---|
533 |
|
---|
534 | if (!savedWireframe)
|
---|
535 | SetFilled();
|
---|
536 |
|
---|
537 | // export view cells
|
---|
538 | ExportBspLeaves(tree);
|
---|
539 |
|
---|
540 | return true;
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | bool X3dExporter::ExportKdTree(const KdTree &tree,
|
---|
545 | const bool exportGeometry)
|
---|
546 | {
|
---|
547 | if (mExportRayDensity) {
|
---|
548 | return ExportKdTreeRayDensity(tree);
|
---|
549 | }
|
---|
550 |
|
---|
551 | stack<KdNode *> tStack;
|
---|
552 |
|
---|
553 | tStack.push(tree.GetRoot());
|
---|
554 |
|
---|
555 | Mesh *mesh = new Mesh;
|
---|
556 |
|
---|
557 | while (!tStack.empty()) {
|
---|
558 | KdNode *node = tStack.top();
|
---|
559 | tStack.pop();
|
---|
560 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
561 | // add 6 vertices of the box
|
---|
562 | int index = (int)mesh->mVertices.size();
|
---|
563 | for (int i=0; i < 8; i++) {
|
---|
564 | Vector3 v;
|
---|
565 | box.GetVertex(i, v);
|
---|
566 | mesh->mVertices.push_back(v);
|
---|
567 | }
|
---|
568 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
569 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
570 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
571 |
|
---|
572 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
573 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
574 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
575 |
|
---|
576 | if (!node->IsLeaf()) {
|
---|
577 | KdInterior *interior = (KdInterior *)node;
|
---|
578 | tStack.push(interior->mFront);
|
---|
579 | tStack.push(interior->mBack);
|
---|
580 | }
|
---|
581 | else if (exportGeometry)
|
---|
582 | {
|
---|
583 | SetFilled();
|
---|
584 | SetForcedMaterial(RandomMaterial());
|
---|
585 | ExportGeometry(dynamic_cast<KdLeaf *>(node)->mObjects);
|
---|
586 | SetWireframe();
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | ExportMesh(mesh);
|
---|
591 | delete mesh;
|
---|
592 |
|
---|
593 | return true;
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | bool
|
---|
598 | X3dExporter::ExportVssTree(const VssTree &tree
|
---|
599 | )
|
---|
600 | {
|
---|
601 | stack<VssTreeNode *> tStack;
|
---|
602 |
|
---|
603 | tStack.push(tree.GetRoot());
|
---|
604 |
|
---|
605 | Mesh *mesh = new Mesh;
|
---|
606 | VssRayContainer rays;
|
---|
607 |
|
---|
608 | while (!tStack.empty()) {
|
---|
609 |
|
---|
610 | VssTreeNode *node = tStack.top();
|
---|
611 | tStack.pop();
|
---|
612 |
|
---|
613 |
|
---|
614 | if (!node->IsLeaf()) {
|
---|
615 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
616 | tStack.push(interior->front);
|
---|
617 | tStack.push(interior->back);
|
---|
618 | } else {
|
---|
619 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
620 | AxisAlignedBox3 box;
|
---|
621 | box = tree.GetBBox(leaf);
|
---|
622 | IncludeBoxInMesh(box, *mesh);
|
---|
623 |
|
---|
624 | if (tree.ValidLeaf(leaf)) {
|
---|
625 |
|
---|
626 | Vector3 origin = box.Center();
|
---|
627 | box = tree.GetDirBBox(leaf);
|
---|
628 | VssRay *ray;
|
---|
629 |
|
---|
630 | const int indices[][2] = {{0,0}, {0,1}, {1,1}, {1,0}};
|
---|
631 | MeshInstance dummy(mesh);
|
---|
632 | for (int i=0; i < 4; i++) {
|
---|
633 | // Vector3 v = box.GetVertex(indices[i][0], indices[i][1], 0);
|
---|
634 | Vector3 v = box.Center();
|
---|
635 |
|
---|
636 | Vector3 direction = VssRay::GetDirection(v.x, v.y);
|
---|
637 | if (Magnitude(direction) > Limits::Small)
|
---|
638 | direction.Normalize();
|
---|
639 | else
|
---|
640 | direction = Vector3(0, 1, 0);
|
---|
641 | float k = 100.0f*leaf->GetImportance();
|
---|
642 | // get 4 corners of the ray directions
|
---|
643 |
|
---|
644 | ray = new VssRay(origin, origin + (direction*k), NULL, &dummy);
|
---|
645 | rays.push_back(ray);
|
---|
646 | }
|
---|
647 | }
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | ExportMesh(mesh);
|
---|
652 | ExportRays(rays);
|
---|
653 | CLEAR_CONTAINER(rays);
|
---|
654 | delete mesh;
|
---|
655 | return true;
|
---|
656 | }
|
---|
657 |
|
---|
658 | bool
|
---|
659 | X3dExporter::ExportVssTree2(const VssTree &tree,
|
---|
660 | const Vector3 direction
|
---|
661 | )
|
---|
662 | {
|
---|
663 | stack<VssTreeNode *> tStack;
|
---|
664 |
|
---|
665 |
|
---|
666 | mUseForcedMaterial = true;
|
---|
667 |
|
---|
668 | Vector3 dirParam;
|
---|
669 |
|
---|
670 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
671 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
672 |
|
---|
673 | float maxImportance = 0.0f;
|
---|
674 | tStack.push(tree.GetRoot());
|
---|
675 | while (!tStack.empty()) {
|
---|
676 |
|
---|
677 | VssTreeNode *node = tStack.top();
|
---|
678 | tStack.pop();
|
---|
679 |
|
---|
680 | if (!node->IsLeaf()) {
|
---|
681 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
682 | if (interior->axis < 3) {
|
---|
683 | tStack.push(interior->front);
|
---|
684 | tStack.push(interior->back);
|
---|
685 | } else {
|
---|
686 | if (dirParam[interior->axis-3] < interior->position)
|
---|
687 | tStack.push(interior->back);
|
---|
688 | else
|
---|
689 | tStack.push(interior->front);
|
---|
690 | }
|
---|
691 | } else {
|
---|
692 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
693 | if (tree.ValidLeaf(leaf)) {
|
---|
694 | float i = leaf->GetImportance();
|
---|
695 | if (i > maxImportance)
|
---|
696 | maxImportance = i;
|
---|
697 | }
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | tStack.push(tree.GetRoot());
|
---|
702 | while (!tStack.empty()) {
|
---|
703 |
|
---|
704 | VssTreeNode *node = tStack.top();
|
---|
705 | tStack.pop();
|
---|
706 |
|
---|
707 |
|
---|
708 | if (!node->IsLeaf()) {
|
---|
709 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
710 | if (interior->axis < 3) {
|
---|
711 | tStack.push(interior->front);
|
---|
712 | tStack.push(interior->back);
|
---|
713 | } else {
|
---|
714 | if (dirParam[interior->axis-3] < interior->position)
|
---|
715 | tStack.push(interior->back);
|
---|
716 | else
|
---|
717 | tStack.push(interior->front);
|
---|
718 | }
|
---|
719 | } else {
|
---|
720 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
721 | if (tree.ValidLeaf(leaf)) {
|
---|
722 | AxisAlignedBox3 box;
|
---|
723 | box = tree.GetBBox(leaf);
|
---|
724 | Mesh *mesh = new Mesh;
|
---|
725 | IncludeBoxInMesh(box, *mesh);
|
---|
726 |
|
---|
727 | // get 4 corners of the ray directions
|
---|
728 |
|
---|
729 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
730 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
731 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
732 |
|
---|
733 | ExportMesh(mesh);
|
---|
734 | delete mesh;
|
---|
735 | }
|
---|
736 | }
|
---|
737 | }
|
---|
738 |
|
---|
739 | mUseForcedMaterial = false;
|
---|
740 |
|
---|
741 | return true;
|
---|
742 | }
|
---|
743 |
|
---|
744 | bool
|
---|
745 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
746 | {
|
---|
747 | stack<BspNode *> tStack;
|
---|
748 |
|
---|
749 | tStack.push(tree.GetRoot());
|
---|
750 |
|
---|
751 | bool fm = mUseForcedMaterial;
|
---|
752 |
|
---|
753 | mUseForcedMaterial = true;
|
---|
754 |
|
---|
755 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
756 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
757 |
|
---|
758 | while (!tStack.empty())
|
---|
759 | {
|
---|
760 | BspNode *node = tStack.top();
|
---|
761 | tStack.pop();
|
---|
762 |
|
---|
763 | if (node->IsLeaf())
|
---|
764 | {
|
---|
765 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
766 | #if 0
|
---|
767 | // set the mesh material according to the ray density
|
---|
768 | if (vc->mPassingRays.mRays)
|
---|
769 | {
|
---|
770 | float importance =
|
---|
771 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
772 |
|
---|
773 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
774 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
775 | ExportViewCell(vc);
|
---|
776 |
|
---|
777 | }
|
---|
778 | #endif
|
---|
779 | }
|
---|
780 | else
|
---|
781 | {
|
---|
782 | BspInterior *interior = (BspInterior *)node;
|
---|
783 | tStack.push(interior->GetFront());
|
---|
784 | tStack.push(interior->GetBack());
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | // restore the state of forced material
|
---|
789 | mUseForcedMaterial = fm;
|
---|
790 |
|
---|
791 | return true;
|
---|
792 | }
|
---|
793 |
|
---|
794 | bool
|
---|
795 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
796 | {
|
---|
797 | stack<KdNode *> tStack;
|
---|
798 |
|
---|
799 | tStack.push(tree.GetRoot());
|
---|
800 |
|
---|
801 | bool fm = mUseForcedMaterial;
|
---|
802 | mUseForcedMaterial = true;
|
---|
803 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
804 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
805 | while (!tStack.empty()) {
|
---|
806 | KdNode *node = tStack.top();
|
---|
807 | tStack.pop();
|
---|
808 | if (node->IsLeaf()) {
|
---|
809 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
810 | Mesh *mesh = new Mesh;
|
---|
811 |
|
---|
812 | // add 6 vertices of the box
|
---|
813 | int index = (int)mesh->mVertices.size();
|
---|
814 | for (int i=0; i < 8; i++) {
|
---|
815 | Vector3 v;
|
---|
816 | box.GetVertex(i, v);
|
---|
817 | mesh->mVertices.push_back(v);
|
---|
818 | }
|
---|
819 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
820 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
821 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
822 |
|
---|
823 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
824 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
825 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
826 |
|
---|
827 |
|
---|
828 | // set the mesh material according to the ray density
|
---|
829 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
830 | if (leaf->mPassingRays.mRays) {
|
---|
831 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
832 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
833 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
834 | ///(float)leaf->mPassingRays.mRays;
|
---|
835 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
836 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
837 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
838 | ExportMesh(mesh);
|
---|
839 | }
|
---|
840 | delete mesh;
|
---|
841 | } else {
|
---|
842 | KdInterior *interior = (KdInterior *)node;
|
---|
843 | tStack.push(interior->mFront);
|
---|
844 | tStack.push(interior->mBack);
|
---|
845 | }
|
---|
846 | }
|
---|
847 | // restore the state of forced material
|
---|
848 | mUseForcedMaterial = fm;
|
---|
849 | return true;
|
---|
850 | }
|
---|
851 |
|
---|
852 |
|
---|
853 | struct BspSplitData
|
---|
854 | {
|
---|
855 | /// the current node
|
---|
856 | BspNode *mNode;
|
---|
857 |
|
---|
858 | vector<Plane3> mPlanes;
|
---|
859 | vector<bool> mSides;
|
---|
860 | bool mIsFront;
|
---|
861 | int mDepth;
|
---|
862 |
|
---|
863 | BspSplitData(BspNode *node):
|
---|
864 | mNode(node), mIsFront(false), mDepth(0)
|
---|
865 | {};
|
---|
866 |
|
---|
867 | BspSplitData(BspNode *node,
|
---|
868 | vector<Plane3> planes,
|
---|
869 | vector<bool> sides,
|
---|
870 | const bool isFront,
|
---|
871 | const int depth):
|
---|
872 | mNode(node), mPlanes(planes), mSides(sides),
|
---|
873 | mIsFront(isFront), mDepth(depth)
|
---|
874 | {};
|
---|
875 | };
|
---|
876 |
|
---|
877 | void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
|
---|
878 | const vector<BspLeaf *> &leaves)
|
---|
879 | {
|
---|
880 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
881 |
|
---|
882 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
883 | {
|
---|
884 | BspNodeGeometry geom;
|
---|
885 | tree.ConstructGeometry(*it, geom);
|
---|
886 |
|
---|
887 | ExportPolygons(geom.GetPolys());
|
---|
888 | }
|
---|
889 | }
|
---|
890 |
|
---|
891 | void X3dExporter::ExportBspNodeSplits(BspNode *root,
|
---|
892 | const AxisAlignedBox3 &box,
|
---|
893 | const bool exportDepth,
|
---|
894 | const bool epsilon)
|
---|
895 | {
|
---|
896 | std::stack<BspSplitData> tStack;
|
---|
897 |
|
---|
898 | BspSplitData tData(root);
|
---|
899 | tStack.push(tData);
|
---|
900 |
|
---|
901 | PolygonContainer polys;
|
---|
902 | vector <int> depths;
|
---|
903 |
|
---|
904 | int maxDepth = 0;
|
---|
905 |
|
---|
906 | while (!tStack.empty())
|
---|
907 | {
|
---|
908 | // filter polygons donw the tree
|
---|
909 | BspSplitData tData = tStack.top();
|
---|
910 | tStack.pop();
|
---|
911 |
|
---|
912 | if (tData.mNode->IsLeaf())
|
---|
913 | {
|
---|
914 | if (tData.mDepth > maxDepth)
|
---|
915 | maxDepth = tData.mDepth;
|
---|
916 | }
|
---|
917 | else
|
---|
918 | {
|
---|
919 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
920 |
|
---|
921 | // add current side of split plane
|
---|
922 | if (tData.mNode != root)
|
---|
923 | tData.mSides.push_back(tData.mIsFront);
|
---|
924 |
|
---|
925 | // bounded plane is added to the polygons
|
---|
926 | Polygon3 *planePoly =
|
---|
927 | box.CrossSection(interior->GetPlane());
|
---|
928 |
|
---|
929 | // do all the splits with the previous planes
|
---|
930 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
|
---|
931 | {
|
---|
932 | if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon)
|
---|
933 | == Polygon3::SPLIT)
|
---|
934 | {
|
---|
935 | Polygon3 *frontPoly = new Polygon3();
|
---|
936 | Polygon3 *backPoly = new Polygon3();
|
---|
937 |
|
---|
938 | planePoly->Split(tData.mPlanes[i],
|
---|
939 | *frontPoly,
|
---|
940 | *backPoly,
|
---|
941 | epsilon);
|
---|
942 |
|
---|
943 | DEL_PTR(planePoly);
|
---|
944 |
|
---|
945 | if(tData.mSides[i] == true)
|
---|
946 | {
|
---|
947 | planePoly = frontPoly;
|
---|
948 | DEL_PTR(backPoly);
|
---|
949 | }
|
---|
950 | else
|
---|
951 | {
|
---|
952 | planePoly = backPoly;
|
---|
953 | DEL_PTR(frontPoly);
|
---|
954 | }
|
---|
955 | }
|
---|
956 | }
|
---|
957 |
|
---|
958 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
|
---|
959 |
|
---|
960 | if (planePoly->Valid(epsilon))
|
---|
961 | {
|
---|
962 | polys.push_back(planePoly);
|
---|
963 | depths.push_back(tData.mDepth);
|
---|
964 | }
|
---|
965 | else
|
---|
966 | DEL_PTR(planePoly);
|
---|
967 |
|
---|
968 | // push the children on the stack
|
---|
969 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
|
---|
970 | tData.mSides, true, tData.mDepth + 1));
|
---|
971 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
|
---|
972 | tData.mSides, false, tData.mDepth + 1));
|
---|
973 | }
|
---|
974 | }
|
---|
975 |
|
---|
976 | if (maxDepth > 0)
|
---|
977 | {
|
---|
978 | mUseForcedMaterial = true;
|
---|
979 |
|
---|
980 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
981 | {
|
---|
982 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
983 | float importance = (float)depths[i]/ (float)maxDepth;
|
---|
984 |
|
---|
985 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
986 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
987 |
|
---|
988 | ExportPolygon(polys[i]);
|
---|
989 | }
|
---|
990 | }
|
---|
991 | else
|
---|
992 | {
|
---|
993 | ExportPolygons(polys);
|
---|
994 | }
|
---|
995 |
|
---|
996 | CLEAR_CONTAINER(polys);
|
---|
997 | }
|
---|
998 |
|
---|
999 | void X3dExporter::ExportBspSplits(const BspTree &tree,
|
---|
1000 | const bool exportDepth)
|
---|
1001 | {
|
---|
1002 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
1003 | tree.GetBoundingBox(),
|
---|
1004 | exportDepth,
|
---|
1005 | tree.GetEpsilon());
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | void X3dExporter::ExportBspSplits(const VspBspTree &tree,
|
---|
1009 | const bool exportDepth)
|
---|
1010 | {
|
---|
1011 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
1012 | tree.GetBoundingBox(),
|
---|
1013 | exportDepth,
|
---|
1014 | tree.GetEpsilon());
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
|
---|
1018 | {
|
---|
1019 | std::stack<BspNode *> tStack;
|
---|
1020 |
|
---|
1021 | tStack.push(tree.GetRoot());
|
---|
1022 |
|
---|
1023 | PolygonContainer polys;
|
---|
1024 |
|
---|
1025 | while (!tStack.empty())
|
---|
1026 | {
|
---|
1027 | // filter polygons donw the tree
|
---|
1028 | BspNode *node = tStack.top();
|
---|
1029 | tStack.pop();
|
---|
1030 |
|
---|
1031 | if (!node->IsLeaf())
|
---|
1032 | {
|
---|
1033 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1034 |
|
---|
1035 | // bounded plane is added to the polygons
|
---|
1036 | polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
|
---|
1037 |
|
---|
1038 | // push the children on the stack
|
---|
1039 | tStack.push(interior->GetBack());
|
---|
1040 | tStack.push(interior->GetFront());
|
---|
1041 | }
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | ExportPolygons(polys);
|
---|
1045 | CLEAR_CONTAINER(polys);
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 |
|
---|
1049 | void X3dExporter::ExportGeometry(const ObjectContainer &objects)
|
---|
1050 | {
|
---|
1051 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
1052 |
|
---|
1053 | if (1)
|
---|
1054 | {
|
---|
1055 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1056 | {
|
---|
1057 | if (0)
|
---|
1058 | SetForcedMaterial(RandomMaterial());
|
---|
1059 |
|
---|
1060 | ExportIntersectable(*oit);
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | return;
|
---|
1064 | }
|
---|
1065 |
|
---|
1066 | // hack: all object exported as one mesh
|
---|
1067 | PolygonContainer polys;
|
---|
1068 |
|
---|
1069 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
1070 | {
|
---|
1071 | polys.push_back(new Polygon3(dynamic_cast<MeshInstance *>(*oit)->GetMesh()->mVertices));
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | Mesh dummyMesh;
|
---|
1075 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
1076 |
|
---|
1077 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
1078 | {
|
---|
1079 | Polygon3 *poly = (*pit);
|
---|
1080 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | ExportMesh(&dummyMesh);
|
---|
1084 |
|
---|
1085 | CLEAR_CONTAINER(polys);
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 |
|
---|
1089 | bool
|
---|
1090 | X3dExporter::ExportRssTree2(const RssTree &tree,
|
---|
1091 | const Vector3 direction
|
---|
1092 | )
|
---|
1093 | {
|
---|
1094 | stack<RssTreeNode *> tStack;
|
---|
1095 |
|
---|
1096 |
|
---|
1097 | mUseForcedMaterial = true;
|
---|
1098 |
|
---|
1099 | Vector3 dirParam;
|
---|
1100 |
|
---|
1101 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
1102 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
1103 |
|
---|
1104 | float maxImportance = 0.0f;
|
---|
1105 | tStack.push(tree.GetRoot());
|
---|
1106 | while (!tStack.empty()) {
|
---|
1107 |
|
---|
1108 | RssTreeNode *node = tStack.top();
|
---|
1109 | tStack.pop();
|
---|
1110 |
|
---|
1111 | if (!node->IsLeaf()) {
|
---|
1112 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
1113 | if (interior->axis < 3) {
|
---|
1114 | tStack.push(interior->front);
|
---|
1115 | tStack.push(interior->back);
|
---|
1116 | } else {
|
---|
1117 | if (dirParam[interior->axis-3] < interior->position)
|
---|
1118 | tStack.push(interior->back);
|
---|
1119 | else
|
---|
1120 | tStack.push(interior->front);
|
---|
1121 | }
|
---|
1122 | } else {
|
---|
1123 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
1124 | if (tree.ValidLeaf(leaf)) {
|
---|
1125 | float i = leaf->GetImportance();
|
---|
1126 | if (i > maxImportance)
|
---|
1127 | maxImportance = i;
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | tStack.push(tree.GetRoot());
|
---|
1133 | while (!tStack.empty()) {
|
---|
1134 |
|
---|
1135 | RssTreeNode *node = tStack.top();
|
---|
1136 | tStack.pop();
|
---|
1137 |
|
---|
1138 |
|
---|
1139 | if (!node->IsLeaf()) {
|
---|
1140 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
1141 | if (interior->axis < 3) {
|
---|
1142 | tStack.push(interior->front);
|
---|
1143 | tStack.push(interior->back);
|
---|
1144 | } else {
|
---|
1145 | if (dirParam[interior->axis-3] < interior->position)
|
---|
1146 | tStack.push(interior->back);
|
---|
1147 | else
|
---|
1148 | tStack.push(interior->front);
|
---|
1149 | }
|
---|
1150 | } else {
|
---|
1151 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
1152 | if (tree.ValidLeaf(leaf)) {
|
---|
1153 | AxisAlignedBox3 box;
|
---|
1154 | box = tree.GetShrankedBBox(leaf);
|
---|
1155 | Mesh *mesh = new Mesh;
|
---|
1156 | IncludeBoxInMesh(box, *mesh);
|
---|
1157 |
|
---|
1158 | // get 4 corners of the ray directions
|
---|
1159 |
|
---|
1160 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
1161 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
1162 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
1163 |
|
---|
1164 | ExportMesh(mesh);
|
---|
1165 | delete mesh;
|
---|
1166 | }
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | mUseForcedMaterial = false;
|
---|
1171 |
|
---|
1172 | return true;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 |
|
---|
1176 | void
|
---|
1177 | X3dExporter::ExportViewpoint(const Vector3 &point,
|
---|
1178 | const Vector3 &direction)
|
---|
1179 | {
|
---|
1180 | stream<<"<Viewpoint "<<endl;
|
---|
1181 |
|
---|
1182 | stream<<"position=\""<<point.x<<" "<<point.y<<" "<<point.z<<"\""<<endl;
|
---|
1183 | // stream<<"orientation "<<direction.x<<direction.y<<direction.z<<endl;
|
---|
1184 |
|
---|
1185 | stream<<">"<<endl;
|
---|
1186 | stream<<"</Viewpoint>"<<endl;
|
---|
1187 |
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | void X3dExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box)
|
---|
1193 | {
|
---|
1194 | if (beam.mMesh)
|
---|
1195 | {
|
---|
1196 | ExportMesh(beam.mMesh);
|
---|
1197 | return;
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | PolygonContainer polys;
|
---|
1201 | //ExportBox(beam.mBox);
|
---|
1202 |
|
---|
1203 | const float zfar = 2.0f * Magnitude(box.Diagonal());
|
---|
1204 |
|
---|
1205 | // box should not never remove part of beam polygons
|
---|
1206 | Vector3 bmin = beam.mBox.Min() - Vector3(zfar * 2.0f);
|
---|
1207 | Vector3 bmax = beam.mBox.Max() + Vector3(zfar * 2.0f);
|
---|
1208 |
|
---|
1209 | AxisAlignedBox3 bbox(bmin, bmax);
|
---|
1210 | Plane3 fplane;
|
---|
1211 | fplane.mNormal = -beam.mPlanes[0].mNormal;
|
---|
1212 |
|
---|
1213 | fplane.mD = beam.mPlanes[0].mD - zfar - 1.0f;
|
---|
1214 |
|
---|
1215 | vector<Plane3> planes = beam.mPlanes;
|
---|
1216 | planes.push_back(fplane);
|
---|
1217 |
|
---|
1218 | for (int i = 0; i < planes.size(); ++ i)
|
---|
1219 | {
|
---|
1220 | Polygon3 *poly = bbox.CrossSection(planes[i]);
|
---|
1221 | if (!poly->Valid(Limits::Small))
|
---|
1222 | DEL_PTR(poly);
|
---|
1223 |
|
---|
1224 | for (int j = 0; (j < planes.size()) && poly; ++ j)
|
---|
1225 | {
|
---|
1226 | if (j != i)
|
---|
1227 | {
|
---|
1228 | Polygon3 *front = new Polygon3();
|
---|
1229 | Polygon3 *back = new Polygon3();
|
---|
1230 |
|
---|
1231 | poly->Split(planes[j], *front, *back, Limits::Small);
|
---|
1232 | DEL_PTR(poly);
|
---|
1233 | DEL_PTR(front);
|
---|
1234 |
|
---|
1235 | if (!back->Valid(Limits::Small))
|
---|
1236 | DEL_PTR(back);
|
---|
1237 | poly = back;
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | if (poly)
|
---|
1241 | polys.push_back(poly);
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | ExportPolygons(polys);
|
---|
1245 | CLEAR_CONTAINER(polys);
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 |
|
---|
1249 | }
|
---|
1250 |
|
---|