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