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

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