source: GTP/trunk/Lib/Vis/Preprocessing/src/X3dExporter.cpp @ 2176

Revision 2176, 28.6 KB checked in by mattausch, 17 years ago (diff)

removed using namespace std from .h

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