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

Revision 426, 24.0 KB checked in by mattausch, 19 years ago (diff)

fixed ray bug in vspkdtree
added visualizations

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