source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.h @ 575

Revision 575, 25.5 KB checked in by mattausch, 18 years ago (diff)

changed view cell parser. warning, not working yet!!

Line 
1#ifndef _ViewCellBsp_H__
2#define _ViewCellBsp_H__
3
4#include "Mesh.h"
5#include "Containers.h"
6#include "Polygon3.h"
7#include <stack>
8#include "Statistics.h"
9#include "VssRay.h"
10#include "ViewCell.h"
11
12class ViewCell;
13//class BspViewCell;
14class Plane3;
15class BspTree; 
16class BspInterior;
17//class Polygon3;
18class AxisAlignedBox3;
19class Ray;
20class ViewCellsStatistics;
21
22class BspNodeGeometry
23{
24public:
25        BspNodeGeometry()
26        {}; 
27
28        // copy constructor copying the polygon array
29        BspNodeGeometry(const BspNodeGeometry &rhs);
30
31        //BspNodeGeometry(const PolygonContainer &polys);
32
33        ~BspNodeGeometry();
34
35        /** Returns accumulated area of all polygons.
36        */
37        float GetArea() const;
38       
39        float GetVolume() const;
40
41        /** Computes new front and back geometry based on the old cell
42                geometry and a new split plane
43        */
44        void SplitGeometry(BspNodeGeometry &front,
45                                           BspNodeGeometry &back,
46                                           const Plane3 &splitPlane,
47                       const AxisAlignedBox3 &box,
48                                           const float epsilon) const;
49
50        /** Computes bounding box of the geometry.
51        */
52        void IncludeInBox(AxisAlignedBox3 &box);
53
54        /** Splits the polygon and returns the part of the polygon inside of the node geometry.
55        */
56        Polygon3 *SplitPolygon(Polygon3 *poly, const float epsilon) const;
57
58        /** Adds node geometry to mesh.
59                @note the mesh vertices will not be connected
60        */
61        void AddToMesh(Mesh &mesh);
62
63        /** Computes mass center of bsp node geometry.
64        */
65        Vector3 CenterOfMass() const;
66
67        /** The polygons the geometry consists of.
68        */
69        PolygonContainer mPolys;
70};
71
72
73/** Data structure used for optimized ray casting.
74*/
75struct BspRayTraversalData
76{
77    BspNode *mNode;
78    Vector3 mExitPoint;
79    float mMaxT;
80   
81    BspRayTraversalData() {}
82
83    BspRayTraversalData(BspNode *n, const Vector3 &extp, const float maxt):
84    mNode(n), mExitPoint(extp), mMaxT(maxt)
85        {}
86
87        BspRayTraversalData(BspNode *n, const Vector3 &extp):
88        mNode(n), mExitPoint(extp)
89        {}
90};
91
92/** Data used for passing ray data down the tree.
93*/
94struct BoundedRay
95{
96        Ray *mRay;
97        float mMinT;
98        float mMaxT;
99               
100        BoundedRay(): mMinT(0), mMaxT(1e6), mRay(NULL)
101        {}
102        BoundedRay(Ray *r, float minT, float maxT):
103        mRay(r), mMinT(minT), mMaxT(maxT)
104        {}
105};
106
107typedef vector<BoundedRay *> BoundedRayContainer;
108
109class BspTreeStatistics: public StatisticsBase
110{
111public:
112        // total number of nodes
113        int nodes;
114        // number of splits
115        int splits[3];
116       
117        // totals number of rays
118        int rays;
119        // maximal reached depth
120        int maxDepth;
121        // minimal depth
122        int minDepth;
123       
124        // max depth nodes
125        int maxDepthNodes;
126        // minimum depth nodes
127        int minDepthNodes;
128        // max depth nodes
129        int minPvsNodes;
130        // nodes with minimum PVS
131        int minRaysNodes;
132        // max ray contribution nodes
133        int maxRayContribNodes;
134        // minimum area nodes
135        int minProbabilityNodes;
136        /// nodes termination because of max cost ratio;
137        int maxCostNodes;
138        // max number of rays per node
139        int maxObjectRefs;
140        // accumulated depth (used to compute average)
141        int accumDepth;
142        // number of initial polygons
143        int polys;
144        /// samples contributing to pvs
145        int contributingSamples;
146        /// sample contributions to pvs
147        int sampleContributions;
148        /// largest pvs
149        int maxPvs;
150        /// number of invalid leaves
151        int invalidLeaves;
152        /// polygon splits
153        int polySplits;
154        /// accumulated number of rays refs
155        int accumRays;
156
157        // Constructor
158        BspTreeStatistics()
159        {
160                Reset();
161        }
162
163        int Nodes() const {return nodes;}
164        int Interior() const { return nodes / 2; }
165        int Leaves() const { return (nodes / 2) + 1; }
166       
167        // TODO: computation wrong
168        double AvgDepth() const { return accumDepth / (double)Leaves();};
169        double AvgRays() const { return accumRays / (double)Leaves();};
170
171        void Reset()
172        {
173                nodes = 0;
174                for (int i = 0; i < 3; ++ i)
175                        splits[i] = 0;
176               
177                maxDepth = 0;
178                minDepth = 99999;
179                polys = 0;
180                accumDepth = 0;
181       
182                maxDepthNodes = 0;
183                minPvsNodes = 0;
184                minRaysNodes = 0;
185                maxRayContribNodes = 0;
186                minProbabilityNodes = 0;
187                maxCostNodes = 0;
188
189                contributingSamples = 0;
190                sampleContributions = 0;
191
192                maxPvs = 0;
193                invalidLeaves = 0;
194                polySplits = 0;
195                accumRays = 0;
196        }
197
198        void Print(ostream &app) const;
199
200        friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat)
201        {
202                stat.Print(s);
203                return s;
204        }
205};
206
207
208/**
209    BspNode abstract class serving for interior and leaf node implementation
210*/
211class BspNode
212{
213        friend class BspTree;
214
215public:
216        BspNode();
217        virtual ~BspNode(){};
218        BspNode(BspInterior *parent);
219
220        /** Determines whether this node is a leaf or not
221        @return true if leaf
222        */
223        virtual bool IsLeaf() const = 0;
224
225        /** Determines whether this node is a root
226        @return true if root
227        */
228        virtual bool IsRoot() const;
229
230        /** Returns parent node.
231        */
232        BspInterior *GetParent();
233
234        /** Sets parent node.
235        */
236        void SetParent(BspInterior *parent);
237
238        /** Returns true if this node is a sibling of node n.
239        */
240        bool IsSibling(BspNode *n) const;
241
242        /** returns depth of the node.
243        */
244        int GetDepth() const;
245
246        /** returns true if the whole subtree is valid
247        */
248        bool TreeValid() const;
249
250        void SetTreeValid(const bool v);
251
252        //-- mailing options
253
254        void Mail() { mMailbox = sMailId; }
255        static void NewMail() { ++ sMailId; }
256        bool Mailed() const { return mMailbox == sMailId; }
257
258        static int sMailId;
259        int mMailbox;
260
261protected:
262
263        /// if this sub tree is a completely valid view space region
264        bool mTreeValid;
265        /// parent of this node
266        BspInterior *mParent;
267};
268
269/** BSP interior node implementation
270*/
271class BspInterior : public BspNode
272{
273        friend class BspTree;
274public:
275        /** Standard contructor taking split plane as argument.
276        */
277        BspInterior(const Plane3 &plane);
278        ~BspInterior();
279        /** @return false since it is an interior node
280        */
281        bool IsLeaf() const;
282
283        BspNode *GetBack();
284        BspNode *GetFront();
285
286        /** Returns split plane.
287        */
288        Plane3 GetPlane() const;
289
290        /** Replace front or back child with new child.
291        */
292        void ReplaceChildLink(BspNode *oldChild, BspNode *newChild);
293        /** Replace front and back child.
294        */
295        void SetupChildLinks(BspNode *b, BspNode *f);
296
297        friend ostream &operator<<(ostream &s, const BspInterior &A)
298        {
299                return s << A.mPlane;
300        }
301
302protected:
303
304        /// Splitting plane corresponding to this node
305        Plane3 mPlane;
306
307        /// back node
308        BspNode *mBack;
309        /// front node
310        BspNode *mFront;
311};
312
313/** BSP leaf node implementation.
314*/
315class BspLeaf: public BspNode
316{
317        friend class BspTree;
318
319public:
320        BspLeaf();
321        BspLeaf(BspViewCell *viewCell);
322        BspLeaf(BspInterior *parent);
323        BspLeaf(BspInterior *parent, BspViewCell *viewCell);
324
325        ~BspLeaf();
326
327        /** @return true since it is an interior node
328        */
329        bool IsLeaf() const;
330       
331        /** Returns pointer of view cell.
332        */
333        BspViewCell *GetViewCell() const;
334
335        /** Sets pointer to view cell.
336        */
337        void SetViewCell(BspViewCell *viewCell);
338
339        /// Rays piercing this leaf.
340        VssRayContainer mVssRays;
341       
342        /// leaf pvs
343        ObjectPvs *mPvs;
344
345        /// Probability that the view point lies in this leaf
346        float mProbability;
347
348protected:
349       
350        /// if NULL this does not correspond to feasible viewcell
351        BspViewCell *mViewCell;
352};
353
354/** Implementation of the view cell BSP tree.
355*/
356class BspTree
357{
358        friend class ViewCellsParseHandlers;
359
360public:
361       
362        /** Additional data which is passed down the BSP tree during traversal.
363        */
364        struct BspTraversalData
365        { 
366                /// the current node
367                BspNode *mNode;
368                /// polygonal data for splitting
369                PolygonContainer *mPolygons;
370                /// current depth
371                int mDepth;
372                /// the view cell associated with this subdivsion
373                ViewCell *mViewCell;
374                /// rays piercing this node
375                BoundedRayContainer *mRays;
376                /// area of current node
377                float mArea;
378                /// geometry of node as induced by planes
379                BspNodeGeometry *mGeometry;
380               
381                /// pvs size
382                int mPvs;
383               
384                /** Returns average ray contribution.
385                */
386                float GetAvgRayContribution() const
387                {
388                        return (float)mPvs / ((float)mRays->size() + Limits::Small);
389                }
390
391
392                BspTraversalData():
393                mNode(NULL),
394                mPolygons(NULL),
395                mDepth(0),
396                mViewCell(NULL),
397                mRays(NULL),
398                mPvs(0),
399                mArea(0.0),
400                mGeometry(NULL)
401                {}
402               
403                BspTraversalData(BspNode *node,
404                                                 PolygonContainer *polys,
405                                                 const int depth,
406                                                 ViewCell *viewCell,
407                                                 BoundedRayContainer *rays,
408                                                 int pvs,
409                                                 float area,
410                                                 BspNodeGeometry *cell):
411                mNode(node),
412                mPolygons(polys),
413                mDepth(depth),
414                mViewCell(viewCell),
415                mRays(rays),
416                mPvs(pvs),
417                mArea(area),
418                mGeometry(cell)
419                {}
420    };
421       
422        typedef std::stack<BspTraversalData> BspTraversalStack;
423       
424        /** Default constructor reading the environment file and
425                creating an empty tree.
426        */
427        BspTree();
428        /** Destroys tree and nodes.
429        */
430        ~BspTree();
431
432        /** Returns detailed statistics of the BSP tree.
433        */
434        const BspTreeStatistics &GetStatistics() const;
435 
436        /** Constructs tree using the given list of view cells.
437            For this type of construction we filter all view cells down the
438                tree. If there is no polygon left, the last split plane
439            decides inside or outside of the viewcell. A pointer to the
440                appropriate view cell is stored within each leaf.
441                Many leafs can point to the same viewcell.
442        */
443        void Construct(const ViewCellContainer &viewCells);
444
445        /** Constructs tree using the given list of objects.
446            @note the objects are not taken as view cells, but the view cells are
447                constructed from the subdivision: Each leaf is taken as one viewcell.
448                @param objects list of objects
449        */
450        void Construct(const ObjectContainer &objects);
451
452        void Construct(const ObjectContainer &objects,
453                                   const RayContainer &sampleRays);
454
455        /** Constructs the tree from a given set of rays.
456                @param sampleRays the set of sample rays the construction is based on
457                @param viewCells if not NULL, new view cells are
458                created in the leafs and stored in the conatainer
459        */
460        void Construct(const RayContainer &sampleRays);
461
462        /** Returns list of BSP leaves.
463        */
464        void CollectLeaves(vector<BspLeaf *> &leaves) const;
465
466        /** Returns box which bounds the whole tree.
467        */
468        AxisAlignedBox3 GetBoundingBox()const;
469
470        /** Returns root of BSP tree.
471        */
472        BspNode *GetRoot() const;
473
474        /** Exports Bsp tree to file.
475        */
476        bool Export(const string filename);
477
478        /** Collects the leaf view cells of the tree
479                @param viewCells returns the view cells
480        */
481        void CollectViewCells(ViewCellContainer &viewCells) const;
482
483        /** A ray is cast possible intersecting the tree.
484                @param the ray that is cast.
485                @returns the number of intersections with objects stored in the tree.
486        */
487        int     _CastRay(Ray &ray);
488
489
490        int     CastLineSegment(const Vector3 &origin,
491                                                const Vector3 &termination,
492                                                ViewCellContainer &viewcells
493                                                );
494
495        ViewCell *GetViewCell(const Vector3 &point);
496 
497        /// bsp tree construction types
498        enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
499
500        /** Returns statistics.
501        */
502        BspTreeStatistics &GetStat();
503
504        /** finds neighbouring leaves of this tree node.
505        */
506        int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
507                                          const bool onlyUnmailed) const;
508
509        /** Constructs geometry of view cell returning a BSP node geometry type.
510        */
511        void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const;
512       
513        /** Construct geometry of view cell.
514        */
515        void ConstructGeometry(BspViewCell *vc, BspNodeGeometry &geom) const;
516
517        /** Returns random leaf of BSP tree.
518                @param halfspace defines the halfspace from which the leaf is taken.
519        */
520        BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
521
522        /** Returns random leaf of BSP tree.
523                @param onlyUnmailed if only unmailed leaves should be returned.
524        */
525        BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
526
527        /** Returns view cell corresponding to unbounded space.
528        */
529        BspViewCell *GetRootCell() const;
530
531        /** Returns epsilon of this tree.
532        */
533        float GetEpsilon() const;
534
535protected:
536
537        // --------------------------------------------------------------
538        // For sorting objects
539        // --------------------------------------------------------------
540        struct SortableEntry
541        {
542                enum {POLY_MIN, POLY_MAX};
543   
544                int type;
545                float value;
546                Polygon3 *poly;
547                SortableEntry() {}
548                SortableEntry(const int t, const float v, Polygon3 *poly):
549                type(t), value(v), poly(poly) {}
550               
551                bool operator<(const SortableEntry &b) const
552                {
553                        return value < b.value;
554                } 
555        };
556
557        /** Evaluates tree stats in the BSP tree leafs.
558        */
559        void EvaluateLeafStats(const BspTraversalData &data);
560
561        /** Subdivides node with respect to the traversal data.
562            @param tStack current traversal stack
563                @param tData traversal data also holding node to be subdivided
564                @returns new root of the subtree
565        */
566        BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData);
567
568        /** Constructs the tree from the given list of polygons and rays.
569                @param polys stores set of polygons on which subdivision may be based
570                @param rays storesset of rays on which subdivision may be based
571        */
572        void Construct(PolygonContainer *polys, BoundedRayContainer *rays);
573
574        /** Selects the best possible splitting plane.
575                @param leaf the leaf to be split
576                @param polys the polygon list on which the split decition is based
577                @param rays ray container on which selection may be based
578                @note the polygons can be reordered in the process
579                @returns the split plane
580        */
581        Plane3 SelectPlane(BspLeaf *leaf,
582                                           BspTraversalData &data);
583
584        /** Evaluates the contribution of the candidate split plane.
585               
586                @param candidatePlane the candidate split plane
587                @param polys the polygons the split can be based on
588                @param rays the rays the split can be based on
589
590                @returns the cost of the candidate split plane
591        */
592        float SplitPlaneCost(const Plane3 &candidatePlane,
593                                                 BspTraversalData &data) const;
594
595        /** Strategies where the effect of the split plane is tested
596            on all input rays.
597                @returns the cost of the candidate split plane
598        */
599        float SplitPlaneCost(const Plane3 &candidatePlane,
600                                                 const PolygonContainer &polys) const;
601
602        /** Strategies where the effect of the split plane is tested
603            on all input rays.
604
605                @returns the cost of the candidate split plane
606        */
607        float SplitPlaneCost(const Plane3 &candidatePlane,
608                                                 const BoundedRayContainer &rays,
609                                                 const int pvs,
610                                                 const float area,
611                                                 const BspNodeGeometry &cell) const;
612
613        /** Filters next view cell down the tree and inserts it into the appropriate leaves
614                (i.e., possibly more than one leaf).
615        */
616        void InsertViewCell(ViewCell *viewCell);
617        /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached,
618                then further subdivided.
619        */
620        void InsertPolygons(PolygonContainer *polys);
621
622        /** Subdivide leaf.
623                @param leaf the leaf to be subdivided
624               
625                @param polys the polygons to be split
626                @param frontPolys returns the polygons in front of the split plane
627                @param backPolys returns the polygons in the back of the split plane
628               
629                @param rays the polygons to be filtered
630                @param frontRays returns the polygons in front of the split plane
631                @param backRays returns the polygons in the back of the split plane
632
633                @returns the root of the subdivision
634        */
635
636        BspInterior *SubdivideNode(BspTraversalData &tData,
637                                                           BspTraversalData &frontData,
638                                                           BspTraversalData &backData,
639                                                           PolygonContainer &coincident);
640
641        /** Filters polygons down the tree.
642                @param node the current BSP node
643                @param polys the polygons to be filtered
644                @param frontPolys returns the polygons in front of the split plane
645                @param backPolys returns the polygons in the back of the split plane
646        */
647        void FilterPolygons(BspInterior *node,
648                                                PolygonContainer *polys,
649                                                PolygonContainer *frontPolys,
650                                                PolygonContainer *backPolys);
651
652        /** Take 3 ray endpoints, where two are minimum and one a maximum
653                point or the other way round.
654        */
655        Plane3 ChooseCandidatePlane(const BoundedRayContainer &rays) const;
656
657        /** Take plane normal as plane normal and the midpoint of the ray.
658                PROBLEM: does not resemble any point where visibility is likely to change
659        */
660        Plane3 ChooseCandidatePlane2(const BoundedRayContainer &rays) const;
661
662        /** Fit the plane between the two lines so that the plane has equal shortest
663                distance to both lines.
664        */
665        Plane3 ChooseCandidatePlane3(const BoundedRayContainer &rays) const;
666
667        /** Selects the split plane in order to construct a tree with
668                certain characteristics (e.g., balanced tree, least splits,
669                2.5d aligned)
670                @param polygons container of polygons
671                @param rays bundle of rays on which the split can be based
672        */
673        Plane3 SelectPlaneHeuristics(BspLeaf *leaf,
674                                                                 BspTraversalData &data);
675
676        /** Extracts the meshes of the objects and adds them to polygons.
677                Adds object aabb to the aabb of the tree.
678                @param maxPolys the maximal number of objects to be stored as polygons
679                @returns the number of polygons
680        */
681        int AddToPolygonSoup(const ObjectContainer &objects,
682                                                 PolygonContainer &polys,
683                                                 int maxObjects = 0);
684
685        /** Extracts the meshes of the view cells and and adds them to polygons.
686                Adds view cell aabb to the aabb of the tree.
687                @param maxPolys the maximal number of objects to be stored as polygons
688                @returns the number of polygons
689        */
690        int AddToPolygonSoup(const ViewCellContainer &viewCells,
691                                                 PolygonContainer &polys,
692                                                 int maxObjects = 0);
693
694        /** Extract polygons of this mesh and add to polygon container.
695                @param mesh the mesh that drives the polygon construction
696                @param parent the parent intersectable this polygon is constructed from
697                @returns number of polygons
698        */
699        int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
700
701        /** Helper function which extracts a view cell on the front and the back
702                of the split plane.
703                @param backViewCell returns view cell on the back of the split plane
704                @param frontViewCell returns a view cell on the front of the split plane
705                @param coincident container of polygons coincident to the split plane
706                @param splitPlane the split plane which decides about back and front
707                @param extractBack if a back view cell is extracted
708                @param extractFront if a front view cell is extracted
709        */
710        void ExtractViewCells(BspTraversalData &frontData,
711                                                  BspTraversalData &backData,
712                                                  const PolygonContainer &coincident,
713                                                  const Plane3 &splitPlane) const;
714       
715        /** Computes best cost ratio for the suface area heuristics for axis aligned
716                splits. This heuristics minimizes the cost for ray traversal.
717                @param polys the polygons guiding the ratio computation
718                @param box the bounding box of the leaf
719                @param axis the current split axis
720                @param position returns the split position
721                @param objectsBack the number of objects in the back of the split plane
722                @param objectsFront the number of objects in the front of the split plane
723        */
724        float BestCostRatio(const PolygonContainer &polys,
725                                                const AxisAlignedBox3 &box,
726                                                const int axis,
727                                                float &position,
728                                                int &objectsBack,
729                                                int &objectsFront) const;
730       
731        /** Sorts split candidates for surface area heuristics for axis aligned splits.
732                @param polys the input for choosing split candidates
733                @param axis the current split axis
734                @param splitCandidates returns sorted list of split candidates
735        */
736        void SortSplitCandidates(const PolygonContainer &polys,
737                                                         const int axis,
738                                                         vector<SortableEntry> &splitCandidates) const;
739
740        /** Selects an axis aligned split plane.
741                Returns true if split is valied
742        */
743        bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
744
745        /** Subdivides the rays into front and back rays according to the split plane.
746               
747                @param plane the split plane
748                @param rays contains the rays to be split. The rays are
749                           distributed into front and back rays.
750                @param frontRays returns rays on the front side of the plane
751                @param backRays returns rays on the back side of the plane
752               
753                @returns the number of splits
754        */
755        int SplitRays(const Plane3 &plane,
756                                  BoundedRayContainer &rays,
757                              BoundedRayContainer &frontRays,
758                                  BoundedRayContainer &backRays);
759
760
761        /** Extracts the split planes representing the space bounded by node n.
762        */
763        void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
764
765        /** Adds the object to the pvs of the front and back leaf with a given classification.
766
767                @param obj the object to be added
768                @param cf the ray classification regarding the split plane
769                @param frontPvs returns the PVS of the front partition
770                @param backPvs returns the PVS of the back partition
771       
772        */
773        void AddObjToPvs(Intersectable *obj, const int cf, int &frontPvs, int &backPvs) const;
774
775        /** Computes PVS size induced by the rays.
776        */
777        int ComputePvsSize(const BoundedRayContainer &rays) const;
778
779        /** Returns true if tree can be terminated.
780        */
781        inline bool TerminationCriteriaMet(const BspTraversalData &data) const;
782
783        /** Computes accumulated ray lenght of this rays.
784        */
785        float AccumulatedRayLength(BoundedRayContainer &rays) const;
786
787        /** Splits polygons with respect to the split plane.
788                @param polys the polygons to be split. the polygons are consumed and
789                           distributed to the containers frontPolys, backPolys, coincident.
790                @param frontPolys returns the polygons in the front of the split plane
791                @param backPolys returns the polygons in the back of the split plane
792                @param coincident returns the polygons coincident to the split plane
793
794                @returns the number of splits   
795        */
796        int SplitPolygons(const Plane3 &plane,
797                                          PolygonContainer &polys,
798                                          PolygonContainer &frontPolys,
799                                          PolygonContainer &backPolys,
800                                          PolygonContainer &coincident) const;
801
802        /** Adds ray sample contributions to the PVS.
803                @param sampleContributions the number contributions of the samples
804                @param contributingSampels the number of contributing rays
805               
806        */
807        void AddToPvs(BspLeaf *leaf,
808                                  const BoundedRayContainer &rays,
809                                  int &sampleContributions,     
810                                  int &contributingSamples);
811
812        /// Pointer to the root of the tree.
813        BspNode *mRoot;
814
815        /// Stores statistics during traversal.
816        BspTreeStatistics mStat;
817
818        /// Strategies for choosing next split plane.
819        enum {NO_STRATEGY = 0,
820                  RANDOM_POLYGON = 1,
821                  AXIS_ALIGNED = 2,
822                  LEAST_SPLITS = 4,
823                  BALANCED_POLYS = 8,
824                  BALANCED_VIEW_CELLS = 16,
825                  LARGEST_POLY_AREA = 32,
826                  VERTICAL_AXIS = 64,
827                  BLOCKED_RAYS = 128,
828                  LEAST_RAY_SPLITS = 256,
829                  BALANCED_RAYS = 512,
830                  PVS = 1024
831                };
832
833        /// box around the whole view domain
834        AxisAlignedBox3 mBox;
835
836        /// view cell corresponding to unbounded space
837        BspViewCell *mRootCell;
838
839        /// if view cells should be generated or the given view cells should be used.
840        bool mGenerateViewCells;
841
842        /// maximal number of polygons before subdivision termination
843        int mTermMinPolys;
844        /// maximal number of rays before subdivision termination
845        int mTermMinRays;
846        /// maximal possible depth
847        int mTermMaxDepth;
848        /// mininum area
849        float mTermMinArea;
850        /// mininum PVS
851        int mTermMinPvs;
852
853        /// minimal number of polygons for axis aligned split
854        int mTermMinPolysForAxisAligned;
855        /// minimal number of rays for axis aligned split
856        int mTermMinRaysForAxisAligned;
857        /// minimal number of objects for axis aligned split
858        int mTermMinObjectsForAxisAligned;
859        /// maximal contribution per ray
860        float mTermMaxRayContribution;
861        /// minimal accumulated ray length
862        float mTermMinAccRayLength;
863
864
865        /// strategy to get the best split plane
866        int mSplitPlaneStrategy;
867        /// number of candidates evaluated for the next split plane
868        int mMaxPolyCandidates;
869        /// number of candidates for split planes evaluated using the rays
870        int mMaxRayCandidates;
871        /// maximum tests for split plane evaluation with a single candidate
872        int mMaxTests;
873
874        float mCtDivCi;
875
876        /// axis aligned split criteria
877        float mAxisAlignedCtDivCi;
878        float mSplitBorder;
879        float mMaxCostRatio;
880
881        // factors guiding the split plane heuristics
882        float mVerticalSplitsFactor;
883        float mLargestPolyAreaFactor;
884        float mBlockedRaysFactor;
885        float mLeastRaySplitsFactor;
886        float mBalancedRaysFactor;
887        float mPvsFactor;
888        float mLeastSplitsFactor;
889        float mBalancedPolysFactor;
890        float mBalancedViewCellsFactor;
891
892        /// if area or accumulated ray lenght should be used for PVS heuristics
893        bool mUseAreaForPvs;
894
895        /// epsilon where two points are still considered equal
896        float mEpsilon;
897
898private:
899       
900        /** Evaluates split plane classification with respect to the plane's
901                contribution for a balanced tree.
902        */
903        static const float sLeastPolySplitsTable[4];
904        /** Evaluates split plane classification with respect to the plane's
905                contribution for a minimum number splits in the tree.
906        */
907        static const float sBalancedPolysTable[4];
908        /** Evaluates split plane classification with respect to the plane's
909                contribution for a minimum number of ray splits.
910        */
911        static const float sLeastRaySplitsTable[5];
912        /** Evaluates split plane classification with respect to the plane's
913                contribution for balanced rays.
914        */
915        static const float sBalancedRaysTable[5];
916
917        /// Generates unique ids for PVS criterium
918        static void GenerateUniqueIdsForPvs();
919
920        //-- unique ids for PVS criterium
921        static int sFrontId;
922        static int sBackId;
923        static int sFrontAndBackId;
924};
925
926struct BspIntersection
927{
928        // the point of intersection
929        float mT;
930 
931        BspLeaf *mLeaf;
932 
933        BspIntersection(const float t, BspLeaf *l):
934        mT(t), mLeaf(l) {}
935 
936        BspIntersection() {}
937 
938        bool operator<(const BspIntersection &b) const
939        {
940                return mT < b.mT;
941        }
942};
943
944struct BspRay
945{
946        VssRay *vssRay;
947
948        std::vector<BspIntersection> intersections;
949
950        BspRay(VssRay *ray): vssRay(ray) {}
951};
952
953#endif
Note: See TracBrowser for help on using the repository browser.