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

Revision 2015, 34.5 KB checked in by bittner, 17 years ago (diff)

pvs efficiency tuning

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