1 | #include "X3dExporter.h"
|
---|
2 | #include "VrmlExporter.h"
|
---|
3 | #include "OspTree.h"
|
---|
4 | #include "KdTree.h"
|
---|
5 | #include "IntersectableWrapper.h"
|
---|
6 | #include "BvHierarchy.h"
|
---|
7 | #include "Triangle3.h"
|
---|
8 | #include "Polygon3.h"
|
---|
9 | #include "TraversalTree.h"
|
---|
10 | #include "Material.h"
|
---|
11 |
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | namespace GtpVisibilityPreprocessor {
|
---|
16 |
|
---|
17 | Exporter *
|
---|
18 | Exporter::GetExporter(const string filename)
|
---|
19 | {
|
---|
20 | Exporter *exporter = NULL;
|
---|
21 |
|
---|
22 | if (strstr(filename.c_str(), ".x3d"))
|
---|
23 | {
|
---|
24 | exporter = new X3dExporter(filename);
|
---|
25 | }
|
---|
26 | else if (strstr(filename.c_str(), ".wrl"))
|
---|
27 | {
|
---|
28 | exporter = new VrmlExporter(filename);
|
---|
29 | }
|
---|
30 | else
|
---|
31 | {
|
---|
32 | cerr<<"Error: Currently unsuported export format, filename " << filename << endl;
|
---|
33 | }
|
---|
34 |
|
---|
35 | return exporter;
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | bool Exporter::ExportOspTree(const OspTree &ospTree,
|
---|
40 | const int maxPvs
|
---|
41 | )
|
---|
42 | {
|
---|
43 | vector<KdLeaf *> leaves;
|
---|
44 | ospTree.CollectLeaves(leaves);
|
---|
45 | mUseForcedMaterial = true;
|
---|
46 |
|
---|
47 | vector<KdLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
48 |
|
---|
49 | Material white;
|
---|
50 | white.mDiffuseColor.r = 1;
|
---|
51 | white.mDiffuseColor.g = 1;
|
---|
52 | white.mDiffuseColor.b = 1;
|
---|
53 |
|
---|
54 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
55 | {
|
---|
56 | KdLeaf *leaf = *it;
|
---|
57 |
|
---|
58 | SetWireframe();
|
---|
59 | SetForcedMaterial(white);
|
---|
60 | ExportBox(ospTree.GetBoundingBox(leaf));
|
---|
61 |
|
---|
62 | SetFilled();
|
---|
63 |
|
---|
64 | if (maxPvs) // color code pvs
|
---|
65 | {
|
---|
66 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
67 | const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
|
---|
68 |
|
---|
69 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
70 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
71 | }
|
---|
72 | else
|
---|
73 | {
|
---|
74 | SetForcedMaterial(RandomMaterial());
|
---|
75 | }
|
---|
76 | if (0) ExportGeometry(leaf->mObjects);
|
---|
77 | }
|
---|
78 |
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void Exporter::ExportKdIntersectable(const KdIntersectable &kdObj)
|
---|
84 | {
|
---|
85 | KdNode *root = kdObj.GetItem();
|
---|
86 | Intersectable::NewMail();
|
---|
87 |
|
---|
88 | stack<KdNode *> tStack;
|
---|
89 |
|
---|
90 | tStack.push(root);
|
---|
91 |
|
---|
92 | while (!tStack.empty()) {
|
---|
93 | KdNode *node = tStack.top();
|
---|
94 | tStack.pop();
|
---|
95 | // todo: traverse to leaves
|
---|
96 | if (node->IsLeaf())
|
---|
97 | {
|
---|
98 | // eyport leaf pvs
|
---|
99 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
100 |
|
---|
101 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
102 |
|
---|
103 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
104 | {
|
---|
105 | Intersectable *obj = *oit;
|
---|
106 |
|
---|
107 | if (1 || !obj->Mailed())
|
---|
108 | {
|
---|
109 | ExportIntersectable(obj);
|
---|
110 | obj->Mail();
|
---|
111 | }
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | KdInterior *interior = (KdInterior *)node;
|
---|
115 | tStack.push(interior->mBack);
|
---|
116 | tStack.push(interior->mFront);
|
---|
117 |
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | bool Exporter::ExportBvHierarchy(const BvHierarchy &bvHierarchy,
|
---|
125 | const float maxPvs,
|
---|
126 | AxisAlignedBox3 *box,
|
---|
127 | const bool exportBoundingBoxes)
|
---|
128 | {
|
---|
129 | vector<BvhLeaf *> leaves;
|
---|
130 | bvHierarchy.CollectLeaves(bvHierarchy.GetRoot(), leaves);
|
---|
131 |
|
---|
132 | mUseForcedMaterial = true;
|
---|
133 | vector<BvhLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
134 |
|
---|
135 | Material white;
|
---|
136 | white.mDiffuseColor.r = 1;
|
---|
137 | white.mDiffuseColor.g = 1;
|
---|
138 | white.mDiffuseColor.b = 1;
|
---|
139 |
|
---|
140 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
141 | {
|
---|
142 | BvhLeaf *leaf = *it;
|
---|
143 |
|
---|
144 | if (leaf->mObjects.empty() ||
|
---|
145 | (box && !Overlap(*box, leaf->GetBoundingBox())))
|
---|
146 | continue;
|
---|
147 |
|
---|
148 | if (exportBoundingBoxes)
|
---|
149 | {
|
---|
150 | SetWireframe();
|
---|
151 | SetForcedMaterial(white);
|
---|
152 | ExportBox(leaf->GetBoundingBox());
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (maxPvs > 0) // color code pvs
|
---|
156 | {
|
---|
157 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
158 | const float importance = (float)leaf->mRenderCost / (float)maxPvs;
|
---|
159 |
|
---|
160 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
161 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | SetForcedMaterial(RandomMaterial());
|
---|
166 | }
|
---|
167 |
|
---|
168 | SetFilled();
|
---|
169 |
|
---|
170 | ExportGeometry(leaf->mObjects, true, box);
|
---|
171 | }
|
---|
172 |
|
---|
173 | // reset to filled
|
---|
174 | SetFilled();
|
---|
175 |
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void Exporter::ExportIntersectable(Intersectable *object)
|
---|
181 | {
|
---|
182 | switch (object->Type())
|
---|
183 | {
|
---|
184 | case Intersectable::MESH_INSTANCE:
|
---|
185 | ExportMeshInstance((MeshInstance *)object);
|
---|
186 | break;
|
---|
187 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
188 | ExportTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
189 | break;
|
---|
190 | case Intersectable::VIEW_CELL:
|
---|
191 | ExportViewCell(static_cast<ViewCell *>(object));
|
---|
192 | break;
|
---|
193 | case Intersectable::KD_INTERSECTABLE:
|
---|
194 | ExportKdIntersectable(*(static_cast<KdIntersectable *>(object)));
|
---|
195 | break;
|
---|
196 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
197 | {
|
---|
198 | // if (mClampToBox && !Overlap(mBoundingBox, object->GetBox())) return;
|
---|
199 | const Triangle3 triangle = static_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
200 |
|
---|
201 | Polygon3 poly(triangle);
|
---|
202 | ExportPolygon(&poly);
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | case Intersectable::BVH_INTERSECTABLE:
|
---|
206 | {
|
---|
207 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
208 |
|
---|
209 | if (node->IsLeaf())
|
---|
210 | {
|
---|
211 | ExportGeometry(static_cast<BvhLeaf *>(node)->mObjects, true);
|
---|
212 | }
|
---|
213 |
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | default:
|
---|
217 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
218 | break;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | void Exporter::ExportMeshInstance(MeshInstance *object)
|
---|
224 | {
|
---|
225 | // $$JB$$
|
---|
226 | // in the future check whether the mesh was not already exported
|
---|
227 | // and use a reference to the that mesh instead
|
---|
228 | ExportMesh(object->GetMesh());
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | void Exporter::ExportTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
233 | {
|
---|
234 | Mesh mesh(*mi->GetMesh());
|
---|
235 |
|
---|
236 | Matrix4x4 m;
|
---|
237 | mi->GetWorldTransform(m);
|
---|
238 | mesh.ApplyTransformation(m);
|
---|
239 |
|
---|
240 | ExportMesh(&mesh);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | void Exporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
245 | {
|
---|
246 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
247 |
|
---|
248 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
249 | {
|
---|
250 | ExportViewCell(*it);
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
256 | const bool exportSingleMesh,
|
---|
257 | AxisAlignedBox3 *bbox)
|
---|
258 | {
|
---|
259 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
260 |
|
---|
261 | if (!exportSingleMesh)
|
---|
262 | {
|
---|
263 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
264 | {
|
---|
265 | if (!bbox || Overlap(*bbox, (*oit)->GetBox()))
|
---|
266 | {
|
---|
267 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
268 | ExportIntersectable(*oit);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | return;
|
---|
272 | }
|
---|
273 |
|
---|
274 | ///////////////////////
|
---|
275 | //-- all objects exported as one mesh
|
---|
276 | //-- warning: currently works only for triangles
|
---|
277 |
|
---|
278 | PolygonContainer polys;
|
---|
279 |
|
---|
280 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
281 | {
|
---|
282 | Intersectable *obj = *oit;
|
---|
283 |
|
---|
284 | if (bbox && !Overlap(*bbox, obj->GetBox()))
|
---|
285 | {
|
---|
286 | continue;
|
---|
287 | }
|
---|
288 | switch (obj->Type())
|
---|
289 | {
|
---|
290 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
291 | {
|
---|
292 | TriangleIntersectable *ti = static_cast<TriangleIntersectable *>(obj);
|
---|
293 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
294 | break;
|
---|
295 | }
|
---|
296 | case Intersectable::MESH_INSTANCE:
|
---|
297 | {
|
---|
298 | MeshInstance *mi = static_cast<MeshInstance *>(obj);
|
---|
299 | ExportMesh(mi->GetMesh());
|
---|
300 | break;
|
---|
301 | }
|
---|
302 | default:
|
---|
303 | cout << "merge of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
304 | break;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | Mesh dummyMesh;
|
---|
309 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
310 |
|
---|
311 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
312 | {
|
---|
313 | Polygon3 *poly = (*pit);
|
---|
314 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
315 | }
|
---|
316 |
|
---|
317 | ExportMesh(&dummyMesh);
|
---|
318 | CLEAR_CONTAINER(polys);
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | bool Exporter::ExportTraversalTree(const TraversalTree &tree,
|
---|
323 | const bool exportGeometry)
|
---|
324 | {
|
---|
325 | stack<TraversalNode *> tStack;
|
---|
326 |
|
---|
327 | tStack.push(tree.GetRoot());
|
---|
328 |
|
---|
329 | Mesh *mesh = new Mesh;
|
---|
330 |
|
---|
331 | SetWireframe();
|
---|
332 |
|
---|
333 | while (!tStack.empty())
|
---|
334 | {
|
---|
335 | TraversalNode *node = tStack.top();
|
---|
336 | tStack.pop();
|
---|
337 | const AxisAlignedBox3 box = tree.GetBox(node);
|
---|
338 |
|
---|
339 | // add 6 vertices of the box
|
---|
340 | const int index = (int)mesh->mVertices.size();
|
---|
341 |
|
---|
342 | SetForcedMaterial(RandomMaterial());
|
---|
343 |
|
---|
344 | for (int i=0; i < 8; i++)
|
---|
345 | {
|
---|
346 | Vector3 v;
|
---|
347 | box.GetVertex(i, v);
|
---|
348 | mesh->mVertices.push_back(v);
|
---|
349 | }
|
---|
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 | {
|
---|
361 | TraversalInterior *interior = static_cast<TraversalInterior *>(node);
|
---|
362 |
|
---|
363 | tStack.push(interior->mFront);
|
---|
364 | tStack.push(interior->mBack);
|
---|
365 | }
|
---|
366 | else if (exportGeometry)
|
---|
367 | {
|
---|
368 | SetFilled();
|
---|
369 | SetForcedMaterial(RandomMaterial());
|
---|
370 |
|
---|
371 | ExportViewCells(static_cast<TraversalLeaf *>(node)->mViewCells);
|
---|
372 | SetWireframe();
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | ExportMesh(mesh);
|
---|
377 | delete mesh;
|
---|
378 |
|
---|
379 | return true;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | }
|
---|