source: trunk/VUT/GtpVisibilityPreprocessor/src/X3dExporter.cpp @ 438

Revision 438, 28.0 KB checked in by bittner, 19 years ago (diff)

vss updates

RevLine 
[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"
[386]10#include "VssRay.h"
[418]11#include "VspKdTree.h"
[434]12#include "VssTree.h"
[162]13
14X3dExporter::X3dExporter(const string filename):Exporter(filename)
15{
16  stream.open(mFilename.c_str());
17  stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
18  stream<<"<X3D>"<<endl;
19  stream<<"<Scene>"<<endl;
20 
21}
22
23X3dExporter::~X3dExporter()
24{
25  stream<<"</Scene>"<<endl;
26  stream<<"</X3D>"<<endl;
27  stream.close();
28}
29
[176]30
[386]31//  bool
32//  X3dExporter::ExportRays(const vector<Ray> &rays,
33//                                                                                              const float length,
34//                                                                                              const RgbColor &color)
35//  {
36//    vector<Ray>::const_iterator ri = rays.begin();
37//    stream<<"<Shape>"<<endl;
38//    stream<<"<Appearance>"<<endl;
39//    stream<<"<Material ambientColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
40//      "\" />"<<endl;
41//    stream<<"</Appearance>"<<endl;
42 
43//    stream<<"<IndexedLineSet coordIndex=\""<<endl;
44
45//    int index = 0;
46//    for (; ri != rays.end(); ri++) {
47//      stream<<index<<" "<<index+1<<" -1\n";
48//      index+=2;
49//    }
50 
51//    stream<<"\" >"<<endl;
52 
53//    stream<<"<Coordinate  point=\""<<endl;
54 
55//    ri = rays.begin();
56//    for (; ri != rays.end(); ri++) {
57//      Vector3 a = (*ri).GetLoc();
58   
59//      Vector3 b;
60//      if (length < 0)
61//        b = (*ri).GetLoc() - length*(*ri).GetDir();
62//      else
63//        if ((*ri).intersections.size()==0)
64//      b = (*ri).GetLoc() + length*(*ri).GetDir();
65//        else
66//      b = (*ri).Extrap((*ri).intersections[0].mT);
67   
68//      stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
69//      stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
70//    }
71 
72//    stream<<"\" >"<<endl;
73//    stream<<"</Coordinate>"<<endl;
74//    stream<<"</IndexedLineSet>"<<endl;
75//    stream<<"</Shape>"<<endl;
76//    return true;
77//  }
78
79
[162]80bool
[386]81X3dExporter::ExportRays(const RayContainer &rays,
82                                                                                                const float length,
83                                                                                                const RgbColor &color)
[162]84{
[386]85  RayContainer::const_iterator ri = rays.begin();
86
[162]87  stream<<"<Shape>"<<endl;
[176]88  stream<<"<Appearance>"<<endl;
[427]89  stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
[176]90    "\" />"<<endl;
91  stream<<"</Appearance>"<<endl;
92 
[209]93  stream<<"<IndexedLineSet coordIndex=\""<<endl;
[162]94
95  int index = 0;
96  for (; ri != rays.end(); ri++) {
97    stream<<index<<" "<<index+1<<" -1\n";
98    index+=2;
99  }
100 
101  stream<<"\" >"<<endl;
102 
103  stream<<"<Coordinate  point=\""<<endl;
104 
105  ri = rays.begin();
106  for (; ri != rays.end(); ri++) {
[386]107    Vector3 a = (*ri)->GetLoc();
[162]108   
[176]109    Vector3 b;
110    if (length < 0)
[386]111      b = (*ri)->GetLoc() - length*(*ri)->GetDir();
[176]112    else
[386]113      if ((*ri)->intersections.size()==0)
114        b = (*ri)->GetLoc() + length*(*ri)->GetDir();
[176]115      else
[386]116        b = (*ri)->Extrap((*ri)->intersections[0].mT);
[176]117   
[162]118    stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
119    stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
120  }
121 
122  stream<<"\" >"<<endl;
123  stream<<"</Coordinate>"<<endl;
124  stream<<"</IndexedLineSet>"<<endl;
125  stream<<"</Shape>"<<endl;
[386]126
[162]127  return true;
128}
129
[349]130bool
[386]131X3dExporter::ExportRays(const VssRayContainer &rays,
132                                                                                                const RgbColor &color)
[349]133{
[386]134  VssRayContainer::const_iterator ri = rays.begin();
[349]135
136  stream<<"<Shape>"<<endl;
137  stream<<"<Appearance>"<<endl;
[427]138  stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
[349]139    "\" />"<<endl;
140  stream<<"</Appearance>"<<endl;
141 
142  stream<<"<IndexedLineSet coordIndex=\""<<endl;
143
144  int index = 0;
145  for (; ri != rays.end(); ri++) {
146    stream<<index<<" "<<index+1<<" -1\n";
147    index+=2;
148  }
149 
150  stream<<"\" >"<<endl;
151 
152  stream<<"<Coordinate  point=\""<<endl;
153 
154  ri = rays.begin();
155  for (; ri != rays.end(); ri++) {
[386]156    Vector3 a = (*ri)->GetOrigin();
[434]157                Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize(b - a);
[426]158       
[349]159    stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
[434]160                stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
[349]161  }
162 
163  stream<<"\" >"<<endl;
164  stream<<"</Coordinate>"<<endl;
165  stream<<"</IndexedLineSet>"<<endl;
166  stream<<"</Shape>"<<endl;
[386]167       
[349]168  return true;
169}
170
[162]171void
172X3dExporter::ExportSceneNode(SceneGraphNode *node)
173{
174  stream<<"<Group>"<<endl;
175
176  SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
177  for (; ni != node->mChildren.end(); ni++)
178    ExportSceneNode(*ni);
179 
180 
[176]181  ObjectContainer::const_iterator mi = node->mGeometry.begin();
[162]182  for (; mi != node->mGeometry.end(); mi++) {
183    // export the transform...
[176]184    ExportIntersectable(*mi);
[162]185  }
186 
187  stream<<"</Group>"<<endl;
188
189}
[176]190void
191X3dExporter::ExportIntersectable(Intersectable *object)
192{
193  switch (object->Type()) {
194  case Intersectable::MESH_INSTANCE:
195  case Intersectable::TRANSFORMED_MESH_INSTANCE:
196    ExportMeshInstance((MeshInstance *)object);
[242]197        break;
[308]198  case Intersectable::VIEW_CELL:
[242]199        ExportViewCell((ViewCell *)object);
[176]200    break;
201  default:
202    cerr<<"Sorry the export for object not yet defined"<<endl;
203    break;
204  }
205}
[162]206
207void
[176]208X3dExporter::ExportMeshInstance(MeshInstance *object)
209{
210  // $$JB$$
[312]211  // in the future check whether the mesh was not already exported
[176]212  // and use a reference to the that mesh instead
213  ExportMesh(object->GetMesh());
214}
215
[313]216void
217X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
[261]218{
[313]219        ViewCellContainer::const_iterator it, it_end = viewCells.end();
[261]220
[313]221        for (it = viewCells.begin(); it != it_end; ++ it)
[261]222                ExportViewCell(*it);
223}
[313]224
[373]225void
[379]226X3dExporter::ExportBspViewCellPartition(const BspTree &tree, const int maxPvs)
[373]227{
228        ViewCellContainer viewCells;
229        tree.CollectViewCells(viewCells);
230
231        ViewCellContainer::const_iterator it, it_end = viewCells.end();
232
[380]233        if (maxPvs > 0)
[379]234                mUseForcedMaterial = true;
235
[373]236        for (it = viewCells.begin(); it != it_end; ++ it)
237        {
[379]238                if (maxPvs > 0)
[380]239                {
240                        mForcedMaterial.mDiffuseColor.b = 1.0f;
241                        float importance = (float)(*it)->GetPvs().GetSize() / (float)maxPvs;
[379]242
[380]243                        mForcedMaterial.mDiffuseColor.r = importance;
244                        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
245                }
246
[373]247                if ((*it)->GetMesh())
248                        ExportViewCell(*it);
249                else
250                {
251                        PolygonContainer cell;
252                        tree.ConstructGeometry(dynamic_cast<BspViewCell *>(*it), cell);
[379]253
[373]254                        ExportPolygons(cell);
255                }
256        }
257}
258
259void
[396]260X3dExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
[373]261{
[396]262        stack<pair<BspNode *, BspNodeGeometry *> > tStack;
263        ViewCell::NewMail();
[373]264
[396]265        BspNodeGeometry *geom = new BspNodeGeometry();
266        tree.ConstructGeometry(tree.GetRoot(), *geom);
[373]267
[396]268        tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
269
270        if (maxPvs > 0)
271                mUseForcedMaterial = true;
272
273        while (!tStack.empty())
[373]274        {
[396]275                BspNode *node = tStack.top().first;
276                BspNodeGeometry *cell = tStack.top().second;
277                tStack.pop();
278
279                if (!node->IsLeaf())
280                {
281                        BspInterior *interior = dynamic_cast<BspInterior *>(node);
282                                                               
283                        BspNodeGeometry *front = new BspNodeGeometry();
284                        BspNodeGeometry *back = new BspNodeGeometry();
285
286                        cell->SplitGeometry(*front, *back, tree, *interior->GetPlane());
287
288                        tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
289                        tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetBack(), back));
290                }
291                else
292                {
293                        if (maxPvs > 0)
294                        {
295                                BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
296
297                                mForcedMaterial.mDiffuseColor.b = 1.0f;
298                                float importance = (float)leaf->GetViewCell()->GetPvs().GetSize() / (float)maxPvs;
299
300                                mForcedMaterial.mDiffuseColor.r = importance;
301                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
302                        }
303
304                        ExportPolygons(cell->mPolys);
305                }
306               
307                DEL_PTR(cell);
[373]308        }
309}
310
[176]311void
[261]312X3dExporter::ExportViewCell(ViewCell *viewCell)
[242]313{
[261]314        if (viewCell->GetMesh())
315                ExportMesh(viewCell->GetMesh());
[242]316}
317
318void
[162]319X3dExporter::ExportMesh(Mesh *mesh)
320{
321
322  stream<<"<Shape>"<<endl;
323  stream<<"<Appearance>"<<endl;
[176]324 
[162]325  // $$ tmp -> random material
326 
327  float r, g, b;
328
[176]329  if (mUseForcedMaterial) {
330    r = mForcedMaterial.mDiffuseColor.r;
331    g = mForcedMaterial.mDiffuseColor.g;
332    b = mForcedMaterial.mDiffuseColor.b;
333   
334  } else
335    if (mesh->mMaterial) {
336      r = mesh->mMaterial->mDiffuseColor.r;
337      g = mesh->mMaterial->mDiffuseColor.g;
338      b = mesh->mMaterial->mDiffuseColor.b;
339    } else {
340      r = RandomValue(0.5, 1.0);
341      g = RandomValue(0.5, 1.0);
342      b = RandomValue(0.5, 1.0);
343    }
[162]344  stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
345    "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
346  stream<<"</Appearance>"<<endl;
347
348
[191]349  if (mWireframe)
[427]350    stream<<"<IndexedLineSet coordIndex=\""<<endl;
[162]351  else
352    stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
353
354  FaceContainer::const_iterator fi = mesh->mFaces.begin();
355
356  int index = 0;
357 
358  for (; fi != mesh->mFaces.end(); fi++) {
359    Face *face = *fi;
360    VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
361    for (; vi != face->mVertexIndices.end(); vi++)
362      stream<<*vi<<" ";
[378]363        if (mWireframe) // final line to finish polygon
364                stream << (*face->mVertexIndices.begin()) << " ";
365
[162]366    stream<<"-1"<<endl;
367  }
368  stream<<"\" >"<<endl;
369
370  stream<<"<Coordinate  point=\""<<endl;
371 
372  VertexContainer::const_iterator vi = mesh->mVertices.begin();
373  for (; vi != mesh->mVertices.end(); vi++) {
374    stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
375    stream<<","<<endl;
376  }
377 
378  stream<<"\" >"<<endl;
379  stream<<"</Coordinate>"<<endl;
380
[191]381  if (mWireframe)
[162]382    stream<<"</IndexedLineSet>"<<endl;
383  else
384    stream<<"</IndexedFaceSet>"<<endl;
385 
386  stream<<"</Shape>"<<endl;
387
388}
389
[242]390
391void X3dExporter::ExportPolygon(Polygon3 *poly)
392{
393        stream << "<Shape>" << endl;
394        stream << "<Appearance>" << endl;
395 
396        // $$ tmp -> random material
397 
398        float r, g, b;
399
400        if (mUseForcedMaterial)
401        {
402                r = mForcedMaterial.mDiffuseColor.r;
403                g = mForcedMaterial.mDiffuseColor.g;
404                b = mForcedMaterial.mDiffuseColor.b;
405        }
406        else if (poly->mMaterial)
407        {
408                r = poly->mMaterial->mDiffuseColor.r;
409                g = poly->mMaterial->mDiffuseColor.g;
410                b = poly->mMaterial->mDiffuseColor.b;
411        } else
412        {
413                r = RandomValue(0.5, 1.0);
414                g = RandomValue(0.5, 1.0);
415                b = RandomValue(0.5, 1.0);
416        }
417
418        stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
419                   << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
420
421    stream << "</Appearance>" << endl;
422
423
[262]424        //-- create and write indices
[242]425        if (mWireframe)
426                stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
427        else
428                stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
429
[262]430        int index = 0;
431       
432        VertexContainer::const_iterator vi; 
433       
[333]434        for (index = 0; index < (int)poly->mVertices.size(); ++ index)
[318]435                stream << index << " ";
436       
437        if (mWireframe) // final line to finish polygon
438                stream << "0 ";
439
[262]440        stream << "-1" << endl;
441        stream << "\" >" << endl;
442       
[242]443        stream << "<Coordinate  point=\"" << endl;
444 
[262]445        for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
[242]446        {
447                stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
448                stream << "," << endl;
449        }
450 
[262]451        stream << "\" >" << endl;
452        stream << "</Coordinate>" << endl;
[242]453
454        if (mWireframe)
455                stream << "</IndexedLineSet>" << endl;
456        else
457                stream << "</IndexedFaceSet>" << endl;
458 
459        stream << "</Shape>" << endl;
460}
461
[313]462void X3dExporter::ExportPolygons(const PolygonContainer &polys)
[263]463{
[318]464        stream << "<Shape>" << endl;
[263]465        stream << "<Appearance>" << endl;
466 
467        // $$ tmp -> random material
468 
469        float r, g, b;
[242]470
[263]471        if (mUseForcedMaterial)
472        {
473                r = mForcedMaterial.mDiffuseColor.r;
474                g = mForcedMaterial.mDiffuseColor.g;
475                b = mForcedMaterial.mDiffuseColor.b;
476        }
477        else
478        {
479                r = RandomValue(0.5, 1.0);
480                g = RandomValue(0.5, 1.0);
481                b = RandomValue(0.5, 1.0);
482        }
483
484        stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
485                   << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
486
487    stream << "</Appearance>" << endl;
488
489
490        //-- create and write indices
491        if (mWireframe)
492                stream << "<IndexedLineSet ccw=\"TRUE\" coordIndex=\"" << endl;
493        else
494                stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
495
496        int index = 0;
497       
498        PolygonContainer::const_iterator pit;
499
500    VertexContainer::const_iterator vi; 
501       
[313]502        for (pit = polys.begin(); pit != polys.end(); ++pit)
[263]503        {
504                Polygon3 *poly = *pit;
[318]505                int startIdx = index;
[263]506                for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
507                {
508                        stream << index ++ << " ";
509                }
[318]510
511                stream << startIdx << " ";// finish line
[263]512                stream << "-1" << endl;
513        }
514
515        stream << "\" >" << endl;
516       
517        stream << "<Coordinate  point=\"" << endl;
[318]518        for (pit = polys.begin(); pit != polys.end(); ++ pit)
[263]519        {
520                Polygon3 *poly = *pit;
521        for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
522                {
523                        stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
524                        stream << "," << endl;
525                }
526        }
527        stream << "\" >" << endl;
528        stream << "</Coordinate>" << endl;
529
530        if (mWireframe)
531                stream << "</IndexedLineSet>" << endl;
532        else
533                stream << "</IndexedFaceSet>" << endl;
534 
535        stream << "</Shape>" << endl;
536}
537
[162]538bool
539X3dExporter::ExportBox(const AxisAlignedBox3 &box)
540{
541  Mesh *mesh = new Mesh;
542  // add 6 vertices of the box
[289]543  int index = (int)mesh->mVertices.size();
[162]544  for (int i=0; i < 8; i++) {
545    Vector3 v;
546    box.GetVertex(i, v);
547    mesh->mVertices.push_back(v);
548  }
549 
550  mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
551  mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
552  mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
553 
554  mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
555  mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
556  mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
557 
558  ExportMesh(mesh);
559  delete mesh;
560  return true;
561}
562
563bool
[242]564X3dExporter::ExportBspTree(const BspTree &tree)
[162]565{
[242]566        if (mExportRayDensity)
567        {
[263]568                return ExportBspTreeRayDensity(tree);
[242]569        }
570 
[264]571        bool savedWireframe = mWireframe;
572
573        SetWireframe();
574       
[396]575        ExportBox(tree.GetBoundingBox());
576       
[264]577        if (!savedWireframe)
578                SetFilled();
579
[373]580        // export view cells
581        ExportBspViewCellPartition(tree);       
[289]582
[263]583        return true;
[242]584}
585
[425]586bool X3dExporter::ExportVspKdTree(const VspKdTree &tree, const int maxPvs)
[418]587{
[422]588        stack<VspKdTreeNode *> tStack;
[418]589
[422]590        tStack.push(tree.GetRoot());
591
[425]592        //Mesh *mesh = new Mesh;
[418]593 
[425]594        if (maxPvs > 0)
595                mUseForcedMaterial = true;
596
[422]597        while (!tStack.empty())
598        {
599                VspKdTreeNode *node = tStack.top();
[418]600   
[422]601                tStack.pop();
[418]602
[426]603                if (node->IsLeaf())
604                {
605                        AxisAlignedBox3 box = tree.GetBBox(node);
[425]606
[426]607                        Mesh *mesh = new Mesh;
[425]608
[426]609                        // add 6 vertices of the box
610                        int index = (int)mesh->mVertices.size();
[418]611
[426]612                        for (int i=0; i < 8; ++ i)
613                        {
614                                Vector3 v;
615                                box.GetVertex(i, v);
616                                mesh->mVertices.push_back(v);
617                        }
[418]618
[426]619                        mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
620                        mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
621                        mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
[418]622
[426]623                        mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
624                        mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
625                        mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
[425]626
[426]627                        if (maxPvs > 0)
628                        {
629                                VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node);
[425]630
[426]631                                mForcedMaterial.mDiffuseColor.b = 1.0f;
632                               
633                                leaf->UpdatePvsSize();
634                       
635                                const float importance = (float)leaf->GetPvsSize() / (float)maxPvs;
636                                mForcedMaterial.mDiffuseColor.r = importance;
637                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
638                        }
[425]639
[426]640                        ExportMesh(mesh);
641                        DEL_PTR(mesh);
642                }
643                else 
[422]644                {
645                        VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node);
646                        tStack.push(interior->GetFront());
647                        tStack.push(interior->GetBack());
648                }
649        }
[418]650 
[425]651        //ExportMesh(mesh);
652        //DEL_PTR(mesh);
[418]653
[422]654        return true;
[418]655}
656
[242]657bool X3dExporter::ExportKdTree(const KdTree &tree)
658{
659         if (mExportRayDensity) {
[191]660    return ExportKdTreeRayDensity(tree);
661  }
662 
[162]663  stack<KdNode *> tStack;
664
665  tStack.push(tree.GetRoot());
666
667  Mesh *mesh = new Mesh;
668 
669  while (!tStack.empty()) {
670    KdNode *node = tStack.top();
671    tStack.pop();
672    AxisAlignedBox3 box = tree.GetBox(node);
673    // add 6 vertices of the box
[289]674    int index = (int)mesh->mVertices.size();
[162]675    for (int i=0; i < 8; i++) {
676      Vector3 v;
677      box.GetVertex(i, v);
678      mesh->mVertices.push_back(v);
679    }
680    mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
681    mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
682    mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
683
684    mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
685    mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
686    mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
687
688    if (!node->IsLeaf()) {
689      KdInterior *interior = (KdInterior *)node;
690      tStack.push(interior->mFront);
691      tStack.push(interior->mBack);
692    }
693  }
694 
695  ExportMesh(mesh);
696  delete mesh;
697  return true;
[242]698        // TODO
699        return true;
[162]700}
701
702
[434]703void
704X3dExporter::AddBoxToMesh(const AxisAlignedBox3 &box,
705                                                                                                        Mesh *mesh)
706{
707        // add 6 vertices of the box
708        int index = (int)mesh->mVertices.size();
709       
710        for (int i=0; i < 8; i++) {
711                Vector3 v;
712                box.GetVertex(i, v);
713                mesh->mVertices.push_back(v);
714        }
715       
716        mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
717        mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
718        mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
719       
720        mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
721        mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
722        mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
723
724}
725
[191]726bool
[438]727X3dExporter::ExportVssTree(const VssTree &tree
728                                                   )
[434]729{
730  stack<VssTreeNode *> tStack;
731       
732  tStack.push(tree.GetRoot());
733       
734  Mesh *mesh = new Mesh;
735  VssRayContainer rays;
736       
737  while (!tStack.empty()) {
738
739                VssTreeNode *node = tStack.top();
740    tStack.pop();
741
742                       
743    if (!node->IsLeaf()) {
744      VssTreeInterior *interior = (VssTreeInterior *)node;
745      tStack.push(interior->front);
746      tStack.push(interior->back);
747    } else {
748                        VssTreeLeaf *leaf = (VssTreeLeaf *)node;
749                        AxisAlignedBox3 box;
750                        box = tree.GetBBox(leaf);
751                        AddBoxToMesh(box, mesh);
752
753                        if (tree.ValidLeaf(leaf)) {
754                               
755                                Vector3 origin = box.Center();
756                                box = tree.GetDirBBox(leaf);
757                                VssRay *ray;
758                               
759                                const indices[][2] = {{0,0}, {0,1}, {1,1}, {1,0}};
760                                MeshInstance dummy(mesh);
761                                for (int i=0; i < 4; i++) {
762                                        //                              Vector3 v = box.GetVertex(indices[i][0], indices[i][1], 0);
763                                        Vector3 v = box.Center();
764                                       
765                                        Vector3 direction = VssRay::GetDirection(v.x, v.y);
[438]766                                        if (Magnitude(direction) > Limits::Small)
767                                          direction.Normalize();
768                                        else
769                                          direction = Vector3(0, 1, 0);
[435]770                                        float k = 100.0f*leaf->GetImportance();
[434]771                                        // get 4 corners of the ray directions
772                                       
773                                        ray = new VssRay(origin, origin + (direction*k), NULL, &dummy);
774                                        rays.push_back(ray);
775                                }
776                        }
777                }
778  }
779
780  ExportMesh(mesh);
[438]781  ExportRays(rays);
782  CLEAR_CONTAINER(rays);
[434]783  delete mesh;
784  return true;
785}
786
787bool
[438]788X3dExporter::ExportVssTree2(const VssTree &tree,
789                                                        const Vector3 direction
790                                                        )
791{
792  stack<VssTreeNode *> tStack;
793       
794
795  mUseForcedMaterial = true;
796
797  Vector3 dirParam;
798
799  dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
800  dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
801
802  float maxImportance = 0.0f;
803  tStack.push(tree.GetRoot());
804  while (!tStack.empty()) {
805       
806        VssTreeNode *node = tStack.top();
807    tStack.pop();
808       
809    if (!node->IsLeaf()) {
810      VssTreeInterior *interior = (VssTreeInterior *)node;
811          if (interior->axis < 3) {
812                tStack.push(interior->front);
813                tStack.push(interior->back);
814          } else {
815                if (dirParam[interior->axis-3] < interior->position)
816                  tStack.push(interior->back);
817                else
818                  tStack.push(interior->front);
819          }
820    } else {
821          VssTreeLeaf *leaf = (VssTreeLeaf *)node;
822          if (tree.ValidLeaf(leaf)) {
823                float i = leaf->GetImportance();
824                if (i > maxImportance)
825                  maxImportance = i;
826          }
827        }
828  }
829
830  tStack.push(tree.GetRoot());
831  while (!tStack.empty()) {
832
833        VssTreeNode *node = tStack.top();
834    tStack.pop();
835       
836                       
837    if (!node->IsLeaf()) {
838      VssTreeInterior *interior = (VssTreeInterior *)node;
839          if (interior->axis < 3) {
840                tStack.push(interior->front);
841                tStack.push(interior->back);
842          } else {
843                if (dirParam[interior->axis-3] < interior->position)
844                  tStack.push(interior->back);
845                else
846                  tStack.push(interior->front);
847          }
848    } else {
849          VssTreeLeaf *leaf = (VssTreeLeaf *)node;
850          if (tree.ValidLeaf(leaf)) {
851                AxisAlignedBox3 box;
852                box = tree.GetBBox(leaf);
853                Mesh *mesh = new Mesh;
854                AddBoxToMesh(box, mesh);
855               
856                // get 4 corners of the ray directions
857               
858                mForcedMaterial.mDiffuseColor.b = 1.0f;
859                mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
860                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
861               
862                ExportMesh(mesh);
863                delete mesh;
864          }
865        }
866  }
867
868  mUseForcedMaterial = false;
869
870  return true;
871}
872
873bool
[263]874X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
875{
[311]876        stack<BspNode *> tStack;
877
878        tStack.push(tree.GetRoot());
879
880        bool fm = mUseForcedMaterial;
881       
882        mUseForcedMaterial = true;
883       
884        mForcedMaterial.mDiffuseColor.g = 1.0f;
885        mForcedMaterial.mDiffuseColor.b = 1.0f;
886 
887        while (!tStack.empty())
888        {
889                BspNode *node = tStack.top();
890                tStack.pop();
891
892                if (node->IsLeaf())
893                {
894                        ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
895     
896                        // set the mesh material according to the ray density
897                        if (vc->mPassingRays.mRays)
898                        {
[313]899                                float importance =
[438]900                                  vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
901                               
[311]902                                mForcedMaterial.mDiffuseColor.r = importance;
903                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
904                                ExportViewCell(vc);
[438]905                        }
[311]906                } else
907                {
908                        BspInterior *interior = (BspInterior *)node;
909                        tStack.push(interior->GetFront());
910                        tStack.push(interior->GetBack());
911                }
912        }
913 
914        // restore the state of forced material
915        mUseForcedMaterial = fm;
916
[263]917        return true;
918}
919
920bool
[191]921X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
922{
923  stack<KdNode *> tStack;
924
925  tStack.push(tree.GetRoot());
926
927  bool fm = mUseForcedMaterial;
928  mUseForcedMaterial = true;
929  mForcedMaterial.mDiffuseColor.g = 1.0f;
930  mForcedMaterial.mDiffuseColor.b = 1.0f;
931  while (!tStack.empty()) {
932    KdNode *node = tStack.top();
933    tStack.pop();
934    if (node->IsLeaf()) {
935      AxisAlignedBox3 box = tree.GetBox(node);
936      Mesh *mesh = new Mesh;
937     
938      // add 6 vertices of the box
[289]939      int index = (int)mesh->mVertices.size();
[191]940      for (int i=0; i < 8; i++) {
941        Vector3 v;
942        box.GetVertex(i, v);
943        mesh->mVertices.push_back(v);
944      }
945      mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
946      mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
947      mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
948     
949      mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
950      mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
951      mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
952
953
954      // set the mesh material according to the ray density
955      KdLeaf *leaf = (KdLeaf *) node;
956      if (leaf->mPassingRays.mRays) {
957        float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
958        //      float importance = leaf->mPassingRays.mContributions/1000.0f;
959        //      float importance = leaf->mPassingRays.mRays/1000.0f;
960        ///(float)leaf->mPassingRays.mRays;
961        // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
962        mForcedMaterial.mDiffuseColor.r = importance;
963        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
964        ExportMesh(mesh);
965      }
966      delete mesh;
967    } else {
968      KdInterior *interior = (KdInterior *)node;
969      tStack.push(interior->mFront);
970      tStack.push(interior->mBack);
971    }
972  }
973  // restore the state of forced material
974  mUseForcedMaterial = fm;
975  return true;
976}
[312]977
[329]978
[312]979struct BspSplitData
980{
[372]981        /// the current node
982        BspNode *mNode;
983
984        vector<Plane3 *> mPlanes;
985        vector<bool> mSides;
986        bool mIsFront;
[383]987        int mDepth;
[372]988
[313]989        BspSplitData(BspNode *node):
[383]990        mNode(node), mIsFront(false), mDepth(0)
[372]991        {};     
[383]992
[312]993        BspSplitData(BspNode *node,
[329]994                                vector<Plane3 *> planes,
995                                vector<bool> sides,
[383]996                                const bool isFront,
997                                const int depth):
998        mNode(node), mPlanes(planes), mSides(sides),
999        mIsFront(isFront), mDepth(depth)
[312]1000        {};
1001};
1002
[383]1003void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
1004                                                                           const vector<BspLeaf *> &leaves)
[360]1005{
1006        vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
1007
1008        for (it = leaves.begin(); it != it_end; ++ it)
1009        {
1010                PolygonContainer cell;
1011                tree.ConstructGeometry(*it, cell);
1012               
1013                ExportPolygons(cell);
1014
1015                CLEAR_CONTAINER(cell);
1016        }
1017}
1018
[383]1019void X3dExporter::ExportBspSplits(const BspTree &tree,
1020                                                                  const bool exportDepth)
[372]1021{
1022        std::stack<BspSplitData> tStack;
1023
1024        BspSplitData tData(tree.GetRoot());
1025        tStack.push(tData);
1026 
1027        PolygonContainer polys;
[383]1028        vector <int> depths;
[372]1029
[383]1030        int maxDepth = 0;
1031
[372]1032        while (!tStack.empty())
1033        {
1034                // filter polygons donw the tree
1035                BspSplitData tData = tStack.top();
1036            tStack.pop();       
1037               
[383]1038                if (tData.mNode->IsLeaf())
[372]1039                {
[383]1040                        if (tData.mDepth > maxDepth)
1041                                maxDepth = tData.mDepth;
1042                }
1043                else
1044                {
[372]1045                        BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
1046
[383]1047                        // add current side of split plane
[372]1048                        if (tData.mNode != tree.GetRoot())
[383]1049                                tData.mSides.push_back(tData.mIsFront);
[372]1050
1051                        // bounded plane is added to the polygons
[383]1052                        Polygon3 *planePoly =
1053                                tree.GetBoundingBox().CrossSection(*interior->GetPlane());
[372]1054               
1055                        // do all the splits with the previous planes
1056                        for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
[436]1057                        {                               
[372]1058                                if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
1059                                {
1060                                        Polygon3 *frontPoly = new Polygon3();
1061                                        Polygon3 *backPoly = new Polygon3();
1062
[436]1063                                        planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly);
[372]1064                                        DEL_PTR(planePoly);
1065
1066                                        if(tData.mSides[i] == true)
1067                                        {
1068                                                planePoly = frontPoly;
1069                                                DEL_PTR(backPoly);
1070                                        }
1071                                        else
1072                                        {
1073                                                planePoly = backPoly;
1074                                                DEL_PTR(frontPoly);
1075                                        }
1076                                }
1077                        }
1078
1079                        tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
1080
1081                        if (planePoly->Valid())
[383]1082                        {
[372]1083                                polys.push_back(planePoly);
[383]1084                                depths.push_back(tData.mDepth);
1085                        }
[372]1086                        else
1087                                DEL_PTR(planePoly);
1088                       
1089                        // push the children on the stack
[383]1090                        tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
1091                                                     tData.mSides, true, tData.mDepth + 1));
1092                        tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
1093                                                     tData.mSides, false, tData.mDepth + 1));
[372]1094                }
[383]1095        }
1096
1097        if (maxDepth > 0)
1098        {       
1099                mUseForcedMaterial = true;
1100                       
1101                for (int i = 0; i < (int)polys.size(); ++ i)
1102                {
1103                        mForcedMaterial.mDiffuseColor.b = 1.0f;
1104                        float importance =  (float)depths[i]/ (float)maxDepth;
1105           
1106                        mForcedMaterial.mDiffuseColor.r = importance;
1107                        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
1108
1109                        ExportPolygon(polys[i]);
1110                }
1111        }
1112        else
1113        {
1114                ExportPolygons(polys);
1115        }
1116
[372]1117        CLEAR_CONTAINER(polys);
1118}
1119
1120void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
1121{
1122        std::stack<BspNode *> tStack;
1123
1124        tStack.push(tree.GetRoot());
1125 
1126        PolygonContainer polys;
1127
1128        while (!tStack.empty())
1129        {
1130                // filter polygons donw the tree
1131                BspNode *node = tStack.top();
1132            tStack.pop();       
1133               
1134                if (!node->IsLeaf())
1135                {
1136                        BspInterior *interior = dynamic_cast<BspInterior *>(node);
1137
1138                        // bounded plane is added to the polygons
1139                        polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane()));
1140               
1141                        // push the children on the stack
1142                        tStack.push(interior->GetBack());
1143                        tStack.push(interior->GetFront());
1144                }
1145        }
1146
1147        ExportPolygons(polys);
1148        CLEAR_CONTAINER(polys);
1149}
Note: See TracBrowser for help on using the repository browser.