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 | if (mWireframe)
|
---|
240 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
241 | else
|
---|
242 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
243 |
|
---|
244 | VertexContainer::const_iterator vi = poly->mVertices.begin();
|
---|
245 |
|
---|
246 | stream << "<Coordinate point=\"" << endl;
|
---|
247 |
|
---|
248 | for (; vi != poly->mVertices.end(); ++vi)
|
---|
249 | {
|
---|
250 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
251 | stream << "," << endl;
|
---|
252 | }
|
---|
253 |
|
---|
254 | stream<<"\" >"<<endl;
|
---|
255 | stream<<"</Coordinate>"<<endl;
|
---|
256 |
|
---|
257 | if (mWireframe)
|
---|
258 | stream << "</IndexedLineSet>" << endl;
|
---|
259 | else
|
---|
260 | stream << "</IndexedFaceSet>" << endl;
|
---|
261 |
|
---|
262 | stream << "</Shape>" << endl;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | bool
|
---|
267 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
268 | {
|
---|
269 | Mesh *mesh = new Mesh;
|
---|
270 | // add 6 vertices of the box
|
---|
271 | int index = mesh->mVertices.size();
|
---|
272 | for (int i=0; i < 8; i++) {
|
---|
273 | Vector3 v;
|
---|
274 | box.GetVertex(i, v);
|
---|
275 | mesh->mVertices.push_back(v);
|
---|
276 | }
|
---|
277 |
|
---|
278 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
279 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
280 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
281 |
|
---|
282 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
283 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
284 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
285 |
|
---|
286 | ExportMesh(mesh);
|
---|
287 | delete mesh;
|
---|
288 | return true;
|
---|
289 | }
|
---|
290 |
|
---|
291 | bool
|
---|
292 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
293 | {
|
---|
294 | if (mExportRayDensity)
|
---|
295 | {
|
---|
296 | return false;//ExportKdTreeRayDensity(tree);
|
---|
297 | }
|
---|
298 |
|
---|
299 | stack<BspNode *> tStack;
|
---|
300 |
|
---|
301 | tStack.push(tree.GetRoot());
|
---|
302 |
|
---|
303 | Mesh *mesh = new Mesh;
|
---|
304 |
|
---|
305 | //AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
306 | //ExportBox(box);
|
---|
307 |
|
---|
308 | while (!tStack.empty())
|
---|
309 | {
|
---|
310 | BspNode *node = tStack.top();
|
---|
311 |
|
---|
312 | tStack.pop();
|
---|
313 |
|
---|
314 | if (tree.StorePolys()) // extract the polygons
|
---|
315 | {
|
---|
316 | PolygonContainer::const_iterator it;
|
---|
317 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
318 |
|
---|
319 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
320 | ExportPolygon(*it);
|
---|
321 | }
|
---|
322 |
|
---|
323 | if (!node->IsLeaf())
|
---|
324 | {
|
---|
325 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
326 |
|
---|
327 | tStack.push(interior->GetFront());
|
---|
328 | tStack.push(interior->GetBack());
|
---|
329 |
|
---|
330 | }
|
---|
331 | else
|
---|
332 | {
|
---|
333 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
334 | // export view cell geometry
|
---|
335 | if (!tree.StorePolys() && leaf->GetViewCell())
|
---|
336 | ExportViewCell(leaf->GetViewCell());
|
---|
337 | }
|
---|
338 | }
|
---|
339 |
|
---|
340 | return true;
|
---|
341 | }
|
---|
342 |
|
---|
343 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
344 | {
|
---|
345 | if (mExportRayDensity) {
|
---|
346 | return ExportKdTreeRayDensity(tree);
|
---|
347 | }
|
---|
348 |
|
---|
349 | stack<KdNode *> tStack;
|
---|
350 |
|
---|
351 | tStack.push(tree.GetRoot());
|
---|
352 |
|
---|
353 | Mesh *mesh = new Mesh;
|
---|
354 |
|
---|
355 | while (!tStack.empty()) {
|
---|
356 | KdNode *node = tStack.top();
|
---|
357 | tStack.pop();
|
---|
358 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
359 | // add 6 vertices of the box
|
---|
360 | int index = 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 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
367 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
368 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
369 |
|
---|
370 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
371 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
372 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
373 |
|
---|
374 | if (!node->IsLeaf()) {
|
---|
375 | KdInterior *interior = (KdInterior *)node;
|
---|
376 | tStack.push(interior->mFront);
|
---|
377 | tStack.push(interior->mBack);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | ExportMesh(mesh);
|
---|
382 | delete mesh;
|
---|
383 | return true;
|
---|
384 | // TODO
|
---|
385 | return true;
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | bool
|
---|
390 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
391 | {
|
---|
392 | stack<KdNode *> tStack;
|
---|
393 |
|
---|
394 | tStack.push(tree.GetRoot());
|
---|
395 |
|
---|
396 | bool fm = mUseForcedMaterial;
|
---|
397 | mUseForcedMaterial = true;
|
---|
398 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
399 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
400 | while (!tStack.empty()) {
|
---|
401 | KdNode *node = tStack.top();
|
---|
402 | tStack.pop();
|
---|
403 | if (node->IsLeaf()) {
|
---|
404 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
405 | Mesh *mesh = new Mesh;
|
---|
406 |
|
---|
407 | // add 6 vertices of the box
|
---|
408 | int index = mesh->mVertices.size();
|
---|
409 | for (int i=0; i < 8; i++) {
|
---|
410 | Vector3 v;
|
---|
411 | box.GetVertex(i, v);
|
---|
412 | mesh->mVertices.push_back(v);
|
---|
413 | }
|
---|
414 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
415 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
416 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
417 |
|
---|
418 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
419 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
420 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
421 |
|
---|
422 |
|
---|
423 | // set the mesh material according to the ray density
|
---|
424 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
425 | if (leaf->mPassingRays.mRays) {
|
---|
426 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
427 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
428 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
429 | ///(float)leaf->mPassingRays.mRays;
|
---|
430 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
431 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
432 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
433 | ExportMesh(mesh);
|
---|
434 | }
|
---|
435 | delete mesh;
|
---|
436 | } else {
|
---|
437 | KdInterior *interior = (KdInterior *)node;
|
---|
438 | tStack.push(interior->mFront);
|
---|
439 | tStack.push(interior->mBack);
|
---|
440 | }
|
---|
441 | }
|
---|
442 | // restore the state of forced material
|
---|
443 | mUseForcedMaterial = fm;
|
---|
444 | return true;
|
---|
445 | }
|
---|