[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 |
|
---|
[2394] | 155 | ExportGeometry(leaf->mObjects, true, box);
|
---|
[1287] | 156 | }
|
---|
| 157 |
|
---|
[1420] | 158 | // reset to filled
|
---|
[1418] | 159 | SetFilled();
|
---|
| 160 |
|
---|
[1287] | 161 | return true;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
[1344] | 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:
|
---|
[2017] | 173 | ExportTransformedMeshInstance(static_cast<TransformedMeshInstance *>(object));
|
---|
[1344] | 174 | break;
|
---|
| 175 | case Intersectable::VIEW_CELL:
|
---|
[2017] | 176 | ExportViewCell(static_cast<ViewCell *>(object));
|
---|
[1344] | 177 | break;
|
---|
| 178 | case Intersectable::KD_INTERSECTABLE:
|
---|
[2017] | 179 | ExportKdIntersectable(*(static_cast<KdIntersectable *>(object)));
|
---|
[1344] | 180 | break;
|
---|
| 181 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
| 182 | {
|
---|
[1764] | 183 | // if (mClampToBox && !Overlap(mBoundingBox, object->GetBox())) return;
|
---|
[2017] | 184 | const Triangle3 triangle = static_cast<TriangleIntersectable *>(object)->GetItem();
|
---|
[1764] | 185 |
|
---|
| 186 | Polygon3 poly(triangle);
|
---|
| 187 | ExportPolygon(&poly);
|
---|
| 188 | break;
|
---|
[1344] | 189 | }
|
---|
[1418] | 190 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 191 | {
|
---|
[2017] | 192 | BvhNode *node = static_cast<BvhNode *>(object);
|
---|
[1764] | 193 |
|
---|
[1418] | 194 | if (node->IsLeaf())
|
---|
[1764] | 195 | {
|
---|
[2017] | 196 | ExportGeometry(static_cast<BvhLeaf *>(node)->mObjects, true);
|
---|
[1764] | 197 | }
|
---|
| 198 |
|
---|
[1418] | 199 | break;
|
---|
| 200 | }
|
---|
[1344] | 201 | default:
|
---|
| 202 | cerr << "Sorry the export for object type " << Intersectable::GetTypeName(object) << " is not available yet" << endl;
|
---|
| 203 | break;
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[1418] | 207 |
|
---|
[1344] | 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 |
|
---|
[1404] | 240 | void Exporter::ExportGeometry(const ObjectContainer &objects,
|
---|
[1415] | 241 | const bool exportSingleMesh,
|
---|
| 242 | AxisAlignedBox3 *bbox)
|
---|
[1404] | 243 | {
|
---|
| 244 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
[1344] | 245 |
|
---|
[1723] | 246 | if (!exportSingleMesh)
|
---|
[1404] | 247 | {
|
---|
| 248 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 249 | {
|
---|
[1418] | 250 | if (!bbox || Overlap(*bbox, (*oit)->GetBox()))
|
---|
[1415] | 251 | {
|
---|
| 252 | if (0) SetForcedMaterial(RandomMaterial());
|
---|
| 253 | ExportIntersectable(*oit);
|
---|
| 254 | }
|
---|
[1404] | 255 | }
|
---|
| 256 | return;
|
---|
| 257 | }
|
---|
[1418] | 258 |
|
---|
| 259 | ///////////////////////
|
---|
[1404] | 260 | //-- all objects exported as one mesh
|
---|
[1418] | 261 | //-- warning: currently works only for triangles
|
---|
[1415] | 262 |
|
---|
[1404] | 263 | PolygonContainer polys;
|
---|
| 264 |
|
---|
| 265 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 266 | {
|
---|
| 267 | Intersectable *obj = *oit;
|
---|
[1415] | 268 |
|
---|
[1418] | 269 | if (bbox && !Overlap(*bbox, obj->GetBox()))
|
---|
| 270 | {
|
---|
[1415] | 271 | continue;
|
---|
[1418] | 272 | }
|
---|
[1404] | 273 | switch (obj->Type())
|
---|
| 274 | {
|
---|
| 275 | case Intersectable::TRIANGLE_INTERSECTABLE:
|
---|
[1421] | 276 | {
|
---|
[2017] | 277 | TriangleIntersectable *ti = static_cast<TriangleIntersectable *>(obj);
|
---|
[1404] | 278 | polys.push_back(new Polygon3(ti->GetItem()));
|
---|
| 279 | break;
|
---|
| 280 | }
|
---|
[1418] | 281 | case Intersectable::MESH_INSTANCE:
|
---|
[1421] | 282 | {
|
---|
[2017] | 283 | MeshInstance *mi = static_cast<MeshInstance *>(obj);
|
---|
[1418] | 284 | ExportMesh(mi->GetMesh());
|
---|
[1421] | 285 | break;
|
---|
[1418] | 286 | }
|
---|
[1404] | 287 | default:
|
---|
[2206] | 288 | cout << "merge of object type " << obj->Type() << " not implemented yet" << endl;
|
---|
[1404] | 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 |
|
---|
[2124] | 307 | bool Exporter::ExportTraversalTree(const TraversalTree &tree,
|
---|
| 308 | const bool exportGeometry)
|
---|
| 309 | {
|
---|
| 310 | stack<TraversalNode *> tStack;
|
---|
[1404] | 311 |
|
---|
[2124] | 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;
|
---|
[1694] | 365 | }
|
---|
[2124] | 366 |
|
---|
| 367 |
|
---|
| 368 | }
|
---|