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

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