[162] | 1 | #include <stack>
|
---|
| 2 | #include "common.h"
|
---|
| 3 | #include "SceneGraph.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Mesh.h"
|
---|
| 6 | #include "KdTree.h"
|
---|
[242] | 7 | #include "ViewCellBsp.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
[260] | 9 | #include "Polygon3.h"
|
---|
[292] | 10 | ViewCellContainer X3dExporter::foundViewCells; // TODO: delete later
|
---|
[162] | 11 |
|
---|
| 12 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
| 13 | {
|
---|
| 14 | stream.open(mFilename.c_str());
|
---|
| 15 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 16 | stream<<"<X3D>"<<endl;
|
---|
| 17 | stream<<"<Scene>"<<endl;
|
---|
| 18 |
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | X3dExporter::~X3dExporter()
|
---|
| 22 | {
|
---|
| 23 | stream<<"</Scene>"<<endl;
|
---|
| 24 | stream<<"</X3D>"<<endl;
|
---|
| 25 | stream.close();
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[176] | 28 |
|
---|
[162] | 29 | bool
|
---|
[176] | 30 | X3dExporter::ExportRays(const vector<Ray> &rays,
|
---|
| 31 | const float length,
|
---|
| 32 | const RgbColor &color)
|
---|
[162] | 33 | {
|
---|
| 34 | vector<Ray>::const_iterator ri = rays.begin();
|
---|
| 35 | stream<<"<Shape>"<<endl;
|
---|
[176] | 36 | stream<<"<Appearance>"<<endl;
|
---|
| 37 | stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
| 38 | "\" />"<<endl;
|
---|
| 39 | stream<<"</Appearance>"<<endl;
|
---|
| 40 |
|
---|
[209] | 41 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[162] | 42 |
|
---|
| 43 | int index = 0;
|
---|
| 44 | for (; ri != rays.end(); ri++) {
|
---|
| 45 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
| 46 | index+=2;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | stream<<"\" >"<<endl;
|
---|
| 50 |
|
---|
| 51 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 52 |
|
---|
| 53 | ri = rays.begin();
|
---|
| 54 | for (; ri != rays.end(); ri++) {
|
---|
| 55 | Vector3 a = (*ri).GetLoc();
|
---|
| 56 |
|
---|
[176] | 57 | Vector3 b;
|
---|
| 58 | if (length < 0)
|
---|
| 59 | b = (*ri).GetLoc() - length*(*ri).GetDir();
|
---|
| 60 | else
|
---|
| 61 | if ((*ri).intersections.size()==0)
|
---|
| 62 | b = (*ri).GetLoc() + length*(*ri).GetDir();
|
---|
| 63 | else
|
---|
| 64 | b = (*ri).Extrap((*ri).intersections[0].mT);
|
---|
| 65 |
|
---|
[162] | 66 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
| 67 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | stream<<"\" >"<<endl;
|
---|
| 71 | stream<<"</Coordinate>"<<endl;
|
---|
| 72 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 73 | stream<<"</Shape>"<<endl;
|
---|
| 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void
|
---|
| 78 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
| 79 | {
|
---|
| 80 | stream<<"<Group>"<<endl;
|
---|
| 81 |
|
---|
| 82 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 83 | for (; ni != node->mChildren.end(); ni++)
|
---|
| 84 | ExportSceneNode(*ni);
|
---|
| 85 |
|
---|
| 86 |
|
---|
[176] | 87 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
[162] | 88 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
| 89 | // export the transform...
|
---|
[176] | 90 | ExportIntersectable(*mi);
|
---|
[162] | 91 | }
|
---|
| 92 |
|
---|
| 93 | stream<<"</Group>"<<endl;
|
---|
| 94 |
|
---|
| 95 | }
|
---|
[176] | 96 | void
|
---|
| 97 | X3dExporter::ExportIntersectable(Intersectable *object)
|
---|
| 98 | {
|
---|
| 99 | switch (object->Type()) {
|
---|
| 100 | case Intersectable::MESH_INSTANCE:
|
---|
| 101 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
| 102 | ExportMeshInstance((MeshInstance *)object);
|
---|
[242] | 103 | break;
|
---|
[308] | 104 | case Intersectable::VIEW_CELL:
|
---|
[242] | 105 | ExportViewCell((ViewCell *)object);
|
---|
[176] | 106 | break;
|
---|
| 107 | default:
|
---|
| 108 | cerr<<"Sorry the export for object not yet defined"<<endl;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[162] | 112 |
|
---|
| 113 | void
|
---|
[176] | 114 | X3dExporter::ExportMeshInstance(MeshInstance *object)
|
---|
| 115 | {
|
---|
| 116 | // $$JB$$
|
---|
[312] | 117 | // in the future check whether the mesh was not already exported
|
---|
[176] | 118 | // and use a reference to the that mesh instead
|
---|
| 119 | ExportMesh(object->GetMesh());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[313] | 122 | void
|
---|
| 123 | X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
|
---|
[261] | 124 | {
|
---|
[313] | 125 | ViewCellContainer::const_iterator it, it_end = viewCells.end();
|
---|
[261] | 126 |
|
---|
[313] | 127 | for (it = viewCells.begin(); it != it_end; ++ it)
|
---|
[261] | 128 | ExportViewCell(*it);
|
---|
| 129 | }
|
---|
[313] | 130 |
|
---|
[176] | 131 | void
|
---|
[261] | 132 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
[242] | 133 | {
|
---|
[261] | 134 | if (viewCell->GetMesh())
|
---|
| 135 | ExportMesh(viewCell->GetMesh());
|
---|
[242] | 136 | }
|
---|
| 137 |
|
---|
| 138 | void
|
---|
[162] | 139 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
| 140 | {
|
---|
| 141 |
|
---|
| 142 | stream<<"<Shape>"<<endl;
|
---|
| 143 | stream<<"<Appearance>"<<endl;
|
---|
[176] | 144 |
|
---|
[162] | 145 | // $$ tmp -> random material
|
---|
| 146 |
|
---|
| 147 | float r, g, b;
|
---|
| 148 |
|
---|
[176] | 149 | if (mUseForcedMaterial) {
|
---|
| 150 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 151 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 152 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 153 |
|
---|
| 154 | } else
|
---|
| 155 | if (mesh->mMaterial) {
|
---|
| 156 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
| 157 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
| 158 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
| 159 | } else {
|
---|
| 160 | r = RandomValue(0.5, 1.0);
|
---|
| 161 | g = RandomValue(0.5, 1.0);
|
---|
| 162 | b = RandomValue(0.5, 1.0);
|
---|
| 163 | }
|
---|
[162] | 164 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
| 165 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
| 166 | stream<<"</Appearance>"<<endl;
|
---|
| 167 |
|
---|
| 168 |
|
---|
[191] | 169 | if (mWireframe)
|
---|
[162] | 170 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 171 | else
|
---|
| 172 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 173 |
|
---|
| 174 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
| 175 |
|
---|
| 176 | int index = 0;
|
---|
| 177 |
|
---|
| 178 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
| 179 | Face *face = *fi;
|
---|
| 180 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
| 181 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
| 182 | stream<<*vi<<" ";
|
---|
| 183 | stream<<"-1"<<endl;
|
---|
| 184 | }
|
---|
| 185 | stream<<"\" >"<<endl;
|
---|
| 186 |
|
---|
| 187 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 188 |
|
---|
| 189 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
| 190 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
| 191 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
| 192 | stream<<","<<endl;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | stream<<"\" >"<<endl;
|
---|
| 196 | stream<<"</Coordinate>"<<endl;
|
---|
| 197 |
|
---|
[191] | 198 | if (mWireframe)
|
---|
[162] | 199 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 200 | else
|
---|
| 201 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
| 202 |
|
---|
| 203 | stream<<"</Shape>"<<endl;
|
---|
| 204 |
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[242] | 207 |
|
---|
| 208 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
| 209 | {
|
---|
| 210 | stream << "<Shape>" << endl;
|
---|
| 211 | stream << "<Appearance>" << endl;
|
---|
| 212 |
|
---|
| 213 | // $$ tmp -> random material
|
---|
| 214 |
|
---|
| 215 | float r, g, b;
|
---|
| 216 |
|
---|
| 217 | if (mUseForcedMaterial)
|
---|
| 218 | {
|
---|
| 219 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 220 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 221 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 222 | }
|
---|
| 223 | else if (poly->mMaterial)
|
---|
| 224 | {
|
---|
| 225 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
| 226 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
| 227 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
| 228 | } else
|
---|
| 229 | {
|
---|
| 230 | r = RandomValue(0.5, 1.0);
|
---|
| 231 | g = RandomValue(0.5, 1.0);
|
---|
| 232 | b = RandomValue(0.5, 1.0);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 236 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 237 |
|
---|
| 238 | stream << "</Appearance>" << endl;
|
---|
| 239 |
|
---|
| 240 |
|
---|
[262] | 241 | //-- create and write indices
|
---|
[242] | 242 | if (mWireframe)
|
---|
| 243 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 244 | else
|
---|
| 245 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 246 |
|
---|
[262] | 247 | int index = 0;
|
---|
| 248 |
|
---|
| 249 | VertexContainer::const_iterator vi;
|
---|
| 250 |
|
---|
[318] | 251 | for (int index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
| 252 | stream << index << " ";
|
---|
| 253 |
|
---|
| 254 | if (mWireframe) // final line to finish polygon
|
---|
| 255 | stream << "0 ";
|
---|
| 256 |
|
---|
[262] | 257 | stream << "-1" << endl;
|
---|
| 258 | stream << "\" >" << endl;
|
---|
| 259 |
|
---|
[242] | 260 | stream << "<Coordinate point=\"" << endl;
|
---|
| 261 |
|
---|
[262] | 262 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
[242] | 263 | {
|
---|
| 264 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 265 | stream << "," << endl;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[262] | 268 | stream << "\" >" << endl;
|
---|
| 269 | stream << "</Coordinate>" << endl;
|
---|
[242] | 270 |
|
---|
| 271 | if (mWireframe)
|
---|
| 272 | stream << "</IndexedLineSet>" << endl;
|
---|
| 273 | else
|
---|
| 274 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 275 |
|
---|
| 276 | stream << "</Shape>" << endl;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[313] | 279 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
[263] | 280 | {
|
---|
[318] | 281 | stream << "<Shape>" << endl;
|
---|
[263] | 282 | stream << "<Appearance>" << endl;
|
---|
| 283 |
|
---|
| 284 | // $$ tmp -> random material
|
---|
| 285 |
|
---|
| 286 | float r, g, b;
|
---|
[242] | 287 |
|
---|
[263] | 288 | if (mUseForcedMaterial)
|
---|
| 289 | {
|
---|
| 290 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 291 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 292 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 293 | }
|
---|
| 294 | else
|
---|
| 295 | {
|
---|
| 296 | r = RandomValue(0.5, 1.0);
|
---|
| 297 | g = RandomValue(0.5, 1.0);
|
---|
| 298 | b = RandomValue(0.5, 1.0);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 302 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 303 |
|
---|
| 304 | stream << "</Appearance>" << endl;
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | //-- create and write indices
|
---|
| 308 | if (mWireframe)
|
---|
| 309 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 310 | else
|
---|
| 311 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 312 |
|
---|
| 313 | int index = 0;
|
---|
| 314 |
|
---|
| 315 | PolygonContainer::const_iterator pit;
|
---|
| 316 |
|
---|
| 317 | VertexContainer::const_iterator vi;
|
---|
| 318 |
|
---|
[313] | 319 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
[263] | 320 | {
|
---|
| 321 | Polygon3 *poly = *pit;
|
---|
[318] | 322 | int startIdx = index;
|
---|
[263] | 323 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 324 | {
|
---|
| 325 | stream << index ++ << " ";
|
---|
| 326 | }
|
---|
[318] | 327 |
|
---|
| 328 | stream << startIdx << " ";// finish line
|
---|
[263] | 329 | stream << "-1" << endl;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | stream << "\" >" << endl;
|
---|
| 333 |
|
---|
| 334 | stream << "<Coordinate point=\"" << endl;
|
---|
[318] | 335 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
[263] | 336 | {
|
---|
| 337 | Polygon3 *poly = *pit;
|
---|
| 338 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 339 | {
|
---|
| 340 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 341 | stream << "," << endl;
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | stream << "\" >" << endl;
|
---|
| 345 | stream << "</Coordinate>" << endl;
|
---|
| 346 |
|
---|
| 347 | if (mWireframe)
|
---|
| 348 | stream << "</IndexedLineSet>" << endl;
|
---|
| 349 | else
|
---|
| 350 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 351 |
|
---|
| 352 | stream << "</Shape>" << endl;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[162] | 355 | bool
|
---|
| 356 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
| 357 | {
|
---|
| 358 | Mesh *mesh = new Mesh;
|
---|
| 359 | // add 6 vertices of the box
|
---|
[289] | 360 | int index = (int)mesh->mVertices.size();
|
---|
[162] | 361 | for (int i=0; i < 8; i++) {
|
---|
| 362 | Vector3 v;
|
---|
| 363 | box.GetVertex(i, v);
|
---|
| 364 | mesh->mVertices.push_back(v);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 368 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 369 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 370 |
|
---|
| 371 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 372 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 373 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 374 |
|
---|
| 375 | ExportMesh(mesh);
|
---|
| 376 | delete mesh;
|
---|
| 377 | return true;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | bool
|
---|
[242] | 381 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
[162] | 382 | {
|
---|
[242] | 383 | if (mExportRayDensity)
|
---|
| 384 | {
|
---|
[263] | 385 | return ExportBspTreeRayDensity(tree);
|
---|
[242] | 386 | }
|
---|
| 387 |
|
---|
| 388 | stack<BspNode *> tStack;
|
---|
| 389 |
|
---|
| 390 | tStack.push(tree.GetRoot());
|
---|
| 391 |
|
---|
| 392 | Mesh *mesh = new Mesh;
|
---|
| 393 |
|
---|
[262] | 394 | AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
[264] | 395 | bool savedWireframe = mWireframe;
|
---|
| 396 |
|
---|
| 397 | SetWireframe();
|
---|
[262] | 398 | ExportBox(box);
|
---|
[264] | 399 |
|
---|
| 400 | if (!savedWireframe)
|
---|
| 401 | SetFilled();
|
---|
| 402 |
|
---|
[292] | 403 | //ViewCellContainer foundViewCells;
|
---|
[265] | 404 |
|
---|
[289] | 405 | if (tree.StorePolys())
|
---|
[242] | 406 | {
|
---|
[289] | 407 | while (!tStack.empty())
|
---|
| 408 | {
|
---|
| 409 | BspNode *node = tStack.top();
|
---|
[242] | 410 |
|
---|
[289] | 411 | tStack.pop();
|
---|
[242] | 412 |
|
---|
[289] | 413 | if (tree.StorePolys()) // extract the polygons
|
---|
| 414 | {
|
---|
| 415 | PolygonContainer::const_iterator it;
|
---|
| 416 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
[260] | 417 |
|
---|
[289] | 418 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
| 419 | ExportPolygon(*it);
|
---|
| 420 | }
|
---|
[260] | 421 |
|
---|
[289] | 422 | if (!node->IsLeaf())
|
---|
| 423 | {
|
---|
| 424 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
[242] | 425 |
|
---|
[289] | 426 | tStack.push(interior->GetFront());
|
---|
| 427 | tStack.push(interior->GetBack());
|
---|
[260] | 428 |
|
---|
[289] | 429 | }
|
---|
[242] | 430 | }
|
---|
[289] | 431 | }
|
---|
| 432 | else // export view cells
|
---|
| 433 | {
|
---|
| 434 | while (!tStack.empty())
|
---|
[242] | 435 | {
|
---|
[289] | 436 | BspNode *node = tStack.top();
|
---|
| 437 |
|
---|
| 438 | tStack.pop();
|
---|
| 439 |
|
---|
| 440 | if (node->IsLeaf())
|
---|
| 441 | {
|
---|
| 442 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
| 443 | if (viewCell)
|
---|
[292] | 444 | foundViewCells.push_back(viewCell);
|
---|
[289] | 445 | }
|
---|
| 446 | else
|
---|
| 447 | {
|
---|
| 448 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 449 |
|
---|
| 450 | tStack.push(interior->GetFront());
|
---|
| 451 | tStack.push(interior->GetBack());
|
---|
| 452 | }
|
---|
[242] | 453 | }
|
---|
[289] | 454 |
|
---|
[292] | 455 | Debug << "Number of view cells with dublicates: " << (int)foundViewCells.size() << endl;
|
---|
[289] | 456 |
|
---|
| 457 | //-- erase dublicates
|
---|
[292] | 458 | sort(foundViewCells.begin(), foundViewCells.end());
|
---|
| 459 | ViewCellContainer::iterator new_end = unique(foundViewCells.begin(), foundViewCells.end());
|
---|
| 460 | foundViewCells.erase(new_end, foundViewCells.end());
|
---|
[313] | 461 | ExportViewCells(foundViewCells);
|
---|
[289] | 462 |
|
---|
[292] | 463 | Debug << "Number of view cells after erasing dublicates: " << (int)foundViewCells.size() << endl;
|
---|
[265] | 464 | }
|
---|
| 465 |
|
---|
[263] | 466 | return true;
|
---|
[242] | 467 | }
|
---|
| 468 |
|
---|
| 469 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
| 470 | {
|
---|
| 471 | if (mExportRayDensity) {
|
---|
[191] | 472 | return ExportKdTreeRayDensity(tree);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[162] | 475 | stack<KdNode *> tStack;
|
---|
| 476 |
|
---|
| 477 | tStack.push(tree.GetRoot());
|
---|
| 478 |
|
---|
| 479 | Mesh *mesh = new Mesh;
|
---|
| 480 |
|
---|
| 481 | while (!tStack.empty()) {
|
---|
| 482 | KdNode *node = tStack.top();
|
---|
| 483 | tStack.pop();
|
---|
| 484 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 485 | // add 6 vertices of the box
|
---|
[289] | 486 | int index = (int)mesh->mVertices.size();
|
---|
[162] | 487 | for (int i=0; i < 8; i++) {
|
---|
| 488 | Vector3 v;
|
---|
| 489 | box.GetVertex(i, v);
|
---|
| 490 | mesh->mVertices.push_back(v);
|
---|
| 491 | }
|
---|
| 492 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 493 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 494 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 495 |
|
---|
| 496 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 497 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 498 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 499 |
|
---|
| 500 | if (!node->IsLeaf()) {
|
---|
| 501 | KdInterior *interior = (KdInterior *)node;
|
---|
| 502 | tStack.push(interior->mFront);
|
---|
| 503 | tStack.push(interior->mBack);
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | ExportMesh(mesh);
|
---|
| 508 | delete mesh;
|
---|
| 509 | return true;
|
---|
[242] | 510 | // TODO
|
---|
| 511 | return true;
|
---|
[162] | 512 | }
|
---|
| 513 |
|
---|
| 514 |
|
---|
[191] | 515 | bool
|
---|
[263] | 516 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
| 517 | {
|
---|
[311] | 518 | stack<BspNode *> tStack;
|
---|
| 519 |
|
---|
| 520 | tStack.push(tree.GetRoot());
|
---|
| 521 |
|
---|
| 522 | bool fm = mUseForcedMaterial;
|
---|
| 523 |
|
---|
| 524 | mUseForcedMaterial = true;
|
---|
| 525 |
|
---|
| 526 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 527 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 528 |
|
---|
| 529 | while (!tStack.empty())
|
---|
| 530 | {
|
---|
| 531 | BspNode *node = tStack.top();
|
---|
| 532 | tStack.pop();
|
---|
| 533 |
|
---|
| 534 | if (node->IsLeaf())
|
---|
| 535 | {
|
---|
| 536 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
| 537 |
|
---|
| 538 | // set the mesh material according to the ray density
|
---|
| 539 | if (vc->mPassingRays.mRays)
|
---|
| 540 | {
|
---|
[313] | 541 | float importance =
|
---|
| 542 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
[311] | 543 |
|
---|
| 544 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 545 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 546 | ExportViewCell(vc);
|
---|
| 547 | }
|
---|
| 548 | } else
|
---|
| 549 | {
|
---|
| 550 | BspInterior *interior = (BspInterior *)node;
|
---|
| 551 | tStack.push(interior->GetFront());
|
---|
| 552 | tStack.push(interior->GetBack());
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | // restore the state of forced material
|
---|
| 557 | mUseForcedMaterial = fm;
|
---|
| 558 |
|
---|
[263] | 559 | return true;
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | bool
|
---|
[191] | 563 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
| 564 | {
|
---|
| 565 | stack<KdNode *> tStack;
|
---|
| 566 |
|
---|
| 567 | tStack.push(tree.GetRoot());
|
---|
| 568 |
|
---|
| 569 | bool fm = mUseForcedMaterial;
|
---|
| 570 | mUseForcedMaterial = true;
|
---|
| 571 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 572 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 573 | while (!tStack.empty()) {
|
---|
| 574 | KdNode *node = tStack.top();
|
---|
| 575 | tStack.pop();
|
---|
| 576 | if (node->IsLeaf()) {
|
---|
| 577 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 578 | Mesh *mesh = new Mesh;
|
---|
| 579 |
|
---|
| 580 | // add 6 vertices of the box
|
---|
[289] | 581 | int index = (int)mesh->mVertices.size();
|
---|
[191] | 582 | for (int i=0; i < 8; i++) {
|
---|
| 583 | Vector3 v;
|
---|
| 584 | box.GetVertex(i, v);
|
---|
| 585 | mesh->mVertices.push_back(v);
|
---|
| 586 | }
|
---|
| 587 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 588 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 589 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 590 |
|
---|
| 591 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 592 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 593 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 594 |
|
---|
| 595 |
|
---|
| 596 | // set the mesh material according to the ray density
|
---|
| 597 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 598 | if (leaf->mPassingRays.mRays) {
|
---|
| 599 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
| 600 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
| 601 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
| 602 | ///(float)leaf->mPassingRays.mRays;
|
---|
| 603 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
| 604 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 605 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 606 | ExportMesh(mesh);
|
---|
| 607 | }
|
---|
| 608 | delete mesh;
|
---|
| 609 | } else {
|
---|
| 610 | KdInterior *interior = (KdInterior *)node;
|
---|
| 611 | tStack.push(interior->mFront);
|
---|
| 612 | tStack.push(interior->mBack);
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
| 615 | // restore the state of forced material
|
---|
| 616 | mUseForcedMaterial = fm;
|
---|
| 617 | return true;
|
---|
| 618 | }
|
---|
[312] | 619 |
|
---|
| 620 | struct BspSplitData
|
---|
| 621 | {
|
---|
| 622 | /// the current node |
---|
| 623 | BspNode *mNode; |
---|
| 624 | |
---|
| 625 | vector<Plane3 *> mPlanes; |
---|
| 626 | vector<bool> mSides; |
---|
| 627 | bool mIsFront; |
---|
| 628 | |
---|
[313] | 629 | BspSplitData(BspNode *node):
|
---|
| 630 | mNode(node), mIsFront(false)
|
---|
[312] | 631 | {}; |
---|
| 632 | BspSplitData(BspNode *node,
|
---|
| 633 | vector<Plane3 *> planes,
|
---|
| 634 | vector<bool> sides,
|
---|
| 635 | bool isFront):
|
---|
[313] | 636 | mNode(node), mPlanes(planes),
|
---|
[312] | 637 | mSides(sides), mIsFront(isFront)
|
---|
| 638 | {};
|
---|
| 639 | };
|
---|
| 640 |
|
---|
| 641 | void X3dExporter::ExportBspSplits(const BspTree &tree) |
---|
| 642 | { |
---|
| 643 | std::stack<BspSplitData> tStack; |
---|
| 644 | |
---|
[313] | 645 | BspSplitData tData(tree.GetRoot()); |
---|
[312] | 646 | tStack.push(tData); |
---|
| 647 | |
---|
[313] | 648 | PolygonContainer polys; |
---|
| 649 | |
---|
[312] | 650 | while (!tStack.empty()) |
---|
| 651 | { |
---|
| 652 | // filter polygons donw the tree |
---|
| 653 | BspSplitData tData = tStack.top(); |
---|
| 654 | tStack.pop(); |
---|
| 655 | |
---|
| 656 | if (!tData.mNode->IsLeaf()) |
---|
| 657 | { |
---|
| 658 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode); |
---|
[313] | 659 | if (tData.mNode != tree.GetRoot()) |
---|
| 660 | tData.mSides.push_back(tData.mIsFront); // add current side |
---|
[312] | 661 | |
---|
| 662 | // bounded plane is added to the polygons |
---|
[318] | 663 | Polygon3 *planePoly = tree.GetBoundingBox().CrossSection(*interior->GetPlane()); |
---|
[312] | 664 | |
---|
| 665 | // do all the splits with the previous planes |
---|
| 666 | for (int i = 0; i < (int)tData.mPlanes.size(); ++i) |
---|
| 667 | { |
---|
| 668 | VertexContainer splitPts; |
---|
| 669 | Polygon3 *frontPoly = new Polygon3(); |
---|
| 670 | Polygon3 *backPoly = new Polygon3(); |
---|
| 671 | |
---|
| 672 | if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT) |
---|
| 673 | { |
---|
| 674 | planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts); |
---|
| 675 | DEL_PTR(planePoly); |
---|
| 676 | |
---|
| 677 | if(tData.mSides[i] == true) |
---|
| 678 | { |
---|
| 679 | planePoly = frontPoly; |
---|
| 680 | DEL_PTR(backPoly); |
---|
| 681 | } |
---|
| 682 | else |
---|
| 683 | { |
---|
| 684 | planePoly = backPoly; |
---|
| 685 | DEL_PTR(frontPoly); |
---|
| 686 | } |
---|
| 687 | } |
---|
| 688 | } |
---|
| 689 | |
---|
[313] | 690 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes |
---|
| 691 | if (planePoly->Valid()) |
---|
| 692 | polys.push_back(planePoly); |
---|
[312] | 693 | else |
---|
| 694 | { |
---|
[318] | 695 | //Debug << "polygon not valid: " << *planePoly << " size: " << (int)planePoly->mVertices.size() << endl; |
---|
[313] | 696 | DEL_PTR(planePoly); |
---|
[312] | 697 | } |
---|
[313] | 698 | // push the children on the stack |
---|
| 699 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes, tData.mSides, true)); |
---|
| 700 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes, tData.mSides, false)); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | ExportPolygons(polys); |
---|
| 704 | CLEAR_CONTAINER(polys); |
---|
| 705 | } |
---|
[312] | 706 | |
---|
[313] | 707 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree) |
---|
| 708 | { |
---|
| 709 | std::stack<BspNode *> tStack; |
---|
[312] | 710 | |
---|
[313] | 711 | tStack.push(tree.GetRoot()); |
---|
| 712 | |
---|
| 713 | PolygonContainer polys; |
---|
[312] | 714 | |
---|
[313] | 715 | while (!tStack.empty()) |
---|
| 716 | { |
---|
| 717 | // filter polygons donw the tree |
---|
| 718 | BspNode *node = tStack.top(); |
---|
| 719 | tStack.pop(); |
---|
| 720 | |
---|
| 721 | if (!node->IsLeaf()) |
---|
[312] | 722 | { |
---|
[313] | 723 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
| 724 | |
---|
| 725 | // bounded plane is added to the polygons |
---|
[318] | 726 | polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane())); |
---|
[313] | 727 | |
---|
| 728 | // push the children on the stack |
---|
| 729 | tStack.push(interior->GetBack()); |
---|
| 730 | tStack.push(interior->GetFront()); |
---|
[312] | 731 | } |
---|
| 732 | } |
---|
[313] | 733 | |
---|
| 734 | ExportPolygons(polys); |
---|
| 735 | CLEAR_CONTAINER(polys); |
---|
| 736 | } |
---|