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

Revision 446, 30.3 KB checked in by bittner, 19 years ago (diff)

non-functional merged version

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