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