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

Revision 2227, 36.1 KB checked in by mattausch, 17 years ago (diff)

added render cost bound for objects, improved undersampling compensation

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