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

Revision 1201, 28.4 KB checked in by mattausch, 18 years ago (diff)

added loader for osp trees

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        virtual 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        /** 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 the view cell is equivalent to a
478                node of the spatial hierarchy. This function can be used for merged
479                view cell.
480                e.g. to see if the spatial tree can be reduced on this location
481        */
482        virtual bool EqualToSpatialNode(ViewCell *viewCell) const;
483
484        /** If true, the kd nodes are stored instead of the object pvs.
485        */
486        void SetStoreKdPvs(const bool storeKdPvs);
487
488        /** Returns true if the kd nodes are stored instead of the object pvs.
489        **/
490        bool GetStoreKdPVs() const;
491
492protected:
493
494        /** Exports bounding boxes as xml stream
495        */
496        //bool ExportBoundingBoxes(ofstream &xmlstream, const ObjectContainer &objects) const;
497
498        /** Intersects box with the tree and returns the number of intersected boxes.
499                @returns number of view cells found
500        */
501        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
502                                                                                ViewCellContainer &viewCells) const;
503
504        /** Tests the visibility filter functionality.
505        */
506        virtual void TestFilter(const ObjectContainer &objects) {};
507
508        /** If the view cells tree was already constructed or not.
509        */
510        bool ViewCellsTreeConstructed() const;
511
512        int CastPassSamples(const int samplesPerPass,
513                                                const int sampleType,
514                                                VssRayContainer &vssRays) const;
515
516        /** Parse the options from the environment file.
517        */
518        void ParseEnvironment();
519
520        /** Creates unique view cell ids.
521        */
522        void CreateUniqueViewCellIds();
523
524        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
525        */
526        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
527
528        /** Recollects view cells and resets statistics.
529        */
530        void ResetViewCells();
531       
532        /** Collects the view cells in the view cell container.
533        */
534        virtual void CollectViewCells() = 0;
535       
536        /** Evaluates view cells statistics and stores it in
537                mViewCellsStatistics.
538        */
539        void EvaluateViewCellsStats();
540
541        //-- helper functions for view cell visualization
542
543        /** Exports the view cell partition.
544        */
545        void ExportViewCellsForViz(Exporter *exporter) const;
546
547        /** Sets exporter color.
548        */
549        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const;
550
551        /** Returns volume of the view space.
552        */
553        virtual float GetViewSpaceVolume();
554       
555        /** Creates meshes from the view cells.
556        */
557        void CreateViewCellMeshes();
558
559        /** Takes different measures to prepares the view cells after loading them from disc.
560        */
561        virtual void PrepareLoadedViewCells() {};
562
563        /** Constructs local view cell merge hierarchy.
564        */
565        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
566                                                                          const ViewCellContainer &viewCells);
567
568        /** Constructs local view cell merge hierarchy based solely on similarity with the
569                current viewcell
570        */
571        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
572                                                                           const ViewCellContainer &viewCells);
573 
574        /** Creates clip plane for visualization.
575        */
576        void CreateClipPlane();
577
578        /** Updates pvs of all view cells for statistical evaluation after some more sampling
579        */
580        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
581
582       
583        ////////////////////////////////////////////////
584
585        Preprocessor *mPreprocessor;
586        /// if bounding boxes should be exported together with the view cells
587        bool mExportBboxesForPvs;
588               
589        //Environment *environment;
590        AxisAlignedPlane mClipPlane;
591
592        bool mUseClipPlaneForViz;
593
594
595        /// Renders the view cells.
596        Renderer *mRenderer;
597
598        /// Loaded view cells
599        ViewCellContainer mViewCells;
600        /// the corresponding view cell tree holding the logical description of view cells
601        ViewCellsTree *mViewCellsTree;
602        /// if empty view cells should be pruned (i.e., invalidated) from this point on
603        bool mPruneEmptyViewCells;
604
605        /// if the values in the view cell leaves and the interiors are up to date
606        /// this is meant for lazy storing of the pvs, where only a scalar indicating
607        /// pvs size is stored in interiors and not the pvs itself.
608        bool mViewCellPvsIsUpdated;
609
610        /// maximum number of samples taken for construction of the view cells
611        int mConstructionSamples;
612        int mSamplesPerPass;
613        int mInitialSamples;
614        int mPostProcessSamples;
615        int mVisualizationSamples;
616
617        float mTotalAreaValid;
618        float mTotalArea;
619
620        int mMaxPvsSize;
621        int mMinPvsSize;
622        float mMaxPvsRatio;
623
624        int mSamplingType;
625        int mEvaluationSamplingType;
626        int mNumActiveViewCells;
627        bool mCompressViewCells;
628
629        /// holds the current view cell statistics
630        ViewCellsStatistics mCurrentViewCellsStats;
631        /// the scene bounding box
632        AxisAlignedBox3 mViewSpaceBox;
633       
634        /// if view cells should be exported
635        bool mExportViewCells;
636
637        // if only valid view cells should be considered for processing
638        bool mOnlyValidViewCells;
639
640        /// if rays should be used to collect merge candidates
641        bool mUseRaysForMerge;
642
643        /// if there should be an additional merge step after the subdivision
644        bool mMergeViewCells;
645
646        /// the width of the box filter
647        float mFilterWidth;
648        int mMaxFilterSize;
649
650        //-- visualization options
651       
652        /// color code for view cells visualization
653        int mColorCode;
654        bool mExportGeometry;
655        bool mExportRays;
656
657        bool mViewCellsFinished;
658
659        bool mEvaluateViewCells;
660
661        bool mShowVisualization;
662
663        // HACK in order to detect empty view cells
664        void CollectEmptyViewCells();
665        void TestEmptyViewCells(const ObjectContainer &obj);
666
667        ViewCellContainer mEmptyViewCells;
668
669        int mRenderCostEvaluationType;
670
671        /// if pvs should be exported with view cells
672        bool mExportPvs;
673
674        bool mStoreKdPvs;
675
676        VssRayContainer storedRays;
677};
678
679
680/**
681        Manages different higher order operations on the view cells.
682*/
683class BspViewCellsManager: public ViewCellsManager
684{
685
686public:
687        /** Constructor taking the bsp tree and the number of samples
688                used to construct the bsp tree.
689        */
690        BspViewCellsManager(BspTree *tree);
691
692        ~BspViewCellsManager();
693
694        int ConstructSubdivision(const ObjectContainer &objects,
695                                  const VssRayContainer &rays);
696
697        int PostProcess(const ObjectContainer &objects,
698                                        const VssRayContainer &rays);
699
700        void Visualize(const ObjectContainer &objects,
701                                   const VssRayContainer &sampleRays);
702
703        int GetType() const;
704       
705        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
706
707        bool ViewCellsConstructed() const;
708
709        //void PrintStatistics(ostream &s) const;
710
711        int CastLineSegment(const Vector3 &origin,
712                                                const Vector3 &termination,
713                                                ViewCellContainer &viewcells);
714       
715        float GetProbability(ViewCell *viewCell);
716       
717
718        /** Get a viewcell containing the specified point
719        */
720        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
721
722        void CreateMesh(ViewCell *vc);
723
724        void ExportViewCellGeometry(Exporter *exporter,
725                                                                ViewCell *vc,
726                                                                const AxisAlignedPlane *clipPlane = NULL) const;
727       
728        void CollectMergeCandidates(const VssRayContainer &rays,
729                                                                vector<MergeCandidate> &candidates);
730
731        void Finalize(ViewCell *viewCell, const bool createMesh);
732
733        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
734
735        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
736        */
737        ViewCell *ConstructSpatialMergeTree(BspNode *root);
738
739       
740protected:
741
742        void CollectViewCells();
743
744       
745        /// the BSP tree.
746        BspTree *mBspTree;
747       
748        vector<BspRay *> mBspRays;
749
750private:
751
752        /** Exports visualization of the BSP splits.
753        */
754        void ExportSplits(const ObjectContainer &objects);
755
756        /** Exports visualization of the BSP PVS.
757        */
758        void ExportBspPvs(const ObjectContainer &objects);
759
760        /** test if subdivision is valid in terms of volume / area.
761        */
762        void TestSubdivision();
763};
764
765/**
766        Manages different higher order operations on the KD type view cells.
767*/
768class KdViewCellsManager: public ViewCellsManager
769{
770
771public:
772
773        KdViewCellsManager(KdTree *tree);
774
775        int ConstructSubdivision(const ObjectContainer &objects,
776                                  const VssRayContainer &rays);
777
778        int CastLineSegment(const Vector3 &origin,
779                                                const Vector3 &termination,
780                                                ViewCellContainer &viewcells);
781
782        int PostProcess(const ObjectContainer &objects,
783                                        const VssRayContainer &rays);
784
785        void Visualize(const ObjectContainer &objects,
786                                   const VssRayContainer &sampleRays);
787
788        int GetType() const;
789
790        bool ViewCellsConstructed() const;
791
792        ViewCell *GenerateViewCell(Mesh *mesh) const;
793
794        /** Prints out statistics of this approach.
795        */
796        //  virtual void PrintStatistics(ostream &s) const;
797        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const { return NULL; }
798
799        float GetProbability(ViewCell *viewCell);
800       
801
802        void CreateMesh(ViewCell *vc);
803
804        void ExportViewCellGeometry(Exporter *exporter,
805                                                                ViewCell *vc,
806                                                                const AxisAlignedPlane *clipPlane = NULL) const;
807
808        void CollectMergeCandidates(const VssRayContainer &rays,
809                                                                vector<MergeCandidate> &candidates);
810
811
812protected:
813
814        /** Collects view cells from a hierarchy.
815        */
816        void CollectViewCells();
817
818        KdNode *GetNodeForPvs(KdLeaf *leaf);
819
820
821        /// the BSP tree.
822        KdTree *mKdTree;
823
824        /// depth of the KD tree nodes with represent the view cells
825        int mKdPvsDepth;
826
827
828};
829
830/**
831        Manages different higher order operations on the view cells.
832*/
833class VspBspViewCellsManager: public ViewCellsManager
834{
835
836public:
837
838        VspBspViewCellsManager(VspBspTree *tree);
839        ~VspBspViewCellsManager();
840
841        int ConstructSubdivision(const ObjectContainer &objects,
842                                  const VssRayContainer &rays);
843
844        int PostProcess(const ObjectContainer &objects,
845                                        const VssRayContainer &rays);
846
847        void Visualize(const ObjectContainer &objects,
848                                   const VssRayContainer &sampleRays);
849
850        int GetType() const;
851       
852        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
853
854        bool ViewCellsConstructed() const;
855
856       
857        int CastLineSegment(const Vector3 &origin,
858                                                const Vector3 &termination,
859                                                ViewCellContainer &viewcells);
860
861        float GetProbability(ViewCell *viewCell);
862       
863        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
864
865        bool GetViewPoint(Vector3 &viewPoint) const;
866
867        bool ViewPointValid(const Vector3 &viewPoint) const;
868
869        void CreateMesh(ViewCell *vc);
870
871        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
872
873        int CastBeam(Beam &beam);
874
875        void ExportViewCellGeometry(Exporter *exporter,
876                                                                ViewCell *vc,
877                                                                const AxisAlignedPlane *clipPlane = NULL) const;
878
879        //float GetVolume(ViewCell *viewCell) const;
880
881        void Finalize(ViewCell *viewCell, const bool createMesh);
882
883        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
884
885        /** Returns true if this view cell is equivavalent to a spatial node.
886        */
887        bool EqualToSpatialNode(ViewCell *viewCell) const;
888
889
890protected:
891
892        int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
893       
894        /** Returns node of the spatial hierarchy corresponding to the view cell
895                if such a node exists.
896        */
897        BspNode *GetSpatialNode(ViewCell *viewCell) const;
898
899        /** Merges view cells according to some criteria
900        */
901        void MergeViewCells(const VssRayContainer &rays,
902                                                const ObjectContainer &objects);
903       
904        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
905
906        void CollectViewCells();
907
908        /** Returns maximal depth difference of view cell
909                leaves in tree.
910        */
911        int GetMaxTreeDiff(ViewCell *vc) const;
912       
913
914        /** Prepare view cells for use after loading them from disc.
915        */
916        void PrepareLoadedViewCells();
917
918        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
919        */
920        ViewCell *ConstructSpatialMergeTree(BspNode *root);
921
922        /// HACK for testing visibility filter functionality
923        void TestFilter(const ObjectContainer &objects);
924
925        /** Visualization of the pvs difference to exact visubility using
926                from point queries.
927        */
928        void VisualizeWithFromPointQueries();
929
930        /** Evaluate from point queries for the current scene.
931        */
932        void EvalFromPointQueries();
933
934        /** Exports visualization of the BSP splits.
935        */
936        void ExportSplits(const ObjectContainer &objects,
937                                          const VssRayContainer &rays);
938
939        /** Exports visualization of the BSP PVS.
940        */
941        void ExportBspPvs(const ObjectContainer &objects,
942                                          const VssRayContainer &rays);
943
944
945        /// the view space partition BSP tree.
946        VspBspTree *mVspBspTree;
947
948
949private:
950
951        /** test if subdivision is valid in terms of volume / area.
952        */
953        void TestSubdivision();
954};
955
956#define TEST_EVALUATION 1
957
958/**
959        Manages different higher order operations on the view cells.
960*/
961class VspOspViewCellsManager: public ViewCellsManager
962{
963
964public:
965
966        VspOspViewCellsManager(VspTree *tree, OspTree *ospTree);
967        ~VspOspViewCellsManager();
968
969        int ConstructSubdivision(const ObjectContainer &objects,
970                                                         const VssRayContainer &rays);
971
972        int PostProcess(const ObjectContainer &objects,
973                                        const VssRayContainer &rays);
974
975        void Visualize(const ObjectContainer &objects,
976                                   const VssRayContainer &sampleRays);
977
978        int GetType() const;
979       
980        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
981
982        bool ViewCellsConstructed() const;
983
984       
985        int CastLineSegment(const Vector3 &origin,
986                                                const Vector3 &termination,
987                                                ViewCellContainer &viewcells);
988
989        float GetProbability(ViewCell *viewCell);
990       
991        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
992
993        bool GetViewPoint(Vector3 &viewPoint) const;
994
995        bool ViewPointValid(const Vector3 &viewPoint) const;
996
997        void CreateMesh(ViewCell *vc);
998
999        bool ExportViewCells(const string filename,
1000                                                 const bool exportPvs,
1001                                                 const ObjectContainer &objects);
1002
1003        int CastBeam(Beam &beam);
1004
1005        void Finalize(ViewCell *viewCell, const bool createMesh);
1006
1007        /** Stores sample contribution for kd cells or objects.
1008        */
1009        virtual float ComputeSampleContribution(VssRay &ray,
1010                                                                                        const bool addRays,
1011                                                                                        const bool storeViewCells);
1012
1013        ViewCellsManager *LoadViewCells(const string &filename,
1014                ObjectContainer *objects,
1015                const bool finalizeViewCells,
1016                BoundingBoxConverter *bconverter);
1017
1018
1019protected:
1020
1021#if TEST_EVALUATION
1022        virtual void EvalViewCellPartition();
1023#endif
1024        /** Exports view cell geometry.
1025        */
1026        void ExportViewCellGeometry(Exporter *exporter,
1027                ViewCell *vc,
1028                const AxisAlignedPlane *clipPlane = NULL) const;
1029
1030        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1031                ViewCellContainer &viewCells) const;
1032       
1033        void CollectViewCells();
1034
1035
1036        /** Prepare view cells for use after loading them from disc.
1037        */
1038        void PrepareLoadedViewCells();
1039
1040        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1041        */
1042        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1043
1044        /** Exports visualization of the PVS.
1045        */
1046        void ExportPvs(
1047                const ObjectContainer &objects,
1048                const VssRayContainer &rays);
1049
1050
1051        /////////////////////////////////////////
1052        /// the view space partition tree.
1053        VspTree *mVspTree;
1054        OspTree *mOspTree;
1055
1056        HierarchyManager *mHierarchyManager;
1057};
1058
1059
1060/*class ViewCellsManagerFactory
1061{
1062public:
1063        ViewCellsManager *Create(const string mName);
1064};*/
1065
1066}
1067
1068#endif
Note: See TracBrowser for help on using the repository browser.