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

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