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

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