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

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