source: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp @ 1404

Revision 1404, 28.5 KB checked in by mattausch, 18 years ago (diff)

debugging after merge

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