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

Revision 574, 25.4 KB checked in by mattausch, 18 years ago (diff)

finished function for view cell construction
removed bsp rays from vspbspmanager

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{
358public:
359       
360        /** Additional data which is passed down the BSP tree during traversal.
361        */
362        struct BspTraversalData
363        { 
364                /// the current node
365                BspNode *mNode;
366                /// polygonal data for splitting
367                PolygonContainer *mPolygons;
368                /// current depth
369                int mDepth;
370                /// the view cell associated with this subdivsion
371                ViewCell *mViewCell;
372                /// rays piercing this node
373                BoundedRayContainer *mRays;
374                /// area of current node
375                float mArea;
376                /// geometry of node as induced by planes
377                BspNodeGeometry *mGeometry;
378               
379                /// pvs size
380                int mPvs;
381               
382                /** Returns average ray contribution.
383                */
384                float GetAvgRayContribution() const
385                {
386                        return (float)mPvs / ((float)mRays->size() + Limits::Small);
387                }
388
389
390                BspTraversalData():
391                mNode(NULL),
392                mPolygons(NULL),
393                mDepth(0),
394                mViewCell(NULL),
395                mRays(NULL),
396                mPvs(0),
397                mArea(0.0),
398                mGeometry(NULL)
399                {}
400               
401                BspTraversalData(BspNode *node,
402                                                 PolygonContainer *polys,
403                                                 const int depth,
404                                                 ViewCell *viewCell,
405                                                 BoundedRayContainer *rays,
406                                                 int pvs,
407                                                 float area,
408                                                 BspNodeGeometry *cell):
409                mNode(node),
410                mPolygons(polys),
411                mDepth(depth),
412                mViewCell(viewCell),
413                mRays(rays),
414                mPvs(pvs),
415                mArea(area),
416                mGeometry(cell)
417                {}
418    };
419       
420        typedef std::stack<BspTraversalData> BspTraversalStack;
421       
422        /** Default constructor reading the environment file and
423                creating an empty tree.
424        */
425        BspTree();
426        /** Destroys tree and nodes.
427        */
428        ~BspTree();
429
430        /** Returns detailed statistics of the BSP tree.
431        */
432        const BspTreeStatistics &GetStatistics() const;
433 
434        /** Constructs tree using the given list of view cells.
435            For this type of construction we filter all view cells down the
436                tree. If there is no polygon left, the last split plane
437            decides inside or outside of the viewcell. A pointer to the
438                appropriate view cell is stored within each leaf.
439                Many leafs can point to the same viewcell.
440        */
441        void Construct(const ViewCellContainer &viewCells);
442
443        /** Constructs tree using the given list of objects.
444            @note the objects are not taken as view cells, but the view cells are
445                constructed from the subdivision: Each leaf is taken as one viewcell.
446                @param objects list of objects
447        */
448        void Construct(const ObjectContainer &objects);
449
450        void Construct(const ObjectContainer &objects,
451                                   const RayContainer &sampleRays);
452
453        /** Constructs the tree from a given set of rays.
454                @param sampleRays the set of sample rays the construction is based on
455                @param viewCells if not NULL, new view cells are
456                created in the leafs and stored in the conatainer
457        */
458        void Construct(const RayContainer &sampleRays);
459
460        /** Returns list of BSP leaves.
461        */
462        void CollectLeaves(vector<BspLeaf *> &leaves) const;
463
464        /** Returns box which bounds the whole tree.
465        */
466        AxisAlignedBox3 GetBoundingBox()const;
467
468        /** Returns root of BSP tree.
469        */
470        BspNode *GetRoot() const;
471
472        /** Exports Bsp tree to file.
473        */
474        bool Export(const string filename);
475
476        /** Collects the leaf view cells of the tree
477                @param viewCells returns the view cells
478        */
479        void CollectViewCells(ViewCellContainer &viewCells) const;
480
481        /** A ray is cast possible intersecting the tree.
482                @param the ray that is cast.
483                @returns the number of intersections with objects stored in the tree.
484        */
485        int     _CastRay(Ray &ray);
486
487
488        int     CastLineSegment(const Vector3 &origin,
489                                                const Vector3 &termination,
490                                                ViewCellContainer &viewcells
491                                                );
492
493        ViewCell *GetViewCell(const Vector3 &point);
494 
495        /// bsp tree construction types
496        enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
497
498        /** Returns statistics.
499        */
500        BspTreeStatistics &GetStat();
501
502        /** finds neighbouring leaves of this tree node.
503        */
504        int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
505                                          const bool onlyUnmailed) const;
506
507        /** Constructs geometry of view cell returning a BSP node geometry type.
508        */
509        void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const;
510       
511        /** Construct geometry of view cell.
512        */
513        void ConstructGeometry(BspViewCell *vc, BspNodeGeometry &geom) const;
514
515        /** Returns random leaf of BSP tree.
516                @param halfspace defines the halfspace from which the leaf is taken.
517        */
518        BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
519
520        /** Returns random leaf of BSP tree.
521                @param onlyUnmailed if only unmailed leaves should be returned.
522        */
523        BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
524
525        /** Returns view cell corresponding to unbounded space.
526        */
527        BspViewCell *GetRootCell() const;
528
529        /** Returns epsilon of this tree.
530        */
531        float GetEpsilon() const;
532
533protected:
534
535        // --------------------------------------------------------------
536        // For sorting objects
537        // --------------------------------------------------------------
538        struct SortableEntry
539        {
540                enum {POLY_MIN, POLY_MAX};
541   
542                int type;
543                float value;
544                Polygon3 *poly;
545                SortableEntry() {}
546                SortableEntry(const int t, const float v, Polygon3 *poly):
547                type(t), value(v), poly(poly) {}
548               
549                bool operator<(const SortableEntry &b) const
550                {
551                        return value < b.value;
552                } 
553        };
554
555        /** Evaluates tree stats in the BSP tree leafs.
556        */
557        void EvaluateLeafStats(const BspTraversalData &data);
558
559        /** Subdivides node with respect to the traversal data.
560            @param tStack current traversal stack
561                @param tData traversal data also holding node to be subdivided
562                @returns new root of the subtree
563        */
564        BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData);
565
566        /** Constructs the tree from the given list of polygons and rays.
567                @param polys stores set of polygons on which subdivision may be based
568                @param rays storesset of rays on which subdivision may be based
569        */
570        void Construct(PolygonContainer *polys, BoundedRayContainer *rays);
571
572        /** Selects the best possible splitting plane.
573                @param leaf the leaf to be split
574                @param polys the polygon list on which the split decition is based
575                @param rays ray container on which selection may be based
576                @note the polygons can be reordered in the process
577                @returns the split plane
578        */
579        Plane3 SelectPlane(BspLeaf *leaf,
580                                           BspTraversalData &data);
581
582        /** Evaluates the contribution of the candidate split plane.
583               
584                @param candidatePlane the candidate split plane
585                @param polys the polygons the split can be based on
586                @param rays the rays the split can be based on
587
588                @returns the cost of the candidate split plane
589        */
590        float SplitPlaneCost(const Plane3 &candidatePlane,
591                                                 BspTraversalData &data) const;
592
593        /** Strategies where the effect of the split plane is tested
594            on all input rays.
595                @returns the cost of the candidate split plane
596        */
597        float SplitPlaneCost(const Plane3 &candidatePlane,
598                                                 const PolygonContainer &polys) const;
599
600        /** Strategies where the effect of the split plane is tested
601            on all input rays.
602
603                @returns the cost of the candidate split plane
604        */
605        float SplitPlaneCost(const Plane3 &candidatePlane,
606                                                 const BoundedRayContainer &rays,
607                                                 const int pvs,
608                                                 const float area,
609                                                 const BspNodeGeometry &cell) const;
610
611        /** Filters next view cell down the tree and inserts it into the appropriate leaves
612                (i.e., possibly more than one leaf).
613        */
614        void InsertViewCell(ViewCell *viewCell);
615        /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached,
616                then further subdivided.
617        */
618        void InsertPolygons(PolygonContainer *polys);
619
620        /** Subdivide leaf.
621                @param leaf the leaf to be subdivided
622               
623                @param polys the polygons to be split
624                @param frontPolys returns the polygons in front of the split plane
625                @param backPolys returns the polygons in the back of the split plane
626               
627                @param rays the polygons to be filtered
628                @param frontRays returns the polygons in front of the split plane
629                @param backRays returns the polygons in the back of the split plane
630
631                @returns the root of the subdivision
632        */
633
634        BspInterior *SubdivideNode(BspTraversalData &tData,
635                                                           BspTraversalData &frontData,
636                                                           BspTraversalData &backData,
637                                                           PolygonContainer &coincident);
638
639        /** Filters polygons down the tree.
640                @param node the current BSP node
641                @param polys the polygons to be filtered
642                @param frontPolys returns the polygons in front of the split plane
643                @param backPolys returns the polygons in the back of the split plane
644        */
645        void FilterPolygons(BspInterior *node,
646                                                PolygonContainer *polys,
647                                                PolygonContainer *frontPolys,
648                                                PolygonContainer *backPolys);
649
650        /** Take 3 ray endpoints, where two are minimum and one a maximum
651                point or the other way round.
652        */
653        Plane3 ChooseCandidatePlane(const BoundedRayContainer &rays) const;
654
655        /** Take plane normal as plane normal and the midpoint of the ray.
656                PROBLEM: does not resemble any point where visibility is likely to change
657        */
658        Plane3 ChooseCandidatePlane2(const BoundedRayContainer &rays) const;
659
660        /** Fit the plane between the two lines so that the plane has equal shortest
661                distance to both lines.
662        */
663        Plane3 ChooseCandidatePlane3(const BoundedRayContainer &rays) const;
664
665        /** Selects the split plane in order to construct a tree with
666                certain characteristics (e.g., balanced tree, least splits,
667                2.5d aligned)
668                @param polygons container of polygons
669                @param rays bundle of rays on which the split can be based
670        */
671        Plane3 SelectPlaneHeuristics(BspLeaf *leaf,
672                                                                 BspTraversalData &data);
673
674        /** Extracts the meshes of the objects and adds them to polygons.
675                Adds object aabb to the aabb of the tree.
676                @param maxPolys the maximal number of objects to be stored as polygons
677                @returns the number of polygons
678        */
679        int AddToPolygonSoup(const ObjectContainer &objects,
680                                                 PolygonContainer &polys,
681                                                 int maxObjects = 0);
682
683        /** Extracts the meshes of the view cells and and adds them to polygons.
684                Adds view cell aabb to the aabb of the tree.
685                @param maxPolys the maximal number of objects to be stored as polygons
686                @returns the number of polygons
687        */
688        int AddToPolygonSoup(const ViewCellContainer &viewCells,
689                                                 PolygonContainer &polys,
690                                                 int maxObjects = 0);
691
692        /** Extract polygons of this mesh and add to polygon container.
693                @param mesh the mesh that drives the polygon construction
694                @param parent the parent intersectable this polygon is constructed from
695                @returns number of polygons
696        */
697        int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
698
699        /** Helper function which extracts a view cell on the front and the back
700                of the split plane.
701                @param backViewCell returns view cell on the back of the split plane
702                @param frontViewCell returns a view cell on the front of the split plane
703                @param coincident container of polygons coincident to the split plane
704                @param splitPlane the split plane which decides about back and front
705                @param extractBack if a back view cell is extracted
706                @param extractFront if a front view cell is extracted
707        */
708        void ExtractViewCells(BspTraversalData &frontData,
709                                                  BspTraversalData &backData,
710                                                  const PolygonContainer &coincident,
711                                                  const Plane3 &splitPlane) const;
712       
713        /** Computes best cost ratio for the suface area heuristics for axis aligned
714                splits. This heuristics minimizes the cost for ray traversal.
715                @param polys the polygons guiding the ratio computation
716                @param box the bounding box of the leaf
717                @param axis the current split axis
718                @param position returns the split position
719                @param objectsBack the number of objects in the back of the split plane
720                @param objectsFront the number of objects in the front of the split plane
721        */
722        float BestCostRatio(const PolygonContainer &polys,
723                                                const AxisAlignedBox3 &box,
724                                                const int axis,
725                                                float &position,
726                                                int &objectsBack,
727                                                int &objectsFront) const;
728       
729        /** Sorts split candidates for surface area heuristics for axis aligned splits.
730                @param polys the input for choosing split candidates
731                @param axis the current split axis
732                @param splitCandidates returns sorted list of split candidates
733        */
734        void SortSplitCandidates(const PolygonContainer &polys,
735                                                         const int axis,
736                                                         vector<SortableEntry> &splitCandidates) const;
737
738        /** Selects an axis aligned split plane.
739                Returns true if split is valied
740        */
741        bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
742
743        /** Subdivides the rays into front and back rays according to the split plane.
744               
745                @param plane the split plane
746                @param rays contains the rays to be split. The rays are
747                           distributed into front and back rays.
748                @param frontRays returns rays on the front side of the plane
749                @param backRays returns rays on the back side of the plane
750               
751                @returns the number of splits
752        */
753        int SplitRays(const Plane3 &plane,
754                                  BoundedRayContainer &rays,
755                              BoundedRayContainer &frontRays,
756                                  BoundedRayContainer &backRays);
757
758
759        /** Extracts the split planes representing the space bounded by node n.
760        */
761        void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
762
763        /** Adds the object to the pvs of the front and back leaf with a given classification.
764
765                @param obj the object to be added
766                @param cf the ray classification regarding the split plane
767                @param frontPvs returns the PVS of the front partition
768                @param backPvs returns the PVS of the back partition
769       
770        */
771        void AddObjToPvs(Intersectable *obj, const int cf, int &frontPvs, int &backPvs) const;
772
773        /** Computes PVS size induced by the rays.
774        */
775        int ComputePvsSize(const BoundedRayContainer &rays) const;
776
777        /** Returns true if tree can be terminated.
778        */
779        inline bool TerminationCriteriaMet(const BspTraversalData &data) const;
780
781        /** Computes accumulated ray lenght of this rays.
782        */
783        float AccumulatedRayLength(BoundedRayContainer &rays) const;
784
785        /** Splits polygons with respect to the split plane.
786                @param polys the polygons to be split. the polygons are consumed and
787                           distributed to the containers frontPolys, backPolys, coincident.
788                @param frontPolys returns the polygons in the front of the split plane
789                @param backPolys returns the polygons in the back of the split plane
790                @param coincident returns the polygons coincident to the split plane
791
792                @returns the number of splits   
793        */
794        int SplitPolygons(const Plane3 &plane,
795                                          PolygonContainer &polys,
796                                          PolygonContainer &frontPolys,
797                                          PolygonContainer &backPolys,
798                                          PolygonContainer &coincident) const;
799
800        /** Adds ray sample contributions to the PVS.
801                @param sampleContributions the number contributions of the samples
802                @param contributingSampels the number of contributing rays
803               
804        */
805        void AddToPvs(BspLeaf *leaf,
806                                  const BoundedRayContainer &rays,
807                                  int &sampleContributions,     
808                                  int &contributingSamples);
809
810        /// Pointer to the root of the tree.
811        BspNode *mRoot;
812
813        /// Stores statistics during traversal.
814        BspTreeStatistics mStat;
815
816        /// Strategies for choosing next split plane.
817        enum {NO_STRATEGY = 0,
818                  RANDOM_POLYGON = 1,
819                  AXIS_ALIGNED = 2,
820                  LEAST_SPLITS = 4,
821                  BALANCED_POLYS = 8,
822                  BALANCED_VIEW_CELLS = 16,
823                  LARGEST_POLY_AREA = 32,
824                  VERTICAL_AXIS = 64,
825                  BLOCKED_RAYS = 128,
826                  LEAST_RAY_SPLITS = 256,
827                  BALANCED_RAYS = 512,
828                  PVS = 1024
829                };
830
831        /// box around the whole view domain
832        AxisAlignedBox3 mBox;
833
834        /// view cell corresponding to unbounded space
835        BspViewCell *mRootCell;
836
837        /// if view cells should be generated or the given view cells should be used.
838        bool mGenerateViewCells;
839
840        /// maximal number of polygons before subdivision termination
841        int mTermMinPolys;
842        /// maximal number of rays before subdivision termination
843        int mTermMinRays;
844        /// maximal possible depth
845        int mTermMaxDepth;
846        /// mininum area
847        float mTermMinArea;
848        /// mininum PVS
849        int mTermMinPvs;
850
851        /// minimal number of polygons for axis aligned split
852        int mTermMinPolysForAxisAligned;
853        /// minimal number of rays for axis aligned split
854        int mTermMinRaysForAxisAligned;
855        /// minimal number of objects for axis aligned split
856        int mTermMinObjectsForAxisAligned;
857        /// maximal contribution per ray
858        float mTermMaxRayContribution;
859        /// minimal accumulated ray length
860        float mTermMinAccRayLength;
861
862
863        /// strategy to get the best split plane
864        int mSplitPlaneStrategy;
865        /// number of candidates evaluated for the next split plane
866        int mMaxPolyCandidates;
867        /// number of candidates for split planes evaluated using the rays
868        int mMaxRayCandidates;
869        /// maximum tests for split plane evaluation with a single candidate
870        int mMaxTests;
871
872        float mCtDivCi;
873
874        /// axis aligned split criteria
875        float mAxisAlignedCtDivCi;
876        float mSplitBorder;
877        float mMaxCostRatio;
878
879        // factors guiding the split plane heuristics
880        float mVerticalSplitsFactor;
881        float mLargestPolyAreaFactor;
882        float mBlockedRaysFactor;
883        float mLeastRaySplitsFactor;
884        float mBalancedRaysFactor;
885        float mPvsFactor;
886        float mLeastSplitsFactor;
887        float mBalancedPolysFactor;
888        float mBalancedViewCellsFactor;
889
890        /// if area or accumulated ray lenght should be used for PVS heuristics
891        bool mUseAreaForPvs;
892
893        /// epsilon where two points are still considered equal
894        float mEpsilon;
895
896private:
897       
898        /** Evaluates split plane classification with respect to the plane's
899                contribution for a balanced tree.
900        */
901        static const float sLeastPolySplitsTable[4];
902        /** Evaluates split plane classification with respect to the plane's
903                contribution for a minimum number splits in the tree.
904        */
905        static const float sBalancedPolysTable[4];
906        /** Evaluates split plane classification with respect to the plane's
907                contribution for a minimum number of ray splits.
908        */
909        static const float sLeastRaySplitsTable[5];
910        /** Evaluates split plane classification with respect to the plane's
911                contribution for balanced rays.
912        */
913        static const float sBalancedRaysTable[5];
914
915        /// Generates unique ids for PVS criterium
916        static void GenerateUniqueIdsForPvs();
917
918        //-- unique ids for PVS criterium
919        static int sFrontId;
920        static int sBackId;
921        static int sFrontAndBackId;
922};
923
924struct BspIntersection
925{
926        // the point of intersection
927        float mT;
928 
929        BspLeaf *mLeaf;
930 
931        BspIntersection(const float t, BspLeaf *l):
932        mT(t), mLeaf(l) {}
933 
934        BspIntersection() {}
935 
936        bool operator<(const BspIntersection &b) const
937        {
938                return mT < b.mT;
939        }
940};
941
942struct BspRay
943{
944        VssRay *vssRay;
945
946        std::vector<BspIntersection> intersections;
947
948        BspRay(VssRay *ray): vssRay(ray) {}
949};
950
951#endif
Note: See TracBrowser for help on using the repository browser.