[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"
|
---|
[162] | 10 |
|
---|
[1121] | 11 |
|
---|
[2176] | 12 | using namespace std;
|
---|
| 13 |
|
---|
[863] | 14 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 15 |
|
---|
[162] | 16 | Exporter *
|
---|
| 17 | Exporter::GetExporter(const string filename)
|
---|
| 18 | {
|
---|
[712] | 19 | Exporter *exporter = NULL;
|
---|
[162] | 20 |
|
---|
[712] | 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);
|
---|
[162] | 28 | }
|
---|
[712] | 29 | else
|
---|
| 30 | {
|
---|
| 31 | cerr<<"Error: Currently unsuported export format, filename " << filename << endl;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return exporter;
|
---|
[162] | 35 | }
|
---|
| 36 |
|
---|
[1121] | 37 |
|
---|
[1415] | 38 | bool Exporter::ExportOspTree(const OspTree &ospTree,
|
---|
| 39 | const int maxPvs
|
---|
| 40 | )
|
---|
[1121] | 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);
|
---|
[1144] | 59 | ExportBox(ospTree.GetBoundingBox(leaf));
|
---|
[1121] | 60 |
|
---|
| 61 | SetFilled();
|
---|
| 62 |
|
---|
| 63 | if (maxPvs) // color code pvs
|
---|
| 64 | {
|
---|
| 65 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
[1181] | 66 | const float importance = (float)leaf->mObjects.size() / (float)maxPvs;
|
---|
[1121] | 67 |
|
---|
| 68 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 69 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 70 | }
|
---|
| 71 | else
|
---|
| 72 | {
|
---|
| 73 | SetForcedMaterial(RandomMaterial());
|
---|
| 74 | }
|
---|
[1287] | 75 | if (0) ExportGeometry(leaf->mObjects);
|
---|
[1121] | 76 | }
|
---|
| 77 |
|
---|
| 78 | return true;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[1181] | 81 |
|
---|
[1139] | 82 | void Exporter::ExportKdIntersectable(const KdIntersectable &kdObj)
|
---|
| 83 | {
|
---|
[1233] | 84 | KdNode *node = kdObj.GetItem();
|
---|
[1139] | 85 | Intersectable::NewMail();
|
---|
| 86 |
|
---|
[1144] | 87 | // todo: traverse to leaves
|
---|
[1139] | 88 | if (node->IsLeaf())
|
---|
| 89 | {
|
---|
[1144] | 90 | // eyport leaf pvs
|
---|
[2017] | 91 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
[1144] | 92 |
|
---|
[1139] | 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 |
|
---|
[1287] | 109 | bool Exporter::ExportBvHierarchy(const BvHierarchy &bvHierarchy,
|
---|
[1920] | 110 | const float maxPvs,
|
---|
[1764] | 111 | AxisAlignedBox3 *box,
|
---|
[1418] | 112 | const bool exportBoundingBoxes)
|
---|
[1287] | 113 | {
|
---|
| 114 | vector<BvhLeaf *> leaves;
|
---|
[1707] | 115 | bvHierarchy.CollectLeaves(bvHierarchy.GetRoot(), leaves);
|
---|
[1287] | 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;
|
---|
[1293] | 124 |
|
---|
[1287] | 125 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 126 | {
|
---|
| 127 | BvhLeaf *leaf = *it;
|
---|
| 128 |
|
---|
[1418] | 129 | if (leaf->mObjects.empty() ||
|
---|
| 130 | (box && !Overlap(*box, leaf->GetBoundingBox())))
|
---|
[1415] | 131 | continue;
|
---|
| 132 |
|
---|
[1418] | 133 | if (exportBoundingBoxes)
|
---|
| 134 | {
|
---|
| 135 | SetWireframe();
|
---|
| 136 | SetForcedMaterial(white);
|
---|
| 137 | ExportBox(leaf->GetBoundingBox());
|
---|
| 138 | }
|
---|
[1287] | 139 |
|
---|
[1920] | 140 | if (maxPvs > 0) // color code pvs
|
---|
[1287] | 141 | {
|
---|
| 142 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
[1920] | 143 | const float importance = (float)leaf->mRenderCost / (float)maxPvs;
|
---|
[1287] | 144 |
|
---|
| 145 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 146 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 147 | }
|
---|
| 148 | else
|
---|
| 149 | {
|
---|
| 150 | SetForcedMaterial(RandomMaterial());
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[1418] | 153 | SetFilled();
|
---|
[2003] | 154 |
|
---|
[2231] | 155 | if (1)
|
---|
[2003] | 156 | ExportGeometry(leaf->mObjects, true, box);
|
---|
| 157 | else
|
---|
| 158 | ExportBox(leaf->GetBoundingBox());
|
---|
[1287] | 159 | }
|
---|
| 160 |
|
---|
[1420] | 161 | // reset to filled
|
---|
[1418] | 162 | SetFilled();
|
---|
| 163 |
|
---|
[1287] | 164 | return true;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
[1344] | 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:
|
---|
[2017] | 176 | ExportTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
[1344] | 177 | break;
|
---|
| 178 | case Intersectable::VIEW_CELL:
|
---|
[2017] | 179 | ExportViewCell(static_cast<ViewCell *>(object));
|
---|
[1344] | 180 | break;
|
---|
| 181 | case Intersectable::KD_INTERSECTABLE:
|
---|
[2017] | 182 | ExportKdIntersectable(*(static_cast<KdIntersectable *>(object)));
|
---|
[1344] | 183 | break;
|
---|
| 184 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
| 185 | {
|
---|
[1764] | 186 | // if (mClampToBox && !Overlap(mBoundingBox, object->GetBox())) return;
|
---|
[2017] | 187 | const Triangle3 triangle = static_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
[1764] | 188 |
|
---|
| 189 | Polygon3 poly(triangle);
|
---|
| 190 | ExportPolygon(&poly);
|
---|
| 191 | break;
|
---|
[1344] | 192 | }
|
---|
[1418] | 193 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 194 | {
|
---|
[2017] | 195 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
[1764] | 196 |
|
---|
[1418] | 197 | if (node->IsLeaf())
|
---|
[1764] | 198 | {
|
---|
[2017] | 199 | ExportGeometry(static_cast<BvhLeaf *>(node)->mObjects, true);
|
---|
[1764] | 200 | }
|
---|
| 201 |
|
---|
[1418] | 202 | break;
|
---|
| 203 | }
|
---|
[1344] | 204 | default:
|
---|
| 205 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
| 206 | break;
|
---|
| 207 | }
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[1418] | 210 |
|
---|
[1344] | 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 |
|
---|
[1404] | 243 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
[1415] | 244 | const bool exportSingleMesh,
|
---|
| 245 | AxisAlignedBox3 *bbox)
|
---|
[1404] | 246 | {
|
---|
| 247 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[1344] | 248 |
|
---|
[1723] | 249 | if (!exportSingleMesh)
|
---|
[1404] | 250 | {
|
---|
| 251 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 252 | {
|
---|
[1418] | 253 | if (!bbox || Overlap(*bbox, (*oit)->GetBox()))
|
---|
[1415] | 254 | {
|
---|
| 255 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
| 256 | ExportIntersectable(*oit);
|
---|
| 257 | }
|
---|
[1404] | 258 | }
|
---|
| 259 | return;
|
---|
| 260 | }
|
---|
[1418] | 261 |
|
---|
| 262 | ///////////////////////
|
---|
[1404] | 263 | //-- all objects exported as one mesh
|
---|
[1418] | 264 | //-- warning: currently works only for triangles
|
---|
[1415] | 265 |
|
---|
[1404] | 266 | PolygonContainer polys;
|
---|
| 267 |
|
---|
| 268 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 269 | {
|
---|
| 270 | Intersectable *obj = *oit;
|
---|
[1415] | 271 |
|
---|
[1418] | 272 | if (bbox && !Overlap(*bbox, obj->GetBox()))
|
---|
| 273 | {
|
---|
[1415] | 274 | continue;
|
---|
[1418] | 275 | }
|
---|
[1404] | 276 | switch (obj->Type())
|
---|
| 277 | {
|
---|
| 278 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
[1421] | 279 | {
|
---|
[2017] | 280 | TriangleIntersectable *ti = static_cast<TriangleIntersectable *>(obj);
|
---|
[1404] | 281 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
| 282 | break;
|
---|
| 283 | }
|
---|
[1418] | 284 | case Intersectable::MESH_INSTANCE:
|
---|
[1421] | 285 | {
|
---|
[2017] | 286 | MeshInstance *mi = static_cast<MeshInstance *>(obj);
|
---|
[1418] | 287 | ExportMesh(mi->GetMesh());
|
---|
[1421] | 288 | break;
|
---|
[1418] | 289 | }
|
---|
[1404] | 290 | default:
|
---|
[2206] | 291 | cout << "merge of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
[1404] | 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 |
|
---|
[2124] | 310 | bool Exporter::ExportTraversalTree(const TraversalTree &tree,
|
---|
| 311 | const bool exportGeometry)
|
---|
| 312 | {
|
---|
| 313 | stack<TraversalNode *> tStack;
|
---|
[1404] | 314 |
|
---|
[2124] | 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;
|
---|
[1694] | 368 | }
|
---|
[2124] | 369 |
|
---|
| 370 |
|
---|
| 371 | }
|
---|