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

Revision 2017, 35.3 KB checked in by mattausch, 17 years ago (diff)

changed to static cast

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