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

Revision 580, 19.1 KB checked in by mattausch, 18 years ago (diff)

implemented variance
started implementing merge history

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