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

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