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

Revision 579, 18.1 KB checked in by mattausch, 18 years ago (diff)

started to include variance into the measures

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;
27class Beam;
28class Preprocessor;
29
30struct BspRay;
31
32/**
33        Manages different higher order operations on the view cells.
34*/
35class ViewCellsManager
36{
37
38public:
39  struct PvsStatistics {
40        int minPvs;
41        int maxPvs;
42        float avgPvs;
43        int viewcells;
44  };
45 
46  /// view cell container types
47  enum {BSP, KD, VSP_KD, VSP_BSP};
48 
49        /** Constructor taking the initial and construction samples.
50                @param initialSamples the maximal number of samples used for creating the hierarchy
51                of view cells
52                @param constructionSamples the maximal number of samples used for construction
53        */
54        ViewCellsManager(const int initialSamples, const int constructionSamples);
55
56        ViewCellsManager();
57
58        virtual ~ViewCellsManager();
59
60        /** Constructs view cells container taking a preprocessor
61                @returns the output rays (if not null)
62        */
63        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
64
65        /** Constructs view cell container with a given number of samples.
66        */
67        virtual int ConstructSubdivision(const ObjectContainer &objects,
68                                                  const VssRayContainer &rays) = 0;
69
70        /** Computes sample contributions of the rays to the view cells PVS.
71       
72                @param rays bundle of rays used to find intersections with view cells and
73                adding the contribution
74                @param addRays true if rays should be added to the PVSs of the viewcells they
75                intersect
76                @param storeViewCells true if view cells should be stored in the ray
77        */
78  float ComputeSampleContributions(const VssRayContainer &rays,
79                                                                   const bool addContributions,
80                                                                   const bool storeViewCells);
81
82  /** Add sample contributions to the viewcells they intersect */
83  void AddSampleContributions(const VssRayContainer &rays);
84 
85
86        /** Computes sample contribution of a simgle ray to the view cells PVS.
87                @param ray finds intersections with view cells and holds the contribution
88                @param castRay true if ray should be cast to gain the information, false if ray
89                is already holding the information and need not be recast.
90         
91                @returns number of sample contributions
92        */
93        virtual float ComputeSampleContributions(VssRay &ray, const bool addRays, const bool storeViewCells);
94
95  virtual void AddSampleContributions(VssRay &ray);
96
97        /** Prints out statistics of the view cells.
98        */
99        virtual void PrintStatistics(ostream &s) const;
100
101        /** Post processes view cells givemŽa number of rays.
102        */
103        virtual int PostProcess(const ObjectContainer &objects,
104                                                        const VssRayContainer &rays) = 0;
105
106        /** Show visualization of the view cells.
107        */
108        virtual void Visualize(const ObjectContainer &objects,
109                                                   const VssRayContainer &sampleRays) = 0;
110
111        /** type of the view cell container.
112        */
113        virtual int GetType() const = 0;
114
115        /** Load the input viewcells. The input viewcells should be given as a collection
116                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
117                the viewcell. The method then builds a BSP tree of these view cells.
118               
119                @param filename file to load
120                @return true on success
121    */
122    virtual bool LoadViewCellsGeometry(const string filename);
123
124       
125        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
126                @param the base triangle
127                @param the height of the newly created view cell
128        */
129        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
130
131        /** Merges two view cells.
132                @note the piercing rays of the front and back will be ordered   
133                @returns new view cell based on the merging.
134        */
135        ViewCell *MergeViewCells(ViewCell &front, ViewCell &back) const;
136       
137        /** Generates view cell of type specified by this manager
138        */
139        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
140
141        /** Adds a new view cell to the list of predefined view cells.
142        */
143        void AddViewCell(ViewCell *viewCell);
144
145        /** Derive view cells from objects.
146        */
147        void DeriveViewCells(const ObjectContainer &objects,
148                                                 ViewCellContainer &viewCells,
149                                                 const int maxViewCells) const;
150
151        /** Sets maximal number of samples used for the
152                construction of the view cells.
153        */
154        void SetVisualizationSamples(const int visSamples);
155
156        /** Sets maximal number of samples used for the construction of the view cells.
157        */
158        void SetConstructionSamples(const int constructionSamples);
159
160        /** Sets maximal number of samples used for the visualization of the view cells.
161        */
162        void SetInitialSamples(const int initialSamples);
163
164        /** Sets maximal number of samples used for the post processing of the view cells.
165        */
166        void SetPostProcessSamples(const int postProcessingSamples);
167
168        /** See set.
169        */
170        int GetVisualizationSamples() const;
171
172        /** See set.
173        */
174        int GetConstructionSamples() const;
175
176        /** See set.
177        */
178        int GetPostProcessSamples() const;
179
180        /** Returns true if view cells wer already constructed.
181        */
182        virtual bool ViewCellsConstructed() const = 0;
183
184        /** cast line segment to get a list of unique viewcells which are intersected
185                by this line segment
186        */
187 
188        virtual int CastLineSegment(const Vector3 &origin,
189                                                          const Vector3 &termination,
190                                                          ViewCellContainer &viewcells
191                                                          ) = 0;
192
193        virtual void GetPvsStatistics(PvsStatistics &stat);
194
195        /** Get a viewcell containing the specified point */
196        virtual ViewCell *GetViewCell(const Vector3 &point) const = 0;
197 
198        virtual void PrintPvsStatistics(ostream &s);
199
200        /** Returns probability that view point lies in one view cell.
201        */
202        virtual float GetProbability(ViewCell *viewCell) = 0;
203
204        /** Returns render cost of a single view cell given the render cost of an object.
205        */
206        virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
207
208        /** Returns vector of loaded / generated view cells.
209        */
210        ViewCellContainer &GetViewCells();
211
212        /** Helper function used to split ray sets uniformly
213                into one that is currently used and the other that
214                is saved for later processing.
215                @param sourceRays the input ray set
216                @param maxSize the maximal number of rays that will be used
217                @param usedRays returns the used ray set
218                @param savedRays if not null, returns the saved ray set
219        */
220        void GetRaySets(const VssRayContainer &sourceRays,
221                                        const int maxSize,
222                                        VssRayContainer &usedRays,
223                                        VssRayContainer *savedRays = NULL) const;
224       
225        /** Returns accumulated area of all view cells.
226        */
227        float GetAccVcArea();
228
229        /** Returns area of one view cell.
230        */
231        virtual float GetArea(ViewCell *viewCell) const;
232
233        /** Returns volume of view cell.
234        */
235        virtual float GetVolume(ViewCell *viewCell) const;
236
237        /** Sets the current renderer mainly for view cells statistics.
238        */
239        void SetRenderer(Renderer *renderer);
240
241        /** Computes a (random) view point in the valid view space.
242                @returns true if valid view point was found
243        */
244        virtual bool GetViewPoint(Vector3 &viewPoint) const;
245
246        /** Returns true if this view point is in the valid view space.
247        */
248        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
249
250        /** Sets a view space boundary.
251        */
252        void SetViewSpaceBox(const AxisAlignedBox3 &box);
253
254        AxisAlignedBox3 GetViewSpaceBox() const;
255
256        /** Creates mesh for this view cell.
257        */
258        virtual void CreateMesh(ViewCell *vc) = NULL;
259
260        /** Writes view cells to disc.
261        */
262        virtual bool ExportViewCells(const string filename);
263
264        /** Casts beam to collect view cells.
265        */
266        virtual int CastBeam(Beam &beam);
267
268        /** Checks if view cell is considered as valid.
269        */
270        virtual bool CheckValidity(ViewCell *vc,
271                                                           int minPvsSize,
272                                                           int maxPvsSize) const;
273
274       
275        /** Sets validity of view cell
276        */
277        virtual void SetValidity(ViewCell *vc,
278                                                         int minPvsSize,
279                                                         int maxPvsSize) const;
280
281  /** sets validy of all viewcells */
282        virtual void SetValidity(
283                                                         int minPvsSize,
284                                                         int maxPvsSize) const;
285
286  /** set valid viewcells in the range of pvs. sorts the viewcells
287          according to the pvs and then pickups those in the ranges */
288 
289  void
290  SetValidityPercentage(
291                                                const float minValid,
292                                                const float maxValid
293                                                );
294
295  int
296  CountValidViewcells() const;
297
298        /** Returns maximal allowed pvs size.
299        */
300        int GetMaxPvsSize() const;
301
302        /** Returns maximal allowed pvs size.
303        */
304        int GetMinPvsSize() const;
305
306        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
307                where pvs is still considered valid.
308        */
309        float GetMaxPvsRatio() const;
310
311        /** Exports view cell geometry.
312        */
313        virtual void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const = 0;
314
315        virtual void FinalizeViewCells(const bool createMesh);
316
317
318        /** Loads view cells from file. The view cells manager is created with respect to the loaded
319                view cells.
320
321                @returns the view cells manager if loading was successful, false otherwise
322        */
323        static ViewCellsManager *LoadViewCells(const string filename, ObjectContainer *objects);
324
325        /** Evaluates statistics values on view cells.
326        */
327        void EvaluateRenderStatistics(float &totalRenderCost,
328                                                                  float &expectedRenderCost,
329                                                                  float &variance);
330
331protected:
332
333        int CastPassSamples(const int samplesPerPass,
334                                                const int sampleType,
335                                                VssRayContainer &vssRays) const;
336
337        void ParseEnvironment();
338
339        /** Creates unique view cell ids.
340        */
341        void CreateUniqueViewCellIds();
342
343        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
344        */
345        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
346
347        /** Recollects view cells and resets statistics.
348        */
349        void ResetViewCells();
350       
351        /** Collects the view cells in the view cell container.
352        */
353        virtual void CollectViewCells() = 0;
354       
355        /** Evaluates view cells statistics and stores it in
356                mViewCellsStatistics.
357        */
358        void EvaluateViewCellsStats();
359
360        //-- helper functions for view cell visualization
361
362        /** Exports the view cell partition.
363        */
364        void ExportViewCellsForViz(Exporter *exporter) const;
365
366        /** Sets exporter color.
367        */
368        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
369
370
371        virtual float GetViewSpaceVolume();
372       
373        /** Creates meshes from the view cells.
374        */
375        void CreateViewCellMeshes();
376
377        /**
378                Exports single view cell.
379                NOTE: should be in exporter!!
380        */
381        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
382
383
384        /**
385                Takes different measures to prepares the view cells after loading them from disc.
386        */
387        virtual void PrepareLoadedViewCells() {};
388
389        /// Renders the view cells.
390        Renderer *mRenderer;
391
392        /// Loaded view cells
393        ViewCellContainer mViewCells;
394
395        /// maximum number of samples taken for construction of the view cells
396        int mConstructionSamples;
397        int mSamplesPerPass;
398        int mInitialSamples;
399        int mPostProcessSamples;
400        int mVisualizationSamples;
401
402        float mTotalAreaValid;
403        float mTotalArea;
404
405        int mMaxPvsSize;
406        int mMinPvsSize;
407        float mMaxPvsRatio;
408
409        ViewCellsStatistics mViewCellsStats;
410        /// the scene bounding box
411        AxisAlignedBox3 mViewSpaceBox;
412        /// holds the view cell meshes
413        MeshContainer mMeshContainer;
414        /// if view cells should be exported
415        bool mExportViewCells;
416
417        bool mOnlyValidViewCells;
418
419        //-- visualization options
420       
421        /// color code for view cells
422        int mColorCode;
423        bool mExportGeometry;
424        bool mExportRays;
425
426        bool mViewCellsFinished;
427};
428
429
430
431/**
432        Manages different higher order operations on the view cells.
433*/
434class BspViewCellsManager: public ViewCellsManager
435{
436
437public:
438        /** Constructor taking the bsp tree and the number of samples
439                used to construct the bsp tree.
440        */
441        BspViewCellsManager(BspTree *tree);
442
443        ~BspViewCellsManager();
444
445        int ConstructSubdivision(const ObjectContainer &objects,
446                                  const VssRayContainer &rays);
447
448        int PostProcess(const ObjectContainer &objects,
449                                        const VssRayContainer &rays);
450
451        void Visualize(const ObjectContainer &objects,
452                                   const VssRayContainer &sampleRays);
453
454        int GetType() const;
455       
456        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
457
458        bool ViewCellsConstructed() const;
459
460        //void PrintStatistics(ostream &s) const;
461
462        int CastLineSegment(const Vector3 &origin,
463                                                const Vector3 &termination,
464                                                ViewCellContainer &viewcells);
465       
466        float GetProbability(ViewCell *viewCell);
467        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
468
469        /** Get a viewcell containing the specified point */
470        ViewCell *GetViewCell(const Vector3 &point) const;
471
472        void CreateMesh(ViewCell *vc);
473
474        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
475
476protected:
477
478        void CollectViewCells();
479
480        void ConstructBspRays(const VssRayContainer &rays,
481                                                  const int numSamples);
482
483        void ExportColor(Exporter *exporter, ViewCell *vc) const;
484       
485
486
487        /// the BSP tree.
488        BspTree *mBspTree;
489       
490        vector<BspRay *> mBspRays;
491
492private:
493
494        /** Exports visualization of the BSP splits.
495        */
496        void ExportSplits(const ObjectContainer &objects);
497
498        /** Exports visualization of the BSP PVS.
499        */
500        void ExportBspPvs(const ObjectContainer &objects);
501
502};
503
504/**
505        Manages different higher order operations on the KD type view cells.
506*/
507class KdViewCellsManager: public ViewCellsManager
508{
509
510public:
511
512        KdViewCellsManager(KdTree *tree);
513
514        int ConstructSubdivision(const ObjectContainer &objects,
515                                  const VssRayContainer &rays);
516
517        int CastLineSegment(const Vector3 &origin,
518                                                const Vector3 &termination,
519                                                ViewCellContainer &viewcells);
520
521        int PostProcess(const ObjectContainer &objects,
522                                        const VssRayContainer &rays);
523
524        void Visualize(const ObjectContainer &objects,
525                                   const VssRayContainer &sampleRays);
526
527        int GetType() const;
528
529        bool ViewCellsConstructed() const;
530
531
532        /** Prints out statistics of this approach.
533        */
534        //  virtual void PrintStatistics(ostream &s) const;
535        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
536
537        float GetProbability(ViewCell *viewCell);
538        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
539
540        void CreateMesh(ViewCell *vc);
541
542        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
543
544protected:
545
546        void CollectViewCells();
547        KdNode *GetNodeForPvs(KdLeaf *leaf);
548
549        void ExportColor(Exporter *exporter, ViewCell *vc) const;
550
551
552        /// the BSP tree.
553        KdTree *mKdTree;
554
555        /// depth of the KD tree nodes with represent the view cells
556        int mKdPvsDepth;
557};
558
559/**
560        Manages different higher order operations on the view cells
561        for vsp kd tree view cells.
562*/
563class VspKdViewCellsManager: public ViewCellsManager
564{
565
566public:
567
568        VspKdViewCellsManager(VspKdTree *vspKdTree);
569
570        int ConstructSubdivision(const ObjectContainer &objects,
571                                  const VssRayContainer &rays);
572
573
574        int PostProcess(const ObjectContainer &objects,
575                                        const VssRayContainer &rays);
576
577        void Visualize(const ObjectContainer &objects,
578                                   const VssRayContainer &sampleRays);
579
580        int GetType() const;
581       
582        bool ViewCellsConstructed() const;
583
584        //virtual void PrintStatistics(ostream &s) const;
585
586        ViewCell *GenerateViewCell(Mesh *mesh) const;
587
588
589    int CastLineSegment(const Vector3 &origin,
590                                                const Vector3 &termination,
591                                                ViewCellContainer &viewcells);
592
593        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
594
595        float GetProbability(ViewCell *viewCell);
596        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
597
598        void CreateMesh(ViewCell *vc);
599
600        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
601
602protected:
603
604        void ExportLeaves(const ObjectContainer &objects,
605                                          const VssRayContainer &sampleRays);
606
607        void CollectViewCells();
608
609        void ExportColor(Exporter *exporter, ViewCell *vc) const;
610
611
612
613        /// the BSP tree.
614        VspKdTree *mVspKdTree;
615};
616
617
618
619/**
620        Manages different higher order operations on the view cells.
621*/
622class VspBspViewCellsManager: public ViewCellsManager
623{
624
625public:
626
627        VspBspViewCellsManager(VspBspTree *tree);
628        ~VspBspViewCellsManager();
629
630        int ConstructSubdivision(const ObjectContainer &objects,
631                                  const VssRayContainer &rays);
632
633        int PostProcess(const ObjectContainer &objects,
634                                        const VssRayContainer &rays);
635
636        void Visualize(const ObjectContainer &objects,
637                                   const VssRayContainer &sampleRays);
638
639        int GetType() const;
640       
641        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
642
643        bool ViewCellsConstructed() const;
644
645       
646        int CastLineSegment(const Vector3 &origin,
647                                                const Vector3 &termination,
648                                                ViewCellContainer &viewcells);
649
650        float GetProbability(ViewCell *viewCell);
651        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
652       
653        ViewCell *GetViewCell(const Vector3 &point) const;
654
655        bool GetViewPoint(Vector3 &viewPoint) const;
656
657        bool ViewPointValid(const Vector3 &viewPoint) const;
658
659        void CreateMesh(ViewCell *vc);
660
661        //bool LoadViewCellsGeometry(const string filename, ObjectContainer *objects);
662        bool ExportViewCells(const string filename);
663
664        int CastBeam(Beam &beam);
665
666        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
667
668        //float GetVolume(ViewCell *viewCell) const;
669
670        void Finalize(ViewCell *viewCell, const bool createMesh);
671
672protected:
673
674        /** Merges the view cells.
675        */
676        void MergeViewCells(const VssRayContainer &rays,
677                                                const ObjectContainer &objects);
678       
679        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
680
681        void CollectViewCells();
682
683        /** Returns maximal depth difference of view cell
684                leaves in tree.
685        */
686        int GetMaxTreeDiff(ViewCell *vc) const;
687       
688
689        void ExportColor(Exporter *exporter, ViewCell *vc) const;
690
691        void PrepareLoadedViewCells();
692
693        /// the view space partition BSP tree.
694        VspBspTree *mVspBspTree;
695
696private:
697
698        /** Exports visualization of the BSP splits.
699        */
700        void ExportSplits(const ObjectContainer &objects,
701                                          const VssRayContainer &rays);
702
703        /** Exports visualization of the BSP PVS.
704        */
705        void ExportBspPvs(const ObjectContainer &objects,
706                                          const VssRayContainer &rays);
707
708};
709
710
711class ViewCellsManagerFactory
712{
713
714public:
715
716        ViewCellsManager *Create(const string mName);
717
718};
719
720#endif
Note: See TracBrowser for help on using the repository browser.