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

Revision 1919, 33.0 KB checked in by mattausch, 17 years ago (diff)

added mechanism for histogram on certain MB in hierarchymanager
no more bug in undersampling estimation
added sampling strategy to spatial box based (also for eval)
added testing scripts

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