source: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h @ 693

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