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

Revision 2017, 35.3 KB checked in by mattausch, 17 years ago (diff)

changed to static cast

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