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

Revision 2224, 36.1 KB checked in by mattausch, 17 years ago (diff)
RevLine 
[440]1#ifndef _ViewCellsManager_H__
2#define _ViewCellsManager_H__
3
4#include "Ray.h"
[2105]5#include "SimpleRay.h"
[440]6#include "VssRay.h"
7#include "Containers.h"
[475]8#include "ViewCell.h"
[440]9
[2176]10
[860]11namespace GtpVisibilityPreprocessor {
12
[440]13class ViewCell;
14class Intersectable;
15class RenderSimulator;
[480]16class Renderer;
[440]17class Mesh;
18struct Triangle3;
19class SimulationStatistics;
20class BspTree;
21class KdTree;
[1006]22class VspOspTree;
[440]23class VspBspTree;
24class KdNode;
25class KdLeaf;
26class AxisAlignedBox3;
[448]27class BspLeaf;
[475]28class ViewCellsStatistics;
[482]29class Exporter;
[532]30class Beam;
[570]31class Preprocessor;
[580]32class ViewCellsTree;
33class MergeCandidate;
[931]34class BoundingBoxConverter;
[1021]35class VspTree;
36class OspTree;
37class VspNode;
[1027]38class HierarchyManager;
[1259]39class BvHierarchy;
[1264]40class ViewCellsTree;
[1786]41class ViewCell;
[1903]42class MixtureDistribution;
[1047]43struct AxisAlignedPlane;
[475]44struct BspRay;
[2116]45class BspTree;
[475]46
[1903]47
[2015]48
[2116]49struct PvsFilterStatistics
50{
[1773]51        PvsFilterStatistics():
[2116]52        mAvgFilterRadius(0.0f), mLocalFilterCount(0), mGlobalFilterCount(0) {}
53       
[1761]54        float mAvgFilterRadius;
55        int mLocalFilterCount;
56        int mGlobalFilterCount;
[2116]57};
58
[1145]59/** Probably Visible Set
60*/
[697]61class PrVs
62{
63public:
[2116]64        /// root of view cells tree
65        ViewCell *mViewCell;
[704]66
[2116]67        // input parameter is the render budget for the PrVs
68        float mRenderBudget;
[677]69
[2116]70        // some characteristic values could be stored as well
[677]71};
72
[1786]73
74class LocalMergeNode
75{
76public:
77        virtual ~LocalMergeNode() {};
78        virtual bool IsLeaf() const = 0;
79};
80
[2017]81typedef pair<ViewCell *, SimpleRayContainer> ViewCellPoints;
[1786]82
[850]83/**     Manages different higher order operations on the view cells.
[440]84*/
85class ViewCellsManager
86{
87public:
[1983]88
[2116]89        struct PerViewCellStat
90        {
91                float pvsSize;
92                float relPvsIncrease;
[1983]93        };
94
[2116]95        struct SamplesStatistics
[728]96        {
[2116]97                int mRays;
98                int mViewCells;
99                int mContributingRays;
100                int mPvsContributions;
101                void Reset()
102                {
103                        mRays = 0;
104                        mViewCells = 0;
105                        mContributingRays = 0;
106                        mPvsContributions = 0;
107                };
108        };
[1974]109
[2116]110        struct PvsStatistics
111        {
112                float minPvs;
113                float maxPvs;
114                float avgPvs;
[2205]115                float renderCost;
[2116]116                float avgPvsEntries;
117
118                float avgFilteredPvs;
119                float avgFilteredPvsEntries;
120
121                float avgFilterContribution;
122                float avgFilterRadius;
123                float avgFilterRatio;
124                float avgRelPvsIncrease;
125                float devRelPvsIncrease;
126                int viewcells;
[2224]127
128                float mem;
[1545]129        };
[1942]130 
[728]131        /// view cell container types
[1021]132        enum {BSP, KD, VSP_KD, VSP_BSP, VSP_OSP};
[466]133 
[931]134        /// render cost evaluation type
[728]135        enum {PER_OBJECT, PER_TRIANGLE};
136
[1545]137        friend class X3dViewCellsParseHandlers;
[1709]138
[1004]139        /** Default constructor.
[440]140        */
[1264]141        ViewCellsManager(ViewCellsTree *viewCellsTree);
[440]142
[462]143        virtual ~ViewCellsManager();
[440]144
[527]145        /** Constructs view cells container taking a preprocessor
[570]146                @returns the output rays (if not null)
147        */
148        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
[527]149
[440]150        /** Constructs view cell container with a given number of samples.
151        */
[1709]152        virtual int ConstructSubdivision(const ObjectContainer &objects,
153                                                                         const VssRayContainer &rays) = 0;
[440]154
[441]155        /** Computes sample contributions of the rays to the view cells PVS.
156       
157                @param rays bundle of rays used to find intersections with view cells and
158                adding the contribution
[563]159                @param addRays true if rays should be added to the PVSs of the viewcells they
[574]160                intersect
161                @param storeViewCells true if view cells should be stored in the ray
162        */
[697]163        float ComputeSampleContributions(const VssRayContainer &rays,
[1155]164                                                                         const bool addContributions,
[1990]165                                                                         const bool storeViewCells,
166                                                                         const bool useHitObject = false);
[440]167
[475]168        /** Computes sample contribution of a simgle ray to the view cells PVS.
169                @param ray finds intersections with view cells and holds the contribution
[1570]170                @param addSample if sample should be added to the pvs
171                 
[475]172                @returns number of sample contributions
173        */
[1159]174        virtual float ComputeSampleContribution(VssRay &ray,
[1981]175                                                                                        const bool addContributions,
[1990]176                                                                                        const bool storeViewCells,
177                                                                                        const bool useHitObject = false);
[563]178
[1977]179        /** Compute sample contribution only for current view cell.
180        */
181        virtual float ComputeSampleContribution(VssRay &ray,
[1981]182                                                                                        const bool addContributions,
[1990]183                                                                                        ViewCell *currentViewCell,
184                                                                                        const bool useHitObject = false);
[563]185
[452]186        /** Prints out statistics of the view cells.
187        */
[2176]188        virtual void PrintStatistics(std::ostream &s) const;
[452]189
[440]190        /** Post processes view cells givemŽa number of rays.
191        */
[475]192        virtual int PostProcess(const ObjectContainer &objects,
193                                                        const VssRayContainer &rays) = 0;
[440]194
195        /** Show visualization of the view cells.
196        */
197        virtual void Visualize(const ObjectContainer &objects,
[466]198                                                   const VssRayContainer &sampleRays) = 0;
[440]199
[1982]200        /** collect objects intersecting a given spatial box.
201        */
202        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
[1737]203
[440]204        /** type of the view cell container.
205        */
206        virtual int GetType() const = 0;
207
[463]208        /** Load the input viewcells. The input viewcells should be given as a collection
[1545]209                of meshes. Each mesh is assume to form a bounded polyhedron
210                defining the interior of the viewcell. The view cells manager
211                is responsible for building a hierarchy over these view cells.
[463]212               
213                @param filename file to load
214                @return true on success
[1982]215        */
216        virtual bool LoadViewCellsGeometry(const string filename, const bool extrudeBaseTriangle);
[577]217       
[440]218        /** Merges two view cells.
219                @note the piercing rays of the front and back will be ordered   
220                @returns new view cell based on the merging.
221        */
[582]222        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
223
224        /** Merges a container of view cells.
225                @returns new view cell based on the merging.
226        */
227        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
[440]228       
[1545]229        /** Generates view cell of the type specified by this manager
[440]230        */
[580]231        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
[440]232
[1545]233        /** Constructs view cell from base triangle.
234                The view cell is extruded along the normal vector.
235                @param the base triangle
236                @param the height of the newly created view cell
[440]237        */
[1545]238        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
[440]239
[1982]240        virtual Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
[1743]241
[440]242        /** Sets maximal number of samples used for the
243                construction of the view cells.
244        */
245        void SetVisualizationSamples(const int visSamples);
246
[574]247        /** Sets maximal number of samples used for the construction of the view cells.
[440]248        */
249        void SetConstructionSamples(const int constructionSamples);
250
[574]251        /** Sets maximal number of samples used for the visualization of the view cells.
252        */
253        void SetInitialSamples(const int initialSamples);
254
[440]255        /** Sets maximal number of samples used for the post processing of the view cells.
256        */
257        void SetPostProcessSamples(const int postProcessingSamples);
258
259        /** See set.
260        */
261        int GetVisualizationSamples() const;
262
263        /** See set.
264        */
265        int GetConstructionSamples() const;
266
267        /** See set.
268        */
269        int GetPostProcessSamples() const;
270
[1002]271        /** Returns true if view cells are already constructed.
[440]272        */
273        virtual bool ViewCellsConstructed() const = 0;
[441]274
[477]275        /** cast line segment to get a list of unique viewcells which are intersected
276                by this line segment
277        */
[1977]278        virtual int CastLineSegment(const Vector3 &origin,
[466]279                                                          const Vector3 &termination,
280                                                          ViewCellContainer &viewcells
281                                                          ) = 0;
282
[1977]283        /** Tests if this line segment intersects a particular view cell.
284        */
285        virtual bool LineSegmentIntersects(const Vector3 &origin,
286                                                                           const Vector3 &termination,
287                                                                           ViewCell *viewCell) = 0;
288
[1294]289        /** Returns a stats about the global pvs.
290        */
[477]291        virtual void GetPvsStatistics(PvsStatistics &stat);
[466]292
[904]293        virtual void GetPrVS(const Vector3 &viewPoint, PrVs &prvs, const float filterWidth);
[677]294 
[2111]295        /** Get a viewcell containing the specified view point.
[879]296                @param active if the active or elementary view cell should be returned.
[697]297        */
[879]298        virtual ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const = 0;
[492]299 
[1982]300        ViewCell *GetViewCell(const int idx) const {return mViewCells[idx];}
301
[2176]302        virtual void PrintPvsStatistics(std::ostream &s);
[467]303
[719]304        /** Updates pvs of the view cell hierarchy if necessary.
305        */
306        void UpdatePvs();
307
[477]308        /** Returns probability that view point lies in one view cell.
309        */
310        virtual float GetProbability(ViewCell *viewCell) = 0;
[468]311
[477]312        /** Returns render cost of a single view cell given the render cost of an object.
313        */
[728]314        float GetRendercost(ViewCell *viewCell) const;
[468]315
[720]316        /** Returns reference to container of loaded / generated view cells.
[477]317        */
318        ViewCellContainer &GetViewCells();
[468]319
[485]320        /** Helper function used to split ray sets uniformly
321                into one that is currently used and the other that
322                is saved for later processing.
323                @param sourceRays the input ray set
324                @param maxSize the maximal number of rays that will be used
325                @param usedRays returns the used ray set
326                @param savedRays if not null, returns the saved ray set
[477]327        */
328        void GetRaySets(const VssRayContainer &sourceRays,
[485]329                                        const int maxSize,
330                                        VssRayContainer &usedRays,
331                                        VssRayContainer *savedRays = NULL) const;
332       
[480]333        /** Returns accumulated area of all view cells.
334        */
335        float GetAccVcArea();
[470]336
[480]337        /** Returns area of one view cell.
338        */
339        virtual float GetArea(ViewCell *viewCell) const;
[470]340
[551]341        /** Returns volume of view cell.
[480]342        */
343        virtual float GetVolume(ViewCell *viewCell) const;
[470]344
[480]345        /** Sets the current renderer mainly for view cells statistics.
346        */
347        void SetRenderer(Renderer *renderer);
348
[487]349        /** Computes a (random) view point in the valid view space.
350                @returns true if valid view point was found
351        */
352        virtual bool GetViewPoint(Vector3 &viewPoint) const;
[1867]353 
[1982]354        virtual bool GetViewPoint(Vector3 &viewPoint, const Vector3 &params) const;
[487]355
[490]356        /** Returns true if this view point is in the valid view space.
357        */
358        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
359
[487]360        /** Sets a view space boundary.
361        */
362        void SetViewSpaceBox(const AxisAlignedBox3 &box);
363
[519]364        AxisAlignedBox3 GetViewSpaceBox() const;
365
[503]366        /** Creates mesh for this view cell.
367        */
368        virtual void CreateMesh(ViewCell *vc) = NULL;
[487]369
[508]370        /** Writes view cells to disc.
371        */
[1027]372        virtual bool ExportViewCells(const string filename,
373                                                                 const bool exportPvs,
374                                                                 const ObjectContainer &objects);
[508]375
[532]376        /** Casts beam to collect view cells.
377        */
378        virtual int CastBeam(Beam &beam);
379
[547]380        /** Checks if view cell is considered as valid.
381        */
[561]382        virtual bool CheckValidity(ViewCell *vc,
[562]383                                                           int minPvsSize,
384                                                           int maxPvsSize) const;
[547]385
[2015]386
[2117]387        // resort pvss after a pass of the algorithm
388        void  SortViewCellPvs();
[2015]389
[2117]390        // map the ray intersection objects from triangles to high level objects...
391        void DeterminePvsObjects(VssRayContainer &rays,
392                                                         const bool useHitObjects = false);
[2021]393       
[562]394        /** Sets validity of view cell
395        */
396        virtual void SetValidity(ViewCell *vc,
397                                                         int minPvsSize,
398                                                         int maxPvsSize) const;
399
[710]400        /** sets validy of all viewcells
401        */
[810]402        virtual void SetValidity(int minPvsSize,
[569]403                                                         int maxPvsSize) const;
404
[609]405
[2117]406        /** Set valid viewcells in the range of pvs. sorts the viewcells
407                according to the pvs and then pickups those in the ranges
408        */
[710]409        void SetValidityPercentage(const float minValid, const float maxValid);
[569]410
[1545]411        /** Returns number of valid view cells.
412        */
[609]413    int CountValidViewcells() const;
414
[547]415        /** Returns maximal allowed pvs size.
416        */
417        int GetMaxPvsSize() const;
418
[562]419        /** Returns maximal allowed pvs size.
420        */
421        int GetMinPvsSize() const;
[2117]422
[561]423        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
424                where pvs is still considered valid.
425        */
426        float GetMaxPvsRatio() const;
[2117]427
[547]428        /** Exports view cell geometry.
429        */
[2117]430        virtual void ExportViewCellGeometry(Exporter *exporter,
431                                                                                ViewCell *vc,
432                                                                                const AxisAlignedBox3 *box,
433                                                                                const AxisAlignedPlane *clipPlane = NULL
434                                                                                ) const = 0;
[547]435
[860]436        /** Brings the view cells into their final state, computes meshes and volume.
437        */
[551]438        virtual void FinalizeViewCells(const bool createMesh);
439
[579]440        /** Evaluates statistics values on view cells.
441        */
[580]442        void EvaluateRenderStatistics(float &totalRenderCost,
443                                                                  float &expectedRenderCost,
444                                                                  float &deviation,
445                                                                  float &variance,
[1709]446                                                                  float &totalPvs,
[580]447                                                                  float &avgRenderCost);
[579]448
[580]449        /** Returns hierarchy of the view cells.
450        */
451        ViewCellsTree *GetViewCellsTree();
452
[1545]453        /** Collect candidates for the view cell merge.
454        */
[581]455        virtual void CollectMergeCandidates(const VssRayContainer &rays,
[1022]456                                                                                vector<MergeCandidate> &candidates);
[580]457
[1294]458        /** Collects n view cells and stores it as the active view cells.
459        */
[610]460        void CollectViewCells(const int n);
461
[1686]462        /** Sets current view cells set to active,
463                i.e., the sampling is done in this view cell set.
[660]464        */
465        void SetViewCellsActive();
[599]466
[1145]467        /** Evaluates render cost statistics of current view cell hierarchy.
[660]468        */
[1178]469        virtual void EvalViewCellPartition();
[660]470
[728]471        /** Sets maximal size of a view cell filter.
472        */
473        void SetMaxFilterSize(const int size);
[697]474
[840]475        /** Returns maximal filter size.
476        */
477        int GetMaxFilterSize() const;
[746]478
[1686]479        /** Deletes interior nodes from the tree which have negative
480                merge cost set (local merge).
[840]481        */ 
482        void DeleteLocalMergeTree(ViewCell *vc) const;
[713]483 
[728]484        /** Evaluautes histogram for a given number of view cells.
485        */
486        void EvalViewCellHistogram(const string filename, const int nViewCells);
487
[735]488        /** Evaluautes histogram for a given number of view cells.
489        */
[2117]490        void EvalViewCellHistogramForPvsSize(const string filename,
491                                                                                 const int nViewCells);
[735]492
[1919]493        void EvalViewCellHistogramForPvsSize(const string filename,
494                                                                                 ViewCellContainer &viewCells);
495
[728]496        /** Evaluates the render cost of a view cell.
497        */
[1703]498        static float EvalRenderCost(Intersectable *obj);
[1002]499   
500        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
501                of the hierarchy.
502        */
[2117]503        void UpdateScalarPvsSize(ViewCell *vc,
504                                                         const float pvsCost,
505                                                         const int entriesInPvs) const;
[728]506
[870]507        /** Returns bounding box of a view cell.
508        */
509        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
[859]510
[840]511        /** Exports bounding boxes of objects to file.
512        */
[2117]513        bool ExportBoundingBoxes(const string filename,
514                                                         const ObjectContainer &objects) const;
[931]515   
[850]516        /** Load the bounding boxes into the container.
517        */
[2117]518        bool LoadBoundingBoxes(const string filename,
519                                                  IndexedBoundingBoxContainer &boxes) const;
[840]520
[1002]521        /** Returns true if pvs should be exported together with the view cells.
522        */
523        bool GetExportPvs() const;
[870]524
[1786]525        /** Efficiently merges the view cells in the container.
526        */
[1787]527        void MergeViewCellsRecursivly(ObjectPvs &pvs,
528                                                                  const ViewCellContainer &viewCells) const;
[1786]529
[2117]530        /** Compress the view cells.
531        */
[1845]532        virtual void CompressViewCells();
533
[2017]534        ViewCell *GetViewCellById(const int id);
535
536        /** Returns number of view cells.
537        */
[1982]538        int GetNumViewCells() const;
539
[2017]540        bool GenerateRandomViewCells(ViewCellContainer &viewCells,
541                                                                 const int numViewCells);
542
543        bool GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
544                                                                 const int nViewCells,
545                                                                 const int nViewPoints);
546
547
548        bool GenerateViewPoints(ViewCell *viewCell,
549                                                        const int numViewPoints,
550                                                        SimpleRayContainer &viewPoints);
551
552        bool ImportRandomViewCells(const string &filename,
553                                                           vector<ViewCellPoints *> &viewCells);
554
[2048]555        bool ImportRandomViewCells(const string &filename);
[2017]556
557        bool ExportRandomViewCells(const string &filename,
558                                                           const vector<ViewCellPoints *> &viewCells);
559
[2048]560        bool ExportRandomViewCells(const string &filename);
561
[2017]562        bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
563
564        bool IsValidViewSpace(ViewCell *vc);
565
[2048]566
[2017]567        //////////////
[1582]568        // static members
569       
570        /** Loads view cells from file. The view cells manager is created with
571                respect to the loaded view cells.
[870]572
[1582]573                @param filename the filename of the view cells
574                @param objects the scene objects
[2115]575                @param finalizeViewCells if the view cells should be post processed, i.e.,
[2048]576                           a mesh is created representing the geometry
[1582]577                @param bconverter a conversion routine working with the similarities of bounding
[2048]578                           boxes: if there is a certain similarity of overlap between
579                           bounding boxes, two tested candidate objects are considered
580                           to be the same objects
[1582]581                @returns the view cells manager if loading was successful, false otherwise
582        */
583        static ViewCellsManager *LoadViewCells(const string &filename,
[2113]584                                                                                   ObjectContainer &pvsObjects,
585                                                                                   ObjectContainer &preprocessorObjects,
[1676]586                                                                                   bool finalizeViewCells = false,
[1582]587                                                                                   BoundingBoxConverter *bconverter = NULL);
[870]588
[2115]589        static ViewCellsManager *LoadViewCells(const string &filename,
590                                                                                   ObjectContainer &pvsObjects,
591                                                                                   bool finalizeViewCells = false,
592                                                                                   BoundingBoxConverter *bconverter = NULL);
[904]593
[2113]594        ///////////////////////
[931]595        // visiblity filter options
[1294]596
[931]597        // TODO: write own visibiltiy filter class
[2048]598        void ApplyFilter(KdTree *kdTree,
599                                         const float viewSpaceFilterSize,
600                                         const float spatialFilterSize);
[904]601
[2048]602        // new adaptive version of the filter
603        PvsFilterStatistics ApplyFilter2(ViewCell *viewCell,
604                                                                         const bool useViewSpaceFilter,
605                                                                         const float filterSize,
606                                                                         ObjectPvs &pvs,
607                                                                         vector<AxisAlignedBox3> *filteredBoxes = NULL);
[1715]608
[931]609        void ApplySpatialFilter(KdTree *kdTree,
610                                                        const float spatialFilterSize,
611                                                        ObjectPvs &pvs);
[480]612
[931]613         void ApplyFilter(ViewCell *viewCell,
614                                          KdTree *kdTree,
615                                          const float viewSpaceFilterSize,
616                                          const float spatialFilterSize,
617                                          ObjectPvs &pvs);
618
619        float GetFilterWidth();
620
621        float GetAbsFilterWidth();
622
[697]623        /** Returns the bounding box of filter width.
624        */
[2117]625        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint,
626                                                                  const float width) const;
[609]627
[2199]628
629
[1294]630        //////////////////////////////////////////////////////////////////
[979]631
[2048]632
[1155]633        /** If true, the kd nodes are stored instead of the object pvs.
634        */
635        void SetStoreKdPvs(const bool storeKdPvs);
636
637        /** Returns true if the kd nodes are stored instead of the object pvs.
638        **/
639        bool GetStoreKdPVs() const;
640
[1570]641        /** Exports single view cells for visualization.
642                @param objects the scene objects
643                @param limit the maximal number of output view cells
644                @param sortViewCells if the view cells should be sorted by pvs size
645                @param exportPvs if the pvs should also be exported
646                @param exportRays if sample rays should be exported as well
[1580]647                @param maxRays maximum number of rays to export
648                @param prefix the prefix for the output file
[1570]649                @param visRays additional rays
650        */
[1686]651        virtual void ExportSingleViewCells(const ObjectContainer &objects,
652                                                                           const int maxViewCells,
653                                                                           const bool sortViewCells,
654                                                                           const bool exportPvs,
655                                                                           const bool exportRays,
656                                                                           const int maxRays,
657                                                                           const string prefix,
658                                                                           VssRayContainer *visRays = NULL) = NULL;
[1570]659
[1932]660
[2048]661        void ResetPvs();
[1580]662
[1932]663        Preprocessor *GetPreprocessor() const {
664                return mPreprocessor;
665        }
[1580]666
[1932]667        void SetPreprocessor(Preprocessor *p) {
668                mPreprocessor = p;
669        }
[1608]670
[2048]671        vector<ViewCellPoints *> *GetViewCellPoints()
672        {
673                return &mViewCellPoints;
674        }
675
[1932]676        void UpdatePvsForEvaluation();
677
[931]678protected:
[1145]679
[1932]680        void ComputeViewCellContribution(ViewCell *viewCell,
681                                                                         VssRay &ray,
682                                                                         Intersectable *obj,
683                                                                         const Vector3 &pt,
684                                                                         const bool addRays);
685
[1787]686        void MergeViewCellsRecursivly(ObjectPvs &pvs,
687                                                                  const ViewCellContainer &viewCells,
688                                                                  const int leftIdx,
689                                                                  const int rightIdx) const;
690
[697]691        /** Intersects box with the tree and returns the number of intersected boxes.
[1932]692        @returns number of view cells found
[697]693        */
[710]694        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
695                                                                                ViewCellContainer &viewCells) const;
[697]696
[880]697        /** Tests the visibility filter functionality.
698        */
[697]699        virtual void TestFilter(const ObjectContainer &objects) {};
700
[931]701        /** If the view cells tree was already constructed or not.
[581]702        */
703        bool ViewCellsTreeConstructed() const;
704
[1294]705        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
706        */
[574]707        int CastPassSamples(const int samplesPerPass,
[1768]708                                                const vector<int> &strategies,
[574]709                                                VssRayContainer &vssRays) const;
[573]710
[2199]711        int CastEvaluationSamples(const int samplesPerPass,
712                                                          VssRayContainer &passSamples);// const;
[1903]713
[931]714        /** Parse the options from the environment file.
715        */
[482]716        void ParseEnvironment();
717
[508]718        /** Creates unique view cell ids.
719        */
720        void CreateUniqueViewCellIds();
[547]721
[551]722        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
723        */
724        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
725
[480]726        /** Recollects view cells and resets statistics.
727        */
728        void ResetViewCells();
[1932]729
[1707]730        /** Sets this view cell to active.
731        */
732        void SetViewCellActive(ViewCell *vc) const;
733
[480]734        /** Collects the view cells in the view cell container.
735        */
736        virtual void CollectViewCells() = 0;
[1932]737
[479]738        /** Evaluates view cells statistics and stores it in
[1932]739        mViewCellsStatistics.
[479]740        */
741        void EvaluateViewCellsStats();
[482]742
[1932]743
[1416]744        ///////////////////////
[482]745        //-- helper functions for view cell visualization
746
747        /** Exports the view cell partition.
748        */
[1686]749        void ExportViewCellsForViz(Exporter *exporter,
750                                                           const AxisAlignedBox3 *box,
[1760]751                                                           const bool colorCode,
[1932]752                                                           const AxisAlignedPlane *clipPlane) const;
[482]753
754        /** Sets exporter color.
755        */
[1773]756        virtual void ExportColor(Exporter *exporter,
757                                                         ViewCell *vc,
758                                                         const bool colorCode) const;
[1932]759
[1416]760        /** Creates meshes from the view cells.
761        */
762        void CreateViewCellMeshes();
[1545]763
[1932]764        /** Creates clip plane for visualization.
[1416]765        */
766        void CreateClipPlane();
[482]767
[1416]768        AxisAlignedPlane *GetClipPlane();
[1932]769
[1551]770        void ExportMergedViewCells(const ObjectContainer &objects);
771
[2048]772
[1416]773        ///////////////////////
774
[1002]775        /** Returns volume of the view space.
776        */
[557]777        virtual float GetViewSpaceVolume();
[2048]778
[1416]779        /** Prepares the view cells for sampling after loading them from disc.
[503]780        */
[577]781        virtual void PrepareLoadedViewCells() {};
[2048]782       
[704]783        /** Constructs local view cell merge hierarchy.
784        */
785        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
[1932]786                const ViewCellContainer &viewCells);
[704]787
[713]788        /** Constructs local view cell merge hierarchy based solely on similarity with the
[2048]789                current viewcell
[713]790        */
791        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
792                                                                           const ViewCellContainer &viewCells);
793 
[860]794        /** Updates pvs of all view cells for statistical evaluation after some more sampling
795        */
[1168]796        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
[752]797
[1679]798        virtual void ExportStats(const string &mergeStats);
[1582]799
[2048]800
[979]801        ////////////////////////////////////////////////
[752]802
[2048]803
[2199]804        ofstream mStats;
[1145]805        Preprocessor *mPreprocessor;
[1902]806       
[1145]807        /// if bounding boxes should be exported together with the view cells
[840]808        bool mExportBboxesForPvs;
[1902]809       
[1416]810        /// the clip plane for visualization
811        AxisAlignedPlane mClipPlaneForViz;
[1902]812       
[1416]813        /// if the visualization is using the clip plane
[660]814        bool mUseClipPlaneForViz;
[1902]815       
[480]816        /// Renders the view cells.
817        Renderer *mRenderer;
[1902]818       
[440]819        /// Loaded view cells
[469]820        ViewCellContainer mViewCells;
[1902]821       
[1416]822        /// the view cell hierarchy (i.e., the logical description of view cells)
[580]823        ViewCellsTree *mViewCellsTree;
[1414]824       
[1773]825        std::vector<int> mStrategies;
826
[1416]827        /** if the values in the view cell leaves and the interiors are up to date
828                this is meant for lazy storing of the pvs, where only a scalar indicating
829                pvs size is stored in interiors and not the pvs itself.
830        */
[719]831        bool mViewCellPvsIsUpdated;
[1002]832
[1416]833        /// maximum number of samples for the view cell construction
[440]834        int mConstructionSamples;
[574]835        int mSamplesPerPass;
836        int mInitialSamples;
[440]837        int mPostProcessSamples;
838        int mVisualizationSamples;
[441]839
[470]840        float mTotalAreaValid;
841        float mTotalArea;
[475]842
[547]843        int mMaxPvsSize;
[562]844        int mMinPvsSize;
[547]845        float mMaxPvsRatio;
846
[660]847        int mSamplingType;
[722]848        int mEvaluationSamplingType;
[600]849        int mNumActiveViewCells;
[586]850        bool mCompressViewCells;
851
[2048]852        vector<ViewCellPoints *> mViewCellPoints;
853
[1002]854        /// holds the current view cell statistics
[660]855        ViewCellsStatistics mCurrentViewCellsStats;
[2048]856
[487]857        /// the scene bounding box
[542]858        AxisAlignedBox3 mViewSpaceBox;
[1001]859       
[508]860        /// if view cells should be exported
861        bool mExportViewCells;
[547]862
[1002]863        // if only valid view cells should be considered for processing
[564]864        bool mOnlyValidViewCells;
[547]865
[580]866        /// if rays should be used to collect merge candidates
867        bool mUseRaysForMerge;
[1021]868
[1002]869        /// if there should be an additional merge step after the subdivision
[592]870        bool mMergeViewCells;
[697]871
872        /// the width of the box filter
873        float mFilterWidth;
[1570]874        /// Maximal size of the filter in terms of contributing view cells
[697]875        int mMaxFilterSize;
[1570]876       
[1576]877        // only for debugging
[1686]878        VssRayContainer storedRays;
879
[1902]880        bool mUseKdPvs;
[1974]881        bool mUseKdPvsAfterFiltering;
[1902]882
[1903]883        MixtureDistribution *mMixtureDistribution;
884
[2048]885
[1545]886        //////////////////
[487]887        //-- visualization options
888       
[1002]889        /// color code for view cells visualization
[1570]890        bool mShowVisualization;
[487]891        int mColorCode;
892        bool mExportGeometry;
893        bool mExportRays;
[517]894        bool mViewCellsFinished;
[664]895        bool mEvaluateViewCells;
[844]896
897        /// if pvs should be exported with view cells
898        bool mExportPvs;
[1155]899
[1703]900        static int sRenderCostEvaluationType;
901
902        /// if view cells geometry should be used from other sources
[1545]903        bool mUsePredefinedViewCells;
[1942]904
905 
906  vector<PerViewCellStat> mPerViewCellStat;
[1983]907  SamplesStatistics mSamplesStat;
[440]908};
909
[441]910
[1545]911/** Manages different higher order operations on the view cells.
[440]912*/
913class BspViewCellsManager: public ViewCellsManager
914{
915
916public:
[485]917        /** Constructor taking the bsp tree and the number of samples
918                used to construct the bsp tree.
919        */
[1264]920        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
[440]921
[477]922        ~BspViewCellsManager();
[1582]923
[574]924        int ConstructSubdivision(const ObjectContainer &objects,
[487]925                                  const VssRayContainer &rays);
[440]926
927        int PostProcess(const ObjectContainer &objects,
[466]928                                        const VssRayContainer &rays);
[440]929
930        void Visualize(const ObjectContainer &objects,
[466]931                                   const VssRayContainer &sampleRays);
[440]932
933        int GetType() const;
934       
935        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
936
937        bool ViewCellsConstructed() const;
938
[475]939        //void PrintStatistics(ostream &s) const;
[452]940
[468]941        int CastLineSegment(const Vector3 &origin,
942                                                const Vector3 &termination,
943                                                ViewCellContainer &viewcells);
944       
[1294]945        /** Returns the probability that the view point lies
946                in this view cells.
947        */
[471]948        float GetProbability(ViewCell *viewCell);
[466]949
[1294]950        /** Get a viewcell containing the specified point.
[879]951        */
952        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[492]953
[1294]954        /** Creates mesh for this view cell.
955        */
[503]956        void CreateMesh(ViewCell *vc);
957
[1416]958        void ExportViewCellGeometry(Exporter *exporter,
959                                                                ViewCell *vc,
960                                                                const AxisAlignedBox3 *box,
961                                                                const AxisAlignedPlane *clipPlane = NULL
962                                                                ) const;
[580]963       
[587]964        void CollectMergeCandidates(const VssRayContainer &rays,
965                                                                vector<MergeCandidate> &candidates);
[547]966
[587]967        void Finalize(ViewCell *viewCell, const bool createMesh);
968
[1416]969        bool ExportViewCells(const string filename,
970                                                 const bool exportPvs,
971                                                 const ObjectContainer &objects);
[590]972
[752]973        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
974        */
[650]975        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[610]976
[1686]977        void ExportSingleViewCells(const ObjectContainer &objects,
978                                                           const int maxViewCells,
979                                                           const bool sortViewCells,
980                                                           const bool exportPvs,
981                                                           const bool exportRays,
982                                                           const int maxRays,
983                                                           const string prefix,
984                                                           VssRayContainer *visRays = NULL);
[1545]985
[1977]986        bool LineSegmentIntersects(const Vector3 &origin,
987                                                           const Vector3 &termination,
988                                                           ViewCell *viewCell);
989
[440]990protected:
991
[480]992        void CollectViewCells();
[547]993       
[440]994        /// the BSP tree.
995        BspTree *mBspTree;
[475]996        vector<BspRay *> mBspRays;
[440]997
998private:
999
[1545]1000        /** Constructs a spatial merge tree only 2 levels deep.
1001        */
1002        ViewCell *ConstructDummyMergeTree(BspNode *root);
1003
[440]1004        /** Exports visualization of the BSP splits.
1005        */
[477]1006        void ExportSplits(const ObjectContainer &objects);
[440]1007
[1002]1008        /** test if subdivision is valid in terms of volume / area.
1009        */
[693]1010        void TestSubdivision();
[440]1011};
1012
[1545]1013
[440]1014/**
1015        Manages different higher order operations on the KD type view cells.
1016*/
1017class KdViewCellsManager: public ViewCellsManager
1018{
1019
1020public:
1021
[1264]1022        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
[440]1023
[574]1024        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1025                                                         const VssRayContainer &rays);
[440]1026
[478]1027        int CastLineSegment(const Vector3 &origin,
1028                                                const Vector3 &termination,
1029                                                ViewCellContainer &viewcells);
[440]1030
1031        int PostProcess(const ObjectContainer &objects,
[466]1032                                        const VssRayContainer &rays);
[440]1033
1034        void Visualize(const ObjectContainer &objects,
[466]1035                                   const VssRayContainer &sampleRays);
[440]1036
1037        int GetType() const;
1038
1039        bool ViewCellsConstructed() const;
1040
[580]1041        ViewCell *GenerateViewCell(Mesh *mesh) const;
[440]1042
[452]1043        /** Prints out statistics of this approach.
1044        */
[503]1045        //  virtual void PrintStatistics(ostream &s) const;
[1416]1046        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1047        { return NULL; }
[452]1048
[503]1049        float GetProbability(ViewCell *viewCell);
[609]1050       
[503]1051        void CreateMesh(ViewCell *vc);
1052
[1686]1053        void ExportViewCellGeometry(Exporter *exporter,
1054                                                                ViewCell *vc,
1055                                                                const AxisAlignedBox3 *box,
1056                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[547]1057
[1416]1058
[591]1059        void CollectMergeCandidates(const VssRayContainer &rays,
1060                                                                vector<MergeCandidate> &candidates);
[580]1061
[1686]1062        void ExportSingleViewCells(const ObjectContainer &objects,
1063                                                           const int maxViewCells,
1064                                                           const bool sortViewCells,
1065                                                           const bool exportPvs,
1066                                                           const bool exportRays,
1067                                                           const int maxRays,
1068                                                           const string prefix,
1069                                                           VssRayContainer *visRays = NULL);
[752]1070
[1977]1071        bool LineSegmentIntersects(const Vector3 &origin,
1072                                                           const Vector3 &termination,
1073                                                           ViewCell *viewCell);
1074
[440]1075protected:
1076
[580]1077        /** Collects view cells from a hierarchy.
1078        */
[480]1079        void CollectViewCells();
[580]1080
[480]1081        KdNode *GetNodeForPvs(KdLeaf *leaf);
[440]1082
[1570]1083        ////////////////////////////////////////
[440]1084
[480]1085        /// the BSP tree.
1086        KdTree *mKdTree;
[440]1087
[480]1088        /// depth of the KD tree nodes with represent the view cells
1089        int mKdPvsDepth;
[938]1090
1091
[440]1092};
1093
[1570]1094
1095/** Manages different higher order operations on the view cells.
[442]1096*/
1097class VspBspViewCellsManager: public ViewCellsManager
1098{
1099
1100public:
1101
[1264]1102        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
[475]1103        ~VspBspViewCellsManager();
[442]1104
[574]1105        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1106                                                         const VssRayContainer &rays);
[442]1107
1108        int PostProcess(const ObjectContainer &objects,
[466]1109                                        const VssRayContainer &rays);
[442]1110
1111        void Visualize(const ObjectContainer &objects,
[466]1112                                   const VssRayContainer &sampleRays);
[442]1113
1114        int GetType() const;
1115       
1116        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1117
1118        bool ViewCellsConstructed() const;
[468]1119       
1120        int CastLineSegment(const Vector3 &origin,
1121                                                const Vector3 &termination,
1122                                                ViewCellContainer &viewcells);
[452]1123
[471]1124        float GetProbability(ViewCell *viewCell);
[479]1125       
[879]1126        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[477]1127
[487]1128        bool GetViewPoint(Vector3 &viewPoint) const;
1129
[490]1130        bool ViewPointValid(const Vector3 &viewPoint) const;
[503]1131
1132        void CreateMesh(ViewCell *vc);
1133
[1686]1134        bool ExportViewCells(const string filename,
1135                                                 const bool exportPvs,
1136                                                 const ObjectContainer &objects);
[508]1137
[532]1138        int CastBeam(Beam &beam);
[1416]1139       
[1686]1140        void ExportViewCellGeometry(Exporter *exporter,
1141                                                                ViewCell *vc,
1142                                                                const AxisAlignedBox3 *box,
1143                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[532]1144
[547]1145
[555]1146        void Finalize(ViewCell *viewCell, const bool createMesh);
[551]1147
[1686]1148        void CollectMergeCandidates(const VssRayContainer &rays,
1149                                                                vector<MergeCandidate> &candidates);
[580]1150
[1686]1151        void ExportSingleViewCells(const ObjectContainer &objects,
1152                                                           const int maxViewCells,
1153                                                           const bool sortViewCells,
1154                                                           const bool exportPvs,
1155                                                           const bool exportRays,               
1156                                                           const int maxRays,
1157                                                           const string prefix,
1158                                                           VssRayContainer *visRays = NULL);
[1570]1159
[1920]1160       
[1977]1161        bool LineSegmentIntersects(const Vector3 &origin,
1162                                                           const Vector3 &termination,
1163                                                           ViewCell *viewCell);
1164
[442]1165protected:
[475]1166
[1686]1167        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1168                                                                ViewCellContainer &viewCells) const;
[651]1169       
[1021]1170        /** Merges view cells according to some criteria
[442]1171        */
[508]1172        void MergeViewCells(const VssRayContainer &rays,
1173                                                const ObjectContainer &objects);
[503]1174       
[564]1175        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
[442]1176
[480]1177        void CollectViewCells();
1178
[482]1179        /** Returns maximal depth difference of view cell
1180                leaves in tree.
1181        */
1182        int GetMaxTreeDiff(ViewCell *vc) const;
1183
[752]1184        /** Prepare view cells for use after loading them from disc.
1185        */
[577]1186        void PrepareLoadedViewCells();
1187
[752]1188        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1189        */
[650]1190        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[649]1191
[979]1192        /// HACK for testing visibility filter functionality
[697]1193        void TestFilter(const ObjectContainer &objects);
1194
[979]1195        /** Visualization of the pvs difference to exact visubility using
1196                from point queries.
1197        */
1198        void VisualizeWithFromPointQueries();
[697]1199
[991]1200        /** Evaluate from point queries for the current scene.
1201        */
1202        void EvalFromPointQueries();
[979]1203
[1021]1204        /** Exports visualization of the BSP splits.
1205        */
1206        void ExportSplits(const ObjectContainer &objects,
1207                                          const VssRayContainer &rays);
[991]1208
[1686]1209
1210        /////////////////////////
1211
[442]1212        /// the view space partition BSP tree.
1213        VspBspTree *mVspBspTree;
1214
[591]1215
[442]1216private:
1217
[1021]1218        /** test if subdivision is valid in terms of volume / area.
[442]1219        */
[1021]1220        void TestSubdivision();
1221};
[442]1222
[1662]1223#define TEST_EVALUATION 0
[1021]1224
1225/**
1226        Manages different higher order operations on the view cells.
1227*/
1228class VspOspViewCellsManager: public ViewCellsManager
1229{
[1740]1230        friend class ViewCellsParseHandlers;
[1977]1231
[1021]1232public:
1233
[2113]1234        VspOspViewCellsManager(ViewCellsTree *vcTree,
1235                                                   const string &hierarchyType);
[1278]1236       
[1021]1237        ~VspOspViewCellsManager();
1238
1239        int ConstructSubdivision(const ObjectContainer &objects,
1240                                                         const VssRayContainer &rays);
1241
1242        int PostProcess(const ObjectContainer &objects,
1243                                        const VssRayContainer &rays);
1244
[1686]1245        void Visualize(const ObjectContainer &objects,
[1021]1246                                   const VssRayContainer &sampleRays);
1247
1248        int GetType() const;
1249       
1250        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1251
[1977]1252        virtual Intersectable *GetIntersectable(
1253                const VssRay &ray, const bool isTermination) const;
[1743]1254
[1021]1255        bool ViewCellsConstructed() const;
1256
1257       
1258        int CastLineSegment(const Vector3 &origin,
1259                                                const Vector3 &termination,
1260                                                ViewCellContainer &viewcells);
1261
[1977]1262        bool LineSegmentIntersects(const Vector3 &origin,
1263                                                           const Vector3 &termination,
1264                                                           ViewCell *viewCell);
1265
[1021]1266        float GetProbability(ViewCell *viewCell);
1267       
[2113]1268        ViewCell *GetViewCell(const Vector3 &point,
1269                                                  const bool active = false) const;
[1021]1270
1271        bool GetViewPoint(Vector3 &viewPoint) const;
1272
1273        bool ViewPointValid(const Vector3 &viewPoint) const;
1274
1275        void CreateMesh(ViewCell *vc);
1276
[1027]1277        bool ExportViewCells(const string filename,
1278                                                 const bool exportPvs,
1279                                                 const ObjectContainer &objects);
[1021]1280
1281        int CastBeam(Beam &beam);
1282
1283        void Finalize(ViewCell *viewCell, const bool createMesh);
1284
[1666]1285        void ExportSingleViewCells(const ObjectContainer &objects,
1286                                                           const int maxViewCells,
1287                                                           const bool sortViewCells,
1288                                                           const bool exportPvs,
1289                                                           const bool exportRays,
1290                                                           const int maxRays,
1291                                                           const string prefix,
1292                                                           VssRayContainer *visRays = NULL);
[1920]1293       
1294        float UpdateObjectCosts();
[1570]1295
[2124]1296        virtual void FinalizeViewCells(const bool createMesh);
1297
[1021]1298protected:
[1180]1299
[1740]1300        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1301       
[1709]1302#if 1//TEST_EVALUATION
1303        virtual void EvalViewCellPartition();
1304#endif
[1416]1305
[1027]1306        /** Exports view cell geometry.
1307        */
[1666]1308        void ExportViewCellGeometry(Exporter *exporter,
1309                                                                ViewCell *vc,
1310                                                                const AxisAlignedBox3 *box,
1311                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[1027]1312
[1666]1313        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1314                                                                ViewCellContainer &viewCells) const;
[1021]1315       
1316        void CollectViewCells();
1317
[1845]1318        virtual void CompressViewCells();
1319
[1021]1320        /** Prepare view cells for use after loading them from disc.
1321        */
1322        void PrepareLoadedViewCells();
1323
1324        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1325        */
1326        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1327
[1074]1328        /** Exports visualization of the PVS.
[442]1329        */
[1666]1330        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
[442]1331
[1740]1332        /** Returns a hierarchy manager of the given name.
1333        */
1334        static HierarchyManager *CreateHierarchyManager(const string &name);
1335
[1679]1336        //void ExportStats(const string &mergeStats);
[1021]1337
[1887]1338        /** collect objects intersecting a given spatial box
[1845]1339        */
1340        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
[1737]1341
[1887]1342
1343        /////////////////////////////////////////
1344
1345        bool mCompressObjects;
1346
[1027]1347        HierarchyManager *mHierarchyManager;
[442]1348};
1349
[575]1350
[860]1351}
1352
[440]1353#endif
Note: See TracBrowser for help on using the repository browser.