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

Revision 503, 14.9 KB checked in by mattausch, 19 years ago (diff)

added mesh creation function

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