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

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