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

Revision 581, 19.2 KB checked in by mattausch, 18 years ago (diff)

added function for pvs compression

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