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

Revision 503, 29.8 KB checked in by mattausch, 19 years ago (diff)

added mesh creation function

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
680
681
682bool
683X3dExporter::ExportVssTree(const VssTree &tree
684                                                   )
685{
686  stack<VssTreeNode *> tStack;
687       
688  tStack.push(tree.GetRoot());
689       
690  Mesh *mesh = new Mesh;
691  VssRayContainer rays;
692       
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      tStack.push(interior->front);
702      tStack.push(interior->back);
703    } else {
704                        VssTreeLeaf *leaf = (VssTreeLeaf *)node;
705                        AxisAlignedBox3 box;
706                        box = tree.GetBBox(leaf);
707                        box.AddBoxToMesh(mesh);
708
709                        if (tree.ValidLeaf(leaf)) {
710                               
711                                Vector3 origin = box.Center();
712                                box = tree.GetDirBBox(leaf);
713                                VssRay *ray;
714                               
715                                const indices[][2] = {{0,0}, {0,1}, {1,1}, {1,0}};
716                                MeshInstance dummy(mesh);
717                                for (int i=0; i < 4; i++) {
718                                        //                              Vector3 v = box.GetVertex(indices[i][0], indices[i][1], 0);
719                                        Vector3 v = box.Center();
720                                       
721                                        Vector3 direction = VssRay::GetDirection(v.x, v.y);
722                                        if (Magnitude(direction) > Limits::Small)
723                                          direction.Normalize();
724                                        else
725                                          direction = Vector3(0, 1, 0);
726                                        float k = 100.0f*leaf->GetImportance();
727                                        // get 4 corners of the ray directions
728                                       
729                                        ray = new VssRay(origin, origin + (direction*k), NULL, &dummy);
730                                        rays.push_back(ray);
731                                }
732                        }
733                }
734  }
735
736  ExportMesh(mesh);
737  ExportRays(rays);
738  CLEAR_CONTAINER(rays);
739  delete mesh;
740  return true;
741}
742
743bool
744X3dExporter::ExportVssTree2(const VssTree &tree,
745                                                        const Vector3 direction
746                                                        )
747{
748  stack<VssTreeNode *> tStack;
749       
750
751  mUseForcedMaterial = true;
752
753  Vector3 dirParam;
754
755  dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
756  dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
757
758  float maxImportance = 0.0f;
759  tStack.push(tree.GetRoot());
760  while (!tStack.empty()) {
761       
762        VssTreeNode *node = tStack.top();
763    tStack.pop();
764       
765    if (!node->IsLeaf()) {
766      VssTreeInterior *interior = (VssTreeInterior *)node;
767          if (interior->axis < 3) {
768                tStack.push(interior->front);
769                tStack.push(interior->back);
770          } else {
771                if (dirParam[interior->axis-3] < interior->position)
772                  tStack.push(interior->back);
773                else
774                  tStack.push(interior->front);
775          }
776    } else {
777          VssTreeLeaf *leaf = (VssTreeLeaf *)node;
778          if (tree.ValidLeaf(leaf)) {
779                float i = leaf->GetImportance();
780                if (i > maxImportance)
781                  maxImportance = i;
782          }
783        }
784  }
785
786  tStack.push(tree.GetRoot());
787  while (!tStack.empty()) {
788
789        VssTreeNode *node = tStack.top();
790    tStack.pop();
791       
792                       
793    if (!node->IsLeaf()) {
794      VssTreeInterior *interior = (VssTreeInterior *)node;
795          if (interior->axis < 3) {
796                tStack.push(interior->front);
797                tStack.push(interior->back);
798          } else {
799                if (dirParam[interior->axis-3] < interior->position)
800                  tStack.push(interior->back);
801                else
802                  tStack.push(interior->front);
803          }
804    } else {
805          VssTreeLeaf *leaf = (VssTreeLeaf *)node;
806          if (tree.ValidLeaf(leaf)) {
807                AxisAlignedBox3 box;
808                box = tree.GetBBox(leaf);
809                Mesh *mesh = new Mesh;
810                box.AddBoxToMesh(mesh);
811               
812                // get 4 corners of the ray directions
813               
814                mForcedMaterial.mDiffuseColor.b = 1.0f;
815                mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
816                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
817               
818                ExportMesh(mesh);
819                delete mesh;
820          }
821        }
822  }
823
824  mUseForcedMaterial = false;
825
826  return true;
827}
828
829bool
830X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
831{
832        stack<BspNode *> tStack;
833
834        tStack.push(tree.GetRoot());
835
836        bool fm = mUseForcedMaterial;
837       
838        mUseForcedMaterial = true;
839       
840        mForcedMaterial.mDiffuseColor.g = 1.0f;
841        mForcedMaterial.mDiffuseColor.b = 1.0f;
842 
843        while (!tStack.empty())
844        {
845                BspNode *node = tStack.top();
846                tStack.pop();
847
848                if (node->IsLeaf())
849                {
850                        ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
851     
852                        // set the mesh material according to the ray density
853                        if (vc->mPassingRays.mRays)
854                        {
855                                float importance =
856                                  vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
857                               
858                                mForcedMaterial.mDiffuseColor.r = importance;
859                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
860                                ExportViewCell(vc);
861                        }
862                } else
863                {
864                        BspInterior *interior = (BspInterior *)node;
865                        tStack.push(interior->GetFront());
866                        tStack.push(interior->GetBack());
867                }
868        }
869 
870        // restore the state of forced material
871        mUseForcedMaterial = fm;
872
873        return true;
874}
875
876bool
877X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
878{
879  stack<KdNode *> tStack;
880
881  tStack.push(tree.GetRoot());
882
883  bool fm = mUseForcedMaterial;
884  mUseForcedMaterial = true;
885  mForcedMaterial.mDiffuseColor.g = 1.0f;
886  mForcedMaterial.mDiffuseColor.b = 1.0f;
887  while (!tStack.empty()) {
888    KdNode *node = tStack.top();
889    tStack.pop();
890    if (node->IsLeaf()) {
891      AxisAlignedBox3 box = tree.GetBox(node);
892      Mesh *mesh = new Mesh;
893     
894      // add 6 vertices of the box
895      int index = (int)mesh->mVertices.size();
896      for (int i=0; i < 8; i++) {
897        Vector3 v;
898        box.GetVertex(i, v);
899        mesh->mVertices.push_back(v);
900      }
901      mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
902      mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
903      mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
904     
905      mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
906      mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
907      mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
908
909
910      // set the mesh material according to the ray density
911      KdLeaf *leaf = (KdLeaf *) node;
912      if (leaf->mPassingRays.mRays) {
913        float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
914        //      float importance = leaf->mPassingRays.mContributions/1000.0f;
915        //      float importance = leaf->mPassingRays.mRays/1000.0f;
916        ///(float)leaf->mPassingRays.mRays;
917        // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
918        mForcedMaterial.mDiffuseColor.r = importance;
919        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
920        ExportMesh(mesh);
921      }
922      delete mesh;
923    } else {
924      KdInterior *interior = (KdInterior *)node;
925      tStack.push(interior->mFront);
926      tStack.push(interior->mBack);
927    }
928  }
929  // restore the state of forced material
930  mUseForcedMaterial = fm;
931  return true;
932}
933
934
935struct BspSplitData
936{
937        /// the current node
938        BspNode *mNode;
939
940        vector<Plane3> mPlanes;
941        vector<bool> mSides;
942        bool mIsFront;
943        int mDepth;
944
945        BspSplitData(BspNode *node):
946        mNode(node), mIsFront(false), mDepth(0)
947        {};     
948
949        BspSplitData(BspNode *node,
950                                vector<Plane3> planes,
951                                vector<bool> sides,
952                                const bool isFront,
953                                const int depth):
954        mNode(node), mPlanes(planes), mSides(sides),
955        mIsFront(isFront), mDepth(depth)
956        {};
957};
958
959void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
960                                                                           const vector<BspLeaf *> &leaves)
961{
962        vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
963
964        for (it = leaves.begin(); it != it_end; ++ it)
965        {
966                BspNodeGeometry geom;
967                tree.ConstructGeometry(*it, geom);
968               
969                ExportPolygons(geom.mPolys);
970        }
971}
972
973void X3dExporter::ExportBspNodeSplits(BspNode *root,
974                                                                          const AxisAlignedBox3 &box,
975                                                                          const bool exportDepth,
976                                                                          const bool epsilon)
977{
978        std::stack<BspSplitData> tStack;
979
980        BspSplitData tData(root);
981        tStack.push(tData);
982 
983        PolygonContainer polys;
984        vector <int> depths;
985
986        int maxDepth = 0;
987
988        while (!tStack.empty())
989        {
990                // filter polygons donw the tree
991                BspSplitData tData = tStack.top();
992            tStack.pop();       
993               
994                if (tData.mNode->IsLeaf())
995                {
996                        if (tData.mDepth > maxDepth)
997                                maxDepth = tData.mDepth;
998                }
999                else
1000                {
1001                        BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
1002
1003                        // add current side of split plane
1004                        if (tData.mNode != root)
1005                                tData.mSides.push_back(tData.mIsFront);
1006
1007                        // bounded plane is added to the polygons
1008                        Polygon3 *planePoly =
1009                                box.CrossSection(interior->GetPlane());
1010               
1011                        // do all the splits with the previous planes
1012                        for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
1013                        {                               
1014                                if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon)
1015                                        == Polygon3::SPLIT)
1016                                {
1017                                        Polygon3 *frontPoly = new Polygon3();
1018                                        Polygon3 *backPoly = new Polygon3();
1019
1020                                        planePoly->Split(tData.mPlanes[i],
1021                                                                         *frontPoly,
1022                                                                         *backPoly,
1023                                                                         epsilon);
1024
1025                                        DEL_PTR(planePoly);
1026
1027                                        if(tData.mSides[i] == true)
1028                                        {
1029                                                planePoly = frontPoly;
1030                                                DEL_PTR(backPoly);
1031                                        }
1032                                        else
1033                                        {
1034                                                planePoly = backPoly;
1035                                                DEL_PTR(frontPoly);
1036                                        }
1037                                }
1038                        }
1039
1040                        tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
1041
1042                        if (planePoly->Valid(epsilon))
1043                        {
1044                                polys.push_back(planePoly);
1045                                depths.push_back(tData.mDepth);
1046                        }
1047                        else
1048                                DEL_PTR(planePoly);
1049                       
1050                        // push the children on the stack
1051                        tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
1052                                                     tData.mSides, true, tData.mDepth + 1));
1053                        tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
1054                                                     tData.mSides, false, tData.mDepth + 1));
1055                }
1056        }
1057
1058        if (maxDepth > 0)
1059        {       
1060                mUseForcedMaterial = true;
1061                       
1062                for (int i = 0; i < (int)polys.size(); ++ i)
1063                {
1064                        mForcedMaterial.mDiffuseColor.b = 1.0f;
1065                        float importance =  (float)depths[i]/ (float)maxDepth;
1066           
1067                        mForcedMaterial.mDiffuseColor.r = importance;
1068                        mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
1069
1070                        ExportPolygon(polys[i]);
1071                }
1072        }
1073        else
1074        {
1075                ExportPolygons(polys);
1076        }
1077
1078        CLEAR_CONTAINER(polys);
1079}
1080
1081void X3dExporter::ExportBspSplits(const BspTree &tree,
1082                                                                  const bool exportDepth)
1083{
1084        ExportBspNodeSplits(tree.GetRoot(),
1085                                                tree.GetBoundingBox(),
1086                                                exportDepth,
1087                                                tree.GetEpsilon());
1088}
1089
1090void X3dExporter::ExportBspSplits(const VspBspTree &tree,
1091                                                                  const bool exportDepth)
1092{
1093        ExportBspNodeSplits(tree.GetRoot(),
1094                                                tree.GetBoundingBox(),
1095                                                exportDepth,
1096                                                tree.GetEpsilon());
1097}
1098
1099void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
1100{
1101        std::stack<BspNode *> tStack;
1102
1103        tStack.push(tree.GetRoot());
1104 
1105        PolygonContainer polys;
1106
1107        while (!tStack.empty())
1108        {
1109                // filter polygons donw the tree
1110                BspNode *node = tStack.top();
1111            tStack.pop();       
1112               
1113                if (!node->IsLeaf())
1114                {
1115                        BspInterior *interior = dynamic_cast<BspInterior *>(node);
1116
1117                        // bounded plane is added to the polygons
1118                        polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
1119               
1120                        // push the children on the stack
1121                        tStack.push(interior->GetBack());
1122                        tStack.push(interior->GetFront());
1123                }
1124        }
1125
1126        ExportPolygons(polys);
1127        CLEAR_CONTAINER(polys);
1128}
1129
1130
1131void X3dExporter::ExportGeometry(const ObjectContainer &objects)
1132{
1133        for (int j = 0; j < objects.size(); ++ j)
1134                ExportIntersectable(objects[j]);
1135}
1136
1137
1138bool
1139X3dExporter::ExportRssTree2(const RssTree &tree,
1140                                                        const Vector3 direction
1141                                                        )
1142{
1143  stack<RssTreeNode *> tStack;
1144 
1145 
1146  mUseForcedMaterial = true;
1147
1148  Vector3 dirParam;
1149
1150  dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
1151  dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
1152
1153  float maxImportance = 0.0f;
1154  tStack.push(tree.GetRoot());
1155  while (!tStack.empty()) {
1156       
1157        RssTreeNode *node = tStack.top();
1158    tStack.pop();
1159       
1160    if (!node->IsLeaf()) {
1161      RssTreeInterior *interior = (RssTreeInterior *)node;
1162          if (interior->axis < 3) {
1163                tStack.push(interior->front);
1164                tStack.push(interior->back);
1165          } else {
1166                if (dirParam[interior->axis-3] < interior->position)
1167                  tStack.push(interior->back);
1168                else
1169                  tStack.push(interior->front);
1170          }
1171    } else {
1172          RssTreeLeaf *leaf = (RssTreeLeaf *)node;
1173          if (tree.ValidLeaf(leaf)) {
1174                float i = leaf->GetImportance();
1175                if (i > maxImportance)
1176                  maxImportance = i;
1177          }
1178        }
1179  }
1180
1181  tStack.push(tree.GetRoot());
1182  while (!tStack.empty()) {
1183
1184        RssTreeNode *node = tStack.top();
1185    tStack.pop();
1186       
1187                       
1188    if (!node->IsLeaf()) {
1189      RssTreeInterior *interior = (RssTreeInterior *)node;
1190          if (interior->axis < 3) {
1191                tStack.push(interior->front);
1192                tStack.push(interior->back);
1193          } else {
1194                if (dirParam[interior->axis-3] < interior->position)
1195                  tStack.push(interior->back);
1196                else
1197                  tStack.push(interior->front);
1198          }
1199    } else {
1200          RssTreeLeaf *leaf = (RssTreeLeaf *)node;
1201          if (tree.ValidLeaf(leaf)) {
1202                AxisAlignedBox3 box;
1203                box = tree.GetShrankedBBox(leaf);
1204                Mesh *mesh = new Mesh;
1205                box.AddBoxToMesh(mesh);
1206               
1207                // get 4 corners of the ray directions
1208               
1209                mForcedMaterial.mDiffuseColor.b = 1.0f;
1210                mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
1211                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
1212               
1213                ExportMesh(mesh);
1214                delete mesh;
1215          }
1216        }
1217  }
1218
1219  mUseForcedMaterial = false;
1220
1221  return true;
1222}
1223
1224
1225void
1226X3dExporter::ExportViewpoint(const Vector3 &point,
1227                                                         const Vector3 &direction)
1228{
1229  stream<<"<Viewpoint "<<endl;
1230 
1231  stream<<"position=\""<<point.x<<" "<<point.y<<" "<<point.z<<"\""<<endl;
1232  //  stream<<"orientation "<<direction.x<<direction.y<<direction.z<<endl;
1233 
1234  stream<<">"<<endl;
1235  stream<<"</Viewpoint>"<<endl;
1236 
1237}
Note: See TracBrowser for help on using the repository browser.