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

Revision 503, 25.1 KB checked in by mattausch, 19 years ago (diff)

added mesh creation function

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