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

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