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

Revision 485, 13.9 KB checked in by mattausch, 19 years ago (diff)

changed castlinesegment of vspbpstree
changed rayinfo ray classification for bsp tree
implemented shuffling

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