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

Revision 482, 13.6 KB checked in by mattausch, 19 years ago (diff)

added visualizations

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