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