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

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

added bsp stuff

Line 
1#ifndef _ViewCellBsp_H__
2#define _ViewCellBsp_H__
3
4#include "Mesh.h"
5#include "Containers.h"
6
7
8#include <queue>
9#include <stack>
10
11class ViewCell;
12class Plane3;
13//class Mesh;
14
15//namespace GtpVisibilityPreprocessor {
16class BspTree; 
17class BspInterior;
18class Polygon3;
19
20/** Container storing a soup of polygons used during BSP tree construction
21*/
22typedef std::vector<Polygon3 *> PolygonContainer;
23
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
37class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics
38{
39public:
40  // total number of nodes
41  int nodes;
42  // number of splits
43  int splits;
44  // totals number of rays
45  int rays;
46  // maximal reached depth
47  int maxDepth;
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  {
70          Reset();
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;
80          splits = 0;
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
100/**
101    BspNode abstract class serving for interior and leaf node implementation
102*/
103class BspNode
104{
105        friend BspTree;
106
107public:
108        BspNode();
109        BspNode(BspInterior *parent);
110
111        /** Determines whether this node is a leaf or not
112        @return true if leaf
113        */
114        virtual bool IsLeaf() const = 0;
115
116        /** Determines whether this node is a root
117        @return true if root
118        */
119        virtual bool IsRoot() const;
120
121        /** Returns parent node.
122        */
123        BspInterior *GetParent();
124        /** Sets parent node.
125        */
126        void SetParent(BspInterior *parent);
127
128protected:
129
130        /// parent of this node
131        BspInterior *mParent;
132};
133
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;
145
146        BspNode *GetBack();
147        BspNode *GetFront();
148
149        Plane3 *GetPlane();
150
151        void ReplaceChildLink(BspNode *oldChild, BspNode *newChild);
152        void SetupChildLinks(BspNode *b, BspNode *f);
153
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
158                @param splits number of splits
159        */
160        void SplitPolygons(PolygonContainer *polys, PolygonContainer *frontPolys,
161                                           PolygonContainer *backPolys, int &splits);
162
163        friend ostream &operator<<(ostream &s, const BspInterior &A)
164        {
165                return s << A.mPlane;
166        }
167
168
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};
178
179/** BSP leaf node implementation.
180*/
181class BspLeaf : public BspNode
182{
183public:
184        BspLeaf(ViewCell *viewCell = NULL);
185
186        /** @return true since it is an interior node */
187        bool IsLeaf() const;
188        ViewCell *GetViewCell();
189
190protected:
191
192        /// polygonal representation of this viewcell
193        /// if NULL this note does not correspond to feasible viewcell
194        ViewCell *mViewCell;
195};
196
197
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
212                PolygonContainer *mPolygons;
213                /// current depth
214                int mDepth;
215               
216                BspTraversalData() {}
217               
218                BspTraversalData(BspNode *node, BspInterior *parent, PolygonContainer *polys, const int depth):
219                mNode(node), mParent(parent), mPolygons(polys), mDepth(depth) {}
220    };
221
222        typedef std::stack<BspTraversalData> BspTraversalStack;
223
224        /// BSP tree construction type
225        enum {VIEWCELLS, SCENE_GEOMETRY};
226
227        /** Default constructor creating an empty tree.
228        */
229        BspTree();
230
231        ~BspTree();
232
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
247        int BspTree::CollectLeafPvs();
248
249        void CollectLeaves(vector<BspLeaf *> &leaves);
250
251protected:
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        */
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.
274                @returns new root of the subtree
275        */
276        BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL);
277
278        /** Selects a splitting plane.
279                @param polygons the polygon data on which the split decition is based
280        */
281        Plane3 SelectPlane(PolygonContainer *polygons) const;
282
283        /** Filters next viewcell down the tree and inserts it into the appropriate leaves
284                (i.e., possibly more than one leaf).
285        */
286        void InsertViewCell(ViewCell *viewCell);
287       
288        /** Subdivide leaf.
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,
297                                                        PolygonContainer *polys, const int depth,
298                                                        ViewCell *viewCell,
299                                                        PolygonContainer *frontPolys, PolygonContainer *backPolys);
300
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        */
307        void FilterPolygons(BspInterior *node, PolygonContainer *polys,
308                                                PolygonContainer *frontPolys, PolygonContainer *backPolys);
309
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
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
318        */
319        void Copy2PolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxObjects);
320
321        /** Add this mesh as polygons to polygon container.
322        */
323        void AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys);
324
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
331        /// Pointer to the root of the tree
332        BspNode *mRoot;
333
334        /// Pointer to the root cell of the viewspace
335        // ViewCell *mRootCell;
336               
337        BspTreeStatistics mStat;
338
339        /// local maximal polygons (changes depending on method)
340        int mTermMaxPolygons;
341        int mTermMaxDepth;
342
343        /// Strategies for choosing next split plane.
344        enum {NEXT_POLYGON, LEAST_SPLITS, BALANCED_TREE, COMBINED};
345
346        /// box around the whole view domain
347        AxisAlignedBox3 mBox;
348
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;
357        /// strategy to get the best split plane
358        static int sSplitPlaneStrategy;
359        /// number of candidates evaluated for the next split plane
360        static int sMaxCandidates;
361};
362
363//}; // GtpVisibilityPreprocessor
364
365#endif
Note: See TracBrowser for help on using the repository browser.