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

Revision 1545, 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 here
57 
58};
59
60
61/**     Manages different higher order operations on the view cells.
62*/
63class ViewCellsManager
64{
65public:
66        struct PvsStatistics
67        {
68                int minPvs;
69                int maxPvs;
70                float avgPvs;
71                int viewcells;
72        };
73
74        /// view cell container types
75        enum {BSP, KD, VSP_KD, VSP_BSP, VSP_OSP};
76 
77        /// render cost evaluation type
78        enum {PER_OBJECT, PER_TRIANGLE};
79
80        friend class X3dViewCellsParseHandlers;
81        /** Default constructor.
82        */
83        ViewCellsManager(ViewCellsTree *viewCellsTree);
84
85        virtual ~ViewCellsManager();
86
87        /** Constructs view cells container taking a preprocessor
88                @returns the output rays (if not null)
89        */
90        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
91
92        /** Constructs view cell container with a given number of samples.
93        */
94        virtual int ConstructSubdivision(
95                const ObjectContainer &objects,
96                const VssRayContainer &rays) = 0;
97
98        /** Computes sample contributions of the rays to the view cells PVS.
99       
100                @param rays bundle of rays used to find intersections with view cells and
101                adding the contribution
102                @param addRays true if rays should be added to the PVSs of the viewcells they
103                intersect
104                @param storeViewCells true if view cells should be stored in the ray
105        */
106        float ComputeSampleContributions(const VssRayContainer &rays,
107                                                                         const bool addContributions,
108                                                                         const bool storeViewCells);
109
110        /** Add sample contributions to the viewcells they intersect
111        */
112        void AddSampleContributions(const VssRayContainer &rays);
113 
114
115        /** Computes sample contribution of a simgle ray to the view cells PVS.
116                @param ray finds intersections with view cells and holds the contribution
117                @param castRay true if ray should be cast to gain the information, false if ray
118                is already holding the information and need not be recast.
119         
120                @returns number of sample contributions
121        */
122        virtual float ComputeSampleContribution(VssRay &ray,
123                                                                                        const bool addRays,
124                                                                                        const bool storeViewCells);
125
126        virtual void AddSampleContributions(VssRay &ray);
127
128        /** Prints out statistics of the view cells.
129        */
130        virtual void PrintStatistics(ostream &s) const;
131
132        /** Post processes view cells givemŽa number of rays.
133        */
134        virtual int PostProcess(const ObjectContainer &objects,
135                                                        const VssRayContainer &rays) = 0;
136
137        /** Show visualization of the view cells.
138        */
139        virtual void Visualize(const ObjectContainer &objects,
140                                                   const VssRayContainer &sampleRays) = 0;
141
142        /** type of the view cell container.
143        */
144        virtual int GetType() const = 0;
145
146        /** Load the input viewcells. The input viewcells should be given as a collection
147                of meshes. Each mesh is assume to form a bounded polyhedron
148                defining the interior of the viewcell. The view cells manager
149                is responsible for building a hierarchy over these view cells.
150               
151                @param filename file to load
152                @return true on success
153    */
154    virtual bool LoadViewCellsGeometry(const string filename, const bool extrudeBaseTriangle);
155       
156        /** Merges two view cells.
157                @note the piercing rays of the front and back will be ordered   
158                @returns new view cell based on the merging.
159        */
160        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
161
162        /** Merges a container of view cells.
163                @returns new view cell based on the merging.
164        */
165        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
166       
167        /** Generates view cell of the type specified by this manager
168        */
169        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
170
171        /** Constructs view cell from base triangle.
172                The view cell is extruded along the normal vector.
173                @param the base triangle
174                @param the height of the newly created view cell
175        */
176        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) 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 are 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        /** Returns a stats about the global pvs.
221        */
222        virtual void GetPvsStatistics(PvsStatistics &stat);
223
224        virtual void GetPrVS(const Vector3 &viewPoint, PrVs &prvs, const float filterWidth);
225 
226        /** Get a viewcell containing the specified point
227                @param active if the active or elementary view cell should be returned.
228        */
229        virtual ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const = 0;
230 
231        virtual void PrintPvsStatistics(ostream &s);
232
233        /** Updates pvs of the view cell hierarchy if necessary.
234        */
235        void UpdatePvs();
236
237        /** Returns probability that view point lies in one view cell.
238        */
239        virtual float GetProbability(ViewCell *viewCell) = 0;
240
241        /** Returns render cost of a single view cell given the render cost of an object.
242        */
243        float GetRendercost(ViewCell *viewCell) const;
244
245        /** Returns reference to container of loaded / generated view cells.
246        */
247        ViewCellContainer &GetViewCells();
248
249        /** Helper function used to split ray sets uniformly
250                into one that is currently used and the other that
251                is saved for later processing.
252                @param sourceRays the input ray set
253                @param maxSize the maximal number of rays that will be used
254                @param usedRays returns the used ray set
255                @param savedRays if not null, returns the saved ray set
256        */
257        void GetRaySets(const VssRayContainer &sourceRays,
258                                        const int maxSize,
259                                        VssRayContainer &usedRays,
260                                        VssRayContainer *savedRays = NULL) const;
261       
262        /** Returns accumulated area of all view cells.
263        */
264        float GetAccVcArea();
265
266        /** Returns area of one view cell.
267        */
268        virtual float GetArea(ViewCell *viewCell) const;
269
270        /** Returns volume of view cell.
271        */
272        virtual float GetVolume(ViewCell *viewCell) const;
273
274        /** Sets the current renderer mainly for view cells statistics.
275        */
276        void SetRenderer(Renderer *renderer);
277
278        /** Computes a (random) view point in the valid view space.
279                @returns true if valid view point was found
280        */
281        virtual bool GetViewPoint(Vector3 &viewPoint) const;
282
283        /** Returns true if this view point is in the valid view space.
284        */
285        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
286
287        /** Sets a view space boundary.
288        */
289        void SetViewSpaceBox(const AxisAlignedBox3 &box);
290
291        AxisAlignedBox3 GetViewSpaceBox() const;
292
293        /** Creates mesh for this view cell.
294        */
295        virtual void CreateMesh(ViewCell *vc) = NULL;
296
297        /** Writes view cells to disc.
298        */
299        virtual bool ExportViewCells(const string filename,
300                                                                 const bool exportPvs,
301                                                                 const ObjectContainer &objects);
302
303        /** Casts beam to collect view cells.
304        */
305        virtual int CastBeam(Beam &beam);
306
307        /** Checks if view cell is considered as valid.
308        */
309        virtual bool CheckValidity(ViewCell *vc,
310                                                           int minPvsSize,
311                                                           int maxPvsSize) const;
312
313       
314        /** Sets validity of view cell
315        */
316        virtual void SetValidity(ViewCell *vc,
317                                                         int minPvsSize,
318                                                         int maxPvsSize) const;
319
320        /** sets validy of all viewcells
321        */
322        virtual void SetValidity(int minPvsSize,
323                                                         int maxPvsSize) const;
324
325
326        /** set valid viewcells in the range of pvs. sorts the viewcells
327          according to the pvs and then pickups those in the ranges
328          */
329        void SetValidityPercentage(const float minValid, const float maxValid);
330
331        /** Returns number of valid view cells.
332        */
333    int CountValidViewcells() const;
334
335        /** Returns maximal allowed pvs size.
336        */
337        int GetMaxPvsSize() const;
338
339        /** Returns maximal allowed pvs size.
340        */
341        int GetMinPvsSize() const;
342        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
343                where pvs is still considered valid.
344        */
345        float GetMaxPvsRatio() const;
346        /** Exports view cell geometry.
347        */
348        virtual void ExportViewCellGeometry(
349                Exporter *exporter,
350                ViewCell *vc,
351                const AxisAlignedBox3 *box,
352                const AxisAlignedPlane *clipPlane = NULL
353                ) const = 0;
354
355        /** Brings the view cells into their final state, computes meshes and volume.
356        */
357        virtual void FinalizeViewCells(const bool createMesh);
358
359        /** Evaluates statistics values on view cells.
360        */
361        void EvaluateRenderStatistics(float &totalRenderCost,
362                                                                  float &expectedRenderCost,
363                                                                  float &deviation,
364                                                                  float &variance,
365                                                                  int &totalPvs,
366                                                                  float &avgRenderCost);
367
368
369        /** Returns hierarchy of the view cells.
370        */
371        ViewCellsTree *GetViewCellsTree();
372
373        /** Collect candidates for the view cell merge.
374        */
375        virtual void CollectMergeCandidates(const VssRayContainer &rays,
376                                                                                vector<MergeCandidate> &candidates);
377
378        /** Collects n view cells and stores it as the active view cells.
379        */
380        void CollectViewCells(const int n);
381
382        /** Sets current view cells set to active, i.e., the sampling is done in this view cell set.
383        */
384        void SetViewCellsActive();
385
386        /** Evaluates render cost statistics of current view cell hierarchy.
387        */
388        virtual void EvalViewCellPartition();
389
390        /** Sets maximal size of a view cell filter.
391        */
392        void SetMaxFilterSize(const int size);
393
394        /** Returns maximal filter size.
395        */
396        int GetMaxFilterSize() const;
397
398        /** Deletes interior nodes from the tree which have negative merge cost set (local merge).
399        */ 
400        void DeleteLocalMergeTree(ViewCell *vc) const;
401 
402        /** Evaluautes histogram for a given number of view cells.
403        */
404        void EvalViewCellHistogram(const string filename, const int nViewCells);
405
406        /** Evaluautes histogram for a given number of view cells.
407        */
408        void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
409
410        /** Evaluates the render cost of a view cell.
411        */
412        float EvalRenderCost(Intersectable *obj) const;
413   
414        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
415                of the hierarchy.
416        */
417        void UpdateScalarPvsSize(ViewCell *vc, const int pvsSize, const int entriesInPvs) const;
418
419        /** Returns bounding box of a view cell.
420        */
421        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
422
423        /** Exports bounding boxes of objects to file.
424        */
425        bool ExportBoundingBoxes(const string filename, const ObjectContainer &objects) const;
426   
427        /** Load the bounding boxes into the container.
428        */
429        bool LoadBoundingBoxes(const string filename, IndexedBoundingBoxContainer &boxes) const;
430
431        /** Returns true if pvs should be exported together with the view cells.
432        */
433        bool GetExportPvs() const;
434
435        /////////////////////////////
436        // static members
437       
438        /** Loads view cells from file. The view cells manager is created with
439                respect to the loaded view cells.
440
441                @param filename the filename of the view cells
442                @param objects the scene objects
443                @param finalizeViewCells if the view cells should be post processed, i.e.
444                        a mesh is created representing the geometry
445                @param bconverter a conversion routine working with the similarities of bounding
446                boxes: if there is a certain similarity of overlap between bounding boxes, two tested
447                candidate objects are considered to be the same objects
448                @returns the view cells manager if loading was successful, false otherwise
449        */
450        static ViewCellsManager *LoadViewCells(const string &filename,
451                                                                                   ObjectContainer *objects,
452                                                                                   const bool finalizeViewCells,
453                                                                                   BoundingBoxConverter *bconverter = NULL);
454
455
456        ////////////////////////////////////////////////////////7
457        // visiblity filter options
458
459        // TODO: write own visibiltiy filter class
460        void ApplyFilter(KdTree *kdTree,
461                                         const float viewSpaceFilterSize,
462                                         const float spatialFilterSize);
463
464        void ApplySpatialFilter(KdTree *kdTree,
465                                                        const float spatialFilterSize,
466                                                        ObjectPvs &pvs);
467
468         void ApplyFilter(ViewCell *viewCell,
469                                          KdTree *kdTree,
470                                          const float viewSpaceFilterSize,
471                                          const float spatialFilterSize,
472                                          ObjectPvs &pvs);
473
474        float GetFilterWidth();
475
476        float GetAbsFilterWidth();
477
478        /** Returns the bounding box of filter width.
479        */
480        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
481
482        //////////////////////////////////////////////////////////////////
483
484
485        /** Returns true if the view cell is equivalent to a
486                node of the spatial hierarchy. This function can be used for merged
487                view cell.
488                e.g. to see if the spatial tree can be reduced on this location
489                note: not implemented
490        */
491        virtual bool EqualToSpatialNode(ViewCell *viewCell) const;
492
493        /** If true, the kd nodes are stored instead of the object pvs.
494        */
495        void SetStoreKdPvs(const bool storeKdPvs);
496
497        /** Returns true if the kd nodes are stored instead of the object pvs.
498        **/
499        bool GetStoreKdPVs() const;
500
501        virtual bool AddSampleToPvs(
502                Intersectable *obj,
503                const Vector3 &hitPoint,
504                ViewCell *vc,
505                const float pdf,
506                float &contribution) const;
507
508protected:
509
510        /** Intersects box with the tree and returns the number of intersected boxes.
511                @returns number of view cells found
512        */
513        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
514                                                                                ViewCellContainer &viewCells) const;
515
516        /** Tests the visibility filter functionality.
517        */
518        virtual void TestFilter(const ObjectContainer &objects) {};
519
520        /** If the view cells tree was already constructed or not.
521        */
522        bool ViewCellsTreeConstructed() const;
523
524        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
525        */
526        int CastPassSamples(const int samplesPerPass,
527                                                const int sampleType,
528                                                VssRayContainer &vssRays) const;
529
530        /** Parse the options from the environment file.
531        */
532        void ParseEnvironment();
533
534        /** Creates unique view cell ids.
535        */
536        void CreateUniqueViewCellIds();
537
538        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
539        */
540        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
541
542        /** Recollects view cells and resets statistics.
543        */
544        void ResetViewCells();
545       
546        /** Collects the view cells in the view cell container.
547        */
548        virtual void CollectViewCells() = 0;
549       
550        /** Evaluates view cells statistics and stores it in
551                mViewCellsStatistics.
552        */
553        void EvaluateViewCellsStats();
554
555
556        ///////////////////////
557        //-- helper functions for view cell visualization
558
559        /** Exports the view cell partition.
560        */
561        void ExportViewCellsForViz(
562                Exporter *exporter,
563                const AxisAlignedBox3 *box,
564                const AxisAlignedPlane *clipPlane = NULL) const;
565
566        /** Sets exporter color.
567        */
568        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const;
569       
570        /** Creates meshes from the view cells.
571        */
572        void CreateViewCellMeshes();
573
574    /** Creates clip plane for visualization.
575        */
576        void CreateClipPlane();
577
578        AxisAlignedPlane *GetClipPlane();
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        void ExportMergedViewCells(const ObjectContainer &objects);
781};
782
783
784/**
785        Manages different higher order operations on the KD type view cells.
786*/
787class KdViewCellsManager: public ViewCellsManager
788{
789
790public:
791
792        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
793
794        int ConstructSubdivision(const ObjectContainer &objects,
795                                  const VssRayContainer &rays);
796
797        int CastLineSegment(const Vector3 &origin,
798                                                const Vector3 &termination,
799                                                ViewCellContainer &viewcells);
800
801        int PostProcess(const ObjectContainer &objects,
802                                        const VssRayContainer &rays);
803
804        void Visualize(const ObjectContainer &objects,
805                                   const VssRayContainer &sampleRays);
806
807        int GetType() const;
808
809        bool ViewCellsConstructed() const;
810
811        ViewCell *GenerateViewCell(Mesh *mesh) const;
812
813        /** Prints out statistics of this approach.
814        */
815        //  virtual void PrintStatistics(ostream &s) const;
816        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
817        { return NULL; }
818
819        float GetProbability(ViewCell *viewCell);
820       
821        void CreateMesh(ViewCell *vc);
822
823        void ExportViewCellGeometry(
824                Exporter *exporter,
825                ViewCell *vc,
826                const AxisAlignedBox3 *box,
827                const AxisAlignedPlane *clipPlane = NULL
828                ) const;
829
830
831        void CollectMergeCandidates(const VssRayContainer &rays,
832                                                                vector<MergeCandidate> &candidates);
833
834
835protected:
836
837        /** Collects view cells from a hierarchy.
838        */
839        void CollectViewCells();
840
841        KdNode *GetNodeForPvs(KdLeaf *leaf);
842
843
844        /// the BSP tree.
845        KdTree *mKdTree;
846
847        /// depth of the KD tree nodes with represent the view cells
848        int mKdPvsDepth;
849
850
851};
852
853/**
854        Manages different higher order operations on the view cells.
855*/
856class VspBspViewCellsManager: public ViewCellsManager
857{
858
859public:
860
861        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
862        ~VspBspViewCellsManager();
863
864        int ConstructSubdivision(const ObjectContainer &objects,
865                                  const VssRayContainer &rays);
866
867        int PostProcess(const ObjectContainer &objects,
868                                        const VssRayContainer &rays);
869
870        void Visualize(const ObjectContainer &objects,
871                                   const VssRayContainer &sampleRays);
872
873        int GetType() const;
874       
875        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
876
877        bool ViewCellsConstructed() const;
878
879       
880        int CastLineSegment(const Vector3 &origin,
881                                                const Vector3 &termination,
882                                                ViewCellContainer &viewcells);
883
884        float GetProbability(ViewCell *viewCell);
885       
886        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
887
888        bool GetViewPoint(Vector3 &viewPoint) const;
889
890        bool ViewPointValid(const Vector3 &viewPoint) const;
891
892        void CreateMesh(ViewCell *vc);
893
894        bool ExportViewCells(
895                const string filename,
896                const bool exportPvs,
897                const ObjectContainer &objects);
898
899        int CastBeam(Beam &beam);
900       
901        void ExportViewCellGeometry(
902                Exporter *exporter,
903                ViewCell *vc,
904                const AxisAlignedBox3 *box,
905                const AxisAlignedPlane *clipPlane = NULL
906                ) const;
907
908
909        void Finalize(ViewCell *viewCell, const bool createMesh);
910
911        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
912
913        /** Returns true if this view cell is equivavalent to a spatial node.
914        */
915        bool EqualToSpatialNode(ViewCell *viewCell) const;
916
917
918protected:
919
920        int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
921       
922        /** Returns node of the spatial hierarchy corresponding to the view cell
923                if such a node exists.
924        */
925        BspNode *GetSpatialNode(ViewCell *viewCell) const;
926
927        /** Merges view cells according to some criteria
928        */
929        void MergeViewCells(const VssRayContainer &rays,
930                                                const ObjectContainer &objects);
931       
932        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
933
934        void CollectViewCells();
935
936        /** Returns maximal depth difference of view cell
937                leaves in tree.
938        */
939        int GetMaxTreeDiff(ViewCell *vc) const;
940
941        /** Prepare view cells for use after loading them from disc.
942        */
943        void PrepareLoadedViewCells();
944
945        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
946        */
947        ViewCell *ConstructSpatialMergeTree(BspNode *root);
948
949        /// HACK for testing visibility filter functionality
950        void TestFilter(const ObjectContainer &objects);
951
952        /** Visualization of the pvs difference to exact visubility using
953                from point queries.
954        */
955        void VisualizeWithFromPointQueries();
956
957        /** Evaluate from point queries for the current scene.
958        */
959        void EvalFromPointQueries();
960
961        /** Exports visualization of the BSP splits.
962        */
963        void ExportSplits(const ObjectContainer &objects,
964                                          const VssRayContainer &rays);
965
966        /** Exports visualization of the BSP PVS.
967        */
968        void ExportBspPvs(const ObjectContainer &objects,
969                                          const VssRayContainer &rays);
970
971
972        /// the view space partition BSP tree.
973        VspBspTree *mVspBspTree;
974
975
976private:
977
978        /** test if subdivision is valid in terms of volume / area.
979        */
980        void TestSubdivision();
981};
982
983#define TEST_EVALUATION 0
984
985/**
986        Manages different higher order operations on the view cells.
987*/
988class VspOspViewCellsManager: public ViewCellsManager
989{
990
991public:
992
993        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
994       
995        ~VspOspViewCellsManager();
996
997        int ConstructSubdivision(const ObjectContainer &objects,
998                                                         const VssRayContainer &rays);
999
1000        int PostProcess(const ObjectContainer &objects,
1001                                        const VssRayContainer &rays);
1002
1003        void Visualize(const ObjectContainer &objects,
1004                                   const VssRayContainer &sampleRays);
1005
1006        int GetType() const;
1007       
1008        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1009
1010        bool ViewCellsConstructed() const;
1011
1012       
1013        int CastLineSegment(const Vector3 &origin,
1014                                                const Vector3 &termination,
1015                                                ViewCellContainer &viewcells);
1016
1017        float GetProbability(ViewCell *viewCell);
1018       
1019        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1020
1021        bool GetViewPoint(Vector3 &viewPoint) const;
1022
1023        bool ViewPointValid(const Vector3 &viewPoint) const;
1024
1025        void CreateMesh(ViewCell *vc);
1026
1027        bool ExportViewCells(const string filename,
1028                                                 const bool exportPvs,
1029                                                 const ObjectContainer &objects);
1030
1031        int CastBeam(Beam &beam);
1032
1033        void Finalize(ViewCell *viewCell, const bool createMesh);
1034
1035        /** Stores sample contribution for kd cells or objects.
1036        */
1037        virtual float ComputeSampleContribution(VssRay &ray,
1038                                                                                        const bool addRays,
1039                                                                                        const bool storeViewCells);
1040
1041        ViewCellsManager *LoadViewCells(const string &filename,
1042                ObjectContainer *objects,
1043                const bool finalizeViewCells,
1044                BoundingBoxConverter *bconverter);
1045
1046        bool AddSampleToPvs(
1047                Intersectable *obj,
1048                const Vector3 &hitPoint,
1049                ViewCell *vc,
1050                const float pdf,
1051        float &contribution) const;
1052
1053protected:
1054
1055#if TEST_EVALUATION
1056        virtual void EvalViewCellPartition();
1057#endif
1058
1059        /** Exports view cell geometry.
1060        */
1061        void ExportViewCellGeometry(
1062                Exporter *exporter,
1063                ViewCell *vc,
1064                const AxisAlignedBox3 *box,
1065                const AxisAlignedPlane *clipPlane = NULL
1066                ) const;
1067
1068        int ComputeBoxIntersections(
1069                const AxisAlignedBox3 &box,
1070                ViewCellContainer &viewCells) const;
1071       
1072        void CollectViewCells();
1073
1074        /** Prepare view cells for use after loading them from disc.
1075        */
1076        void PrepareLoadedViewCells();
1077
1078        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1079        */
1080        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1081
1082        /** Exports visualization of the PVS.
1083        */
1084        void ExportPvs(
1085                const ObjectContainer &objects,
1086                const VssRayContainer &rays);
1087
1088
1089        /////////////////////////////////////////
1090
1091        HierarchyManager *mHierarchyManager;
1092};
1093
1094
1095}
1096
1097#endif
Note: See TracBrowser for help on using the repository browser.