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

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