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

Revision 1155, 28.1 KB checked in by mattausch, 18 years ago (diff)

added support for kd pvs sampling

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
10namespace GtpVisibilityPreprocessor {
11
12class ViewCell;
13class Intersectable;
14class RenderSimulator;
15class Renderer;
16class Mesh;
17struct Triangle3;
18class SimulationStatistics;
19class BspTree;
20class KdTree;
21class VspOspTree;
22class VspBspTree;
23class KdNode;
24class KdLeaf;
25class AxisAlignedBox3;
26class BspLeaf;
27class ViewCellsStatistics;
28class Exporter;
29class Beam;
30class Preprocessor;
31class ViewCellsTree;
32class MergeCandidate;
33class BoundingBoxConverter;
34class VspTree;
35class OspTree;
36class VspNode;
37class HierarchyManager;
38struct AxisAlignedPlane;
39
40struct BspRay;
41
42
43/** Probably Visible Set
44*/
45class PrVs
46{
47public:
48  /// root of view cells tree
49  ViewCell *mViewCell;
50
51  // input parameter is the render budget for the PrVs
52  float mRenderBudget;
53
54  /// some characteristic values could be here
55 
56};
57
58
59/**     Manages different higher order operations on the view cells.
60*/
61class ViewCellsManager
62{
63public:
64        struct PvsStatistics
65        {
66                int minPvs;
67                int maxPvs;
68                float avgPvs;
69                int viewcells;
70  };
71
72        /// view cell container types
73        enum {BSP, KD, VSP_KD, VSP_BSP, VSP_OSP};
74 
75        /// render cost evaluation type
76        enum {PER_OBJECT, PER_TRIANGLE};
77
78        /** Default constructor.
79        */
80        ViewCellsManager();
81
82        virtual ~ViewCellsManager();
83
84        /** Constructs view cells container taking a preprocessor
85                @returns the output rays (if not null)
86        */
87        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
88
89        /** Constructs view cell container with a given number of samples.
90        */
91        virtual int ConstructSubdivision(const ObjectContainer &objects,
92                                                  const VssRayContainer &rays) = 0;
93
94        /** Computes sample contributions of the rays to the view cells PVS.
95       
96                @param rays bundle of rays used to find intersections with view cells and
97                adding the contribution
98                @param addRays true if rays should be added to the PVSs of the viewcells they
99                intersect
100                @param storeViewCells true if view cells should be stored in the ray
101        */
102        float ComputeSampleContributions(const VssRayContainer &rays,
103                                                                         const bool addContributions,
104                                                                         const bool storeViewCells);
105
106        /** Add sample contributions to the viewcells they intersect
107        */
108        void AddSampleContributions(const VssRayContainer &rays);
109 
110
111        /** Computes sample contribution of a simgle ray to the view cells PVS.
112                @param ray finds intersections with view cells and holds the contribution
113                @param castRay true if ray should be cast to gain the information, false if ray
114                is already holding the information and need not be recast.
115         
116                @returns number of sample contributions
117        */
118        virtual float ComputeSampleContributions(VssRay &ray,
119                                                                                         const bool addRays,
120                                                                                         const bool storeViewCells);
121
122        virtual void AddSampleContributions(VssRay &ray);
123
124        /** Prints out statistics of the view cells.
125        */
126        virtual void PrintStatistics(ostream &s) const;
127
128        /** Post processes view cells givemŽa number of rays.
129        */
130        virtual int PostProcess(const ObjectContainer &objects,
131                                                        const VssRayContainer &rays) = 0;
132
133        /** Show visualization of the view cells.
134        */
135        virtual void Visualize(const ObjectContainer &objects,
136                                                   const VssRayContainer &sampleRays) = 0;
137
138        /** type of the view cell container.
139        */
140        virtual int GetType() const = 0;
141
142        /** Load the input viewcells. The input viewcells should be given as a collection
143                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
144                the viewcell. The method then builds a BSP tree of these view cells.
145               
146                @param filename file to load
147                @return true on success
148    */
149    virtual bool LoadViewCellsGeometry(const string filename);
150
151       
152        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
153                @param the base triangle
154                @param the height of the newly created view cell
155        */
156        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
157
158        /** Merges two view cells.
159                @note the piercing rays of the front and back will be ordered   
160                @returns new view cell based on the merging.
161        */
162        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
163
164        /** Merges a container of view cells.
165                @returns new view cell based on the merging.
166        */
167        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
168       
169        /** Generates view cell of type specified by this manager
170        */
171        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
172
173        /** Adds a new view cell to the list of predefined view cells.
174        */
175        void AddViewCell(ViewCell *viewCell);
176
177        /** Derive view cells from scene objects. The view ells are created by taking the object
178                geometry into account.
179        */
180        void DeriveViewCellsFromObjects(const ObjectContainer &objects,
181                                                                        ViewCellContainer &viewCells,
182                                                                        const int maxViewCells) const;
183
184        /** Sets maximal number of samples used for the
185                construction of the view cells.
186        */
187        void SetVisualizationSamples(const int visSamples);
188
189        /** Sets maximal number of samples used for the construction of the view cells.
190        */
191        void SetConstructionSamples(const int constructionSamples);
192
193        /** Sets maximal number of samples used for the visualization of the view cells.
194        */
195        void SetInitialSamples(const int initialSamples);
196
197        /** Sets maximal number of samples used for the post processing of the view cells.
198        */
199        void SetPostProcessSamples(const int postProcessingSamples);
200
201        /** See set.
202        */
203        int GetVisualizationSamples() const;
204
205        /** See set.
206        */
207        int GetConstructionSamples() const;
208
209        /** See set.
210        */
211        int GetPostProcessSamples() const;
212
213        /** Returns true if view cells are already constructed.
214        */
215        virtual bool ViewCellsConstructed() const = 0;
216
217        /** cast line segment to get a list of unique viewcells which are intersected
218                by this line segment
219        */
220 
221        virtual int CastLineSegment(const Vector3 &origin,
222                                                          const Vector3 &termination,
223                                                          ViewCellContainer &viewcells
224                                                          ) = 0;
225
226        virtual void GetPvsStatistics(PvsStatistics &stat);
227
228        virtual void GetPrVS(const Vector3 &viewPoint, PrVs &prvs, const float filterWidth);
229 
230        /** Get a viewcell containing the specified point
231                @param active if the active or elementary view cell should be returned.
232        */
233        virtual ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const = 0;
234 
235        virtual void PrintPvsStatistics(ostream &s);
236
237        /** Updates pvs of the view cell hierarchy if necessary.
238        */
239        void UpdatePvs();
240
241        /** Returns probability that view point lies in one view cell.
242        */
243        virtual float GetProbability(ViewCell *viewCell) = 0;
244
245        /** Returns render cost of a single view cell given the render cost of an object.
246        */
247        float GetRendercost(ViewCell *viewCell) const;
248
249        /** Returns reference to container of loaded / generated view cells.
250        */
251        ViewCellContainer &GetViewCells();
252
253        /** Helper function used to split ray sets uniformly
254                into one that is currently used and the other that
255                is saved for later processing.
256                @param sourceRays the input ray set
257                @param maxSize the maximal number of rays that will be used
258                @param usedRays returns the used ray set
259                @param savedRays if not null, returns the saved ray set
260        */
261        void GetRaySets(const VssRayContainer &sourceRays,
262                                        const int maxSize,
263                                        VssRayContainer &usedRays,
264                                        VssRayContainer *savedRays = NULL) const;
265       
266        /** Returns accumulated area of all view cells.
267        */
268        float GetAccVcArea();
269
270        /** Returns area of one view cell.
271        */
272        virtual float GetArea(ViewCell *viewCell) const;
273
274        /** Returns volume of view cell.
275        */
276        virtual float GetVolume(ViewCell *viewCell) const;
277
278        /** Sets the current renderer mainly for view cells statistics.
279        */
280        void SetRenderer(Renderer *renderer);
281
282        /** Computes a (random) view point in the valid view space.
283                @returns true if valid view point was found
284        */
285        virtual bool GetViewPoint(Vector3 &viewPoint) const;
286
287        /** Returns true if this view point is in the valid view space.
288        */
289        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
290
291        /** Sets a view space boundary.
292        */
293        void SetViewSpaceBox(const AxisAlignedBox3 &box);
294
295        AxisAlignedBox3 GetViewSpaceBox() const;
296
297        /** Creates mesh for this view cell.
298        */
299        virtual void CreateMesh(ViewCell *vc) = NULL;
300
301        /** Writes view cells to disc.
302        */
303        virtual bool ExportViewCells(const string filename,
304                                                                 const bool exportPvs,
305                                                                 const ObjectContainer &objects);
306
307        /** Casts beam to collect view cells.
308        */
309        virtual int CastBeam(Beam &beam);
310
311        /** Checks if view cell is considered as valid.
312        */
313        virtual bool CheckValidity(ViewCell *vc,
314                                                           int minPvsSize,
315                                                           int maxPvsSize) const;
316
317       
318        /** Sets validity of view cell
319        */
320        virtual void SetValidity(ViewCell *vc,
321                                                         int minPvsSize,
322                                                         int maxPvsSize) const;
323
324        /** sets validy of all viewcells
325        */
326        virtual void SetValidity(int minPvsSize,
327                                                         int maxPvsSize) const;
328
329
330        /** set valid viewcells in the range of pvs. sorts the viewcells
331          according to the pvs and then pickups those in the ranges
332          */
333        void SetValidityPercentage(const float minValid, const float maxValid);
334
335    int CountValidViewcells() const;
336
337        /** Returns maximal allowed pvs size.
338        */
339        int GetMaxPvsSize() const;
340
341        /** Returns maximal allowed pvs size.
342        */
343        int GetMinPvsSize() const;
344
345        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
346                where pvs is still considered valid.
347        */
348        float GetMaxPvsRatio() const;
349
350        /** Exports view cell geometry.
351        */
352        virtual void ExportViewCellGeometry(Exporter *exporter,
353                                                                                ViewCell *vc,
354                                                                                const AxisAlignedPlane *clipPlane = NULL) const = 0;
355
356        /** Brings the view cells into their final state, computes meshes and volume.
357        */
358        virtual void FinalizeViewCells(const bool createMesh);
359
360        /** Evaluates statistics values on view cells.
361        */
362        void EvaluateRenderStatistics(float &totalRenderCost,
363                                                                  float &expectedRenderCost,
364                                                                  float &deviation,
365                                                                  float &variance,
366                                                                  int &totalPvs,
367                                                                  float &avgRenderCost);
368
369
370        /** Returns hierarchy of the view cells.
371        */
372        ViewCellsTree *GetViewCellsTree();
373
374        virtual void CollectMergeCandidates(const VssRayContainer &rays,
375                                                                                vector<MergeCandidate> &candidates);
376
377        void CollectViewCells(const int n);
378
379        /** Sets current view cells set to active, i.e., the sampling is done in this view cell set.
380        */
381        void SetViewCellsActive();
382
383        /** Evaluates render cost statistics of current view cell hierarchy.
384        */
385        void EvalViewCellPartition();
386
387        /** Sets maximal size of a view cell filter.
388        */
389        void SetMaxFilterSize(const int size);
390
391        /** Returns maximal filter size.
392        */
393        int GetMaxFilterSize() const;
394
395        /** Deletes interior nodes from the tree which have negative merge cost set (local merge).
396        */ 
397        void DeleteLocalMergeTree(ViewCell *vc) const;
398 
399        /** Evaluautes histogram for a given number of view cells.
400        */
401        void EvalViewCellHistogram(const string filename, const int nViewCells);
402
403        /** Evaluautes histogram for a given number of view cells.
404        */
405        void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
406
407        /** Evaluates the render cost of a view cell.
408        */
409        float EvalRenderCost(Intersectable *obj) const;
410   
411        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
412                of the hierarchy.
413        */
414        void SetScalarPvsSize(ViewCell *vc, const int pvsSize) const;
415
416
417        /** Returns bounding box of a view cell.
418        */
419        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
420
421        /** Exports bounding boxes of objects to file.
422        */
423        bool ExportBoundingBoxes(const string filename, const ObjectContainer &objects) const;
424   
425        /** Load the bounding boxes into the container.
426        */
427        bool LoadBoundingBoxes(const string filename, IndexedBoundingBoxContainer &boxes) const;
428
429        /** Returns true if pvs should be exported together with the view cells.
430        */
431        bool GetExportPvs() const;
432
433        /** Loads view cells from file. The view cells manager is created with
434                respect to the loaded view cells.
435
436                @param filename the filename of the view cells
437                @param objects the scene objects
438                @param finalizeViewCells if the view cells should be post processed, i.e.
439                        a mesh is created representing the geometry
440                @param bconverter a conversion routine working with the similarities of bounding
441                boxes: if there is a certain similarity of overlap between bounding boxes, two tested
442                candidate objects are considered to be the same objects
443                @returns the view cells manager if loading was successful, false otherwise
444        */
445        static ViewCellsManager *LoadViewCells(const string &filename,
446                                                                                   ObjectContainer *objects,
447                                                                                   const bool finalizeViewCells,
448                                                                                   BoundingBoxConverter *bconverter = NULL);
449
450
451        ////////////////////////////////////////////////////////7
452        // visiblity filter options
453        // TODO: write own visibiltiy filter class
454        void ApplyFilter(KdTree *kdTree,
455                                         const float viewSpaceFilterSize,
456                                         const float spatialFilterSize);
457
458        void ApplySpatialFilter(KdTree *kdTree,
459                                                        const float spatialFilterSize,
460                                                        ObjectPvs &pvs);
461
462         void ApplyFilter(ViewCell *viewCell,
463                                          KdTree *kdTree,
464                                          const float viewSpaceFilterSize,
465                                          const float spatialFilterSize,
466                                          ObjectPvs &pvs);
467
468        float GetFilterWidth();
469
470        float GetAbsFilterWidth();
471
472       
473        /** Returns the bounding box of filter width.
474        */
475        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
476
477
478        /** Returns true if the view cell is equivalent to a
479                node of the spatial hierarchy. This function can be used for merged
480                view cell.
481                e.g. to see if the spatial tree can be reduced on this location
482        */
483        virtual bool EqualToSpatialNode(ViewCell *viewCell) const;
484
485        /** If true, the kd nodes are stored instead of the object pvs.
486        */
487        void SetStoreKdPvs(const bool storeKdPvs);
488
489        /** Returns true if the kd nodes are stored instead of the object pvs.
490        **/
491        bool GetStoreKdPVs() const;
492
493protected:
494
495        /** Exports bounding boxes as xml stream
496        */
497        //bool ExportBoundingBoxes(ofstream &xmlstream, const ObjectContainer &objects) const;
498
499        /** Intersects box with the tree and returns the number of intersected boxes.
500                @returns number of view cells found
501        */
502        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
503                                                                                ViewCellContainer &viewCells) const;
504
505        /** Tests the visibility filter functionality.
506        */
507        virtual void TestFilter(const ObjectContainer &objects) {};
508
509        /** If the view cells tree was already constructed or not.
510        */
511        bool ViewCellsTreeConstructed() const;
512
513        int CastPassSamples(const int samplesPerPass,
514                                                const int sampleType,
515                                                VssRayContainer &vssRays) const;
516
517        /** Parse the options from the environment file.
518        */
519        void ParseEnvironment();
520
521        /** Creates unique view cell ids.
522        */
523        void CreateUniqueViewCellIds();
524
525        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
526        */
527        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
528
529        /** Recollects view cells and resets statistics.
530        */
531        void ResetViewCells();
532       
533        /** Collects the view cells in the view cell container.
534        */
535        virtual void CollectViewCells() = 0;
536       
537        /** Evaluates view cells statistics and stores it in
538                mViewCellsStatistics.
539        */
540        void EvaluateViewCellsStats();
541
542        //-- helper functions for view cell visualization
543
544        /** Exports the view cell partition.
545        */
546        void ExportViewCellsForViz(Exporter *exporter) const;
547
548        /** Sets exporter color.
549        */
550        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const;
551
552        /** Returns volume of the view space.
553        */
554        virtual float GetViewSpaceVolume();
555       
556        /** Creates meshes from the view cells.
557        */
558        void CreateViewCellMeshes();
559
560        /** Takes different measures to prepares the view cells after loading them from disc.
561        */
562        virtual void PrepareLoadedViewCells() {};
563
564        /** Constructs local view cell merge hierarchy.
565        */
566        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
567                                                                          const ViewCellContainer &viewCells);
568
569        /** Constructs local view cell merge hierarchy based solely on similarity with the
570                current viewcell
571        */
572        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
573                                                                           const ViewCellContainer &viewCells);
574 
575        /** Creates clip plane for visualization.
576        */
577        void CreateClipPlane();
578
579        /** Updates pvs of all view cells for statistical evaluation after some more sampling
580        */
581        virtual void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs) = 0;
582
583       
584        ////////////////////////////////////////////////
585
586        Preprocessor *mPreprocessor;
587        /// if bounding boxes should be exported together with the view cells
588        bool mExportBboxesForPvs;
589               
590        //Environment *environment;
591        AxisAlignedPlane mClipPlane;
592
593        bool mUseClipPlaneForViz;
594
595
596        /// Renders the view cells.
597        Renderer *mRenderer;
598
599        /// Loaded view cells
600        ViewCellContainer mViewCells;
601        /// the corresponding view cell tree holding the logical description of view cells
602        ViewCellsTree *mViewCellsTree;
603        /// if empty view cells should be pruned (i.e., invalidated) from this point on
604        bool mPruneEmptyViewCells;
605
606        /// if the values in the view cell leaves and the interiors are up to date
607        /// this is meant for lazy storing of the pvs, where only a scalar indicating
608        /// pvs size is stored in interiors and not the pvs itself.
609        bool mViewCellPvsIsUpdated;
610
611        /// maximum number of samples taken for construction of the view cells
612        int mConstructionSamples;
613        int mSamplesPerPass;
614        int mInitialSamples;
615        int mPostProcessSamples;
616        int mVisualizationSamples;
617
618        float mTotalAreaValid;
619        float mTotalArea;
620
621        int mMaxPvsSize;
622        int mMinPvsSize;
623        float mMaxPvsRatio;
624
625        int mSamplingType;
626        int mEvaluationSamplingType;
627        int mNumActiveViewCells;
628        bool mCompressViewCells;
629
630        /// holds the current view cell statistics
631        ViewCellsStatistics mCurrentViewCellsStats;
632        /// the scene bounding box
633        AxisAlignedBox3 mViewSpaceBox;
634       
635        /// if view cells should be exported
636        bool mExportViewCells;
637
638        // if only valid view cells should be considered for processing
639        bool mOnlyValidViewCells;
640
641        /// if rays should be used to collect merge candidates
642        bool mUseRaysForMerge;
643
644        /// if there should be an additional merge step after the subdivision
645        bool mMergeViewCells;
646
647        /// the width of the box filter
648        float mFilterWidth;
649        int mMaxFilterSize;
650
651        //-- visualization options
652       
653        /// color code for view cells visualization
654        int mColorCode;
655        bool mExportGeometry;
656        bool mExportRays;
657
658        bool mViewCellsFinished;
659
660        bool mEvaluateViewCells;
661
662        bool mShowVisualization;
663
664        // HACK in order to detect empty view cells
665        void CollectEmptyViewCells();
666        void TestEmptyViewCells(const ObjectContainer &obj);
667
668        ViewCellContainer mEmptyViewCells;
669
670        int mRenderCostEvaluationType;
671
672        /// if pvs should be exported with view cells
673        bool mExportPvs;
674
675        bool mStoreKdPvs;
676};
677
678
679/**
680        Manages different higher order operations on the view cells.
681*/
682class BspViewCellsManager: public ViewCellsManager
683{
684
685public:
686        /** Constructor taking the bsp tree and the number of samples
687                used to construct the bsp tree.
688        */
689        BspViewCellsManager(BspTree *tree);
690
691        ~BspViewCellsManager();
692
693        int ConstructSubdivision(const ObjectContainer &objects,
694                                  const VssRayContainer &rays);
695
696        int PostProcess(const ObjectContainer &objects,
697                                        const VssRayContainer &rays);
698
699        void Visualize(const ObjectContainer &objects,
700                                   const VssRayContainer &sampleRays);
701
702        int GetType() const;
703       
704        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
705
706        bool ViewCellsConstructed() const;
707
708        //void PrintStatistics(ostream &s) const;
709
710        int CastLineSegment(const Vector3 &origin,
711                                                const Vector3 &termination,
712                                                ViewCellContainer &viewcells);
713       
714        float GetProbability(ViewCell *viewCell);
715       
716
717        /** Get a viewcell containing the specified point
718        */
719        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
720
721        void CreateMesh(ViewCell *vc);
722
723        void ExportViewCellGeometry(Exporter *exporter,
724                                                                ViewCell *vc,
725                                                                const AxisAlignedPlane *clipPlane = NULL) const;
726       
727        void CollectMergeCandidates(const VssRayContainer &rays,
728                                                                vector<MergeCandidate> &candidates);
729
730        void Finalize(ViewCell *viewCell, const bool createMesh);
731
732        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
733
734        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
735        */
736        ViewCell *ConstructSpatialMergeTree(BspNode *root);
737
738        /** Updates the pvs in the view cells tree.
739        */
740        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
741
742
743protected:
744
745        void CollectViewCells();
746
747       
748        /// the BSP tree.
749        BspTree *mBspTree;
750       
751        vector<BspRay *> mBspRays;
752
753private:
754
755        /** Exports visualization of the BSP splits.
756        */
757        void ExportSplits(const ObjectContainer &objects);
758
759        /** Exports visualization of the BSP PVS.
760        */
761        void ExportBspPvs(const ObjectContainer &objects);
762
763        /** test if subdivision is valid in terms of volume / area.
764        */
765        void TestSubdivision();
766};
767
768/**
769        Manages different higher order operations on the KD type view cells.
770*/
771class KdViewCellsManager: public ViewCellsManager
772{
773
774public:
775
776        KdViewCellsManager(KdTree *tree);
777
778        int ConstructSubdivision(const ObjectContainer &objects,
779                                  const VssRayContainer &rays);
780
781        int CastLineSegment(const Vector3 &origin,
782                                                const Vector3 &termination,
783                                                ViewCellContainer &viewcells);
784
785        int PostProcess(const ObjectContainer &objects,
786                                        const VssRayContainer &rays);
787
788        void Visualize(const ObjectContainer &objects,
789                                   const VssRayContainer &sampleRays);
790
791        int GetType() const;
792
793        bool ViewCellsConstructed() const;
794
795        ViewCell *GenerateViewCell(Mesh *mesh) const;
796
797        /** Prints out statistics of this approach.
798        */
799        //  virtual void PrintStatistics(ostream &s) const;
800        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const { return NULL; }
801
802        float GetProbability(ViewCell *viewCell);
803       
804
805        void CreateMesh(ViewCell *vc);
806
807        void ExportViewCellGeometry(Exporter *exporter,
808                                                                ViewCell *vc,
809                                                                const AxisAlignedPlane *clipPlane = NULL) const;
810
811        void CollectMergeCandidates(const VssRayContainer &rays,
812                                                                vector<MergeCandidate> &candidates);
813
814
815protected:
816
817        virtual void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs) {};
818        /** Collects view cells from a hierarchy.
819        */
820        void CollectViewCells();
821
822        KdNode *GetNodeForPvs(KdLeaf *leaf);
823
824
825        /// the BSP tree.
826        KdTree *mKdTree;
827
828        /// depth of the KD tree nodes with represent the view cells
829        int mKdPvsDepth;
830
831
832};
833
834/**
835        Manages different higher order operations on the view cells.
836*/
837class VspBspViewCellsManager: public ViewCellsManager
838{
839
840public:
841
842        VspBspViewCellsManager(VspBspTree *tree);
843        ~VspBspViewCellsManager();
844
845        int ConstructSubdivision(const ObjectContainer &objects,
846                                  const VssRayContainer &rays);
847
848        int PostProcess(const ObjectContainer &objects,
849                                        const VssRayContainer &rays);
850
851        void Visualize(const ObjectContainer &objects,
852                                   const VssRayContainer &sampleRays);
853
854        int GetType() const;
855       
856        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
857
858        bool ViewCellsConstructed() const;
859
860       
861        int CastLineSegment(const Vector3 &origin,
862                                                const Vector3 &termination,
863                                                ViewCellContainer &viewcells);
864
865        float GetProbability(ViewCell *viewCell);
866       
867        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
868
869        bool GetViewPoint(Vector3 &viewPoint) const;
870
871        bool ViewPointValid(const Vector3 &viewPoint) const;
872
873        void CreateMesh(ViewCell *vc);
874
875        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
876
877        int CastBeam(Beam &beam);
878
879        void ExportViewCellGeometry(Exporter *exporter,
880                                                                ViewCell *vc,
881                                                                const AxisAlignedPlane *clipPlane = NULL) const;
882
883        //float GetVolume(ViewCell *viewCell) const;
884
885        void Finalize(ViewCell *viewCell, const bool createMesh);
886
887        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
888
889        /** Returns true if this view cell is equivavalent to a spatial node.
890        */
891        bool EqualToSpatialNode(ViewCell *viewCell) const;
892
893
894protected:
895
896        int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
897       
898        /** Returns node of the spatial hierarchy corresponding to the view cell
899                if such a node exists.
900        */
901        BspNode *GetSpatialNode(ViewCell *viewCell) const;
902
903        /** Merges view cells according to some criteria
904        */
905        void MergeViewCells(const VssRayContainer &rays,
906                                                const ObjectContainer &objects);
907       
908        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
909
910        void CollectViewCells();
911
912        /** Returns maximal depth difference of view cell
913                leaves in tree.
914        */
915        int GetMaxTreeDiff(ViewCell *vc) const;
916       
917
918        /** Prepare view cells for use after loading them from disc.
919        */
920        void PrepareLoadedViewCells();
921
922        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
923        */
924        ViewCell *ConstructSpatialMergeTree(BspNode *root);
925
926        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
927
928
929        /// HACK for testing visibility filter functionality
930        void TestFilter(const ObjectContainer &objects);
931
932
933        /** Visualization of the pvs difference to exact visubility using
934                from point queries.
935        */
936        void VisualizeWithFromPointQueries();
937
938        /** Evaluate from point queries for the current scene.
939        */
940        void EvalFromPointQueries();
941
942        /** Exports visualization of the BSP splits.
943        */
944        void ExportSplits(const ObjectContainer &objects,
945                                          const VssRayContainer &rays);
946
947        /** Exports visualization of the BSP PVS.
948        */
949        void ExportBspPvs(const ObjectContainer &objects,
950                                          const VssRayContainer &rays);
951
952
953        /// the view space partition BSP tree.
954        VspBspTree *mVspBspTree;
955
956
957private:
958
959        /** test if subdivision is valid in terms of volume / area.
960        */
961        void TestSubdivision();
962};
963
964
965/**
966        Manages different higher order operations on the view cells.
967*/
968class VspOspViewCellsManager: public ViewCellsManager
969{
970
971public:
972
973        VspOspViewCellsManager(VspTree *tree, OspTree *ospTree);
974        ~VspOspViewCellsManager();
975
976        int ConstructSubdivision(const ObjectContainer &objects,
977                                                         const VssRayContainer &rays);
978
979        int PostProcess(const ObjectContainer &objects,
980                                        const VssRayContainer &rays);
981
982        void Visualize(const ObjectContainer &objects,
983                                   const VssRayContainer &sampleRays);
984
985        int GetType() const;
986       
987        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
988
989        bool ViewCellsConstructed() const;
990
991       
992        int CastLineSegment(const Vector3 &origin,
993                                                const Vector3 &termination,
994                                                ViewCellContainer &viewcells);
995
996        float GetProbability(ViewCell *viewCell);
997       
998        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
999
1000        bool GetViewPoint(Vector3 &viewPoint) const;
1001
1002        bool ViewPointValid(const Vector3 &viewPoint) const;
1003
1004        void CreateMesh(ViewCell *vc);
1005
1006        bool ExportViewCells(const string filename,
1007                                                 const bool exportPvs,
1008                                                 const ObjectContainer &objects);
1009
1010        int CastBeam(Beam &beam);
1011
1012        void Finalize(ViewCell *viewCell, const bool createMesh);
1013
1014
1015protected:
1016
1017       
1018        /** Exports view cell geometry.
1019        */
1020        void ExportViewCellGeometry(Exporter *exporter,
1021                ViewCell *vc,
1022                const AxisAlignedPlane *clipPlane = NULL) const;
1023
1024        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1025                ViewCellContainer &viewCells) const;
1026       
1027        void CollectViewCells();
1028
1029
1030        /** Prepare view cells for use after loading them from disc.
1031        */
1032        void PrepareLoadedViewCells();
1033
1034        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1035        */
1036        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1037
1038        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
1039
1040        /** Exports visualization of the PVS.
1041        */
1042        void ExportPvs(const ObjectContainer &objects,
1043                const VssRayContainer &rays);
1044
1045
1046        /// the view space partition tree.
1047        VspTree *mVspTree;
1048        OspTree *mOspTree;
1049
1050        HierarchyManager *mHierarchyManager;
1051};
1052
1053
1054/*class ViewCellsManagerFactory
1055{
1056public:
1057        ViewCellsManager *Create(const string mName);
1058};*/
1059
1060}
1061
1062#endif
Note: See TracBrowser for help on using the repository browser.