[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"
|
---|
[162] | 10 |
|
---|
| 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;
|
---|
| 104 | case Intersectable::VIEWCELL:
|
---|
| 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$$
|
---|
| 117 | // in the future check whether the mesh was not already exportet
|
---|
| 118 | // and use a reference to the that mesh instead
|
---|
| 119 | ExportMesh(object->GetMesh());
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void
|
---|
[242] | 123 | X3dExporter::ExportViewCell(ViewCell *object)
|
---|
| 124 | {
|
---|
| 125 | if (object->GetMesh())
|
---|
| 126 | ExportMesh(object->GetMesh());
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | void
|
---|
[162] | 130 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
| 131 | {
|
---|
| 132 |
|
---|
| 133 | stream<<"<Shape>"<<endl;
|
---|
| 134 | stream<<"<Appearance>"<<endl;
|
---|
[176] | 135 |
|
---|
[162] | 136 | // $$ tmp -> random material
|
---|
| 137 |
|
---|
| 138 | float r, g, b;
|
---|
| 139 |
|
---|
[176] | 140 | if (mUseForcedMaterial) {
|
---|
| 141 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 142 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 143 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 144 |
|
---|
| 145 | } else
|
---|
| 146 | if (mesh->mMaterial) {
|
---|
| 147 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
| 148 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
| 149 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
| 150 | } else {
|
---|
| 151 | r = RandomValue(0.5, 1.0);
|
---|
| 152 | g = RandomValue(0.5, 1.0);
|
---|
| 153 | b = RandomValue(0.5, 1.0);
|
---|
| 154 | }
|
---|
[162] | 155 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
| 156 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
| 157 | stream<<"</Appearance>"<<endl;
|
---|
| 158 |
|
---|
| 159 |
|
---|
[191] | 160 | if (mWireframe)
|
---|
[162] | 161 | stream<<"<IndexedLineSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 162 | else
|
---|
| 163 | stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 164 |
|
---|
| 165 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
| 166 |
|
---|
| 167 | int index = 0;
|
---|
| 168 |
|
---|
| 169 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
| 170 | Face *face = *fi;
|
---|
| 171 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
| 172 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
| 173 | stream<<*vi<<" ";
|
---|
| 174 | stream<<"-1"<<endl;
|
---|
| 175 | }
|
---|
| 176 | stream<<"\" >"<<endl;
|
---|
| 177 |
|
---|
| 178 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 179 |
|
---|
| 180 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
| 181 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
| 182 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
| 183 | stream<<","<<endl;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | stream<<"\" >"<<endl;
|
---|
| 187 | stream<<"</Coordinate>"<<endl;
|
---|
| 188 |
|
---|
[191] | 189 | if (mWireframe)
|
---|
[162] | 190 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 191 | else
|
---|
| 192 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
| 193 |
|
---|
| 194 | stream<<"</Shape>"<<endl;
|
---|
| 195 |
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[242] | 198 |
|
---|
| 199 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
| 200 | {
|
---|
| 201 | stream << "<Shape>" << endl;
|
---|
| 202 | stream << "<Appearance>" << endl;
|
---|
| 203 |
|
---|
| 204 | // $$ tmp -> random material
|
---|
| 205 |
|
---|
| 206 | float r, g, b;
|
---|
| 207 |
|
---|
| 208 | if (mUseForcedMaterial)
|
---|
| 209 | {
|
---|
| 210 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 211 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 212 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 213 | }
|
---|
| 214 | else if (poly->mMaterial)
|
---|
| 215 | {
|
---|
| 216 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
| 217 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
| 218 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
| 219 | } else
|
---|
| 220 | {
|
---|
| 221 | r = RandomValue(0.5, 1.0);
|
---|
| 222 | g = RandomValue(0.5, 1.0);
|
---|
| 223 | b = RandomValue(0.5, 1.0);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 227 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 228 |
|
---|
| 229 | stream << "</Appearance>" << endl;
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | if (mWireframe)
|
---|
| 233 | stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 234 | else
|
---|
| 235 | stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 236 |
|
---|
| 237 | VertexContainer::const_iterator vi = poly->mVertices.begin();
|
---|
| 238 |
|
---|
| 239 | stream << "<Coordinate point=\"" << endl;
|
---|
| 240 |
|
---|
| 241 | for (; vi != poly->mVertices.end(); ++vi)
|
---|
| 242 | {
|
---|
| 243 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 244 | stream << "," << endl;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | stream<<"\" >"<<endl;
|
---|
| 248 | stream<<"</Coordinate>"<<endl;
|
---|
| 249 |
|
---|
| 250 | if (mWireframe)
|
---|
| 251 | stream << "</IndexedLineSet>" << endl;
|
---|
| 252 | else
|
---|
| 253 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 254 |
|
---|
| 255 | stream << "</Shape>" << endl;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
[162] | 259 | bool
|
---|
| 260 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
| 261 | {
|
---|
| 262 | Mesh *mesh = new Mesh;
|
---|
| 263 | // add 6 vertices of the box
|
---|
| 264 | int index = mesh->mVertices.size();
|
---|
| 265 | for (int i=0; i < 8; i++) {
|
---|
| 266 | Vector3 v;
|
---|
| 267 | box.GetVertex(i, v);
|
---|
| 268 | mesh->mVertices.push_back(v);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 272 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 273 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 274 |
|
---|
| 275 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 276 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 277 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 278 |
|
---|
| 279 | ExportMesh(mesh);
|
---|
| 280 | delete mesh;
|
---|
| 281 | return true;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | bool
|
---|
[242] | 285 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
[162] | 286 | {
|
---|
[242] | 287 | if (mExportRayDensity)
|
---|
| 288 | {
|
---|
| 289 | return false;//ExportKdTreeRayDensity(tree);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | stack<BspNode *> tStack;
|
---|
| 293 |
|
---|
| 294 | tStack.push(tree.GetRoot());
|
---|
| 295 |
|
---|
| 296 | Mesh *mesh = new Mesh;
|
---|
| 297 |
|
---|
[260] | 298 | //AxisAlignedBox3 box = tree.GetBoundingBox();
|
---|
| 299 | //ExportBox(box);
|
---|
| 300 |
|
---|
| 301 | while (!tStack.empty())
|
---|
[242] | 302 | {
|
---|
| 303 | BspNode *node = tStack.top();
|
---|
| 304 |
|
---|
| 305 | tStack.pop();
|
---|
| 306 |
|
---|
[260] | 307 | if (tree.StorePolys()) // extract the polygons
|
---|
| 308 | {
|
---|
| 309 | PolygonContainer::const_iterator it;
|
---|
| 310 | PolygonContainer::const_iterator it_end = node->GetPolygons()->end();
|
---|
| 311 |
|
---|
| 312 | for (it = node->GetPolygons()->begin(); it != it_end; ++ it)
|
---|
| 313 | ExportPolygon(*it);
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[242] | 316 | if (!node->IsLeaf())
|
---|
| 317 | {
|
---|
| 318 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 319 |
|
---|
| 320 | tStack.push(interior->GetFront());
|
---|
| 321 | tStack.push(interior->GetBack());
|
---|
[260] | 322 |
|
---|
[242] | 323 | }
|
---|
| 324 | else
|
---|
| 325 | {
|
---|
| 326 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
[260] | 327 | // export view cell geometry
|
---|
| 328 | if (!tree.StorePolys() && leaf->GetViewCell())
|
---|
[242] | 329 | ExportViewCell(leaf->GetViewCell());
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | return true;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | bool X3dExporter::ExportKdTree(const KdTree &tree)
|
---|
| 337 | {
|
---|
| 338 | if (mExportRayDensity) {
|
---|
[191] | 339 | return ExportKdTreeRayDensity(tree);
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[162] | 342 | stack<KdNode *> tStack;
|
---|
| 343 |
|
---|
| 344 | tStack.push(tree.GetRoot());
|
---|
| 345 |
|
---|
| 346 | Mesh *mesh = new Mesh;
|
---|
| 347 |
|
---|
| 348 | while (!tStack.empty()) {
|
---|
| 349 | KdNode *node = tStack.top();
|
---|
| 350 | tStack.pop();
|
---|
| 351 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 352 | // add 6 vertices of the box
|
---|
| 353 | int index = mesh->mVertices.size();
|
---|
| 354 | for (int i=0; i < 8; i++) {
|
---|
| 355 | Vector3 v;
|
---|
| 356 | box.GetVertex(i, v);
|
---|
| 357 | mesh->mVertices.push_back(v);
|
---|
| 358 | }
|
---|
| 359 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 360 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 361 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 362 |
|
---|
| 363 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 364 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 365 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 366 |
|
---|
| 367 | if (!node->IsLeaf()) {
|
---|
| 368 | KdInterior *interior = (KdInterior *)node;
|
---|
| 369 | tStack.push(interior->mFront);
|
---|
| 370 | tStack.push(interior->mBack);
|
---|
| 371 | }
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | ExportMesh(mesh);
|
---|
| 375 | delete mesh;
|
---|
| 376 | return true;
|
---|
[242] | 377 | // TODO
|
---|
| 378 | return true;
|
---|
[162] | 379 | }
|
---|
| 380 |
|
---|
| 381 |
|
---|
[191] | 382 | bool
|
---|
| 383 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
| 384 | {
|
---|
| 385 | stack<KdNode *> tStack;
|
---|
| 386 |
|
---|
| 387 | tStack.push(tree.GetRoot());
|
---|
| 388 |
|
---|
| 389 | bool fm = mUseForcedMaterial;
|
---|
| 390 | mUseForcedMaterial = true;
|
---|
| 391 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 392 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 393 | while (!tStack.empty()) {
|
---|
| 394 | KdNode *node = tStack.top();
|
---|
| 395 | tStack.pop();
|
---|
| 396 | if (node->IsLeaf()) {
|
---|
| 397 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 398 | Mesh *mesh = new Mesh;
|
---|
| 399 |
|
---|
| 400 | // add 6 vertices of the box
|
---|
| 401 | int index = mesh->mVertices.size();
|
---|
| 402 | for (int i=0; i < 8; i++) {
|
---|
| 403 | Vector3 v;
|
---|
| 404 | box.GetVertex(i, v);
|
---|
| 405 | mesh->mVertices.push_back(v);
|
---|
| 406 | }
|
---|
| 407 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 408 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 409 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 410 |
|
---|
| 411 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 412 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 413 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 414 |
|
---|
| 415 |
|
---|
| 416 | // set the mesh material according to the ray density
|
---|
| 417 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 418 | if (leaf->mPassingRays.mRays) {
|
---|
| 419 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
| 420 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
| 421 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
| 422 | ///(float)leaf->mPassingRays.mRays;
|
---|
| 423 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
| 424 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 425 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 426 | ExportMesh(mesh);
|
---|
| 427 | }
|
---|
| 428 | delete mesh;
|
---|
| 429 | } else {
|
---|
| 430 | KdInterior *interior = (KdInterior *)node;
|
---|
| 431 | tStack.push(interior->mFront);
|
---|
| 432 | tStack.push(interior->mBack);
|
---|
| 433 | }
|
---|
| 434 | }
|
---|
| 435 | // restore the state of forced material
|
---|
| 436 | mUseForcedMaterial = fm;
|
---|
| 437 | return true;
|
---|
| 438 | }
|
---|