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, const int maxPvs)
|
---|
36 | {
|
---|
37 | vector<KdLeaf *> leaves;
|
---|
38 | ospTree.CollectLeaves(leaves);
|
---|
39 |
|
---|
40 | mUseForcedMaterial = true;
|
---|
41 |
|
---|
42 | vector<KdLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
43 |
|
---|
44 | Material white;
|
---|
45 | white.mDiffuseColor.r = 1;
|
---|
46 | white.mDiffuseColor.g = 1;
|
---|
47 | white.mDiffuseColor.b = 1;
|
---|
48 |
|
---|
49 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
50 | {
|
---|
51 | KdLeaf *leaf = *it;
|
---|
52 |
|
---|
53 | SetWireframe();
|
---|
54 | SetForcedMaterial(white);
|
---|
55 | ExportBox(ospTree.GetBoundingBox(leaf));
|
---|
56 |
|
---|
57 | SetFilled();
|
---|
58 |
|
---|
59 | if (maxPvs) // color code pvs
|
---|
60 | {
|
---|
61 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
62 | const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
|
---|
63 |
|
---|
64 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
65 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
66 | }
|
---|
67 | else
|
---|
68 | {
|
---|
69 | SetForcedMaterial(RandomMaterial());
|
---|
70 | }
|
---|
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 |
|
---|
83 | Intersectable::NewMail();
|
---|
84 |
|
---|
85 | // todo: traverse to leaves
|
---|
86 | if (node->IsLeaf())
|
---|
87 | {
|
---|
88 | // eyport leaf pvs
|
---|
89 | KdLeaf *leaf = dynamic_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 int maxPvs)
|
---|
109 | {
|
---|
110 | vector<BvhLeaf *> leaves;
|
---|
111 | bvHierarchy.CollectLeaves(leaves);
|
---|
112 |
|
---|
113 | mUseForcedMaterial = true;
|
---|
114 |
|
---|
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 | if (leaf->mObjects.empty())
|
---|
126 | continue;
|
---|
127 |
|
---|
128 | SetWireframe();
|
---|
129 | SetForcedMaterial(white);
|
---|
130 | ExportBox(leaf->GetBoundingBox());
|
---|
131 | SetFilled();
|
---|
132 |
|
---|
133 | if (maxPvs) // color code pvs
|
---|
134 | {
|
---|
135 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
136 | const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
|
---|
137 |
|
---|
138 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
139 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
140 | }
|
---|
141 | else
|
---|
142 | {
|
---|
143 | SetForcedMaterial(RandomMaterial());
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (1)
|
---|
147 | {
|
---|
148 | SetFilled();
|
---|
149 | ExportGeometry(leaf->mObjects, true);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void Exporter::ExportIntersectable(Intersectable *object)
|
---|
158 | {
|
---|
159 | switch (object->Type())
|
---|
160 | {
|
---|
161 | case Intersectable::MESH_INSTANCE:
|
---|
162 | ExportMeshInstance((MeshInstance *)object);
|
---|
163 | break;
|
---|
164 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
165 | ExportTransformedMeshInstance(dynamic_cast<TransformedMeshInstance *>(object));
|
---|
166 | break;
|
---|
167 | case Intersectable::VIEW_CELL:
|
---|
168 | ExportViewCell(dynamic_cast<ViewCell *>(object));
|
---|
169 | break;
|
---|
170 | case Intersectable::KD_INTERSECTABLE:
|
---|
171 | ExportKdIntersectable(*(dynamic_cast<KdIntersectable *>(object)));
|
---|
172 | break;
|
---|
173 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
174 | {
|
---|
175 | const Triangle3 triangle = dynamic_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
176 |
|
---|
177 | VertexContainer vertices;
|
---|
178 | vertices.push_back(triangle.mVertices[0]);
|
---|
179 | vertices.push_back(triangle.mVertices[1]);
|
---|
180 | vertices.push_back(triangle.mVertices[2]);
|
---|
181 |
|
---|
182 | Polygon3 poly(vertices);
|
---|
183 | ExportPolygon(&poly);
|
---|
184 | break;
|
---|
185 | }
|
---|
186 | default:
|
---|
187 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | void Exporter::ExportMeshInstance(MeshInstance *object)
|
---|
193 | {
|
---|
194 | // $$JB$$
|
---|
195 | // in the future check whether the mesh was not already exported
|
---|
196 | // and use a reference to the that mesh instead
|
---|
197 | ExportMesh(object->GetMesh());
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | void Exporter::ExportTransformedMeshInstance(TransformedMeshInstance *mi)
|
---|
202 | {
|
---|
203 | Mesh mesh(*mi->GetMesh());
|
---|
204 |
|
---|
205 | Matrix4x4 m;
|
---|
206 | mi->GetWorldTransform(m);
|
---|
207 | mesh.ApplyTransformation(m);
|
---|
208 |
|
---|
209 | ExportMesh(&mesh);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | void Exporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
214 | {
|
---|
215 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
216 |
|
---|
217 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
218 | {
|
---|
219 | ExportViewCell(*it);
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
225 | const bool exportSingleMesh)
|
---|
226 | {
|
---|
227 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
228 |
|
---|
229 | if (!exportSingleMesh)
|
---|
230 | {
|
---|
231 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
232 | {
|
---|
233 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
234 | ExportIntersectable(*oit);
|
---|
235 | }
|
---|
236 |
|
---|
237 | return;
|
---|
238 | }
|
---|
239 |
|
---|
240 | //-- all objects exported as one mesh
|
---|
241 | //-- hack: currently works only for triangles
|
---|
242 | PolygonContainer polys;
|
---|
243 |
|
---|
244 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
245 | {
|
---|
246 | Intersectable *obj = *oit;
|
---|
247 | switch (obj->Type())
|
---|
248 | {
|
---|
249 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
250 | {
|
---|
251 | TriangleIntersectable *ti = dynamic_cast<TriangleIntersectable *>(obj);
|
---|
252 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
253 | break;
|
---|
254 | }
|
---|
255 | default:
|
---|
256 | cout << "merging of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
257 | break;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | Mesh dummyMesh;
|
---|
262 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
263 |
|
---|
264 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
265 | {
|
---|
266 | Polygon3 *poly = (*pit);
|
---|
267 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
268 | }
|
---|
269 |
|
---|
270 | ExportMesh(&dummyMesh);
|
---|
271 | CLEAR_CONTAINER(polys);
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 |
|
---|
276 | } |
---|