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

Revision 1168, 28.1 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;
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 ComputeSampleContribution(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 UpdateScalarPvsSize(ViewCell *vc, const int pvsSize, const int entriesInPvs) 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        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
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       
739protected:
740
741        void CollectViewCells();
742
743       
744        /// the BSP tree.
745        BspTree *mBspTree;
746       
747        vector<BspRay *> mBspRays;
748
749private:
750
751        /** Exports visualization of the BSP splits.
752        */
753        void ExportSplits(const ObjectContainer &objects);
754
755        /** Exports visualization of the BSP PVS.
756        */
757        void ExportBspPvs(const ObjectContainer &objects);
758
759        /** test if subdivision is valid in terms of volume / area.
760        */
761        void TestSubdivision();
762};
763
764/**
765        Manages different higher order operations on the KD type view cells.
766*/
767class KdViewCellsManager: public ViewCellsManager
768{
769
770public:
771
772        KdViewCellsManager(KdTree *tree);
773
774        int ConstructSubdivision(const ObjectContainer &objects,
775                                  const VssRayContainer &rays);
776
777        int CastLineSegment(const Vector3 &origin,
778                                                const Vector3 &termination,
779                                                ViewCellContainer &viewcells);
780
781        int PostProcess(const ObjectContainer &objects,
782                                        const VssRayContainer &rays);
783
784        void Visualize(const ObjectContainer &objects,
785                                   const VssRayContainer &sampleRays);
786
787        int GetType() const;
788
789        bool ViewCellsConstructed() const;
790
791        ViewCell *GenerateViewCell(Mesh *mesh) const;
792
793        /** Prints out statistics of this approach.
794        */
795        //  virtual void PrintStatistics(ostream &s) const;
796        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const { return NULL; }
797
798        float GetProbability(ViewCell *viewCell);
799       
800
801        void CreateMesh(ViewCell *vc);
802
803        void ExportViewCellGeometry(Exporter *exporter,
804                                                                ViewCell *vc,
805                                                                const AxisAlignedPlane *clipPlane = NULL) const;
806
807        void CollectMergeCandidates(const VssRayContainer &rays,
808                                                                vector<MergeCandidate> &candidates);
809
810
811protected:
812
813        /** Collects view cells from a hierarchy.
814        */
815        void CollectViewCells();
816
817        KdNode *GetNodeForPvs(KdLeaf *leaf);
818
819
820        /// the BSP tree.
821        KdTree *mKdTree;
822
823        /// depth of the KD tree nodes with represent the view cells
824        int mKdPvsDepth;
825
826
827};
828
829/**
830        Manages different higher order operations on the view cells.
831*/
832class VspBspViewCellsManager: public ViewCellsManager
833{
834
835public:
836
837        VspBspViewCellsManager(VspBspTree *tree);
838        ~VspBspViewCellsManager();
839
840        int ConstructSubdivision(const ObjectContainer &objects,
841                                  const VssRayContainer &rays);
842
843        int PostProcess(const ObjectContainer &objects,
844                                        const VssRayContainer &rays);
845
846        void Visualize(const ObjectContainer &objects,
847                                   const VssRayContainer &sampleRays);
848
849        int GetType() const;
850       
851        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
852
853        bool ViewCellsConstructed() const;
854
855       
856        int CastLineSegment(const Vector3 &origin,
857                                                const Vector3 &termination,
858                                                ViewCellContainer &viewcells);
859
860        float GetProbability(ViewCell *viewCell);
861       
862        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
863
864        bool GetViewPoint(Vector3 &viewPoint) const;
865
866        bool ViewPointValid(const Vector3 &viewPoint) const;
867
868        void CreateMesh(ViewCell *vc);
869
870        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
871
872        int CastBeam(Beam &beam);
873
874        void ExportViewCellGeometry(Exporter *exporter,
875                                                                ViewCell *vc,
876                                                                const AxisAlignedPlane *clipPlane = NULL) const;
877
878        //float GetVolume(ViewCell *viewCell) const;
879
880        void Finalize(ViewCell *viewCell, const bool createMesh);
881
882        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
883
884        /** Returns true if this view cell is equivavalent to a spatial node.
885        */
886        bool EqualToSpatialNode(ViewCell *viewCell) const;
887
888
889protected:
890
891        int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
892       
893        /** Returns node of the spatial hierarchy corresponding to the view cell
894                if such a node exists.
895        */
896        BspNode *GetSpatialNode(ViewCell *viewCell) const;
897
898        /** Merges view cells according to some criteria
899        */
900        void MergeViewCells(const VssRayContainer &rays,
901                                                const ObjectContainer &objects);
902       
903        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
904
905        void CollectViewCells();
906
907        /** Returns maximal depth difference of view cell
908                leaves in tree.
909        */
910        int GetMaxTreeDiff(ViewCell *vc) const;
911       
912
913        /** Prepare view cells for use after loading them from disc.
914        */
915        void PrepareLoadedViewCells();
916
917        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
918        */
919        ViewCell *ConstructSpatialMergeTree(BspNode *root);
920
921        /// HACK for testing visibility filter functionality
922        void TestFilter(const ObjectContainer &objects);
923
924        /** Visualization of the pvs difference to exact visubility using
925                from point queries.
926        */
927        void VisualizeWithFromPointQueries();
928
929        /** Evaluate from point queries for the current scene.
930        */
931        void EvalFromPointQueries();
932
933        /** Exports visualization of the BSP splits.
934        */
935        void ExportSplits(const ObjectContainer &objects,
936                                          const VssRayContainer &rays);
937
938        /** Exports visualization of the BSP PVS.
939        */
940        void ExportBspPvs(const ObjectContainer &objects,
941                                          const VssRayContainer &rays);
942
943
944        /// the view space partition BSP tree.
945        VspBspTree *mVspBspTree;
946
947
948private:
949
950        /** test if subdivision is valid in terms of volume / area.
951        */
952        void TestSubdivision();
953};
954
955
956/**
957        Manages different higher order operations on the view cells.
958*/
959class VspOspViewCellsManager: public ViewCellsManager
960{
961
962public:
963
964        VspOspViewCellsManager(VspTree *tree, OspTree *ospTree);
965        ~VspOspViewCellsManager();
966
967        int ConstructSubdivision(const ObjectContainer &objects,
968                                                         const VssRayContainer &rays);
969
970        int PostProcess(const ObjectContainer &objects,
971                                        const VssRayContainer &rays);
972
973        void Visualize(const ObjectContainer &objects,
974                                   const VssRayContainer &sampleRays);
975
976        int GetType() const;
977       
978        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
979
980        bool ViewCellsConstructed() const;
981
982       
983        int CastLineSegment(const Vector3 &origin,
984                                                const Vector3 &termination,
985                                                ViewCellContainer &viewcells);
986
987        float GetProbability(ViewCell *viewCell);
988       
989        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
990
991        bool GetViewPoint(Vector3 &viewPoint) const;
992
993        bool ViewPointValid(const Vector3 &viewPoint) const;
994
995        void CreateMesh(ViewCell *vc);
996
997        bool ExportViewCells(const string filename,
998                                                 const bool exportPvs,
999                                                 const ObjectContainer &objects);
1000
1001        int CastBeam(Beam &beam);
1002
1003        void Finalize(ViewCell *viewCell, const bool createMesh);
1004
1005        /** Stores sample contribution for kd cells or objects.
1006        */
1007        virtual float ComputeSampleContribution(VssRay &ray,
1008                                                                                        const bool addRays,
1009                                                                                        const bool storeViewCells);
1010
1011protected:
1012       
1013        /** Exports view cell geometry.
1014        */
1015        void ExportViewCellGeometry(Exporter *exporter,
1016                ViewCell *vc,
1017                const AxisAlignedPlane *clipPlane = NULL) const;
1018
1019        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1020                ViewCellContainer &viewCells) const;
1021       
1022        void CollectViewCells();
1023
1024
1025        /** Prepare view cells for use after loading them from disc.
1026        */
1027        void PrepareLoadedViewCells();
1028
1029        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1030        */
1031        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1032
1033        /** Exports visualization of the PVS.
1034        */
1035        void ExportPvs(const ObjectContainer &objects,
1036                const VssRayContainer &rays);
1037
1038
1039        /////////////////////////////////////////
1040        /// the view space partition tree.
1041        VspTree *mVspTree;
1042        OspTree *mOspTree;
1043
1044        HierarchyManager *mHierarchyManager;
1045};
1046
1047
1048/*class ViewCellsManagerFactory
1049{
1050public:
1051        ViewCellsManager *Create(const string mName);
1052};*/
1053
1054}
1055
1056#endif
Note: See TracBrowser for help on using the repository browser.