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

Revision 436, 25.9 KB checked in by mattausch, 19 years ago (diff)

bsptree view cells working in VssPreprocessor?

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