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

Revision 490, 14.3 KB checked in by mattausch, 19 years ago (diff)

added loading and storing rays capability

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