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

Revision 1047, 31.7 KB checked in by mattausch, 18 years ago (diff)

fixed vsp part of vsp osp tree

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