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

Revision 2538, 36.6 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
[2332]10#define TEST_PACKETS 0
[2176]11
[860]12namespace GtpVisibilityPreprocessor {
13
[440]14class ViewCell;
15class Intersectable;
16class RenderSimulator;
[480]17class Renderer;
[440]18class Mesh;
19struct Triangle3;
20class SimulationStatistics;
21class BspTree;
22class KdTree;
[1006]23class VspOspTree;
[440]24class VspBspTree;
25class KdNode;
26class KdLeaf;
27class AxisAlignedBox3;
[448]28class BspLeaf;
[475]29class ViewCellsStatistics;
[482]30class Exporter;
[532]31class Beam;
[570]32class Preprocessor;
[580]33class ViewCellsTree;
34class MergeCandidate;
[931]35class BoundingBoxConverter;
[1021]36class VspTree;
37class OspTree;
38class VspNode;
[1027]39class HierarchyManager;
[1259]40class BvHierarchy;
[1264]41class ViewCellsTree;
[1786]42class ViewCell;
[1903]43class MixtureDistribution;
[1047]44struct AxisAlignedPlane;
[475]45struct BspRay;
[2116]46class BspTree;
[475]47
[1903]48
[2015]49
[2116]50struct PvsFilterStatistics
51{
[1773]52        PvsFilterStatistics():
[2116]53        mAvgFilterRadius(0.0f), mLocalFilterCount(0), mGlobalFilterCount(0) {}
54       
[1761]55        float mAvgFilterRadius;
56        int mLocalFilterCount;
57        int mGlobalFilterCount;
[2116]58};
59
[1145]60/** Probably Visible Set
61*/
[697]62class PrVs
63{
64public:
[2116]65        /// root of view cells tree
66        ViewCell *mViewCell;
[704]67
[2116]68        // input parameter is the render budget for the PrVs
69        float mRenderBudget;
[677]70
[2116]71        // some characteristic values could be stored as well
[677]72};
73
[1786]74
75class LocalMergeNode
76{
77public:
78        virtual ~LocalMergeNode() {};
79        virtual bool IsLeaf() const = 0;
80};
81
[2017]82typedef pair<ViewCell *, SimpleRayContainer> ViewCellPoints;
[1786]83
[850]84/**     Manages different higher order operations on the view cells.
[440]85*/
86class ViewCellsManager
87{
88public:
[1983]89
[2116]90        struct PerViewCellStat
91        {
92                float pvsSize;
93                float relPvsIncrease;
[1983]94        };
95
[2116]96        struct SamplesStatistics
[728]97        {
[2116]98                int mRays;
99                int mViewCells;
100                int mContributingRays;
101                int mPvsContributions;
102                void Reset()
103                {
104                        mRays = 0;
105                        mViewCells = 0;
106                        mContributingRays = 0;
107                        mPvsContributions = 0;
108                };
109        };
[1974]110
[2116]111        struct PvsStatistics
112        {
113                float minPvs;
114                float maxPvs;
115                float avgPvs;
[2205]116                float renderCost;
[2116]117                float avgPvsEntries;
118
119                float avgFilteredPvs;
120                float avgFilteredPvsEntries;
121
122                float avgFilterContribution;
123                float avgFilterRadius;
124                float avgFilterRatio;
125                float avgRelPvsIncrease;
126                float devRelPvsIncrease;
127                int viewcells;
[2224]128
129                float mem;
[2227]130                float renderCostRatio;
[1545]131        };
[1942]132 
[728]133        /// view cell container types
[1021]134        enum {BSP, KD, VSP_KD, VSP_BSP, VSP_OSP};
[466]135 
[931]136        /// render cost evaluation type
[728]137        enum {PER_OBJECT, PER_TRIANGLE};
138
[1545]139        friend class X3dViewCellsParseHandlers;
[1709]140
[1004]141        /** Default constructor.
[440]142        */
[1264]143        ViewCellsManager(ViewCellsTree *viewCellsTree);
[440]144
[462]145        virtual ~ViewCellsManager();
[440]146
[527]147        /** Constructs view cells container taking a preprocessor
[570]148                @returns the output rays (if not null)
149        */
150        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
[527]151
[440]152        /** Constructs view cell container with a given number of samples.
153        */
[1709]154        virtual int ConstructSubdivision(const ObjectContainer &objects,
155                                                                         const VssRayContainer &rays) = 0;
[440]156
[441]157        /** Computes sample contributions of the rays to the view cells PVS.
158       
159                @param rays bundle of rays used to find intersections with view cells and
160                adding the contribution
[563]161                @param addRays true if rays should be added to the PVSs of the viewcells they
[574]162                intersect
163                @param storeViewCells true if view cells should be stored in the ray
164        */
[2332]165        virtual float ComputeSampleContributions(const VssRayContainer &rays,
166                                                     const bool addContributions,
167                                                                                         const bool storeViewCells,
168                                                                                         const bool useHitObject = false);
[440]169
[475]170        /** Computes sample contribution of a simgle ray to the view cells PVS.
171                @param ray finds intersections with view cells and holds the contribution
[1570]172                @param addSample if sample should be added to the pvs
173                 
[475]174                @returns number of sample contributions
175        */
[1159]176        virtual float ComputeSampleContribution(VssRay &ray,
[1981]177                                                                                        const bool addContributions,
[1990]178                                                                                        const bool storeViewCells,
179                                                                                        const bool useHitObject = false);
[563]180
[1977]181        /** Compute sample contribution only for current view cell.
182        */
183        virtual float ComputeSampleContribution(VssRay &ray,
[1981]184                                                                                        const bool addContributions,
[1990]185                                                                                        ViewCell *currentViewCell,
186                                                                                        const bool useHitObject = false);
[563]187
[452]188        /** Prints out statistics of the view cells.
189        */
[2176]190        virtual void PrintStatistics(std::ostream &s) const;
[452]191
[440]192        /** Post processes view cells givemŽa number of rays.
193        */
[475]194        virtual int PostProcess(const ObjectContainer &objects,
195                                                        const VssRayContainer &rays) = 0;
[440]196
197        /** Show visualization of the view cells.
198        */
199        virtual void Visualize(const ObjectContainer &objects,
[466]200                                                   const VssRayContainer &sampleRays) = 0;
[440]201
[1982]202        /** collect objects intersecting a given spatial box.
203        */
204        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
[1737]205
[440]206        /** type of the view cell container.
207        */
208        virtual int GetType() const = 0;
209
[463]210        /** Load the input viewcells. The input viewcells should be given as a collection
[1545]211                of meshes. Each mesh is assume to form a bounded polyhedron
212                defining the interior of the viewcell. The view cells manager
213                is responsible for building a hierarchy over these view cells.
[463]214               
215                @param filename file to load
216                @return true on success
[1982]217        */
218        virtual bool LoadViewCellsGeometry(const string filename, const bool extrudeBaseTriangle);
[577]219       
[440]220        /** Merges two view cells.
221                @note the piercing rays of the front and back will be ordered   
222                @returns new view cell based on the merging.
223        */
[582]224        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
225
226        /** Merges a container of view cells.
227                @returns new view cell based on the merging.
228        */
229        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
[440]230       
[1545]231        /** Generates view cell of the type specified by this manager
[440]232        */
[580]233        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
[440]234
[1545]235        /** Constructs view cell from base triangle.
236                The view cell is extruded along the normal vector.
237                @param the base triangle
238                @param the height of the newly created view cell
[440]239        */
[1545]240        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
[440]241
[1982]242        virtual Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
[1743]243
[440]244        /** Sets maximal number of samples used for the
245                construction of the view cells.
246        */
247        void SetVisualizationSamples(const int visSamples);
248
[574]249        /** Sets maximal number of samples used for the construction of the view cells.
[440]250        */
251        void SetConstructionSamples(const int constructionSamples);
252
[574]253        /** Sets maximal number of samples used for the visualization of the view cells.
254        */
255        void SetInitialSamples(const int initialSamples);
256
[440]257        /** Sets maximal number of samples used for the post processing of the view cells.
258        */
259        void SetPostProcessSamples(const int postProcessingSamples);
260
261        /** See set.
262        */
263        int GetVisualizationSamples() const;
264
265        /** See set.
266        */
267        int GetConstructionSamples() const;
268
269        /** See set.
270        */
271        int GetPostProcessSamples() const;
272
[1002]273        /** Returns true if view cells are already constructed.
[440]274        */
275        virtual bool ViewCellsConstructed() const = 0;
[441]276
[477]277        /** cast line segment to get a list of unique viewcells which are intersected
278                by this line segment
279        */
[1977]280        virtual int CastLineSegment(const Vector3 &origin,
[466]281                                                          const Vector3 &termination,
282                                                          ViewCellContainer &viewcells
283                                                          ) = 0;
284
[1977]285        /** Tests if this line segment intersects a particular view cell.
286        */
287        virtual bool LineSegmentIntersects(const Vector3 &origin,
288                                                                           const Vector3 &termination,
289                                                                           ViewCell *viewCell) = 0;
290
[1294]291        /** Returns a stats about the global pvs.
292        */
[477]293        virtual void GetPvsStatistics(PvsStatistics &stat);
[466]294
[904]295        virtual void GetPrVS(const Vector3 &viewPoint, PrVs &prvs, const float filterWidth);
[677]296 
[2111]297        /** Get a viewcell containing the specified view point.
[879]298                @param active if the active or elementary view cell should be returned.
[697]299        */
[879]300        virtual ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const = 0;
[492]301 
[1982]302        ViewCell *GetViewCell(const int idx) const {return mViewCells[idx];}
303
[2176]304        virtual void PrintPvsStatistics(std::ostream &s);
[467]305
[719]306        /** Updates pvs of the view cell hierarchy if necessary.
307        */
308        void UpdatePvs();
309
[477]310        /** Returns probability that view point lies in one view cell.
311        */
312        virtual float GetProbability(ViewCell *viewCell) = 0;
[468]313
[477]314        /** Returns render cost of a single view cell given the render cost of an object.
315        */
[728]316        float GetRendercost(ViewCell *viewCell) const;
[468]317
[720]318        /** Returns reference to container of loaded / generated view cells.
[477]319        */
320        ViewCellContainer &GetViewCells();
[468]321
[485]322        /** Helper function used to split ray sets uniformly
323                into one that is currently used and the other that
324                is saved for later processing.
325                @param sourceRays the input ray set
326                @param maxSize the maximal number of rays that will be used
327                @param usedRays returns the used ray set
328                @param savedRays if not null, returns the saved ray set
[477]329        */
330        void GetRaySets(const VssRayContainer &sourceRays,
[485]331                                        const int maxSize,
332                                        VssRayContainer &usedRays,
333                                        VssRayContainer *savedRays = NULL) const;
334       
[480]335        /** Returns accumulated area of all view cells.
336        */
337        float GetAccVcArea();
[470]338
[480]339        /** Returns area of one view cell.
340        */
341        virtual float GetArea(ViewCell *viewCell) const;
[470]342
[551]343        /** Returns volume of view cell.
[480]344        */
345        virtual float GetVolume(ViewCell *viewCell) const;
[470]346
[480]347        /** Sets the current renderer mainly for view cells statistics.
348        */
349        void SetRenderer(Renderer *renderer);
350
[487]351        /** Computes a (random) view point in the valid view space.
352                @returns true if valid view point was found
353        */
354        virtual bool GetViewPoint(Vector3 &viewPoint) const;
[1867]355 
[1982]356        virtual bool GetViewPoint(Vector3 &viewPoint, const Vector3 &params) const;
[487]357
[490]358        /** Returns true if this view point is in the valid view space.
359        */
360        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
361
[487]362        /** Sets a view space boundary.
363        */
364        void SetViewSpaceBox(const AxisAlignedBox3 &box);
365
[519]366        AxisAlignedBox3 GetViewSpaceBox() const;
367
[503]368        /** Creates mesh for this view cell.
369        */
370        virtual void CreateMesh(ViewCell *vc) = NULL;
[487]371
[508]372        /** Writes view cells to disc.
373        */
[1027]374        virtual bool ExportViewCells(const string filename,
375                                                                 const bool exportPvs,
376                                                                 const ObjectContainer &objects);
[508]377
[532]378        /** Casts beam to collect view cells.
379        */
380        virtual int CastBeam(Beam &beam);
381
[547]382        /** Checks if view cell is considered as valid.
383        */
[561]384        virtual bool CheckValidity(ViewCell *vc,
[562]385                                                           int minPvsSize,
386                                                           int maxPvsSize) const;
[547]387
[2015]388
[2117]389        // resort pvss after a pass of the algorithm
390        void  SortViewCellPvs();
[2015]391
[2117]392        // map the ray intersection objects from triangles to high level objects...
393        void DeterminePvsObjects(VssRayContainer &rays,
394                                                         const bool useHitObjects = false);
[2021]395       
[562]396        /** Sets validity of view cell
397        */
398        virtual void SetValidity(ViewCell *vc,
399                                                         int minPvsSize,
400                                                         int maxPvsSize) const;
401
[710]402        /** sets validy of all viewcells
403        */
[810]404        virtual void SetValidity(int minPvsSize,
[569]405                                                         int maxPvsSize) const;
406
[609]407
[2117]408        /** Set valid viewcells in the range of pvs. sorts the viewcells
409                according to the pvs and then pickups those in the ranges
410        */
[710]411        void SetValidityPercentage(const float minValid, const float maxValid);
[569]412
[1545]413        /** Returns number of valid view cells.
414        */
[609]415    int CountValidViewcells() const;
416
[547]417        /** Returns maximal allowed pvs size.
418        */
419        int GetMaxPvsSize() const;
420
[562]421        /** Returns maximal allowed pvs size.
422        */
423        int GetMinPvsSize() const;
[2117]424
[561]425        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
426                where pvs is still considered valid.
427        */
428        float GetMaxPvsRatio() const;
[2117]429
[547]430        /** Exports view cell geometry.
431        */
[2117]432        virtual void ExportViewCellGeometry(Exporter *exporter,
433                                                                                ViewCell *vc,
434                                                                                const AxisAlignedBox3 *box,
435                                                                                const AxisAlignedPlane *clipPlane = NULL
436                                                                                ) const = 0;
[547]437
[860]438        /** Brings the view cells into their final state, computes meshes and volume.
439        */
[551]440        virtual void FinalizeViewCells(const bool createMesh);
441
[579]442        /** Evaluates statistics values on view cells.
443        */
[580]444        void EvaluateRenderStatistics(float &totalRenderCost,
445                                                                  float &expectedRenderCost,
446                                                                  float &deviation,
447                                                                  float &variance,
[1709]448                                                                  float &totalPvs,
[580]449                                                                  float &avgRenderCost);
[579]450
[580]451        /** Returns hierarchy of the view cells.
452        */
453        ViewCellsTree *GetViewCellsTree();
454
[1545]455        /** Collect candidates for the view cell merge.
456        */
[581]457        virtual void CollectMergeCandidates(const VssRayContainer &rays,
[1022]458                                                                                vector<MergeCandidate> &candidates);
[580]459
[1294]460        /** Collects n view cells and stores it as the active view cells.
461        */
[610]462        void CollectViewCells(const int n);
463
[1686]464        /** Sets current view cells set to active,
465                i.e., the sampling is done in this view cell set.
[660]466        */
467        void SetViewCellsActive();
[599]468
[1145]469        /** Evaluates render cost statistics of current view cell hierarchy.
[660]470        */
[1178]471        virtual void EvalViewCellPartition();
[660]472
[728]473        /** Sets maximal size of a view cell filter.
474        */
475        void SetMaxFilterSize(const int size);
[697]476
[840]477        /** Returns maximal filter size.
478        */
479        int GetMaxFilterSize() const;
[746]480
[1686]481        /** Deletes interior nodes from the tree which have negative
482                merge cost set (local merge).
[840]483        */ 
484        void DeleteLocalMergeTree(ViewCell *vc) const;
[713]485 
[728]486        /** Evaluautes histogram for a given number of view cells.
487        */
488        void EvalViewCellHistogram(const string filename, const int nViewCells);
489
[735]490        /** Evaluautes histogram for a given number of view cells.
491        */
[2117]492        void EvalViewCellHistogramForPvsSize(const string filename,
493                                                                                 const int nViewCells);
[735]494
[1919]495        void EvalViewCellHistogramForPvsSize(const string filename,
496                                                                                 ViewCellContainer &viewCells);
497
[2332]498        float ComputeRenderCost(const int tri, const int obj); //const
499
[1002]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,
[2394]657                                                                           const string &prefix,
[1686]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,
[2394]758                                                         const int 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
[2232]840        int mEvaluationSamples;
841
[470]842        float mTotalAreaValid;
843        float mTotalArea;
[475]844
[547]845        int mMaxPvsSize;
[562]846        int mMinPvsSize;
[547]847        float mMaxPvsRatio;
848
[660]849        int mSamplingType;
[722]850        int mEvaluationSamplingType;
[600]851        int mNumActiveViewCells;
[586]852        bool mCompressViewCells;
853
[2048]854        vector<ViewCellPoints *> mViewCellPoints;
855
[1002]856        /// holds the current view cell statistics
[660]857        ViewCellsStatistics mCurrentViewCellsStats;
[2048]858
[487]859        /// the scene bounding box
[542]860        AxisAlignedBox3 mViewSpaceBox;
[1001]861       
[508]862        /// if view cells should be exported
863        bool mExportViewCells;
[1002]864        // if only valid view cells should be considered for processing
[564]865        bool mOnlyValidViewCells;
[580]866        /// if rays should be used to collect merge candidates
867        bool mUseRaysForMerge;
[1002]868        /// if there should be an additional merge step after the subdivision
[592]869        bool mMergeViewCells;
[697]870        /// the width of the box filter
871        float mFilterWidth;
[1570]872        /// Maximal size of the filter in terms of contributing view cells
[697]873        int mMaxFilterSize;
[2538]874        /// only for debugging: stores some rays used during view cell construction
[1686]875        VssRayContainer storedRays;
[2538]876        /// if kd node based pvs is used for preprocessing
[1902]877        bool mUseKdPvs;
[2538]878        /// types of distributions used for sampling
[1903]879        MixtureDistribution *mMixtureDistribution;
880
[2048]881
[1545]882        //////////////////
[487]883        //-- visualization options
884       
[1002]885        /// color code for view cells visualization
[1570]886        bool mShowVisualization;
[487]887        int mColorCode;
888        bool mExportGeometry;
889        bool mExportRays;
[517]890        bool mViewCellsFinished;
[664]891        bool mEvaluateViewCells;
[844]892
893        /// if pvs should be exported with view cells
894        bool mExportPvs;
[1155]895
[1703]896        /// if view cells geometry should be used from other sources
[1545]897        bool mUsePredefinedViewCells;
[2538]898        /// the weight for one triangle in the heuristics
[2332]899        float mTriangleWeight;
[2538]900        /// the weight for one object (= one entity issued with one draw call) in the heuristics
[2332]901        float mObjectWeight;
902
903        vector<PerViewCellStat> mPerViewCellStat;
904        SamplesStatistics mSamplesStat;
[440]905};
906
[441]907
[1545]908/** Manages different higher order operations on the view cells.
[440]909*/
910class BspViewCellsManager: public ViewCellsManager
911{
912
913public:
[485]914        /** Constructor taking the bsp tree and the number of samples
915                used to construct the bsp tree.
916        */
[1264]917        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
[440]918
[477]919        ~BspViewCellsManager();
[1582]920
[574]921        int ConstructSubdivision(const ObjectContainer &objects,
[487]922                                  const VssRayContainer &rays);
[440]923
924        int PostProcess(const ObjectContainer &objects,
[466]925                                        const VssRayContainer &rays);
[440]926
927        void Visualize(const ObjectContainer &objects,
[466]928                                   const VssRayContainer &sampleRays);
[440]929
930        int GetType() const;
931       
932        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
933
934        bool ViewCellsConstructed() const;
935
[475]936        //void PrintStatistics(ostream &s) const;
[452]937
[468]938        int CastLineSegment(const Vector3 &origin,
939                                                const Vector3 &termination,
940                                                ViewCellContainer &viewcells);
941       
[1294]942        /** Returns the probability that the view point lies
943                in this view cells.
944        */
[471]945        float GetProbability(ViewCell *viewCell);
[466]946
[1294]947        /** Get a viewcell containing the specified point.
[879]948        */
949        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[492]950
[1294]951        /** Creates mesh for this view cell.
952        */
[503]953        void CreateMesh(ViewCell *vc);
954
[1416]955        void ExportViewCellGeometry(Exporter *exporter,
956                                                                ViewCell *vc,
957                                                                const AxisAlignedBox3 *box,
958                                                                const AxisAlignedPlane *clipPlane = NULL
959                                                                ) const;
[580]960       
[587]961        void CollectMergeCandidates(const VssRayContainer &rays,
962                                                                vector<MergeCandidate> &candidates);
[547]963
[587]964        void Finalize(ViewCell *viewCell, const bool createMesh);
965
[1416]966        bool ExportViewCells(const string filename,
967                                                 const bool exportPvs,
968                                                 const ObjectContainer &objects);
[590]969
[752]970        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
971        */
[650]972        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[610]973
[1686]974        void ExportSingleViewCells(const ObjectContainer &objects,
975                                                           const int maxViewCells,
976                                                           const bool sortViewCells,
977                                                           const bool exportPvs,
978                                                           const bool exportRays,
979                                                           const int maxRays,
[2394]980                                                           const string &prefix,
[1686]981                                                           VssRayContainer *visRays = NULL);
[1545]982
[1977]983        bool LineSegmentIntersects(const Vector3 &origin,
984                                                           const Vector3 &termination,
985                                                           ViewCell *viewCell);
986
[440]987protected:
988
[480]989        void CollectViewCells();
[547]990       
[440]991        /// the BSP tree.
992        BspTree *mBspTree;
[475]993        vector<BspRay *> mBspRays;
[440]994
995private:
996
[1545]997        /** Constructs a spatial merge tree only 2 levels deep.
998        */
999        ViewCell *ConstructDummyMergeTree(BspNode *root);
1000
[440]1001        /** Exports visualization of the BSP splits.
1002        */
[477]1003        void ExportSplits(const ObjectContainer &objects);
[440]1004
[1002]1005        /** test if subdivision is valid in terms of volume / area.
1006        */
[693]1007        void TestSubdivision();
[440]1008};
1009
[1545]1010
[440]1011/**
1012        Manages different higher order operations on the KD type view cells.
1013*/
1014class KdViewCellsManager: public ViewCellsManager
1015{
1016
1017public:
1018
[1264]1019        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
[440]1020
[574]1021        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1022                                                         const VssRayContainer &rays);
[440]1023
[478]1024        int CastLineSegment(const Vector3 &origin,
1025                                                const Vector3 &termination,
1026                                                ViewCellContainer &viewcells);
[440]1027
1028        int PostProcess(const ObjectContainer &objects,
[466]1029                                        const VssRayContainer &rays);
[440]1030
1031        void Visualize(const ObjectContainer &objects,
[466]1032                                   const VssRayContainer &sampleRays);
[440]1033
1034        int GetType() const;
1035
1036        bool ViewCellsConstructed() const;
1037
[580]1038        ViewCell *GenerateViewCell(Mesh *mesh) const;
[440]1039
[452]1040        /** Prints out statistics of this approach.
1041        */
[503]1042        //  virtual void PrintStatistics(ostream &s) const;
[1416]1043        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1044        { return NULL; }
[452]1045
[503]1046        float GetProbability(ViewCell *viewCell);
[609]1047       
[503]1048        void CreateMesh(ViewCell *vc);
1049
[1686]1050        void ExportViewCellGeometry(Exporter *exporter,
1051                                                                ViewCell *vc,
1052                                                                const AxisAlignedBox3 *box,
1053                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[547]1054
[1416]1055
[591]1056        void CollectMergeCandidates(const VssRayContainer &rays,
1057                                                                vector<MergeCandidate> &candidates);
[580]1058
[1686]1059        void ExportSingleViewCells(const ObjectContainer &objects,
1060                                                           const int maxViewCells,
1061                                                           const bool sortViewCells,
1062                                                           const bool exportPvs,
1063                                                           const bool exportRays,
1064                                                           const int maxRays,
[2394]1065                                                           const string &prefix,
[1686]1066                                                           VssRayContainer *visRays = NULL);
[752]1067
[1977]1068        bool LineSegmentIntersects(const Vector3 &origin,
1069                                                           const Vector3 &termination,
1070                                                           ViewCell *viewCell);
1071
[440]1072protected:
1073
[580]1074        /** Collects view cells from a hierarchy.
1075        */
[480]1076        void CollectViewCells();
[580]1077
[480]1078        KdNode *GetNodeForPvs(KdLeaf *leaf);
[440]1079
[1570]1080        ////////////////////////////////////////
[440]1081
[480]1082        /// the BSP tree.
1083        KdTree *mKdTree;
[440]1084
[480]1085        /// depth of the KD tree nodes with represent the view cells
1086        int mKdPvsDepth;
[938]1087
1088
[440]1089};
1090
[1570]1091
1092/** Manages different higher order operations on the view cells.
[442]1093*/
1094class VspBspViewCellsManager: public ViewCellsManager
1095{
1096
1097public:
1098
[1264]1099        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
[475]1100        ~VspBspViewCellsManager();
[442]1101
[574]1102        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1103                                                         const VssRayContainer &rays);
[442]1104
1105        int PostProcess(const ObjectContainer &objects,
[466]1106                                        const VssRayContainer &rays);
[442]1107
1108        void Visualize(const ObjectContainer &objects,
[466]1109                                   const VssRayContainer &sampleRays);
[442]1110
1111        int GetType() const;
1112       
1113        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1114
1115        bool ViewCellsConstructed() const;
[468]1116       
1117        int CastLineSegment(const Vector3 &origin,
1118                                                const Vector3 &termination,
1119                                                ViewCellContainer &viewcells);
[452]1120
[471]1121        float GetProbability(ViewCell *viewCell);
[479]1122       
[879]1123        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[477]1124
[487]1125        bool GetViewPoint(Vector3 &viewPoint) const;
1126
[490]1127        bool ViewPointValid(const Vector3 &viewPoint) const;
[503]1128
1129        void CreateMesh(ViewCell *vc);
1130
[1686]1131        bool ExportViewCells(const string filename,
1132                                                 const bool exportPvs,
1133                                                 const ObjectContainer &objects);
[508]1134
[532]1135        int CastBeam(Beam &beam);
[1416]1136       
[1686]1137        void ExportViewCellGeometry(Exporter *exporter,
1138                                                                ViewCell *vc,
1139                                                                const AxisAlignedBox3 *box,
1140                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[532]1141
[547]1142
[555]1143        void Finalize(ViewCell *viewCell, const bool createMesh);
[551]1144
[1686]1145        void CollectMergeCandidates(const VssRayContainer &rays,
1146                                                                vector<MergeCandidate> &candidates);
[580]1147
[1686]1148        void ExportSingleViewCells(const ObjectContainer &objects,
1149                                                           const int maxViewCells,
1150                                                           const bool sortViewCells,
1151                                                           const bool exportPvs,
1152                                                           const bool exportRays,               
1153                                                           const int maxRays,
[2394]1154                                                           const string &prefix,
[1686]1155                                                           VssRayContainer *visRays = NULL);
[1570]1156
[1920]1157       
[1977]1158        bool LineSegmentIntersects(const Vector3 &origin,
1159                                                           const Vector3 &termination,
1160                                                           ViewCell *viewCell);
1161
[442]1162protected:
[475]1163
[1686]1164        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1165                                                                ViewCellContainer &viewCells) const;
[651]1166       
[1021]1167        /** Merges view cells according to some criteria
[442]1168        */
[508]1169        void MergeViewCells(const VssRayContainer &rays,
1170                                                const ObjectContainer &objects);
[503]1171       
[564]1172        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
[442]1173
[480]1174        void CollectViewCells();
1175
[482]1176        /** Returns maximal depth difference of view cell
1177                leaves in tree.
1178        */
1179        int GetMaxTreeDiff(ViewCell *vc) const;
1180
[752]1181        /** Prepare view cells for use after loading them from disc.
1182        */
[577]1183        void PrepareLoadedViewCells();
1184
[752]1185        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1186        */
[650]1187        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[649]1188
[979]1189        /// HACK for testing visibility filter functionality
[697]1190        void TestFilter(const ObjectContainer &objects);
1191
[979]1192        /** Visualization of the pvs difference to exact visubility using
1193                from point queries.
1194        */
1195        void VisualizeWithFromPointQueries();
[697]1196
[991]1197        /** Evaluate from point queries for the current scene.
1198        */
1199        void EvalFromPointQueries();
[979]1200
[1021]1201        /** Exports visualization of the BSP splits.
1202        */
1203        void ExportSplits(const ObjectContainer &objects,
1204                                          const VssRayContainer &rays);
[991]1205
[1686]1206
1207        /////////////////////////
1208
[442]1209        /// the view space partition BSP tree.
1210        VspBspTree *mVspBspTree;
1211
[591]1212
[442]1213private:
1214
[1021]1215        /** test if subdivision is valid in terms of volume / area.
[442]1216        */
[1021]1217        void TestSubdivision();
1218};
[442]1219
[1021]1220
[2353]1221
1222/**     Manages different higher order operations on the view cells.
[1021]1223*/
1224class VspOspViewCellsManager: public ViewCellsManager
1225{
[1740]1226        friend class ViewCellsParseHandlers;
[1977]1227
[1021]1228public:
1229
[2113]1230        VspOspViewCellsManager(ViewCellsTree *vcTree,
1231                                                   const string &hierarchyType);
[1278]1232       
[1021]1233        ~VspOspViewCellsManager();
1234
1235        int ConstructSubdivision(const ObjectContainer &objects,
1236                                                         const VssRayContainer &rays);
1237
1238        int PostProcess(const ObjectContainer &objects,
1239                                        const VssRayContainer &rays);
1240
[1686]1241        void Visualize(const ObjectContainer &objects,
[1021]1242                                   const VssRayContainer &sampleRays);
1243
1244        int GetType() const;
1245       
1246        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1247
[1977]1248        virtual Intersectable *GetIntersectable(
1249                const VssRay &ray, const bool isTermination) const;
[1743]1250
[1021]1251        bool ViewCellsConstructed() const;
1252
1253       
1254        int CastLineSegment(const Vector3 &origin,
1255                                                const Vector3 &termination,
1256                                                ViewCellContainer &viewcells);
1257
[1977]1258        bool LineSegmentIntersects(const Vector3 &origin,
1259                                                           const Vector3 &termination,
1260                                                           ViewCell *viewCell);
1261
[1021]1262        float GetProbability(ViewCell *viewCell);
1263       
[2113]1264        ViewCell *GetViewCell(const Vector3 &point,
1265                                                  const bool active = false) const;
[1021]1266
1267        bool GetViewPoint(Vector3 &viewPoint) const;
1268
1269        bool ViewPointValid(const Vector3 &viewPoint) const;
1270
1271        void CreateMesh(ViewCell *vc);
1272
[1027]1273        bool ExportViewCells(const string filename,
1274                                                 const bool exportPvs,
1275                                                 const ObjectContainer &objects);
[1021]1276
1277        int CastBeam(Beam &beam);
1278
1279        void Finalize(ViewCell *viewCell, const bool createMesh);
1280
[1666]1281        void ExportSingleViewCells(const ObjectContainer &objects,
1282                                                           const int maxViewCells,
1283                                                           const bool sortViewCells,
1284                                                           const bool exportPvs,
1285                                                           const bool exportRays,
1286                                                           const int maxRays,
[2394]1287                                                           const string &prefix,
[1666]1288                                                           VssRayContainer *visRays = NULL);
[1920]1289       
1290        float UpdateObjectCosts();
[1570]1291
[2124]1292        virtual void FinalizeViewCells(const bool createMesh);
1293
[2332]1294#if TEST_PACKETS
1295
[2353]1296        float ComputeSampleContributions(const VssRayContainer &rays,
1297                                             const bool addContributions,
1298                                                                         const bool storeViewCells,
1299                                                                         const bool useHitObjects = false);
[2332]1300#endif
1301
[1021]1302protected:
[1180]1303
[1740]1304        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1305       
[1709]1306        virtual void EvalViewCellPartition();
[1416]1307
[1027]1308        /** Exports view cell geometry.
1309        */
[1666]1310        void ExportViewCellGeometry(Exporter *exporter,
1311                                                                ViewCell *vc,
1312                                                                const AxisAlignedBox3 *box,
1313                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[1027]1314
[1666]1315        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1316                                                                ViewCellContainer &viewCells) const;
[1021]1317       
1318        void CollectViewCells();
1319
[1845]1320        virtual void CompressViewCells();
[1021]1321        /** Prepare view cells for use after loading them from disc.
1322        */
1323        void PrepareLoadedViewCells();
1324        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1325        */
1326        ViewCell *ConstructSpatialMergeTree(VspNode *root);
[1074]1327        /** Exports visualization of the PVS.
[442]1328        */
[1666]1329        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
[1740]1330        /** Returns a hierarchy manager of the given name.
1331        */
1332        static HierarchyManager *CreateHierarchyManager(const string &name);
[1887]1333        /** collect objects intersecting a given spatial box
[1845]1334        */
1335        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
[1737]1336
[2530]1337        static void PrintCompressionStats(HierarchyManager *hm, const int pvsEntries);
[1887]1338
1339
[2530]1340        ////////////////////
1341
[1887]1342        bool mCompressObjects;
1343
[1027]1344        HierarchyManager *mHierarchyManager;
[442]1345};
1346
[575]1347
[860]1348}
1349
[440]1350#endif
Note: See TracBrowser for help on using the repository browser.