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

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