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

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