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

Revision 573, 17.0 KB checked in by mattausch, 18 years ago (diff)

implemented functtion for view cell construction

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