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

Revision 577, 17.9 KB checked in by mattausch, 18 years ago (diff)

fixed loading function: the view cell manager is chosen depending on
the type in the file. the view space box is saved with the file

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