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

Revision 599, 20.2 KB checked in by bittner, 18 years ago (diff)

slider visualization

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