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

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