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

Revision 1145, 27.6 KB checked in by mattausch, 18 years ago (diff)

vsposp debug version

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