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

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