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 |
|
---|
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::VIEWCELL:
|
---|
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 exportet
|
---|
118 | // and use a reference to the that mesh instead
|
---|
119 | ExportMesh(object->GetMesh());
|
---|
120 | }
|
---|
121 |
|
---|
122 | void X3dExporter::ExportViewCells(ViewCellContainer *viewCells)
|
---|
123 | {
|
---|
124 | ViewCellContainer::iterator it, it_end = viewCells->end();
|
---|
125 |
|
---|
126 | for (it = viewCells->begin(); it != it_end; ++it)
|
---|
127 | ExportViewCell(*it);
|
---|
128 | }
|
---|
129 | void
|
---|
130 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
131 | {
|
---|
132 | if (viewCell->GetMesh())
|
---|
133 | ExportMesh(viewCell->GetMesh());
|
---|
134 | }
|
---|
135 |
|
---|
136 | void
|
---|
137 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
138 | {
|
---|
139 |
|
---|
140 | stream<<"<Shape>"<<endl;
|
---|
141 | stream<<"<Appearance>"<<endl;
|
---|
142 |
|
---|
143 | // $$ tmp -> random material
|
---|
144 |
|
---|
145 | float r, g, b;
|
---|
146 |
|
---|
147 | if (mUseForcedMaterial) {
|
---|
148 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
149 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
150 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
151 |
|
---|
152 | } else
|
---|
153 | if (mesh->mMaterial) {
|
---|
154 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
155 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
156 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
157 | } else {
|
---|
158 | r = RandomValue(0.5, 1.0);
|
---|
159 | g = RandomValue(0.5, 1.0);
|
---|
160 | b = RandomValue(0.5, 1.0);
|
---|
161 | }
|
---|
162 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
163 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
164 | stream<<"</Appearance>"<<endl;
|
---|
165 |
|
---|
166 |
|
---|
167 | if (mWireframe)
|
---|
168 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
169 | else
|
---|
170 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
171 |
|
---|
172 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
173 |
|
---|
174 | int index = 0;
|
---|
175 |
|
---|
176 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
177 | Face *face = *fi;
|
---|
178 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
179 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
180 | stream<<*vi<<" ";
|
---|
181 | stream<<"-1"<<endl;
|
---|
182 | }
|
---|
183 | stream<<"\" >"<<endl;
|
---|
184 |
|
---|
185 | stream<<"<Coordinate point=\""<<endl;
|
---|
186 |
|
---|
187 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
188 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
189 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
190 | stream<<","<<endl;
|
---|
191 | }
|
---|
192 |
|
---|
193 | stream<<"\" >"<<endl;
|
---|
194 | stream<<"</Coordinate>"<<endl;
|
---|
195 |
|
---|
196 | if (mWireframe)
|
---|
197 | stream<<"</IndexedLineSet>"<<endl;
|
---|
198 | else
|
---|
199 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
200 |
|
---|
201 | stream<<"</Shape>"<<endl;
|
---|
202 |
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
207 | {
|
---|
208 | stream << "<Shape>" << endl;
|
---|
209 | stream << "<Appearance>" << endl;
|
---|
210 |
|
---|
211 | // $$ tmp -> random material
|
---|
212 |
|
---|
213 | float r, g, b;
|
---|
214 |
|
---|
215 | if (mUseForcedMaterial)
|
---|
216 | {
|
---|
217 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
218 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
219 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
220 | }
|
---|
221 | else if (poly->mMaterial)
|
---|
222 | {
|
---|
223 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
224 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
225 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
226 | } else
|
---|
227 | {
|
---|
228 | r = RandomValue(0.5, 1.0);
|
---|
229 | g = RandomValue(0.5, 1.0);
|
---|
230 | b = RandomValue(0.5, 1.0);
|
---|
231 | }
|
---|
232 |
|
---|
233 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
234 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
235 |
|
---|
236 | stream << "</Appearance>" << endl;
|
---|
237 |
|
---|
238 |
|
---|
239 | //-- create and write indices
|
---|
240 | if (mWireframe)
|
---|
241 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
242 | else
|
---|
243 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
244 |
|
---|
245 | int index = 0;
|
---|
246 |
|
---|
247 | VertexContainer::const_iterator vi;
|
---|
248 |
|
---|
249 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
250 | {
|
---|
251 | stream << index ++ << " ";
|
---|
252 | }
|
---|
253 | stream << "-1" << endl;
|
---|
254 |
|
---|
255 | stream << "\" >" << endl;
|
---|
256 |
|
---|
257 | stream << "<Coordinate point=\"" << endl;
|
---|
258 |
|
---|
259 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
260 | {
|
---|
261 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
262 | stream << "," << endl;
|
---|
263 | }
|
---|
264 |
|
---|
265 | stream << "\" >" << endl;
|
---|
266 | stream << "</Coordinate>" << endl;
|
---|
267 |
|
---|
268 | if (mWireframe)
|
---|
269 | stream << "</IndexedLineSet>" << endl;
|
---|
270 | else
|
---|
271 | stream << "</IndexedFaceSet>" << endl;
|
---|
272 |
|
---|
273 | stream << "</Shape>" << endl;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | bool
|
---|
278 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
279 | {
|
---|
280 | Mesh *mesh = new Mesh;
|
---|
281 | // add 6 vertices of the box
|
---|
282 | int index = mesh->mVertices.size();
|
---|
283 | for (int i=0; i < 8; i++) {
|
---|
284 | Vector3 v;
|
---|
285 | box.GetVertex(i, v);
|
---|
286 | mesh->mVertices.push_back(v);
|
---|
287 | }
|
---|
288 |
|
---|
289 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
290 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
291 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
292 |
|
---|
293 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
294 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
295 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
296 |
|
---|
297 | ExportMesh(mesh);
|
---|
298 | delete mesh;
|
---|
299 | return true;
|
---|
300 | }
|
---|
301 |
|
---|
302 | bool
|
---|
303 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
304 | {
|
---|
305 | if (mExportRayDensity)
|
---|
306 | {
|
---|
307 | return false;//ExportKdTreeRayDensity(tree);
|
---|
308 | }
|
---|
309 |
|
---|
310 | stack<BspNode *> tStack;
|
---|
311 |
|
---|
312 | tStack.push(tree.GetRoot());
|
---|
313 |
|
---|
314 | Mesh *mesh = new Mesh;
|
---|
315 |
|
---|
316 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
317 | ExportBox(box);
|
---|
318 |
|
---|
319 | while (!tStack.empty())
|
---|
320 | {
|
---|
321 | BspNode *node = tStack.top();
|
---|
322 |
|
---|
323 | tStack.pop();
|
---|
324 |
|
---|
325 | if (tree.StorePolys()) // extract the polygons
|
---|
326 | {
|
---|
327 | PolygonContainer::const_iterator it;
|
---|
328 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
329 |
|
---|
330 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
331 | ExportPolygon(*it);
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (!node->IsLeaf())
|
---|
335 | {
|
---|
336 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
337 |
|
---|
338 | tStack.push(interior->GetFront());
|
---|
339 | tStack.push(interior->GetBack());
|
---|
340 |
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
345 |
|
---|
346 | // export view cell geometry
|
---|
347 |
|
---|
348 | if (!tree.StorePolys() && leaf->GetViewCell())
|
---|
349 | {
|
---|
350 | ExportViewCell(leaf->GetViewCell());
|
---|
351 | }
|
---|
352 |
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | return true;
|
---|
357 | }
|
---|
358 |
|
---|
359 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
360 | {
|
---|
361 | if (mExportRayDensity) {
|
---|
362 | return ExportKdTreeRayDensity(tree);
|
---|
363 | }
|
---|
364 |
|
---|
365 | stack<KdNode *> tStack;
|
---|
366 |
|
---|
367 | tStack.push(tree.GetRoot());
|
---|
368 |
|
---|
369 | Mesh *mesh = new Mesh;
|
---|
370 |
|
---|
371 | while (!tStack.empty()) {
|
---|
372 | KdNode *node = tStack.top();
|
---|
373 | tStack.pop();
|
---|
374 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
375 | // add 6 vertices of the box
|
---|
376 | int index = mesh->mVertices.size();
|
---|
377 | for (int i=0; i < 8; i++) {
|
---|
378 | Vector3 v;
|
---|
379 | box.GetVertex(i, v);
|
---|
380 | mesh->mVertices.push_back(v);
|
---|
381 | }
|
---|
382 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
383 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
384 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
385 |
|
---|
386 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
387 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
388 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
389 |
|
---|
390 | if (!node->IsLeaf()) {
|
---|
391 | KdInterior *interior = (KdInterior *)node;
|
---|
392 | tStack.push(interior->mFront);
|
---|
393 | tStack.push(interior->mBack);
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | ExportMesh(mesh);
|
---|
398 | delete mesh;
|
---|
399 | return true;
|
---|
400 | // TODO
|
---|
401 | return true;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | bool
|
---|
406 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
407 | {
|
---|
408 | stack<KdNode *> tStack;
|
---|
409 |
|
---|
410 | tStack.push(tree.GetRoot());
|
---|
411 |
|
---|
412 | bool fm = mUseForcedMaterial;
|
---|
413 | mUseForcedMaterial = true;
|
---|
414 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
415 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
416 | while (!tStack.empty()) {
|
---|
417 | KdNode *node = tStack.top();
|
---|
418 | tStack.pop();
|
---|
419 | if (node->IsLeaf()) {
|
---|
420 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
421 | Mesh *mesh = new Mesh;
|
---|
422 |
|
---|
423 | // add 6 vertices of the box
|
---|
424 | int index = mesh->mVertices.size();
|
---|
425 | for (int i=0; i < 8; i++) {
|
---|
426 | Vector3 v;
|
---|
427 | box.GetVertex(i, v);
|
---|
428 | mesh->mVertices.push_back(v);
|
---|
429 | }
|
---|
430 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
431 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
432 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
433 |
|
---|
434 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
435 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
436 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
437 |
|
---|
438 |
|
---|
439 | // set the mesh material according to the ray density
|
---|
440 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
441 | if (leaf->mPassingRays.mRays) {
|
---|
442 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
443 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
444 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
445 | ///(float)leaf->mPassingRays.mRays;
|
---|
446 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
447 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
448 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
449 | ExportMesh(mesh);
|
---|
450 | }
|
---|
451 | delete mesh;
|
---|
452 | } else {
|
---|
453 | KdInterior *interior = (KdInterior *)node;
|
---|
454 | tStack.push(interior->mFront);
|
---|
455 | tStack.push(interior->mBack);
|
---|
456 | }
|
---|
457 | }
|
---|
458 | // restore the state of forced material
|
---|
459 | mUseForcedMaterial = fm;
|
---|
460 | return true;
|
---|
461 | }
|
---|