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

Revision 488, 14.1 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 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;
26class Exporter;
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) = 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 sets uniformly
188                into one that is currently used and the other that
189                is saved for later processing.
190                @param sourceRays the input ray set
191                @param maxSize the maximal number of rays that will be used
192                @param usedRays returns the used ray set
193                @param savedRays if not null, returns the saved ray set
194        */
195        void GetRaySets(const VssRayContainer &sourceRays,
196                                        const int maxSize,
197                                        VssRayContainer &usedRays,
198                                        VssRayContainer *savedRays = NULL) const;
199       
200        /** Returns accumulated area of all view cells.
201        */
202        float GetAccVcArea();
203
204        /** Returns area of one view cell.
205        */
206        virtual float GetArea(ViewCell *viewCell) const;
207
208        /** Returns volume of view cells.
209        */
210        virtual float GetVolume(ViewCell *viewCell) const;
211
212        virtual AxisAlignedBox3 GetSceneBbox() const = 0;
213
214        /** Sets the current renderer mainly for view cells statistics.
215        */
216        void SetRenderer(Renderer *renderer);
217
218        /** Computes a (random) view point in the valid view space.
219                @returns true if valid view point was found
220        */
221        virtual bool GetViewPoint(Vector3 &viewPoint) const;
222
223        /** Sets a view space boundary.
224        */
225        void SetViewSpaceBox(const AxisAlignedBox3 &box);
226
227
228protected:
229
230        void ParseEnvironment();
231
232        /** Recollects view cells and resets statistics.
233        */
234        void ResetViewCells();
235       
236        /** Collects the view cells in the view cell container.
237        */
238        virtual void CollectViewCells() = 0;
239       
240        /** Evaluates view cells statistics and stores it in
241                mViewCellsStatistics.
242        */
243        void EvaluateViewCellsStats();
244
245
246        //-- helper functions for view cell visualization
247
248        /** Exports the view cell partition.
249        */
250        void ExportViewCells(Exporter *exporter) const;
251
252        /** Sets exporter color.
253        */
254        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
255
256        /** Exports view cell geometry.
257        */
258        virtual void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const = 0;
259
260        /// the view cell corresponding to unbounded space
261        //ViewCell *mUnbounded;
262
263        /// Renders the view cells.
264        Renderer *mRenderer;
265
266        /// Loaded view cells
267        ViewCellContainer mViewCells;
268
269        /// maximum number of samples taken for construction of the view cells
270        int mConstructionSamples;
271        int mPostProcessSamples;
272        int mVisualizationSamples;
273
274        //-- thresholds used for view cells merge
275        int mMinPvsDif;
276        int mMinPvs;
277        int mMaxPvs;
278
279        float mTotalAreaValid;
280        float mTotalArea;
281
282        ViewCellsStatistics mViewCellsStats;
283        /// the scene bounding box
284        AxisAlignedBox3 mSceneBox;
285
286        //-- visualization options
287       
288        /// color code for view cells
289        int mColorCode;
290        bool mExportGeometry;
291        bool mExportRays;
292};
293
294
295
296/**
297        Manages different higher order operations on the view cells.
298*/
299class BspViewCellsManager: public ViewCellsManager
300{
301
302public:
303        /** Constructor taking the bsp tree and the number of samples
304                used to construct the bsp tree.
305        */
306        BspViewCellsManager(BspTree *tree, int constructionSamples);
307
308        ~BspViewCellsManager();
309
310        int Construct(const ObjectContainer &objects,
311                                  const VssRayContainer &rays);
312
313
314        int PostProcess(const ObjectContainer &objects,
315                                        const VssRayContainer &rays);
316
317        void Visualize(const ObjectContainer &objects,
318                                   const VssRayContainer &sampleRays);
319
320        int GetType() const;
321       
322        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
323
324        bool ViewCellsConstructed() const;
325
326        //void PrintStatistics(ostream &s) const;
327
328        int CastLineSegment(const Vector3 &origin,
329                                                const Vector3 &termination,
330                                                ViewCellContainer &viewcells);
331       
332        float GetProbability(ViewCell *viewCell);
333        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
334
335        AxisAlignedBox3 GetSceneBbox() const;
336
337protected:
338
339        void CollectViewCells();
340
341        /** Merges view cells front and back leaf view cell.
342        */
343        bool MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const;
344
345        /** Returns true if front and back leaf should be merged.
346        */
347        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
348
349        void ConstructBspRays(const VssRayContainer &rays,
350                                                  const int numSamples);
351
352        void ExportColor(Exporter *exporter, ViewCell *vc) const;
353        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
354
355        /// the BSP tree.
356        BspTree *mBspTree;
357       
358        vector<BspRay *> mBspRays;
359
360private:
361
362        /** Exports visualization of the BSP splits.
363        */
364        void ExportSplits(const ObjectContainer &objects);
365
366        /** Exports visualization of the BSP PVS.
367        */
368        void ExportBspPvs(const ObjectContainer &objects);
369
370};
371
372/**
373        Manages different higher order operations on the KD type view cells.
374*/
375class KdViewCellsManager: public ViewCellsManager
376{
377
378public:
379
380        KdViewCellsManager(KdTree *tree);
381
382        int Construct(const ObjectContainer &objects,
383                                  const VssRayContainer &rays);
384
385        int CastLineSegment(const Vector3 &origin,
386                                                const Vector3 &termination,
387                                                ViewCellContainer &viewcells);
388
389        int PostProcess(const ObjectContainer &objects,
390                                        const VssRayContainer &rays);
391
392        void Visualize(const ObjectContainer &objects,
393                                   const VssRayContainer &sampleRays);
394
395        int GetType() const;
396
397        bool ViewCellsConstructed() const;
398
399
400        /** Prints out statistics of this approach.
401        */
402        //void PrintStatistics(ostream &s) const;
403
404        float GetProbability(ViewCell *viewCell);
405        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
406       
407        AxisAlignedBox3 GetSceneBbox() const;
408
409protected:
410
411        void CollectViewCells();
412        KdNode *GetNodeForPvs(KdLeaf *leaf);
413
414        void ExportColor(Exporter *exporter, ViewCell *vc) const;
415        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
416
417        /// the BSP tree.
418        KdTree *mKdTree;
419
420        /// depth of the KD tree nodes with represent the view cells
421        int mKdPvsDepth;
422};
423
424/**
425        Manages different higher order operations on the view cells
426        for vsp kd tree view cells.
427*/
428class VspKdViewCellsManager: public ViewCellsManager
429{
430
431public:
432
433        VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
434
435        int Construct(const ObjectContainer &objects,
436                                  const VssRayContainer &rays);
437
438
439        int PostProcess(const ObjectContainer &objects,
440                                        const VssRayContainer &rays);
441
442        void Visualize(const ObjectContainer &objects,
443                                   const VssRayContainer &sampleRays);
444
445        int GetType() const;
446       
447        bool ViewCellsConstructed() const;
448
449        //virtual void PrintStatistics(ostream &s) const;
450
451        ViewCell *GenerateViewCell(Mesh *mesh) const;
452
453
454    int CastLineSegment(const Vector3 &origin,
455                                                const Vector3 &termination,
456                                                ViewCellContainer &viewcells);
457
458        float GetProbability(ViewCell *viewCell);
459        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
460
461        AxisAlignedBox3 GetSceneBbox() const;
462
463protected:
464
465        void ExportLeaves(const ObjectContainer &objects,
466                                          const VssRayContainer &sampleRays);
467
468        void CollectViewCells();
469
470        void ExportColor(Exporter *exporter, ViewCell *vc) const;
471        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
472
473
474        /// the BSP tree.
475        VspKdTree *mVspKdTree;
476};
477
478
479
480/**
481        Manages different higher order operations on the view cells.
482*/
483class VspBspViewCellsManager: public ViewCellsManager
484{
485
486public:
487
488        VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
489        ~VspBspViewCellsManager();
490
491        int Construct(const ObjectContainer &objects,
492                                  const VssRayContainer &rays);
493
494        int PostProcess(const ObjectContainer &objects,
495                                        const VssRayContainer &rays);
496
497        void Visualize(const ObjectContainer &objects,
498                                   const VssRayContainer &sampleRays);
499
500        int GetType() const;
501       
502        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
503
504        bool ViewCellsConstructed() const;
505
506        //void PrintStatistics(ostream &s) const;
507       
508        int CastLineSegment(const Vector3 &origin,
509                                                const Vector3 &termination,
510                                                ViewCellContainer &viewcells);
511
512        float GetProbability(ViewCell *viewCell);
513        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
514       
515        AxisAlignedBox3 GetSceneBbox() const;
516
517        bool GetViewPoint(Vector3 &viewPoint) const;
518
519protected:
520        /** DEPRECATED
521        */
522        int MergeViewCells(const VssRayContainer &rays) const;
523
524        /** Returns true if front and back leaf should be merged.
525        */
526        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
527
528        void CollectViewCells();
529
530        /** Returns maximal depth difference of view cell
531                leaves in tree.
532        */
533        int GetMaxTreeDiff(ViewCell *vc) const;
534       
535
536        void ExportColor(Exporter *exporter, ViewCell *vc) const;
537        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
538
539
540        /// the view space partition BSP tree.
541        VspBspTree *mVspBspTree;
542
543private:
544
545        /** Exports visualization of the BSP splits.
546        */
547        void ExportSplits(const ObjectContainer &objects,
548                                          const VssRayContainer &rays);
549
550        /** Exports visualization of the BSP PVS.
551        */
552        void ExportBspPvs(const ObjectContainer &objects,
553                                          const VssRayContainer &rays);
554
555};
556
557#endif
Note: See TracBrowser for help on using the repository browser.