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

Revision 422, 23.4 KB checked in by mattausch, 19 years ago (diff)

worded on vspkdtree

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