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

RevLine 
[162]1#include <stack>
2#include "common.h"
3#include "SceneGraph.h"
4#include "X3dExporter.h"
5#include "Mesh.h"
6#include "KdTree.h"
[242]7#include "ViewCellBsp.h"
8#include "ViewCell.h"
[260]9#include "Polygon3.h"
[386]10#include "VssRay.h"
[418]11#include "VspKdTree.h"
[434]12#include "VssTree.h"
[449]13#include "VspBspTree.h"
[450]14#include "RssTree.h"
[162]15
[449]16
[162]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
[176]33
[386]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
[162]83bool
[386]84X3dExporter::ExportRays(const RayContainer &rays,
[444]85                                                const float length,
86                                                const RgbColor &color)
[162]87{
[386]88  RayContainer::const_iterator ri = rays.begin();
89
[162]90  stream<<"<Shape>"<<endl;
[176]91  stream<<"<Appearance>"<<endl;
[427]92  stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
[176]93    "\" />"<<endl;
94  stream<<"</Appearance>"<<endl;
95 
[209]96  stream<<"<IndexedLineSet coordIndex=\""<<endl;
[162]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++) {
[386]110    Vector3 a = (*ri)->GetLoc();
[162]111   
[176]112    Vector3 b;
113    if (length < 0)
[386]114      b = (*ri)->GetLoc() - length*(*ri)->GetDir();
[176]115    else
[386]116      if ((*ri)->intersections.size()==0)
[492]117                b = (*ri)->GetLoc() + length*(*ri)->GetDir();
[176]118      else
[492]119                b = (*ri)->Extrap((*ri)->intersections[0].mT);
[176]120   
[162]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;
[386]129
[162]130  return true;
131}
132
[349]133bool
[386]134X3dExporter::ExportRays(const VssRayContainer &rays,
[444]135                                                const RgbColor &color)
[349]136{
[386]137  VssRayContainer::const_iterator ri = rays.begin();
[349]138
139  stream<<"<Shape>"<<endl;
140  stream<<"<Appearance>"<<endl;
[427]141  stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
[349]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++) {
[444]159    const Vector3 a = (*ri)->GetOrigin();
[483]160        //const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
161        const Vector3 b = (*ri)->GetTermination(); // matt: change again!!
162
[349]163    stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
[444]164        stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
[349]165  }
166 
167  stream<<"\" >"<<endl;
168  stream<<"</Coordinate>"<<endl;
169  stream<<"</IndexedLineSet>"<<endl;
170  stream<<"</Shape>"<<endl;
[386]171       
[349]172  return true;
173}
174
[162]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 
[176]185  ObjectContainer::const_iterator mi = node->mGeometry.begin();
[162]186  for (; mi != node->mGeometry.end(); mi++) {
187    // export the transform...
[176]188    ExportIntersectable(*mi);
[162]189  }
190 
191  stream<<"</Group>"<<endl;
192
193}
[176]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);
[242]201        break;
[308]202  case Intersectable::VIEW_CELL:
[242]203        ExportViewCell((ViewCell *)object);
[176]204    break;
205  default:
206    cerr<<"Sorry the export for object not yet defined"<<endl;
207    break;
208  }
209}
[162]210
211void
[176]212X3dExporter::ExportMeshInstance(MeshInstance *object)
213{
214  // $$JB$$
[312]215  // in the future check whether the mesh was not already exported
[176]216  // and use a reference to the that mesh instead
217  ExportMesh(object->GetMesh());
218}
219
[313]220void
221X3dExporter::ExportViewCells(const ViewCellContainer &viewCells)
[261]222{
[313]223        ViewCellContainer::const_iterator it, it_end = viewCells.end();
[261]224
[313]225        for (it = viewCells.begin(); it != it_end; ++ it)
[261]226                ExportViewCell(*it);
227}
[313]228
[373]229void
[396]230X3dExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
[373]231{
[396]232        stack<pair<BspNode *, BspNodeGeometry *> > tStack;
233        ViewCell::NewMail();
[373]234
[396]235        BspNodeGeometry *geom = new BspNodeGeometry();
236        tree.ConstructGeometry(tree.GetRoot(), *geom);
[373]237
[396]238        tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
239
240        if (maxPvs > 0)
241                mUseForcedMaterial = true;
242
243        while (!tStack.empty())
[373]244        {
[396]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
[449]256                        cell->SplitGeometry(*front,
257                                                                *back,
258                                                                interior->GetPlane(),
259                                                                tree.GetBoundingBox(),
260                                                                tree.GetEpsilon());
[396]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);
[373]282        }
283}
284
[176]285void
[261]286X3dExporter::ExportViewCell(ViewCell *viewCell)
[242]287{
[261]288        if (viewCell->GetMesh())
289                ExportMesh(viewCell->GetMesh());
[242]290}
291
292void
[162]293X3dExporter::ExportMesh(Mesh *mesh)
294{
295
296  stream<<"<Shape>"<<endl;
297  stream<<"<Appearance>"<<endl;
[176]298 
[162]299  // $$ tmp -> random material
300 
301  float r, g, b;
302
[176]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    }
[162]318  stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
319    "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
320  stream<<"</Appearance>"<<endl;
321
322
[191]323  if (mWireframe)
[427]324    stream<<"<IndexedLineSet coordIndex=\""<<endl;
[162]325  else
[465]326    //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
327        stream << "<IndexedFaceSet coordIndex=\"" << endl;
[162]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<<" ";
[378]338        if (mWireframe) // final line to finish polygon
339                stream << (*face->mVertexIndices.begin()) << " ";
340
[162]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
[191]356  if (mWireframe)
[162]357    stream<<"</IndexedLineSet>"<<endl;
358  else
359    stream<<"</IndexedFaceSet>"<<endl;
360 
361  stream<<"</Shape>"<<endl;
362
363}
364
[242]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
[262]399        //-- create and write indices
[242]400        if (mWireframe)
[465]401                stream<<"<IndexedLineSet coordIndex=\""<<endl;
[242]402        else
[465]403                //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
404                stream << "<IndexedFaceSet coordIndex=\"" << endl;
[262]405        int index = 0;
406       
407        VertexContainer::const_iterator vi; 
408       
[333]409        for (index = 0; index < (int)poly->mVertices.size(); ++ index)
[318]410                stream << index << " ";
411       
412        if (mWireframe) // final line to finish polygon
413                stream << "0 ";
414
[262]415        stream << "-1" << endl;
416        stream << "\" >" << endl;
417       
[242]418        stream << "<Coordinate  point=\"" << endl;
419 
[262]420        for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
[242]421        {
422                stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
423                stream << "," << endl;
424        }
425 
[262]426        stream << "\" >" << endl;
427        stream << "</Coordinate>" << endl;
[242]428
429        if (mWireframe)
430                stream << "</IndexedLineSet>" << endl;
431        else
432                stream << "</IndexedFaceSet>" << endl;
433 
434        stream << "</Shape>" << endl;
435}
436
[313]437void X3dExporter::ExportPolygons(const PolygonContainer &polys)
[263]438{
[318]439        stream << "<Shape>" << endl;
[263]440        stream << "<Appearance>" << endl;
441 
442        // $$ tmp -> random material
443 
444        float r, g, b;
[242]445
[263]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)
[465]467                stream<<"<IndexedLineSet coordIndex=\""<<endl;
[263]468        else
[465]469                //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
470                stream << "<IndexedFaceSet coordIndex=\"" << endl;
[263]471
472        int index = 0;
473       
474        PolygonContainer::const_iterator pit;
475
476    VertexContainer::const_iterator vi; 
477       
[313]478        for (pit = polys.begin(); pit != polys.end(); ++pit)
[263]479        {
480                Polygon3 *poly = *pit;
[318]481                int startIdx = index;
[263]482                for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
483                {
484                        stream << index ++ << " ";
485                }
[318]486
487                stream << startIdx << " ";// finish line
[263]488                stream << "-1" << endl;
489        }
490
491        stream << "\" >" << endl;
492       
493        stream << "<Coordinate  point=\"" << endl;
[318]494        for (pit = polys.begin(); pit != polys.end(); ++ pit)
[263]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
[162]514bool
515X3dExporter::ExportBox(const AxisAlignedBox3 &box)
516{
517  Mesh *mesh = new Mesh;
518  // add 6 vertices of the box
[289]519  int index = (int)mesh->mVertices.size();
[162]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
[242]540X3dExporter::ExportBspTree(const BspTree &tree)
[162]541{
[242]542        if (mExportRayDensity)
543        {
[263]544                return ExportBspTreeRayDensity(tree);
[242]545        }
546 
[264]547        bool savedWireframe = mWireframe;
548
549        SetWireframe();
550       
[396]551        ExportBox(tree.GetBoundingBox());
552       
[264]553        if (!savedWireframe)
554                SetFilled();
555
[373]556        // export view cells
[482]557        ExportBspLeaves(tree); 
[289]558
[263]559        return true;
[242]560}
561
[425]562bool X3dExporter::ExportVspKdTree(const VspKdTree &tree, const int maxPvs)
[418]563{
[462]564        stack<VspKdNode *> tStack;
[418]565
[422]566        tStack.push(tree.GetRoot());
567
[425]568        //Mesh *mesh = new Mesh;
[418]569 
[425]570        if (maxPvs > 0)
571                mUseForcedMaterial = true;
572
[422]573        while (!tStack.empty())
574        {
[462]575                VspKdNode *node = tStack.top();
[418]576   
[422]577                tStack.pop();
[418]578
[426]579                if (node->IsLeaf())
580                {
581                        AxisAlignedBox3 box = tree.GetBBox(node);
[425]582
[426]583                        Mesh *mesh = new Mesh;
[425]584
[426]585                        // add 6 vertices of the box
586                        int index = (int)mesh->mVertices.size();
[418]587
[426]588                        for (int i=0; i < 8; ++ i)
589                        {
590                                Vector3 v;
591                                box.GetVertex(i, v);
592                                mesh->mVertices.push_back(v);
593                        }
[418]594
[426]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) );
[418]598
[426]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) );
[425]602
[426]603                        if (maxPvs > 0)
604                        {
[462]605                                VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
[425]606
[426]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                        }
[425]615
[426]616                        ExportMesh(mesh);
617                        DEL_PTR(mesh);
618                }
619                else 
[422]620                {
[462]621                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
[422]622                        tStack.push(interior->GetFront());
623                        tStack.push(interior->GetBack());
624                }
625        }
[418]626 
[425]627        //ExportMesh(mesh);
628        //DEL_PTR(mesh);
[418]629
[422]630        return true;
[418]631}
632
[462]633
[242]634bool X3dExporter::ExportKdTree(const KdTree &tree)
635{
636         if (mExportRayDensity) {
[191]637    return ExportKdTreeRayDensity(tree);
638  }
639 
[162]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
[289]651    int index = (int)mesh->mVertices.size();
[162]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;
[242]675        // TODO
676        return true;
[162]677}
678
679
[434]680
681
[191]682bool
[438]683X3dExporter::ExportVssTree(const VssTree &tree
684                                                   )
[434]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);
[486]707                        box.AddBoxToMesh(mesh);
[434]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);
[438]722                                        if (Magnitude(direction) > Limits::Small)
723                                          direction.Normalize();
724                                        else
725                                          direction = Vector3(0, 1, 0);
[435]726                                        float k = 100.0f*leaf->GetImportance();
[434]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);
[438]737  ExportRays(rays);
738  CLEAR_CONTAINER(rays);
[434]739  delete mesh;
740  return true;
741}
742
743bool
[438]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;
[486]810                box.AddBoxToMesh(mesh);
[438]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
[263]830X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
831{
[311]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                        {
[313]855                                float importance =
[438]856                                  vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
857                               
[311]858                                mForcedMaterial.mDiffuseColor.r = importance;
859                                mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
860                                ExportViewCell(vc);
[438]861                        }
[311]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
[263]873        return true;
874}
875
876bool
[191]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
[289]895      int index = (int)mesh->mVertices.size();
[191]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}
[312]933
[329]934
[312]935struct BspSplitData
936{
[372]937        /// the current node
938        BspNode *mNode;
939
[449]940        vector<Plane3> mPlanes;
[372]941        vector<bool> mSides;
942        bool mIsFront;
[383]943        int mDepth;
[372]944
[313]945        BspSplitData(BspNode *node):
[383]946        mNode(node), mIsFront(false), mDepth(0)
[372]947        {};     
[383]948
[312]949        BspSplitData(BspNode *node,
[449]950                                vector<Plane3> planes,
[329]951                                vector<bool> sides,
[383]952                                const bool isFront,
953                                const int depth):
954        mNode(node), mPlanes(planes), mSides(sides),
955        mIsFront(isFront), mDepth(depth)
[312]956        {};
957};
958
[383]959void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
960                                                                           const vector<BspLeaf *> &leaves)
[360]961{
962        vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
963
964        for (it = leaves.begin(); it != it_end; ++ it)
965        {
[503]966                BspNodeGeometry geom;
967                tree.ConstructGeometry(*it, geom);
[360]968               
[503]969                ExportPolygons(geom.mPolys);
[360]970        }
971}
972
[449]973void X3dExporter::ExportBspNodeSplits(BspNode *root,
974                                                                          const AxisAlignedBox3 &box,
975                                                                          const bool exportDepth,
976                                                                          const bool epsilon)
[372]977{
978        std::stack<BspSplitData> tStack;
979
[449]980        BspSplitData tData(root);
[372]981        tStack.push(tData);
982 
983        PolygonContainer polys;
[383]984        vector <int> depths;
[372]985
[383]986        int maxDepth = 0;
987
[372]988        while (!tStack.empty())
989        {
990                // filter polygons donw the tree
991                BspSplitData tData = tStack.top();
992            tStack.pop();       
993               
[383]994                if (tData.mNode->IsLeaf())
[372]995                {
[383]996                        if (tData.mDepth > maxDepth)
997                                maxDepth = tData.mDepth;
998                }
999                else
1000                {
[372]1001                        BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
1002
[383]1003                        // add current side of split plane
[449]1004                        if (tData.mNode != root)
[383]1005                                tData.mSides.push_back(tData.mIsFront);
[372]1006
1007                        // bounded plane is added to the polygons
[383]1008                        Polygon3 *planePoly =
[449]1009                                box.CrossSection(interior->GetPlane());
[372]1010               
1011                        // do all the splits with the previous planes
1012                        for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
[436]1013                        {                               
[449]1014                                if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon)
1015                                        == Polygon3::SPLIT)
[372]1016                                {
1017                                        Polygon3 *frontPoly = new Polygon3();
1018                                        Polygon3 *backPoly = new Polygon3();
1019
[449]1020                                        planePoly->Split(tData.mPlanes[i],
1021                                                                         *frontPoly,
1022                                                                         *backPoly,
1023                                                                         epsilon);
1024
[372]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
[449]1042                        if (planePoly->Valid(epsilon))
[383]1043                        {
[372]1044                                polys.push_back(planePoly);
[383]1045                                depths.push_back(tData.mDepth);
1046                        }
[372]1047                        else
1048                                DEL_PTR(planePoly);
1049                       
1050                        // push the children on the stack
[383]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));
[372]1055                }
[383]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
[372]1078        CLEAR_CONTAINER(polys);
1079}
1080
[449]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
[372]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
[449]1118                        polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
[372]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}
[440]1129
1130
[449]1131void X3dExporter::ExportGeometry(const ObjectContainer &objects)
[440]1132{
[449]1133        for (int j = 0; j < objects.size(); ++ j)
1134                ExportIntersectable(objects[j]);
[440]1135}
[449]1136
1137
[450]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;
[486]1205                box.AddBoxToMesh(mesh);
[450]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}
[459]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.