source: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp @ 1106

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