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

Revision 487, 14.2 KB checked in by mattausch, 19 years ago (diff)

fixed bug in raycasting
added valid view point regions, get view point only from valid regions

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        //-- visualization options
283       
284        /// color code for view cells
285        int mColorCode;
286        bool mExportGeometry;
287        bool mExportRays;
288
289        ViewCellsStatistics mViewCellsStats;
290        /// the scene bounding box
291        AxisAlignedBox3 mSceneBox;
292
293        //-- visualization options
294       
295        /// color code for view cells
296        int mColorCode;
297        bool mExportGeometry;
298        bool mExportRays;
299};
300
301
302
303/**
304        Manages different higher order operations on the view cells.
305*/
306class BspViewCellsManager: public ViewCellsManager
307{
308
309public:
310        /** Constructor taking the bsp tree and the number of samples
311                used to construct the bsp tree.
312        */
313        BspViewCellsManager(BspTree *tree, int constructionSamples);
314
315        ~BspViewCellsManager();
316
317        int Construct(const ObjectContainer &objects,
318                                  const VssRayContainer &rays);
319
320
321        int PostProcess(const ObjectContainer &objects,
322                                        const VssRayContainer &rays);
323
324        void Visualize(const ObjectContainer &objects,
325                                   const VssRayContainer &sampleRays);
326
327        int GetType() const;
328       
329        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
330
331        bool ViewCellsConstructed() const;
332
333        //void PrintStatistics(ostream &s) const;
334
335        int CastLineSegment(const Vector3 &origin,
336                                                const Vector3 &termination,
337                                                ViewCellContainer &viewcells);
338       
339        float GetProbability(ViewCell *viewCell);
340        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
341
342        AxisAlignedBox3 GetSceneBbox() const;
343
344protected:
345
346        void CollectViewCells();
347
348        /** Merges view cells front and back leaf view cell.
349        */
350        bool MergeBspLeafViewCells(BspLeaf *front, BspLeaf *back) const;
351
352        /** Returns true if front and back leaf should be merged.
353        */
354        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
355
356        void ConstructBspRays(const VssRayContainer &rays,
357                                                  const int numSamples);
358
359        void ExportColor(Exporter *exporter, ViewCell *vc) const;
360        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
361
362        /// the BSP tree.
363        BspTree *mBspTree;
364       
365        vector<BspRay *> mBspRays;
366
367private:
368
369        /** Exports visualization of the BSP splits.
370        */
371        void ExportSplits(const ObjectContainer &objects);
372
373        /** Exports visualization of the BSP PVS.
374        */
375        void ExportBspPvs(const ObjectContainer &objects);
376
377};
378
379/**
380        Manages different higher order operations on the KD type view cells.
381*/
382class KdViewCellsManager: public ViewCellsManager
383{
384
385public:
386
387        KdViewCellsManager(KdTree *tree);
388
389        int Construct(const ObjectContainer &objects,
390                                  const VssRayContainer &rays);
391
392        int CastLineSegment(const Vector3 &origin,
393                                                const Vector3 &termination,
394                                                ViewCellContainer &viewcells);
395
396        int PostProcess(const ObjectContainer &objects,
397                                        const VssRayContainer &rays);
398
399        void Visualize(const ObjectContainer &objects,
400                                   const VssRayContainer &sampleRays);
401
402        int GetType() const;
403
404        bool ViewCellsConstructed() const;
405
406
407        /** Prints out statistics of this approach.
408        */
409        //void PrintStatistics(ostream &s) const;
410
411        float GetProbability(ViewCell *viewCell);
412        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
413       
414        AxisAlignedBox3 GetSceneBbox() const;
415
416protected:
417
418        void CollectViewCells();
419        KdNode *GetNodeForPvs(KdLeaf *leaf);
420
421        void ExportColor(Exporter *exporter, ViewCell *vc) const;
422        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
423
424        /// the BSP tree.
425        KdTree *mKdTree;
426
427        /// depth of the KD tree nodes with represent the view cells
428        int mKdPvsDepth;
429};
430
431/**
432        Manages different higher order operations on the view cells
433        for vsp kd tree view cells.
434*/
435class VspKdViewCellsManager: public ViewCellsManager
436{
437
438public:
439
440        VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
441
442        int Construct(const ObjectContainer &objects,
443                                  const VssRayContainer &rays);
444
445
446        int PostProcess(const ObjectContainer &objects,
447                                        const VssRayContainer &rays);
448
449        void Visualize(const ObjectContainer &objects,
450                                   const VssRayContainer &sampleRays);
451
452        int GetType() const;
453       
454        bool ViewCellsConstructed() const;
455
456        //virtual void PrintStatistics(ostream &s) const;
457
458        ViewCell *GenerateViewCell(Mesh *mesh) const;
459
460
461    int CastLineSegment(const Vector3 &origin,
462                                                const Vector3 &termination,
463                                                ViewCellContainer &viewcells);
464
465        float GetProbability(ViewCell *viewCell);
466        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
467
468        AxisAlignedBox3 GetSceneBbox() const;
469
470protected:
471
472        void ExportLeaves(const ObjectContainer &objects,
473                                          const VssRayContainer &sampleRays);
474
475        void CollectViewCells();
476
477        void ExportColor(Exporter *exporter, ViewCell *vc) const;
478        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
479
480
481        /// the BSP tree.
482        VspKdTree *mVspKdTree;
483};
484
485
486
487/**
488        Manages different higher order operations on the view cells.
489*/
490class VspBspViewCellsManager: public ViewCellsManager
491{
492
493public:
494
495        VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
496        ~VspBspViewCellsManager();
497
498        int Construct(const ObjectContainer &objects,
499                                  const VssRayContainer &rays);
500
501        int PostProcess(const ObjectContainer &objects,
502                                        const VssRayContainer &rays);
503
504        void Visualize(const ObjectContainer &objects,
505                                   const VssRayContainer &sampleRays);
506
507        int GetType() const;
508       
509        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
510
511        bool ViewCellsConstructed() const;
512
513        //void PrintStatistics(ostream &s) const;
514       
515        int CastLineSegment(const Vector3 &origin,
516                                                const Vector3 &termination,
517                                                ViewCellContainer &viewcells);
518
519        float GetProbability(ViewCell *viewCell);
520        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
521       
522        AxisAlignedBox3 GetSceneBbox() const;
523
524        bool GetViewPoint(Vector3 &viewPoint) const;
525
526protected:
527        /** DEPRECATED
528        */
529        int MergeViewCells(const VssRayContainer &rays) const;
530
531        /** Returns true if front and back leaf should be merged.
532        */
533        bool ShouldMerge(BspLeaf *front, BspLeaf *back) const;
534
535        void CollectViewCells();
536
537        /** Returns maximal depth difference of view cell
538                leaves in tree.
539        */
540        int GetMaxTreeDiff(ViewCell *vc) const;
541       
542
543        void ExportColor(Exporter *exporter, ViewCell *vc) const;
544        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
545
546
547        /// the view space partition BSP tree.
548        VspBspTree *mVspBspTree;
549
550private:
551
552        /** Exports visualization of the BSP splits.
553        */
554        void ExportSplits(const ObjectContainer &objects,
555                                          const VssRayContainer &rays);
556
557        /** Exports visualization of the BSP PVS.
558        */
559        void ExportBspPvs(const ObjectContainer &objects,
560                                          const VssRayContainer &rays);
561
562};
563
564#endif
Note: See TracBrowser for help on using the repository browser.