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