source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h @ 480

Revision 480, 12.4 KB checked in by mattausch, 19 years ago (diff)

axis aligned split for vsp bsp view cells

Line 
1#ifndef _ViewCellsManager_H__
2#define _ViewCellsManager_H__
3
4#include "Ray.h"
5#include "VssRay.h"
6#include "Containers.h"
7#include "ViewCell.h"
8
9class ViewCell;
10class Intersectable;
11class RenderSimulator;
12class Renderer;
13class Mesh;
14struct Triangle3;
15class SimulationStatistics;
16class BspTree;
17class KdTree;
18class VspKdTree;
19class VspBspTree;
20class KdNode;
21class KdLeaf;
22class VspKdTree;
23class AxisAlignedBox3;
24class BspLeaf;
25class ViewCellsStatistics;
26
27struct BspRay;
28
29/**
30        Manages different higher order operations on the view cells.
31*/
32class ViewCellsManager
33{
34
35public:
36  struct PvsStatistics {
37        int minPvs;
38        int maxPvs;
39        float avgPvs;
40        int viewcells;
41  };
42 
43  /// view cell container types
44  enum {BSP, KD, VSP_KD, VSP_BSP};
45 
46        /** Constructor taking the maximal number of samples used for construction
47        */
48        ViewCellsManager(const int constructionSamples);
49
50        ViewCellsManager();
51
52        virtual ~ViewCellsManager();
53
54        /** Constructs view cell container with a given number of samples.
55        */
56        virtual int Construct(const ObjectContainer &objects,
57                                                  const VssRayContainer &rays,
58                                                  AxisAlignedBox3 *sceneBbox = NULL) = 0;
59
60        /** Computes sample contributions of the rays to the view cells PVS.
61       
62                @param rays bundle of rays used to find intersections with view cells and
63                adding the contribution
64                @param castRays true if ray should be cast to gain the information, false if rays
65                already hold the view cells intersection information and need not be recast.
66                @param sampleContributions returns the number of sample contributions
67                @param contributingSamples returns the number of contributingSamples
68        */
69        void  ComputeSampleContributions(const VssRayContainer &rays);
70
71
72        /** Computes sample contribution of a simgle ray to the view cells PVS.
73                @param ray finds intersections with view cells and holds the contribution
74                @param castRay true if ray should be cast to gain the information, false if ray
75                is already holding the information and need not be recast.
76         
77                @returns number of sample contributions
78        */
79        virtual void ComputeSampleContributions(VssRay &ray);
80 
81        /** Prints out statistics of the view cells.
82        */
83        virtual void PrintStatistics(ostream &s) const;
84
85        /** Post processes view cells givemŽa number of rays.
86        */
87        virtual int PostProcess(const ObjectContainer &objects,
88                                                        const VssRayContainer &rays) = 0;
89
90        /** Show visualization of the view cells.
91        */
92        virtual void Visualize(const ObjectContainer &objects,
93                                                   const VssRayContainer &sampleRays) = 0;
94
95        /** type of the view cell container.
96        */
97        virtual int GetType() const = 0;
98
99        /** Load the input viewcells. The input viewcells should be given as a collection
100                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
101                the viewcell. The method then builds a BSP tree of these view cells.
102               
103                @param filename file to load
104                @return true on success
105    */
106    bool LoadViewCells(const string filename);
107
108        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
109                @param the base triangle
110                @param the height of the newly created view cell
111        */
112        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
113
114        /** Merges two view cells.
115                @note the piercing rays of the front and back will be ordered   
116                @returns new view cell based on the merging.
117        */
118        ViewCell *MergeViewCells(ViewCell &front, ViewCell &back) const;
119       
120        /** Generates view cell of type specified by this manager
121        */
122        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
123
124        /** Adds a new view cell to the list of predefined view cells.
125        */
126        void AddViewCell(ViewCell *viewCell);
127
128        /** Derive view cells from objects.
129        */
130        void DeriveViewCells(const ObjectContainer &objects,
131                                                 ViewCellContainer &viewCells,
132                                                 const int maxViewCells) const;
133
134        /** Sets maximal number of samples used for the
135                construction of the view cells.
136        */
137        void SetVisualizationSamples(const int visSamples);
138
139        /** Sets maximal number of samples used for the visualization of the view cells.
140        */
141        void SetConstructionSamples(const int constructionSamples);
142
143        /** Sets maximal number of samples used for the post processing of the view cells.
144        */
145        void SetPostProcessSamples(const int postProcessingSamples);
146
147        /** See set.
148        */
149        int GetVisualizationSamples() const;
150
151        /** See set.
152        */
153        int GetConstructionSamples() const;
154
155        /** See set.
156        */
157        int GetPostProcessSamples() const;
158
159        /** Returns true if view cells wer already constructed.
160        */
161        virtual bool ViewCellsConstructed() const = 0;
162
163        /** cast line segment to get a list of unique viewcells which are intersected
164                by this line segment
165        */
166 
167        virtual int CastLineSegment(const Vector3 &origin,
168                                                          const Vector3 &termination,
169                                                          ViewCellContainer &viewcells
170                                                          ) = 0;
171
172        virtual void GetPvsStatistics(PvsStatistics &stat);
173
174        virtual void PrintPvsStatistics(ostream &s);
175
176        /** Returns probability that view point lies in one view cell.
177        */
178        virtual float GetProbability(ViewCell *viewCell) = 0;
179
180        /** Returns render cost of a single view cell given the render cost of an object.
181        */
182        virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
183
184        /** Returns vector of loaded / generated view cells.
185        */
186        ViewCellContainer &GetViewCells();
187
188        /** Helper function used to split ray set into one used for view cell
189                construction and one cast after construction.
190        */
191        void GetRaySets(const VssRayContainer &sourceRays,
192                                        VssRayContainer &constructionRays,
193                                        VssRayContainer &savedRays) const;
194
195        /** Returns accumulated area of all view cells.
196        */
197        float GetAccVcArea();
198
199        /** Returns area of one view cell.
200        */
201        virtual float GetArea(ViewCell *viewCell) const;
202
203        /** Returns volume of view cells.
204        */
205        virtual float GetVolume(ViewCell *viewCell) const;
206
207        virtual AxisAlignedBox3 GetSceneBbox() const = 0;
208
209        /** Sets the current renderer mainly for view cells statistics.
210        */
211        void SetRenderer(Renderer *renderer);
212
213       
214protected:
215
216        /** Recollects view cells and resets statistics.
217        */
218        void ResetViewCells();
219        /** Collects the view cells in the view cell container.
220        */
221        virtual void CollectViewCells() = 0;
222
223        /** Evaluates view cells statistics and stores it in
224                mViewCellsStatistics.
225        */
226        void EvaluateViewCellsStats();
227               
228        /// the view cell corresponding to unbounded space
229        ViewCell *mUnbounded;
230
231        /// Renders the view cells.
232        Renderer *mRenderer;
233
234        /// Loaded view cells
235        ViewCellContainer mViewCells;
236
237        /// maximum number of samples taken for construction of the view cells
238        int mConstructionSamples;
239        int mPostProcessSamples;
240        int mVisualizationSamples;
241
242        //-- thresholds used for view cells merge
243        int mMinPvsDif;
244        int mMinPvs;
245        int mMaxPvs;
246
247        float mTotalAreaValid;
248        float mTotalArea;
249
250        ViewCellsStatistics mViewCellsStats;
251};
252
253
254
255/**
256        Manages different higher order operations on the view cells.
257*/
258class BspViewCellsManager: public ViewCellsManager
259{
260
261public:
262
263        BspViewCellsManager(BspTree *tree, int constructionSamples);
264
265        ~BspViewCellsManager();
266
267        int Construct(const ObjectContainer &objects,
268                                  const VssRayContainer &rays,
269                                  AxisAlignedBox3 *sceneBbox);
270
271
272        int PostProcess(const ObjectContainer &objects,
273                                        const VssRayContainer &rays);
274
275        void Visualize(const ObjectContainer &objects,
276                                   const VssRayContainer &sampleRays);
277
278        int GetType() const;
279       
280        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
281
282        bool ViewCellsConstructed() const;
283
284        //void PrintStatistics(ostream &s) const;
285
286        int CastLineSegment(const Vector3 &origin,
287                                                const Vector3 &termination,
288                                                ViewCellContainer &viewcells);
289       
290        float GetProbability(ViewCell *viewCell);
291        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
292
293        AxisAlignedBox3 GetSceneBbox() const;
294
295protected:
296
297        void CollectViewCells();
298
299        /** Merges view cells front and back leaf view cell.
300        */
301        bool MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const;
302
303        /** Returns true if front and back leaf should be merged.
304        */
305        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
306
307        void ConstructBspRays(const VssRayContainer &rays,
308                                                  const int numSamples);
309        /// the BSP tree.
310        BspTree *mBspTree;
311       
312        vector<BspRay *> mBspRays;
313
314private:
315
316        /** Exports visualization of the BSP splits.
317        */
318        void ExportSplits(const ObjectContainer &objects);
319
320        /** Exports visualization of the BSP PVS.
321        */
322        void ExportBspPvs(const ObjectContainer &objects);
323
324};
325
326/**
327        Manages different higher order operations on the KD type view cells.
328*/
329class KdViewCellsManager: public ViewCellsManager
330{
331
332public:
333
334        KdViewCellsManager(KdTree *tree);
335
336        int Construct(const ObjectContainer &objects,
337                                  const VssRayContainer &rays,
338                                  AxisAlignedBox3 *sceneBbox);
339
340        int CastLineSegment(const Vector3 &origin,
341                                                const Vector3 &termination,
342                                                ViewCellContainer &viewcells);
343
344        int PostProcess(const ObjectContainer &objects,
345                                        const VssRayContainer &rays);
346
347        void Visualize(const ObjectContainer &objects,
348                                   const VssRayContainer &sampleRays);
349
350        int GetType() const;
351
352        bool ViewCellsConstructed() const;
353
354
355        /** Prints out statistics of this approach.
356        */
357        //void PrintStatistics(ostream &s) const;
358
359        float GetProbability(ViewCell *viewCell);
360        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
361       
362        AxisAlignedBox3 GetSceneBbox() const;
363
364protected:
365
366        void CollectViewCells();
367        KdNode *GetNodeForPvs(KdLeaf *leaf);
368
369
370        /// the BSP tree.
371        KdTree *mKdTree;
372
373        /// depth of the KD tree nodes with represent the view cells
374        int mKdPvsDepth;
375};
376
377/**
378        Manages different higher order operations on the view cells
379        for vsp kd tree view cells.
380*/
381class VspKdViewCellsManager: public ViewCellsManager
382{
383
384public:
385
386        VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
387
388        int Construct(const ObjectContainer &objects,
389                                  const VssRayContainer &rays,
390                                  AxisAlignedBox3 *sceneBbox);
391
392
393        int PostProcess(const ObjectContainer &objects,
394                                        const VssRayContainer &rays);
395
396        void Visualize(const ObjectContainer &objects,
397                                   const VssRayContainer &sampleRays);
398
399        int GetType() const;
400       
401        bool ViewCellsConstructed() const;
402
403        //virtual void PrintStatistics(ostream &s) const;
404
405        ViewCell *GenerateViewCell(Mesh *mesh) const;
406
407
408    int CastLineSegment(const Vector3 &origin,
409                                                const Vector3 &termination,
410                                                ViewCellContainer &viewcells);
411
412        float GetProbability(ViewCell *viewCell);
413        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
414
415        AxisAlignedBox3 GetSceneBbox() const;
416
417protected:
418
419        void CollectViewCells();
420
421        /// the BSP tree.
422        VspKdTree *mVspKdTree;
423};
424
425
426
427/**
428        Manages different higher order operations on the view cells.
429*/
430class VspBspViewCellsManager: public ViewCellsManager
431{
432
433public:
434
435        VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
436        ~VspBspViewCellsManager();
437
438        int Construct(const ObjectContainer &objects,
439                                  const VssRayContainer &rays,
440                                  AxisAlignedBox3 *sceneBbox);
441
442
443        int PostProcess(const ObjectContainer &objects,
444                                        const VssRayContainer &rays);
445
446        void Visualize(const ObjectContainer &objects,
447                                   const VssRayContainer &sampleRays);
448
449        int GetType() const;
450       
451        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
452
453        bool ViewCellsConstructed() const;
454
455        //void PrintStatistics(ostream &s) const;
456       
457        int CastLineSegment(const Vector3 &origin,
458                                                const Vector3 &termination,
459                                                ViewCellContainer &viewcells);
460
461        float GetProbability(ViewCell *viewCell);
462        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
463       
464        AxisAlignedBox3 GetSceneBbox() const;
465
466protected:
467
468        /** Constructs bsp rays for post processing and visualization.
469        */
470        void ConstructBspRays(const VssRayContainer &rays, const int numSamples);
471
472        /** Merges view cells front and back leaf view cell.
473        */
474        bool MergeVspBspLeafViewCells(BspLeaf *front, BspLeaf *back) const;
475
476        /** Returns true if front and back leaf should be merged.
477        */
478        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
479
480        void CollectViewCells();
481
482        /// the view space partition BSP tree.
483        VspBspTree *mVspBspTree;
484
485        vector<BspRay *> mBspRays;
486
487private:
488
489
490        /** Exports visualization of the BSP splits.
491        */
492        void ExportSplits(const ObjectContainer &objects);
493
494        /** Exports visualization of the BSP PVS.
495        */
496        void ExportBspPvs(const ObjectContainer &objects);
497
498};
499
500#endif
Note: See TracBrowser for help on using the repository browser.