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 | ExportGeometry(leaf->mObjects, true, box);
|
---|
156 | }
|
---|
157 |
|
---|
158 | // reset to filled
|
---|
159 | SetFilled();
|
---|
160 |
|
---|
161 | return true;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | void Exporter::ExportIntersectable(Intersectable *object)
|
---|
166 | {
|
---|
167 | switch (object->Type())
|
---|
168 | {
|
---|
169 | case Intersectable::MESH_INSTANCE:
|
---|
170 | ExportMeshInstance((MeshInstance *)object);
|
---|
171 | break;
|
---|
172 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
173 | ExportTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
174 | break;
|
---|
175 | case Intersectable::VIEW_CELL:
|
---|
176 | ExportViewCell(static_cast<ViewCell *>(object));
|
---|
177 | break;
|
---|
178 | case Intersectable::KD_INTERSECTABLE:
|
---|
179 | ExportKdIntersectable(*(static_cast<KdIntersectable *>(object)));
|
---|
180 | break;
|
---|
181 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
182 | {
|
---|
183 | // if (mClampToBox && !Overlap(mBoundingBox, object->GetBox())) return;
|
---|
184 | const Triangle3 triangle = static_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
185 |
|
---|
186 | Polygon3 poly(triangle);
|
---|
187 | ExportPolygon(&poly);
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | case Intersectable::BVH_INTERSECTABLE:
|
---|
191 | {
|
---|
192 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
193 |
|
---|
194 | if (node->IsLeaf())
|
---|
195 | {
|
---|
196 | ExportGeometry(static_cast<BvhLeaf *>(node)->mObjects, true);
|
---|
197 | }
|
---|
198 |
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | default:
|
---|
202 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
203 | break;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | void Exporter::ExportMeshInstance(MeshInstance *object)
|
---|
209 | {
|
---|
210 | // $$JB$$
|
---|
211 | // in the future check whether the mesh was not already exported
|
---|
212 | // and use a reference to the that mesh instead
|
---|
213 | ExportMesh(object->GetMesh());
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | void Exporter::ExportTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
218 | {
|
---|
219 | Mesh mesh(*mi->GetMesh());
|
---|
220 |
|
---|
221 | Matrix4x4 m;
|
---|
222 | mi->GetWorldTransform(m);
|
---|
223 | mesh.ApplyTransformation(m);
|
---|
224 |
|
---|
225 | ExportMesh(&mesh);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | void Exporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
230 | {
|
---|
231 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
232 |
|
---|
233 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
234 | {
|
---|
235 | ExportViewCell(*it);
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
241 | const bool exportSingleMesh,
|
---|
242 | AxisAlignedBox3 *bbox)
|
---|
243 | {
|
---|
244 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
245 |
|
---|
246 | if (!exportSingleMesh)
|
---|
247 | {
|
---|
248 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
249 | {
|
---|
250 | if (!bbox || Overlap(*bbox, (*oit)->GetBox()))
|
---|
251 | {
|
---|
252 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
253 | ExportIntersectable(*oit);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | return;
|
---|
257 | }
|
---|
258 |
|
---|
259 | ///////////////////////
|
---|
260 | //-- all objects exported as one mesh
|
---|
261 | //-- warning: currently works only for triangles
|
---|
262 |
|
---|
263 | PolygonContainer polys;
|
---|
264 |
|
---|
265 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
266 | {
|
---|
267 | Intersectable *obj = *oit;
|
---|
268 |
|
---|
269 | if (bbox && !Overlap(*bbox, obj->GetBox()))
|
---|
270 | {
|
---|
271 | continue;
|
---|
272 | }
|
---|
273 | switch (obj->Type())
|
---|
274 | {
|
---|
275 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
276 | {
|
---|
277 | TriangleIntersectable *ti = static_cast<TriangleIntersectable *>(obj);
|
---|
278 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | case Intersectable::MESH_INSTANCE:
|
---|
282 | {
|
---|
283 | MeshInstance *mi = static_cast<MeshInstance *>(obj);
|
---|
284 | ExportMesh(mi->GetMesh());
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | default:
|
---|
288 | cout << "merge of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
289 | break;
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | Mesh dummyMesh;
|
---|
294 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
295 |
|
---|
296 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
297 | {
|
---|
298 | Polygon3 *poly = (*pit);
|
---|
299 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
300 | }
|
---|
301 |
|
---|
302 | ExportMesh(&dummyMesh);
|
---|
303 | CLEAR_CONTAINER(polys);
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | bool Exporter::ExportTraversalTree(const TraversalTree &tree,
|
---|
308 | const bool exportGeometry)
|
---|
309 | {
|
---|
310 | stack<TraversalNode *> tStack;
|
---|
311 |
|
---|
312 | tStack.push(tree.GetRoot());
|
---|
313 |
|
---|
314 | Mesh *mesh = new Mesh;
|
---|
315 |
|
---|
316 | SetWireframe();
|
---|
317 |
|
---|
318 | while (!tStack.empty())
|
---|
319 | {
|
---|
320 | TraversalNode *node = tStack.top();
|
---|
321 | tStack.pop();
|
---|
322 | const AxisAlignedBox3 box = tree.GetBox(node);
|
---|
323 |
|
---|
324 | // add 6 vertices of the box
|
---|
325 | const int index = (int)mesh->mVertices.size();
|
---|
326 |
|
---|
327 | SetForcedMaterial(RandomMaterial());
|
---|
328 |
|
---|
329 | for (int i=0; i < 8; i++)
|
---|
330 | {
|
---|
331 | Vector3 v;
|
---|
332 | box.GetVertex(i, v);
|
---|
333 | mesh->mVertices.push_back(v);
|
---|
334 | }
|
---|
335 |
|
---|
336 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
337 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
338 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
339 |
|
---|
340 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
341 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
342 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
343 |
|
---|
344 | if (!node->IsLeaf())
|
---|
345 | {
|
---|
346 | TraversalInterior *interior = static_cast<TraversalInterior *>(node);
|
---|
347 |
|
---|
348 | tStack.push(interior->mFront);
|
---|
349 | tStack.push(interior->mBack);
|
---|
350 | }
|
---|
351 | else if (exportGeometry)
|
---|
352 | {
|
---|
353 | SetFilled();
|
---|
354 | SetForcedMaterial(RandomMaterial());
|
---|
355 |
|
---|
356 | ExportViewCells(static_cast<TraversalLeaf *>(node)->mViewCells);
|
---|
357 | SetWireframe();
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | ExportMesh(mesh);
|
---|
362 | delete mesh;
|
---|
363 |
|
---|
364 | return true;
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 | }
|
---|