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

Revision 418, 23.5 KB checked in by mattausch, 19 years ago (diff)
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         {
589                 return ExportKdTreeRayDensity(tree);
590         }
591     
592         stack<VspKdTreeNode *> tStack;
593
594         tStack.push(tree.GetRoot());
595
596         Mesh *mesh = new Mesh;
597 
598         while (!tStack.empty())
599         {
600                 VspKdTreeNode *node = tStack.top();
601   
602                 tStack.pop();
603                 AxisAlignedBox3 box = tree.GetBBox(node);
604
605                 // add 6 vertices of the box
606
607                 int index = (int)mesh->mVertices.size();
608   
609                 for (int i=0; i < 8; ++ i)
610                 {
611                         Vector3 v;
612                         box.GetVertex(i, v);
613                         mesh->mVertices.push_back(v);
614                 }
615
616                 mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
617                 mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
618                 mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
619
620   
621                 mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
622                 mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
623                 mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
624
625                 if (!node->IsLeaf())
626                 {
627                         VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node);
628                         tStack.push(interior->mFront);
629                         tStack.push(interior->mBack);
630                 }
631         }
632 
633         ExportMesh(mesh);
634         DEL_PTR(mesh);
635
636         return true;
637}
638
639bool X3dExporter::ExportKdTree(const KdTree &tree)
640{
641         if (mExportRayDensity) {
642    return ExportKdTreeRayDensity(tree);
643  }
644 
645  stack<KdNode *> tStack;
646
647  tStack.push(tree.GetRoot());
648
649  Mesh *mesh = new Mesh;
650 
651  while (!tStack.empty()) {
652    KdNode *node = tStack.top();
653    tStack.pop();
654    AxisAlignedBox3 box = tree.GetBox(node);
655    // add 6 vertices of the box
656    int index = (int)mesh->mVertices.size();
657    for (int i=0; i < 8; i++) {
658      Vector3 v;
659      box.GetVertex(i, v);
660      mesh->mVertices.push_back(v);
661    }
662    mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
663    mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
664    mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
665
666    mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
667    mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
668    mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
669
670    if (!node->IsLeaf()) {
671      KdInterior *interior = (KdInterior *)node;
672      tStack.push(interior->mFront);
673      tStack.push(interior->mBack);
674    }
675  }
676 
677  ExportMesh(mesh);
678  delete mesh;
679  return true;
680        // TODO
681        return true;
682}
683
684
685bool
686X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
687{
688        stack<BspNode *> tStack;
689
690        tStack.push(tree.GetRoot());
691
692        bool fm = mUseForcedMaterial;
693       
694        mUseForcedMaterial = true;
695       
696        mForcedMaterial.mDiffuseColor.g = 1.0f;
697        mForcedMaterial.mDiffuseColor.b = 1.0f;
698 
699        while (!tStack.empty())
700        {
701                BspNode *node = tStack.top();
702                tStack.pop();
703
704                if (node->IsLeaf())
705                {
706                        ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
707     
708                        // set the mesh material according to the ray density
709                        if (vc->mPassingRays.mRays)
710                        {
711                                float importance =
712                                        vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
713
714                                mForcedMaterial.mDiffuseColor.r = importance;
715                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
716                                ExportViewCell(vc);
717                        } 
718                } else
719                {
720                        BspInterior *interior = (BspInterior *)node;
721                        tStack.push(interior->GetFront());
722                        tStack.push(interior->GetBack());
723                }
724        }
725 
726        // restore the state of forced material
727        mUseForcedMaterial = fm;
728
729        return true;
730}
731
732bool
733X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
734{
735  stack<KdNode *> tStack;
736
737  tStack.push(tree.GetRoot());
738
739  bool fm = mUseForcedMaterial;
740  mUseForcedMaterial = true;
741  mForcedMaterial.mDiffuseColor.g = 1.0f;
742  mForcedMaterial.mDiffuseColor.b = 1.0f;
743  while (!tStack.empty()) {
744    KdNode *node = tStack.top();
745    tStack.pop();
746    if (node->IsLeaf()) {
747      AxisAlignedBox3 box = tree.GetBox(node);
748      Mesh *mesh = new Mesh;
749     
750      // add 6 vertices of the box
751      int index = (int)mesh->mVertices.size();
752      for (int i=0; i < 8; i++) {
753        Vector3 v;
754        box.GetVertex(i, v);
755        mesh->mVertices.push_back(v);
756      }
757      mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
758      mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
759      mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
760     
761      mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
762      mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
763      mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
764
765
766      // set the mesh material according to the ray density
767      KdLeaf *leaf = (KdLeaf *) node;
768      if (leaf->mPassingRays.mRays) {
769        float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
770        //      float importance = leaf->mPassingRays.mContributions/1000.0f;
771        //      float importance = leaf->mPassingRays.mRays/1000.0f;
772        ///(float)leaf->mPassingRays.mRays;
773        // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
774        mForcedMaterial.mDiffuseColor.r = importance;
775        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
776        ExportMesh(mesh);
777      }
778      delete mesh;
779    } else {
780      KdInterior *interior = (KdInterior *)node;
781      tStack.push(interior->mFront);
782      tStack.push(interior->mBack);
783    }
784  }
785  // restore the state of forced material
786  mUseForcedMaterial = fm;
787  return true;
788}
789
790
791struct BspSplitData
792{
793        /// the current node
794        BspNode *mNode;
795
796        vector<Plane3 *> mPlanes;
797        vector<bool> mSides;
798        bool mIsFront;
799        int mDepth;
800
801        BspSplitData(BspNode *node):
802        mNode(node), mIsFront(false), mDepth(0)
803        {};     
804
805        BspSplitData(BspNode *node,
806                                vector<Plane3 *> planes,
807                                vector<bool> sides,
808                                const bool isFront,
809                                const int depth):
810        mNode(node), mPlanes(planes), mSides(sides),
811        mIsFront(isFront), mDepth(depth)
812        {};
813};
814
815void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
816                                                                           const vector<BspLeaf *> &leaves)
817{
818        vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
819
820        for (it = leaves.begin(); it != it_end; ++ it)
821        {
822                PolygonContainer cell;
823                tree.ConstructGeometry(*it, cell);
824               
825                ExportPolygons(cell);
826
827                CLEAR_CONTAINER(cell);
828        }
829}
830
831void X3dExporter::ExportBspSplits(const BspTree &tree,
832                                                                  const bool exportDepth)
833{
834        std::stack<BspSplitData> tStack;
835
836        BspSplitData tData(tree.GetRoot());
837        tStack.push(tData);
838 
839        PolygonContainer polys;
840        vector <int> depths;
841
842        int maxDepth = 0;
843
844        while (!tStack.empty())
845        {
846                // filter polygons donw the tree
847                BspSplitData tData = tStack.top();
848            tStack.pop();       
849               
850                if (tData.mNode->IsLeaf())
851                {
852                        if (tData.mDepth > maxDepth)
853                                maxDepth = tData.mDepth;
854                }
855                else
856                {
857                        BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
858
859                        // add current side of split plane
860                        if (tData.mNode != tree.GetRoot())
861                                tData.mSides.push_back(tData.mIsFront);
862
863                        // bounded plane is added to the polygons
864                        Polygon3 *planePoly =
865                                tree.GetBoundingBox().CrossSection(*interior->GetPlane());
866               
867                        // do all the splits with the previous planes
868                        for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
869                        {
870                                VertexContainer splitPts;
871                               
872                                if (planePoly->ClassifyPlane(*tData.mPlanes[i]) == Polygon3::SPLIT)
873                                {
874                                        Polygon3 *frontPoly = new Polygon3();
875                                        Polygon3 *backPoly = new Polygon3();
876
877                                        planePoly->Split(*tData.mPlanes[i], *frontPoly, *backPoly, splitPts);
878                                        DEL_PTR(planePoly);
879
880                                        if(tData.mSides[i] == true)
881                                        {
882                                                planePoly = frontPoly;
883                                                DEL_PTR(backPoly);
884                                        }
885                                        else
886                                        {
887                                                planePoly = backPoly;
888                                                DEL_PTR(frontPoly);
889                                        }
890                                }
891                        }
892
893                        tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
894
895                        if (planePoly->Valid())
896                        {
897                                polys.push_back(planePoly);
898                                depths.push_back(tData.mDepth);
899                        }
900                        else
901                                DEL_PTR(planePoly);
902                       
903                        // push the children on the stack
904                        tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
905                                                     tData.mSides, true, tData.mDepth + 1));
906                        tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
907                                                     tData.mSides, false, tData.mDepth + 1));
908                }
909        }
910
911        if (maxDepth > 0)
912        {       
913                mUseForcedMaterial = true;
914                       
915                for (int i = 0; i < (int)polys.size(); ++ i)
916                {
917                        mForcedMaterial.mDiffuseColor.b = 1.0f;
918                        float importance =  (float)depths[i]/ (float)maxDepth;
919           
920                        mForcedMaterial.mDiffuseColor.r = importance;
921                        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
922
923                        ExportPolygon(polys[i]);
924                }
925        }
926        else
927        {
928                ExportPolygons(polys);
929        }
930
931        CLEAR_CONTAINER(polys);
932}
933
934void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
935{
936        std::stack<BspNode *> tStack;
937
938        tStack.push(tree.GetRoot());
939 
940        PolygonContainer polys;
941
942        while (!tStack.empty())
943        {
944                // filter polygons donw the tree
945                BspNode *node = tStack.top();
946            tStack.pop();       
947               
948                if (!node->IsLeaf())
949                {
950                        BspInterior *interior = dynamic_cast<BspInterior *>(node);
951
952                        // bounded plane is added to the polygons
953                        polys.push_back(tree.GetBoundingBox().CrossSection(*interior->GetPlane()));
954               
955                        // push the children on the stack
956                        tStack.push(interior->GetBack());
957                        tStack.push(interior->GetFront());
958                }
959        }
960
961        ExportPolygons(polys);
962        CLEAR_CONTAINER(polys);
963}
Note: See TracBrowser for help on using the repository browser.