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

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