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

Revision 2048, 35.8 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
[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
[2021]383  // map the ray intersection objects from triangles to high level objects...
384  void
385  DeterminePvsObjects(
386                                          VssRayContainer &rays,
387                                          const bool useHitObjects = false);
388       
[562]389        /** Sets validity of view cell
390        */
391        virtual void SetValidity(ViewCell *vc,
392                                                         int minPvsSize,
393                                                         int maxPvsSize) const;
394
[710]395        /** sets validy of all viewcells
396        */
[810]397        virtual void SetValidity(int minPvsSize,
[569]398                                                         int maxPvsSize) const;
399
[609]400
401        /** set valid viewcells in the range of pvs. sorts the viewcells
[710]402          according to the pvs and then pickups those in the ranges
403          */
404        void SetValidityPercentage(const float minValid, const float maxValid);
[569]405
[1545]406        /** Returns number of valid view cells.
407        */
[609]408    int CountValidViewcells() const;
409
[547]410        /** Returns maximal allowed pvs size.
411        */
412        int GetMaxPvsSize() const;
413
[562]414        /** Returns maximal allowed pvs size.
415        */
416        int GetMinPvsSize() const;
[561]417        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
418                where pvs is still considered valid.
419        */
420        float GetMaxPvsRatio() const;
[547]421        /** Exports view cell geometry.
422        */
[1416]423        virtual void ExportViewCellGeometry(
424                Exporter *exporter,
425                ViewCell *vc,
426                const AxisAlignedBox3 *box,
427                const AxisAlignedPlane *clipPlane = NULL
428                ) const = 0;
[547]429
[860]430        /** Brings the view cells into their final state, computes meshes and volume.
431        */
[551]432        virtual void FinalizeViewCells(const bool createMesh);
433
[579]434        /** Evaluates statistics values on view cells.
435        */
[580]436        void EvaluateRenderStatistics(float &totalRenderCost,
437                                                                  float &expectedRenderCost,
438                                                                  float &deviation,
439                                                                  float &variance,
[1709]440                                                                  float &totalPvs,
[580]441                                                                  float &avgRenderCost);
[579]442
[580]443
444        /** Returns hierarchy of the view cells.
445        */
446        ViewCellsTree *GetViewCellsTree();
447
[1545]448        /** Collect candidates for the view cell merge.
449        */
[581]450        virtual void CollectMergeCandidates(const VssRayContainer &rays,
[1022]451                                                                                vector<MergeCandidate> &candidates);
[580]452
[1294]453        /** Collects n view cells and stores it as the active view cells.
454        */
[610]455        void CollectViewCells(const int n);
456
[1686]457        /** Sets current view cells set to active,
458                i.e., the sampling is done in this view cell set.
[660]459        */
460        void SetViewCellsActive();
[599]461
[1145]462        /** Evaluates render cost statistics of current view cell hierarchy.
[660]463        */
[1178]464        virtual void EvalViewCellPartition();
[660]465
[728]466        /** Sets maximal size of a view cell filter.
467        */
468        void SetMaxFilterSize(const int size);
[697]469
[840]470        /** Returns maximal filter size.
471        */
472        int GetMaxFilterSize() const;
[746]473
[1686]474        /** Deletes interior nodes from the tree which have negative
475                merge cost set (local merge).
[840]476        */ 
477        void DeleteLocalMergeTree(ViewCell *vc) const;
[713]478 
[728]479        /** Evaluautes histogram for a given number of view cells.
480        */
481        void EvalViewCellHistogram(const string filename, const int nViewCells);
482
[735]483        /** Evaluautes histogram for a given number of view cells.
484        */
485        void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
486
[1919]487        void EvalViewCellHistogramForPvsSize(const string filename,
488                                                                                 ViewCellContainer &viewCells);
489
[728]490        /** Evaluates the render cost of a view cell.
491        */
[1703]492        static float EvalRenderCost(Intersectable *obj);
[1002]493   
494        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
495                of the hierarchy.
496        */
[1709]497        void UpdateScalarPvsSize(ViewCell *vc, const float pvsCost, const int entriesInPvs) const;
[728]498
[870]499        /** Returns bounding box of a view cell.
500        */
501        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
[859]502
[840]503        /** Exports bounding boxes of objects to file.
504        */
505        bool ExportBoundingBoxes(const string filename, const ObjectContainer &objects) const;
[931]506   
[850]507        /** Load the bounding boxes into the container.
508        */
509        bool LoadBoundingBoxes(const string filename, IndexedBoundingBoxContainer &boxes) const;
[840]510
[1002]511        /** Returns true if pvs should be exported together with the view cells.
512        */
513        bool GetExportPvs() const;
[870]514
[1786]515        /** Efficiently merges the view cells in the container.
516        */
[1787]517        void MergeViewCellsRecursivly(ObjectPvs &pvs,
518                                                                  const ViewCellContainer &viewCells) const;
[1786]519
[1845]520        virtual void CompressViewCells();
521
[2017]522        ViewCell *GetViewCellById(const int id);
523
524        /** Returns number of view cells.
525        */
[1982]526        int GetNumViewCells() const;
527
[2017]528        bool GenerateRandomViewCells(ViewCellContainer &viewCells,
529                                                                 const int numViewCells);
530
531        bool GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
532                                                                 const int nViewCells,
533                                                                 const int nViewPoints);
534
535
536        bool GenerateViewPoints(ViewCell *viewCell,
537                                                        const int numViewPoints,
538                                                        SimpleRayContainer &viewPoints);
539
540        bool ImportRandomViewCells(const string &filename,
541                                                           vector<ViewCellPoints *> &viewCells);
542
[2048]543        bool ImportRandomViewCells(const string &filename);
[2017]544
545        bool ExportRandomViewCells(const string &filename,
546                                                           const vector<ViewCellPoints *> &viewCells);
547
[2048]548        bool ExportRandomViewCells(const string &filename);
549
[2017]550        bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
551
552        bool IsValidViewSpace(ViewCell *vc);
553
[2048]554
[2017]555        //////////////
[1582]556        // static members
557       
558        /** Loads view cells from file. The view cells manager is created with
559                respect to the loaded view cells.
[870]560
[1582]561                @param filename the filename of the view cells
562                @param objects the scene objects
563                @param finalizeViewCells if the view cells should be post processed, i.e.
[2048]564                           a mesh is created representing the geometry
[1582]565                @param bconverter a conversion routine working with the similarities of bounding
[2048]566                           boxes: if there is a certain similarity of overlap between
567                           bounding boxes, two tested candidate objects are considered
568                           to be the same objects
[1582]569                @returns the view cells manager if loading was successful, false otherwise
570        */
571        static ViewCellsManager *LoadViewCells(const string &filename,
572                                                                                   ObjectContainer *objects,
[1676]573                                                                                   bool finalizeViewCells = false,
[1582]574                                                                                   BoundingBoxConverter *bconverter = NULL);
[870]575
[904]576
[931]577        ////////////////////////////////////////////////////////7
578        // visiblity filter options
[1294]579
[931]580        // TODO: write own visibiltiy filter class
[2048]581        void ApplyFilter(KdTree *kdTree,
582                                         const float viewSpaceFilterSize,
583                                         const float spatialFilterSize);
[904]584
[2048]585        // new adaptive version of the filter
586        PvsFilterStatistics ApplyFilter2(ViewCell *viewCell,
587                                                                         const bool useViewSpaceFilter,
588                                                                         const float filterSize,
589                                                                         ObjectPvs &pvs,
590                                                                         vector<AxisAlignedBox3> *filteredBoxes = NULL);
[1715]591
[931]592        void ApplySpatialFilter(KdTree *kdTree,
593                                                        const float spatialFilterSize,
594                                                        ObjectPvs &pvs);
[480]595
[931]596         void ApplyFilter(ViewCell *viewCell,
597                                          KdTree *kdTree,
598                                          const float viewSpaceFilterSize,
599                                          const float spatialFilterSize,
600                                          ObjectPvs &pvs);
601
602        float GetFilterWidth();
603
604        float GetAbsFilterWidth();
605
[697]606        /** Returns the bounding box of filter width.
607        */
608        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
[609]609
[1294]610        //////////////////////////////////////////////////////////////////
[979]611
[2048]612
[1155]613        /** If true, the kd nodes are stored instead of the object pvs.
614        */
615        void SetStoreKdPvs(const bool storeKdPvs);
616
617        /** Returns true if the kd nodes are stored instead of the object pvs.
618        **/
619        bool GetStoreKdPVs() const;
620
[1570]621        /** Exports single view cells for visualization.
622                @param objects the scene objects
623                @param limit the maximal number of output view cells
624                @param sortViewCells if the view cells should be sorted by pvs size
625                @param exportPvs if the pvs should also be exported
626                @param exportRays if sample rays should be exported as well
[1580]627                @param maxRays maximum number of rays to export
628                @param prefix the prefix for the output file
[1570]629                @param visRays additional rays
630        */
[1686]631        virtual void ExportSingleViewCells(const ObjectContainer &objects,
632                                                                           const int maxViewCells,
633                                                                           const bool sortViewCells,
634                                                                           const bool exportPvs,
635                                                                           const bool exportRays,
636                                                                           const int maxRays,
637                                                                           const string prefix,
638                                                                           VssRayContainer *visRays = NULL) = NULL;
[1570]639
[1932]640
[2048]641        void ResetPvs();
[1580]642
[1932]643        Preprocessor *GetPreprocessor() const {
644                return mPreprocessor;
645        }
[1580]646
[1932]647        void SetPreprocessor(Preprocessor *p) {
648                mPreprocessor = p;
649        }
[1608]650
[2048]651        vector<ViewCellPoints *> *GetViewCellPoints()
652        {
653                return &mViewCellPoints;
654        }
655
[1932]656        void UpdatePvsForEvaluation();
657
[931]658protected:
[1145]659
[1932]660        void ComputeViewCellContribution(ViewCell *viewCell,
661                                                                         VssRay &ray,
662                                                                         Intersectable *obj,
663                                                                         const Vector3 &pt,
664                                                                         const bool addRays);
665
[1787]666        void MergeViewCellsRecursivly(ObjectPvs &pvs,
667                                                                  const ViewCellContainer &viewCells,
668                                                                  const int leftIdx,
669                                                                  const int rightIdx) const;
670
[697]671        /** Intersects box with the tree and returns the number of intersected boxes.
[1932]672        @returns number of view cells found
[697]673        */
[710]674        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
675                                                                                ViewCellContainer &viewCells) const;
[697]676
[880]677        /** Tests the visibility filter functionality.
678        */
[697]679        virtual void TestFilter(const ObjectContainer &objects) {};
680
[931]681        /** If the view cells tree was already constructed or not.
[581]682        */
683        bool ViewCellsTreeConstructed() const;
684
[1294]685        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
686        */
[574]687        int CastPassSamples(const int samplesPerPass,
[1768]688                                                const vector<int> &strategies,
[574]689                                                VssRayContainer &vssRays) const;
[573]690
[1903]691        int CastPassSamples2(const int samplesPerPass,
692                                                 VssRayContainer &passSamples) const;
693
[931]694        /** Parse the options from the environment file.
695        */
[482]696        void ParseEnvironment();
697
[508]698        /** Creates unique view cell ids.
699        */
700        void CreateUniqueViewCellIds();
[547]701
[551]702        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
703        */
704        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
705
[480]706        /** Recollects view cells and resets statistics.
707        */
708        void ResetViewCells();
[1932]709
[1707]710        /** Sets this view cell to active.
711        */
712        void SetViewCellActive(ViewCell *vc) const;
713
[480]714        /** Collects the view cells in the view cell container.
715        */
716        virtual void CollectViewCells() = 0;
[1932]717
[479]718        /** Evaluates view cells statistics and stores it in
[1932]719        mViewCellsStatistics.
[479]720        */
721        void EvaluateViewCellsStats();
[482]722
[1932]723
[1416]724        ///////////////////////
[482]725        //-- helper functions for view cell visualization
726
727        /** Exports the view cell partition.
728        */
[1686]729        void ExportViewCellsForViz(Exporter *exporter,
730                                                           const AxisAlignedBox3 *box,
[1760]731                                                           const bool colorCode,
[1932]732                                                           const AxisAlignedPlane *clipPlane) const;
[482]733
734        /** Sets exporter color.
735        */
[1773]736        virtual void ExportColor(Exporter *exporter,
737                                                         ViewCell *vc,
738                                                         const bool colorCode) const;
[1932]739
[1416]740        /** Creates meshes from the view cells.
741        */
742        void CreateViewCellMeshes();
[1545]743
[1932]744        /** Creates clip plane for visualization.
[1416]745        */
746        void CreateClipPlane();
[482]747
[1416]748        AxisAlignedPlane *GetClipPlane();
[1932]749
[1551]750        void ExportMergedViewCells(const ObjectContainer &objects);
751
[2048]752
[1416]753        ///////////////////////
754
[1002]755        /** Returns volume of the view space.
756        */
[557]757        virtual float GetViewSpaceVolume();
[2048]758
[1416]759        /** Prepares the view cells for sampling after loading them from disc.
[503]760        */
[577]761        virtual void PrepareLoadedViewCells() {};
[2048]762       
[704]763        /** Constructs local view cell merge hierarchy.
764        */
765        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
[1932]766                const ViewCellContainer &viewCells);
[704]767
[713]768        /** Constructs local view cell merge hierarchy based solely on similarity with the
[2048]769                current viewcell
[713]770        */
771        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
772                                                                           const ViewCellContainer &viewCells);
773 
[860]774        /** Updates pvs of all view cells for statistical evaluation after some more sampling
775        */
[1168]776        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
[752]777
[1679]778        virtual void ExportStats(const string &mergeStats);
[1582]779
[2048]780
[979]781        ////////////////////////////////////////////////
[752]782
[2048]783
[1145]784        Preprocessor *mPreprocessor;
[1902]785       
[1145]786        /// if bounding boxes should be exported together with the view cells
[840]787        bool mExportBboxesForPvs;
[1902]788       
[1416]789        /// the clip plane for visualization
790        AxisAlignedPlane mClipPlaneForViz;
[1902]791       
[1416]792        /// if the visualization is using the clip plane
[660]793        bool mUseClipPlaneForViz;
[1902]794       
[480]795        /// Renders the view cells.
796        Renderer *mRenderer;
[1902]797       
[440]798        /// Loaded view cells
[469]799        ViewCellContainer mViewCells;
[1902]800       
[1416]801        /// the view cell hierarchy (i.e., the logical description of view cells)
[580]802        ViewCellsTree *mViewCellsTree;
[1414]803       
[1773]804        std::vector<int> mStrategies;
805
[1416]806        /** if the values in the view cell leaves and the interiors are up to date
807                this is meant for lazy storing of the pvs, where only a scalar indicating
808                pvs size is stored in interiors and not the pvs itself.
809        */
[719]810        bool mViewCellPvsIsUpdated;
[1002]811
[1416]812        /// maximum number of samples for the view cell construction
[440]813        int mConstructionSamples;
[574]814        int mSamplesPerPass;
815        int mInitialSamples;
[440]816        int mPostProcessSamples;
817        int mVisualizationSamples;
[441]818
[470]819        float mTotalAreaValid;
820        float mTotalArea;
[475]821
[547]822        int mMaxPvsSize;
[562]823        int mMinPvsSize;
[547]824        float mMaxPvsRatio;
825
[660]826        int mSamplingType;
[722]827        int mEvaluationSamplingType;
[600]828        int mNumActiveViewCells;
[586]829        bool mCompressViewCells;
830
[2048]831        vector<ViewCellPoints *> mViewCellPoints;
832
[1002]833        /// holds the current view cell statistics
[660]834        ViewCellsStatistics mCurrentViewCellsStats;
[2048]835
[487]836        /// the scene bounding box
[542]837        AxisAlignedBox3 mViewSpaceBox;
[1001]838       
[508]839        /// if view cells should be exported
840        bool mExportViewCells;
[547]841
[1002]842        // if only valid view cells should be considered for processing
[564]843        bool mOnlyValidViewCells;
[547]844
[580]845        /// if rays should be used to collect merge candidates
846        bool mUseRaysForMerge;
[1021]847
[1002]848        /// if there should be an additional merge step after the subdivision
[592]849        bool mMergeViewCells;
[697]850
851        /// the width of the box filter
852        float mFilterWidth;
[1570]853        /// Maximal size of the filter in terms of contributing view cells
[697]854        int mMaxFilterSize;
[1570]855       
[1576]856        // only for debugging
[1686]857        VssRayContainer storedRays;
858
[1902]859        bool mUseKdPvs;
[1974]860        bool mUseKdPvsAfterFiltering;
[1902]861
[1903]862        MixtureDistribution *mMixtureDistribution;
863
[2048]864
[1545]865        //////////////////
[487]866        //-- visualization options
867       
[1002]868        /// color code for view cells visualization
[1570]869        bool mShowVisualization;
[487]870        int mColorCode;
871        bool mExportGeometry;
872        bool mExportRays;
[517]873        bool mViewCellsFinished;
[664]874        bool mEvaluateViewCells;
[844]875
876        /// if pvs should be exported with view cells
877        bool mExportPvs;
[1155]878
[1703]879        static int sRenderCostEvaluationType;
880
881        /// if view cells geometry should be used from other sources
[1545]882        bool mUsePredefinedViewCells;
[1942]883
884 
885  vector<PerViewCellStat> mPerViewCellStat;
[1983]886  SamplesStatistics mSamplesStat;
[440]887};
888
[441]889
[1545]890/** Manages different higher order operations on the view cells.
[440]891*/
892class BspViewCellsManager: public ViewCellsManager
893{
894
895public:
[485]896        /** Constructor taking the bsp tree and the number of samples
897                used to construct the bsp tree.
898        */
[1264]899        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
[440]900
[477]901        ~BspViewCellsManager();
[1582]902
[574]903        int ConstructSubdivision(const ObjectContainer &objects,
[487]904                                  const VssRayContainer &rays);
[440]905
906        int PostProcess(const ObjectContainer &objects,
[466]907                                        const VssRayContainer &rays);
[440]908
909        void Visualize(const ObjectContainer &objects,
[466]910                                   const VssRayContainer &sampleRays);
[440]911
912        int GetType() const;
913       
914        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
915
916        bool ViewCellsConstructed() const;
917
[475]918        //void PrintStatistics(ostream &s) const;
[452]919
[468]920        int CastLineSegment(const Vector3 &origin,
921                                                const Vector3 &termination,
922                                                ViewCellContainer &viewcells);
923       
[1294]924        /** Returns the probability that the view point lies
925                in this view cells.
926        */
[471]927        float GetProbability(ViewCell *viewCell);
[466]928
[1294]929        /** Get a viewcell containing the specified point.
[879]930        */
931        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[492]932
[1294]933        /** Creates mesh for this view cell.
934        */
[503]935        void CreateMesh(ViewCell *vc);
936
[1416]937        void ExportViewCellGeometry(Exporter *exporter,
938                                                                ViewCell *vc,
939                                                                const AxisAlignedBox3 *box,
940                                                                const AxisAlignedPlane *clipPlane = NULL
941                                                                ) const;
[580]942       
[587]943        void CollectMergeCandidates(const VssRayContainer &rays,
944                                                                vector<MergeCandidate> &candidates);
[547]945
[587]946        void Finalize(ViewCell *viewCell, const bool createMesh);
947
[1416]948        bool ExportViewCells(const string filename,
949                                                 const bool exportPvs,
950                                                 const ObjectContainer &objects);
[590]951
[752]952        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
953        */
[650]954        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[610]955
[1686]956        void ExportSingleViewCells(const ObjectContainer &objects,
957                                                           const int maxViewCells,
958                                                           const bool sortViewCells,
959                                                           const bool exportPvs,
960                                                           const bool exportRays,
961                                                           const int maxRays,
962                                                           const string prefix,
963                                                           VssRayContainer *visRays = NULL);
[1545]964
[1977]965        bool LineSegmentIntersects(const Vector3 &origin,
966                                                           const Vector3 &termination,
967                                                           ViewCell *viewCell);
968
[440]969protected:
970
[480]971        void CollectViewCells();
[547]972       
[440]973        /// the BSP tree.
974        BspTree *mBspTree;
[475]975        vector<BspRay *> mBspRays;
[440]976
977private:
978
[1545]979        /** Constructs a spatial merge tree only 2 levels deep.
980        */
981        ViewCell *ConstructDummyMergeTree(BspNode *root);
982
[440]983        /** Exports visualization of the BSP splits.
984        */
[477]985        void ExportSplits(const ObjectContainer &objects);
[440]986
[1002]987        /** test if subdivision is valid in terms of volume / area.
988        */
[693]989        void TestSubdivision();
[440]990};
991
[1545]992
[440]993/**
994        Manages different higher order operations on the KD type view cells.
995*/
996class KdViewCellsManager: public ViewCellsManager
997{
998
999public:
1000
[1264]1001        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
[440]1002
[574]1003        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1004                                                         const VssRayContainer &rays);
[440]1005
[478]1006        int CastLineSegment(const Vector3 &origin,
1007                                                const Vector3 &termination,
1008                                                ViewCellContainer &viewcells);
[440]1009
1010        int PostProcess(const ObjectContainer &objects,
[466]1011                                        const VssRayContainer &rays);
[440]1012
1013        void Visualize(const ObjectContainer &objects,
[466]1014                                   const VssRayContainer &sampleRays);
[440]1015
1016        int GetType() const;
1017
1018        bool ViewCellsConstructed() const;
1019
[580]1020        ViewCell *GenerateViewCell(Mesh *mesh) const;
[440]1021
[452]1022        /** Prints out statistics of this approach.
1023        */
[503]1024        //  virtual void PrintStatistics(ostream &s) const;
[1416]1025        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1026        { return NULL; }
[452]1027
[503]1028        float GetProbability(ViewCell *viewCell);
[609]1029       
[503]1030        void CreateMesh(ViewCell *vc);
1031
[1686]1032        void ExportViewCellGeometry(Exporter *exporter,
1033                                                                ViewCell *vc,
1034                                                                const AxisAlignedBox3 *box,
1035                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[547]1036
[1416]1037
[591]1038        void CollectMergeCandidates(const VssRayContainer &rays,
1039                                                                vector<MergeCandidate> &candidates);
[580]1040
[1686]1041        void ExportSingleViewCells(const ObjectContainer &objects,
1042                                                           const int maxViewCells,
1043                                                           const bool sortViewCells,
1044                                                           const bool exportPvs,
1045                                                           const bool exportRays,
1046                                                           const int maxRays,
1047                                                           const string prefix,
1048                                                           VssRayContainer *visRays = NULL);
[752]1049
[1977]1050        bool LineSegmentIntersects(const Vector3 &origin,
1051                                                           const Vector3 &termination,
1052                                                           ViewCell *viewCell);
1053
[440]1054protected:
1055
[580]1056        /** Collects view cells from a hierarchy.
1057        */
[480]1058        void CollectViewCells();
[580]1059
[480]1060        KdNode *GetNodeForPvs(KdLeaf *leaf);
[440]1061
[1570]1062        ////////////////////////////////////////
[440]1063
[480]1064        /// the BSP tree.
1065        KdTree *mKdTree;
[440]1066
[480]1067        /// depth of the KD tree nodes with represent the view cells
1068        int mKdPvsDepth;
[938]1069
1070
[440]1071};
1072
[1570]1073
1074/** Manages different higher order operations on the view cells.
[442]1075*/
1076class VspBspViewCellsManager: public ViewCellsManager
1077{
1078
1079public:
1080
[1264]1081        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
[475]1082        ~VspBspViewCellsManager();
[442]1083
[574]1084        int ConstructSubdivision(const ObjectContainer &objects,
[1686]1085                                                         const VssRayContainer &rays);
[442]1086
1087        int PostProcess(const ObjectContainer &objects,
[466]1088                                        const VssRayContainer &rays);
[442]1089
1090        void Visualize(const ObjectContainer &objects,
[466]1091                                   const VssRayContainer &sampleRays);
[442]1092
1093        int GetType() const;
1094       
1095        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1096
1097        bool ViewCellsConstructed() const;
[468]1098       
1099        int CastLineSegment(const Vector3 &origin,
1100                                                const Vector3 &termination,
1101                                                ViewCellContainer &viewcells);
[452]1102
[471]1103        float GetProbability(ViewCell *viewCell);
[479]1104       
[879]1105        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
[477]1106
[487]1107        bool GetViewPoint(Vector3 &viewPoint) const;
1108
[490]1109        bool ViewPointValid(const Vector3 &viewPoint) const;
[503]1110
1111        void CreateMesh(ViewCell *vc);
1112
[1686]1113        bool ExportViewCells(const string filename,
1114                                                 const bool exportPvs,
1115                                                 const ObjectContainer &objects);
[508]1116
[532]1117        int CastBeam(Beam &beam);
[1416]1118       
[1686]1119        void ExportViewCellGeometry(Exporter *exporter,
1120                                                                ViewCell *vc,
1121                                                                const AxisAlignedBox3 *box,
1122                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[532]1123
[547]1124
[555]1125        void Finalize(ViewCell *viewCell, const bool createMesh);
[551]1126
[1686]1127        void CollectMergeCandidates(const VssRayContainer &rays,
1128                                                                vector<MergeCandidate> &candidates);
[580]1129
[1686]1130        void ExportSingleViewCells(const ObjectContainer &objects,
1131                                                           const int maxViewCells,
1132                                                           const bool sortViewCells,
1133                                                           const bool exportPvs,
1134                                                           const bool exportRays,               
1135                                                           const int maxRays,
1136                                                           const string prefix,
1137                                                           VssRayContainer *visRays = NULL);
[1570]1138
[1920]1139       
[1977]1140        bool LineSegmentIntersects(const Vector3 &origin,
1141                                                           const Vector3 &termination,
1142                                                           ViewCell *viewCell);
1143
[442]1144protected:
[475]1145
[1686]1146        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1147                                                                ViewCellContainer &viewCells) const;
[651]1148       
[1021]1149        /** Merges view cells according to some criteria
[442]1150        */
[508]1151        void MergeViewCells(const VssRayContainer &rays,
1152                                                const ObjectContainer &objects);
[503]1153       
[564]1154        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
[442]1155
[480]1156        void CollectViewCells();
1157
[482]1158        /** Returns maximal depth difference of view cell
1159                leaves in tree.
1160        */
1161        int GetMaxTreeDiff(ViewCell *vc) const;
1162
[752]1163        /** Prepare view cells for use after loading them from disc.
1164        */
[577]1165        void PrepareLoadedViewCells();
1166
[752]1167        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1168        */
[650]1169        ViewCell *ConstructSpatialMergeTree(BspNode *root);
[649]1170
[979]1171        /// HACK for testing visibility filter functionality
[697]1172        void TestFilter(const ObjectContainer &objects);
1173
[979]1174        /** Visualization of the pvs difference to exact visubility using
1175                from point queries.
1176        */
1177        void VisualizeWithFromPointQueries();
[697]1178
[991]1179        /** Evaluate from point queries for the current scene.
1180        */
1181        void EvalFromPointQueries();
[979]1182
[1021]1183        /** Exports visualization of the BSP splits.
1184        */
1185        void ExportSplits(const ObjectContainer &objects,
1186                                          const VssRayContainer &rays);
[991]1187
[1686]1188
1189        /////////////////////////
1190
[442]1191        /// the view space partition BSP tree.
1192        VspBspTree *mVspBspTree;
1193
[591]1194
[442]1195private:
1196
[1021]1197        /** test if subdivision is valid in terms of volume / area.
[442]1198        */
[1021]1199        void TestSubdivision();
1200};
[442]1201
[1662]1202#define TEST_EVALUATION 0
[1021]1203
1204/**
1205        Manages different higher order operations on the view cells.
1206*/
1207class VspOspViewCellsManager: public ViewCellsManager
1208{
[1740]1209        friend class ViewCellsParseHandlers;
[1977]1210
[1021]1211public:
1212
[1740]1213        VspOspViewCellsManager(ViewCellsTree *vcTree, const string &hierarchyType);
[1278]1214       
[1021]1215        ~VspOspViewCellsManager();
1216
1217        int ConstructSubdivision(const ObjectContainer &objects,
1218                                                         const VssRayContainer &rays);
1219
1220        int PostProcess(const ObjectContainer &objects,
1221                                        const VssRayContainer &rays);
1222
[1686]1223        void Visualize(const ObjectContainer &objects,
[1021]1224                                   const VssRayContainer &sampleRays);
1225
1226        int GetType() const;
1227       
1228        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1229
[1977]1230        virtual Intersectable *GetIntersectable(
1231                const VssRay &ray, const bool isTermination) const;
[1743]1232
[1021]1233        bool ViewCellsConstructed() const;
1234
1235       
1236        int CastLineSegment(const Vector3 &origin,
1237                                                const Vector3 &termination,
1238                                                ViewCellContainer &viewcells);
1239
[1977]1240        bool LineSegmentIntersects(const Vector3 &origin,
1241                                                           const Vector3 &termination,
1242                                                           ViewCell *viewCell);
1243
[1021]1244        float GetProbability(ViewCell *viewCell);
1245       
1246        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1247
1248        bool GetViewPoint(Vector3 &viewPoint) const;
1249
1250        bool ViewPointValid(const Vector3 &viewPoint) const;
1251
1252        void CreateMesh(ViewCell *vc);
1253
[1027]1254        bool ExportViewCells(const string filename,
1255                                                 const bool exportPvs,
1256                                                 const ObjectContainer &objects);
[1021]1257
1258        int CastBeam(Beam &beam);
1259
1260        void Finalize(ViewCell *viewCell, const bool createMesh);
1261
[1977]1262        ViewCellsManager *LoadViewCells(const string &filename,
[1666]1263                                                                        ObjectContainer *objects,
1264                                                                        const bool finalizeViewCells,
1265                                                                        BoundingBoxConverter *bconverter);
[1201]1266
[1666]1267        void ExportSingleViewCells(const ObjectContainer &objects,
1268                                                           const int maxViewCells,
1269                                                           const bool sortViewCells,
1270                                                           const bool exportPvs,
1271                                                           const bool exportRays,
1272                                                           const int maxRays,
1273                                                           const string prefix,
1274                                                           VssRayContainer *visRays = NULL);
[1920]1275       
1276        float UpdateObjectCosts();
[1570]1277
[1021]1278protected:
[1180]1279
[1740]1280        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1281       
[1709]1282#if 1//TEST_EVALUATION
1283        virtual void EvalViewCellPartition();
1284#endif
[1416]1285
[1027]1286        /** Exports view cell geometry.
1287        */
[1666]1288        void ExportViewCellGeometry(Exporter *exporter,
1289                                                                ViewCell *vc,
1290                                                                const AxisAlignedBox3 *box,
1291                                                                const AxisAlignedPlane *clipPlane = NULL) const;
[1027]1292
[1666]1293        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1294                                                                ViewCellContainer &viewCells) const;
[1021]1295       
1296        void CollectViewCells();
1297
[1845]1298        virtual void CompressViewCells();
1299
[1021]1300        /** Prepare view cells for use after loading them from disc.
1301        */
1302        void PrepareLoadedViewCells();
1303
1304        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1305        */
1306        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1307
[1074]1308        /** Exports visualization of the PVS.
[442]1309        */
[1666]1310        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
[442]1311
[1740]1312        /** Returns a hierarchy manager of the given name.
1313        */
1314        static HierarchyManager *CreateHierarchyManager(const string &name);
1315
[1679]1316        //void ExportStats(const string &mergeStats);
[1021]1317
[1887]1318        /** collect objects intersecting a given spatial box
[1845]1319        */
1320        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
[1737]1321
[1887]1322
1323        /////////////////////////////////////////
1324
1325        bool mCompressObjects;
1326
[1027]1327        HierarchyManager *mHierarchyManager;
[442]1328};
1329
[575]1330
[860]1331}
1332
[440]1333#endif
Note: See TracBrowser for help on using the repository browser.