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