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

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