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

Revision 241, 9.2 KB checked in by mattausch, 19 years ago (diff)

added bsp stuff

RevLine 
[221]1#ifndef _ViewCellBsp_H__
2#define _ViewCellBsp_H__
3
4#include "Mesh.h"
[224]5#include "Containers.h"
[241]6
7
[225]8#include <queue>
[233]9#include <stack>
[224]10
[221]11class ViewCell;
12class Plane3;
13//class Mesh;
14
15//namespace GtpVisibilityPreprocessor {
[224]16class BspTree; 
[221]17class BspInterior;
[235]18class Polygon3;
[221]19
[238]20/** Container storing a soup of polygons used during BSP tree construction
[235]21*/
[238]22typedef std::vector<Polygon3 *> PolygonContainer;
[224]23
[241]24struct BspRayTraversalData
25{
26    BspNode *mNode;
27    Vector3 mExitPoint;
28    float mMaxT;
29   
30    BspRayTraversalData() {}
31
32    BspRayTraversalData(BspNode *n, const Vector3 &p, const float maxt):
33    mNode(n), mExitPoint(p), mMaxT(maxt)
34        {}
35};
36
[234]37class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics
38{
39public:
40  // total number of nodes
41  int nodes;
[235]42  // number of splits
43  int splits;
[234]44  // totals number of rays
45  int rays;
[235]46  // maximal reached depth
47  int maxDepth;
[234]48  // total number of query domains
49  int queryDomains;
50  // total number of ray references
51  int rayRefs;
52  // refs in non empty leafs
53  int rayRefsNonZeroQuery;
54  // total number of query references
55  int objectRefs;
56  // nodes with zero queries
57  int zeroQueryNodes;
58  // max depth nodes
59  int maxDepthNodes;
60  // max number of rays per node
61  int maxObjectRefs;
62  // number of dynamically added ray refs
63  int addedRayRefs;
64  // number of dynamically removed ray refs
65  int removedRayRefs;
66 
67  // Constructor
68  BspTreeStatistics()
69  {
[235]70          Reset();
[234]71  }
72
73  int Nodes() const {return nodes;}
74  int Interior() const { return nodes/2; }
75  int Leaves() const { return (nodes/2) + 1; }
76
77  void Reset()
78  {
79          nodes = 0;
[235]80          splits = 0;
[234]81          rays = queryDomains = 0;
82          rayRefs = rayRefsNonZeroQuery = objectRefs = 0;
83          zeroQueryNodes = 0;
84      maxDepthNodes = 0;
85      //minCostNodes = 0;
86          maxObjectRefs = 0;
87          addedRayRefs = removedRayRefs = 0;
88  }
89
90  void
91  Print(ostream &app) const;
92
93  friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) {
94    stat.Print(s);
95    return s;
96  }
97 
98};
99
[233]100/**
101    BspNode abstract class serving for interior and leaf node implementation
102*/
103class BspNode
104{
105        friend BspTree;
[221]106
[233]107public:
[235]108        BspNode();
109        BspNode(BspInterior *parent);
110
[233]111        /** Determines whether this node is a leaf or not
112        @return true if leaf
113        */
114        virtual bool IsLeaf() const = 0;
[221]115
[233]116        /** Determines whether this node is a root
117        @return true if root
118        */
119        virtual bool IsRoot() const;
[221]120
[233]121        /** Returns parent node.
122        */
123        BspInterior *GetParent();
[235]124        /** Sets parent node.
125        */
126        void SetParent(BspInterior *parent);
127
[233]128protected:
[221]129
[233]130        /// parent of this node
131        BspInterior *mParent;
132};
[221]133
[233]134/** BSP interior node implementation
135*/
136class BspInterior : public BspNode
137{
138public:
139        /** Standard contructor taking split plane as argument.
140        */
141        BspInterior(Plane3 plane);
142        /** @return false since it is an interior node
143        */
144        bool IsLeaf() const;
[222]145
[233]146        BspNode *GetBack();
147        BspNode *GetFront();
[221]148
[233]149        Plane3 *GetPlane();
[221]150
[233]151        void ReplaceChildLink(BspNode *oldChild, BspNode *newChild);
152        void SetupChildLinks(BspNode *b, BspNode *f);
[225]153
[233]154        /** Splits polygons.
155                @param polys the polygons to be split
156                @param frontPolys returns the polygons in the front of the split plane
157                @param backPolys returns the polygons in the back of the split plane
[235]158                @param splits number of splits
[233]159        */
[238]160        void SplitPolygons(PolygonContainer *polys, PolygonContainer *frontPolys,
161                                           PolygonContainer *backPolys, int &splits);
[225]162
[237]163        friend ostream &operator<<(ostream &s, const BspInterior &A)
164        {
165                return s << A.mPlane;
166        }
167
168
[233]169protected:
170       
171        /// Splitting plane corresponding to this node
172        Plane3 mPlane;
173        /// back node
174        BspNode *mBack;
175        /// front node
176        BspNode *mFront;
177};
[225]178
[237]179/** BSP leaf node implementation.
180*/
[233]181class BspLeaf : public BspNode
182{
183public:
184        BspLeaf(ViewCell *viewCell = NULL);
[225]185
[233]186        /** @return true since it is an interior node */
187        bool IsLeaf() const;
188        ViewCell *GetViewCell();
[225]189
[233]190protected:
[225]191
[233]192        /// polygonal representation of this viewcell
193        /// if NULL this note does not correspond to feasible viewcell
194        ViewCell *mViewCell;
195};
[225]196
197
[233]198/** Implementation of the ViewCell BSP tree. */
199class BspTree
200{
201public:
202               
203        /** Additional data which is passed down the BSP tree during traversal.
204        */
205        struct BspTraversalData
206        { 
207                /// the current node
208                BspNode *mNode;
209                /// parent of current node
210                BspInterior *mParent;
211                /// polygonal data for splitting
[238]212                PolygonContainer *mPolygons;
[233]213                /// current depth
214                int mDepth;
215               
216                BspTraversalData() {}
217               
[238]218                BspTraversalData(BspNode *node, BspInterior *parent, PolygonContainer *polys, const int depth):
[235]219                mNode(node), mParent(parent), mPolygons(polys), mDepth(depth) {}
[233]220    };
221
222        typedef std::stack<BspTraversalData> BspTraversalStack;
223
[235]224        /// BSP tree construction type
225        enum {VIEWCELLS, SCENE_GEOMETRY};
226
227        /** Default constructor creating an empty tree.
[233]228        */
[235]229        BspTree();
[233]230
231        ~BspTree();
232
[235]233        const BspTreeStatistics &GetStatistics() const;
234 
235        /** Constructs tree using the given list of viewcells.
236            A pointer to the appropriate viewcell is stored within each leaf.
237                Many leafs can point to the same viewcell.
238        */
239        void Construct(const ViewCellContainer &viewCells);
240        /** Constructs tree using the given list of objects. Each leaf is taken as viewcell.
241            No objects are treated as viewcells explicitly.
242
243                @param objects list of objects
244        */
245        void Construct(const ObjectContainer &objects);
246
[240]247        int BspTree::CollectLeafPvs();
[235]248
[240]249        void CollectLeaves(vector<BspLeaf *> &leaves);
250
[233]251protected:
[238]252       
253        /** Evaluates plane classification with respect to the plane's
254                contribution for a balanced tree.
255        */
256        static inline int EvalForBalancedTree(const int classficiation);
257        /** Evaluates plane classification with respect to the plane's
258                contribution for a minimum number splits in the tree.
259        */
260        static inline int EvalForLeastSplits(const int classification);
261
262        /** Evaluates the contribution of the candidate split plane.
263        */
264        int EvalSplitPlane(PolygonContainer *polygons, const Plane3 &candidatePlane) const;
265
266        /** Evaluates tree stats in the BSP tree leafs.
267        */
[235]268        void EvaluateLeafStats(const BspTraversalData &data);
269
270        /** Subdivides node with respect to the traversal data.
271            @param tStack current traversal stack
272                @param tData traversal data also holding node to be subdivided
273                @param viewCell the view cell that will be represented with this part of the Bsp tree.
[239]274                @returns new root of the subtree
[233]275        */
[239]276        BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL);
[233]277
[235]278        /** Selects a splitting plane.
[238]279                @param polygons the polygon data on which the split decition is based
[233]280        */
[238]281        Plane3 SelectPlane(PolygonContainer *polygons) const;
[224]282
[233]283        /** Filters next viewcell down the tree and inserts it into the appropriate leaves
284                (i.e., possibly more than one leaf).
285        */
[235]286        void InsertViewCell(ViewCell *viewCell);
[233]287       
[235]288        /** Subdivide leaf.
[233]289                @param leaf the leaf to be subdivided
290                @param parent the parent node of this leaf
291                @param polys the input polygons
292                @param depth the current tree depth
293                @param frontPolys the polygons of the front child node as a result from splitting
294                @param backPolys the polygons of the back child node as a result from splitting
295        */
296        BspNode *SubdivideNode(BspLeaf *leaf, BspInterior *parent,
[238]297                                                        PolygonContainer *polys, const int depth,
[233]298                                                        ViewCell *viewCell,
[238]299                                                        PolygonContainer *frontPolys, PolygonContainer *backPolys);
[221]300
[233]301        /** Filters polygons down the tree.
302                @param node the current BSP node
303                @param polys the polygons to be filtered
304                @param frontPolys returns the polygons in front of the split plane
305                @param backPolys returns the polygons in the back of the split plane
306        */
[238]307        void FilterPolygons(BspInterior *node, PolygonContainer *polys,
308                                                PolygonContainer *frontPolys, PolygonContainer *backPolys);
[224]309
[238]310        /** Selects the split plane in order to get a balanced tree.
311                @param polygons container of polygons
312                @param maxTests the maximal number of candidate tests
313        */
314        Plane3 SelectPlaneHeuristics(PolygonContainer *polygons, int maxTests) const;
315
[241]316        /** Extracts the meshes of the objects and copies them into the mesh. Also calculcates the bounding box of the tree.
317                @param maxPolys the maximal number of objects to be stored as polygons
[233]318        */
[241]319        void Copy2PolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxObjects);
[224]320
[241]321        /** Add this mesh as polygons to polygon container.
[235]322        */
[241]323        void AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys);
[233]324
[241]325        /** A ray is cast possible intersecting the tree.
326                @param the ray that is cast.
327                @returns the number of intersections with objects stored in the tree.
328        */
329        int CastRay(Ray &ray);
330
[233]331        /// Pointer to the root of the tree
332        BspNode *mRoot;
[237]333
[233]334        /// Pointer to the root cell of the viewspace
335        // ViewCell *mRootCell;
[237]336               
[235]337        BspTreeStatistics mStat;
[234]338
[235]339        /// local maximal polygons (changes depending on method)
340        int mTermMaxPolygons;
341        int mTermMaxDepth;
342
[237]343        /// Strategies for choosing next split plane.
[238]344        enum {NEXT_POLYGON, LEAST_SPLITS, BALANCED_TREE, COMBINED};
[236]345
[241]346        /// box around the whole view domain
347        AxisAlignedBox3 mBox;
348
[235]349public:
350        /// Parses the environment and stores the global BSP tree parameters
351        static void ParseEnvironment();
352
353        /// maximal number of polygons where tree construction is terminated
354        static int sTermMaxPolygons;
355        /// maximal possible depth
356        static int sTermMaxDepth;
[238]357        /// strategy to get the best split plane
[236]358        static int sSplitPlaneStrategy;
[238]359        /// number of candidates evaluated for the next split plane
360        static int sMaxCandidates;
[233]361};
362
[221]363//}; // GtpVisibilityPreprocessor
364
365#endif
Note: See TracBrowser for help on using the repository browser.