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

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