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

Revision 492, 14.7 KB checked in by bittner, 19 years ago (diff)

Large merge - viewcells seem not functional now

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