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

Revision 2021, 35.5 KB checked in by bittner, 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
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  // map the ray intersection objects from triangles to high level objects...
384  void
385  DeterminePvsObjects(
386                                          VssRayContainer &rays,
387                                          const bool useHitObjects = false);
388       
389        /** Sets validity of view cell
390        */
391        virtual void SetValidity(ViewCell *vc,
392                                                         int minPvsSize,
393                                                         int maxPvsSize) const;
394
395        /** sets validy of all viewcells
396        */
397        virtual void SetValidity(int minPvsSize,
398                                                         int maxPvsSize) const;
399
400
401        /** set valid viewcells in the range of pvs. sorts the viewcells
402          according to the pvs and then pickups those in the ranges
403          */
404        void SetValidityPercentage(const float minValid, const float maxValid);
405
406        /** Returns number of valid view cells.
407        */
408    int CountValidViewcells() const;
409
410        /** Returns maximal allowed pvs size.
411        */
412        int GetMaxPvsSize() const;
413
414        /** Returns maximal allowed pvs size.
415        */
416        int GetMinPvsSize() const;
417        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
418                where pvs is still considered valid.
419        */
420        float GetMaxPvsRatio() const;
421        /** Exports view cell geometry.
422        */
423        virtual void ExportViewCellGeometry(
424                Exporter *exporter,
425                ViewCell *vc,
426                const AxisAlignedBox3 *box,
427                const AxisAlignedPlane *clipPlane = NULL
428                ) const = 0;
429
430        /** Brings the view cells into their final state, computes meshes and volume.
431        */
432        virtual void FinalizeViewCells(const bool createMesh);
433
434        /** Evaluates statistics values on view cells.
435        */
436        void EvaluateRenderStatistics(float &totalRenderCost,
437                                                                  float &expectedRenderCost,
438                                                                  float &deviation,
439                                                                  float &variance,
440                                                                  float &totalPvs,
441                                                                  float &avgRenderCost);
442
443
444        /** Returns hierarchy of the view cells.
445        */
446        ViewCellsTree *GetViewCellsTree();
447
448        /** Collect candidates for the view cell merge.
449        */
450        virtual void CollectMergeCandidates(const VssRayContainer &rays,
451                                                                                vector<MergeCandidate> &candidates);
452
453        /** Collects n view cells and stores it as the active view cells.
454        */
455        void CollectViewCells(const int n);
456
457        /** Sets current view cells set to active,
458                i.e., the sampling is done in this view cell set.
459        */
460        void SetViewCellsActive();
461
462        /** Evaluates render cost statistics of current view cell hierarchy.
463        */
464        virtual void EvalViewCellPartition();
465
466        /** Sets maximal size of a view cell filter.
467        */
468        void SetMaxFilterSize(const int size);
469
470        /** Returns maximal filter size.
471        */
472        int GetMaxFilterSize() const;
473
474        /** Deletes interior nodes from the tree which have negative
475                merge cost set (local merge).
476        */ 
477        void DeleteLocalMergeTree(ViewCell *vc) const;
478 
479        /** Evaluautes histogram for a given number of view cells.
480        */
481        void EvalViewCellHistogram(const string filename, const int nViewCells);
482
483        /** Evaluautes histogram for a given number of view cells.
484        */
485        void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
486
487        void EvalViewCellHistogramForPvsSize(const string filename,
488                                                                                 ViewCellContainer &viewCells);
489
490        /** Evaluates the render cost of a view cell.
491        */
492        static float EvalRenderCost(Intersectable *obj);
493   
494        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
495                of the hierarchy.
496        */
497        void UpdateScalarPvsSize(ViewCell *vc, const float pvsCost, const int entriesInPvs) const;
498
499        /** Returns bounding box of a view cell.
500        */
501        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
502
503        /** Exports bounding boxes of objects to file.
504        */
505        bool ExportBoundingBoxes(const string filename, const ObjectContainer &objects) const;
506   
507        /** Load the bounding boxes into the container.
508        */
509        bool LoadBoundingBoxes(const string filename, IndexedBoundingBoxContainer &boxes) const;
510
511        /** Returns true if pvs should be exported together with the view cells.
512        */
513        bool GetExportPvs() const;
514
515        /** Efficiently merges the view cells in the container.
516        */
517        void MergeViewCellsRecursivly(ObjectPvs &pvs,
518                                                                  const ViewCellContainer &viewCells) const;
519
520        virtual void CompressViewCells();
521
522        ViewCell *GetViewCellById(const int id);
523
524        /** Returns number of view cells.
525        */
526        int GetNumViewCells() const;
527
528        bool GenerateRandomViewCells(ViewCellContainer &viewCells,
529                                                                 const int numViewCells);
530
531        bool GenerateRandomViewCells(vector<ViewCellPoints *> &viewCells,
532                                                                 const int nViewCells,
533                                                                 const int nViewPoints);
534
535
536        bool GenerateViewPoints(ViewCell *viewCell,
537                                                        const int numViewPoints,
538                                                        SimpleRayContainer &viewPoints);
539
540        bool ImportRandomViewCells(const string &filename,
541                                                           vector<ViewCellPoints *> &viewCells);
542
543
544        bool ExportRandomViewCells(const string &filename,
545                                                           const vector<ViewCellPoints *> &viewCells);
546
547        bool GenerateViewPoint(ViewCell *viewCell, SimpleRay &ray);
548
549        bool IsValidViewSpace(ViewCell *vc);
550
551        //////////////
552        // static members
553       
554        /** Loads view cells from file. The view cells manager is created with
555                respect to the loaded view cells.
556
557                @param filename the filename of the view cells
558                @param objects the scene objects
559                @param finalizeViewCells if the view cells should be post processed, i.e.
560                        a mesh is created representing the geometry
561                @param bconverter a conversion routine working with the similarities of bounding
562                        boxes: if there is a certain similarity of overlap between
563                        bounding boxes, two tested
564                candidate objects are considered to be the same objects
565                @returns the view cells manager if loading was successful, false otherwise
566        */
567        static ViewCellsManager *LoadViewCells(const string &filename,
568                                                                                   ObjectContainer *objects,
569                                                                                   bool finalizeViewCells = false,
570                                                                                   BoundingBoxConverter *bconverter = NULL);
571
572
573        ////////////////////////////////////////////////////////7
574        // visiblity filter options
575
576        // TODO: write own visibiltiy filter class
577  void ApplyFilter(KdTree *kdTree,
578                                   const float viewSpaceFilterSize,
579                                   const float spatialFilterSize);
580
581  // new adaptive version of the filter
582  PvsFilterStatistics
583  ApplyFilter2(ViewCell *viewCell,
584                           const bool useViewSpaceFilter,
585                           const float filterSize,
586                           ObjectPvs &pvs,
587                           vector<AxisAlignedBox3> *filteredBoxes = NULL);
588
589        void ApplySpatialFilter(KdTree *kdTree,
590                                                        const float spatialFilterSize,
591                                                        ObjectPvs &pvs);
592
593         void ApplyFilter(ViewCell *viewCell,
594                                          KdTree *kdTree,
595                                          const float viewSpaceFilterSize,
596                                          const float spatialFilterSize,
597                                          ObjectPvs &pvs);
598
599        float GetFilterWidth();
600
601        float GetAbsFilterWidth();
602
603        /** Returns the bounding box of filter width.
604        */
605        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
606
607        //////////////////////////////////////////////////////////////////
608
609        /** If true, the kd nodes are stored instead of the object pvs.
610        */
611        void SetStoreKdPvs(const bool storeKdPvs);
612
613        /** Returns true if the kd nodes are stored instead of the object pvs.
614        **/
615        bool GetStoreKdPVs() const;
616
617        /** Exports single view cells for visualization.
618                @param objects the scene objects
619                @param limit the maximal number of output view cells
620                @param sortViewCells if the view cells should be sorted by pvs size
621                @param exportPvs if the pvs should also be exported
622                @param exportRays if sample rays should be exported as well
623                @param maxRays maximum number of rays to export
624                @param prefix the prefix for the output file
625                @param visRays additional rays
626        */
627        virtual void ExportSingleViewCells(const ObjectContainer &objects,
628                                                                           const int maxViewCells,
629                                                                           const bool sortViewCells,
630                                                                           const bool exportPvs,
631                                                                           const bool exportRays,
632                                                                           const int maxRays,
633                                                                           const string prefix,
634                                                                           VssRayContainer *visRays = NULL) = NULL;
635
636
637        void ViewCellsManager::ResetPvs();
638
639        Preprocessor *GetPreprocessor() const {
640                return mPreprocessor;
641        }
642
643        void SetPreprocessor(Preprocessor *p) {
644                mPreprocessor = p;
645        }
646
647        void UpdatePvsForEvaluation();
648
649protected:
650
651        void ComputeViewCellContribution(ViewCell *viewCell,
652                                                                         VssRay &ray,
653                                                                         Intersectable *obj,
654                                                                         const Vector3 &pt,
655                                                                         const bool addRays);
656
657        void MergeViewCellsRecursivly(ObjectPvs &pvs,
658                                                                  const ViewCellContainer &viewCells,
659                                                                  const int leftIdx,
660                                                                  const int rightIdx) const;
661
662        /** Intersects box with the tree and returns the number of intersected boxes.
663        @returns number of view cells found
664        */
665        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
666                                                                                ViewCellContainer &viewCells) const;
667
668        /** Tests the visibility filter functionality.
669        */
670        virtual void TestFilter(const ObjectContainer &objects) {};
671
672        /** If the view cells tree was already constructed or not.
673        */
674        bool ViewCellsTreeConstructed() const;
675
676        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
677        */
678        int CastPassSamples(const int samplesPerPass,
679                                                const vector<int> &strategies,
680                                                VssRayContainer &vssRays) const;
681
682        int CastPassSamples2(const int samplesPerPass,
683                                                 VssRayContainer &passSamples) const;
684
685        /** Parse the options from the environment file.
686        */
687        void ParseEnvironment();
688
689        /** Creates unique view cell ids.
690        */
691        void CreateUniqueViewCellIds();
692
693        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
694        */
695        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
696
697        /** Recollects view cells and resets statistics.
698        */
699        void ResetViewCells();
700
701        /** Sets this view cell to active.
702        */
703        void SetViewCellActive(ViewCell *vc) const;
704
705        /** Collects the view cells in the view cell container.
706        */
707        virtual void CollectViewCells() = 0;
708
709        /** Evaluates view cells statistics and stores it in
710        mViewCellsStatistics.
711        */
712        void EvaluateViewCellsStats();
713
714
715        ///////////////////////
716        //-- helper functions for view cell visualization
717
718        /** Exports the view cell partition.
719        */
720        void ExportViewCellsForViz(Exporter *exporter,
721                                                           const AxisAlignedBox3 *box,
722                                                           const bool colorCode,
723                                                           const AxisAlignedPlane *clipPlane) const;
724
725        /** Sets exporter color.
726        */
727        virtual void ExportColor(Exporter *exporter,
728                                                         ViewCell *vc,
729                                                         const bool colorCode) const;
730
731        /** Creates meshes from the view cells.
732        */
733        void CreateViewCellMeshes();
734
735        /** Creates clip plane for visualization.
736        */
737        void CreateClipPlane();
738
739        AxisAlignedPlane *GetClipPlane();
740
741        void ExportMergedViewCells(const ObjectContainer &objects);
742
743        ///////////////////////
744
745        /** Returns volume of the view space.
746        */
747        virtual float GetViewSpaceVolume();
748        /** Prepares the view cells for sampling after loading them from disc.
749        */
750        virtual void PrepareLoadedViewCells() {};
751        /** Constructs local view cell merge hierarchy.
752        */
753        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
754                const ViewCellContainer &viewCells);
755
756        /** Constructs local view cell merge hierarchy based solely on similarity with the
757        current viewcell
758        */
759        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
760                                                                           const ViewCellContainer &viewCells);
761 
762        /** Updates pvs of all view cells for statistical evaluation after some more sampling
763        */
764        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
765
766        virtual void ExportStats(const string &mergeStats);
767
768 
769        ////////////////////////////////////////////////
770
771        Preprocessor *mPreprocessor;
772       
773        /// if bounding boxes should be exported together with the view cells
774        bool mExportBboxesForPvs;
775       
776        /// the clip plane for visualization
777        AxisAlignedPlane mClipPlaneForViz;
778       
779        /// if the visualization is using the clip plane
780        bool mUseClipPlaneForViz;
781       
782        /// Renders the view cells.
783        Renderer *mRenderer;
784       
785        /// Loaded view cells
786        ViewCellContainer mViewCells;
787       
788        /// the view cell hierarchy (i.e., the logical description of view cells)
789        ViewCellsTree *mViewCellsTree;
790       
791        std::vector<int> mStrategies;
792
793        /** if the values in the view cell leaves and the interiors are up to date
794                this is meant for lazy storing of the pvs, where only a scalar indicating
795                pvs size is stored in interiors and not the pvs itself.
796        */
797        bool mViewCellPvsIsUpdated;
798
799        /// maximum number of samples for the view cell construction
800        int mConstructionSamples;
801        int mSamplesPerPass;
802        int mInitialSamples;
803        int mPostProcessSamples;
804        int mVisualizationSamples;
805
806        float mTotalAreaValid;
807        float mTotalArea;
808
809        int mMaxPvsSize;
810        int mMinPvsSize;
811        float mMaxPvsRatio;
812
813        int mSamplingType;
814        int mEvaluationSamplingType;
815        int mNumActiveViewCells;
816        bool mCompressViewCells;
817
818        /// holds the current view cell statistics
819        ViewCellsStatistics mCurrentViewCellsStats;
820        /// the scene bounding box
821        AxisAlignedBox3 mViewSpaceBox;
822       
823        /// if view cells should be exported
824        bool mExportViewCells;
825
826        // if only valid view cells should be considered for processing
827        bool mOnlyValidViewCells;
828
829        /// if rays should be used to collect merge candidates
830        bool mUseRaysForMerge;
831
832        /// if there should be an additional merge step after the subdivision
833        bool mMergeViewCells;
834
835        /// the width of the box filter
836        float mFilterWidth;
837        /// Maximal size of the filter in terms of contributing view cells
838        int mMaxFilterSize;
839       
840        // only for debugging
841        VssRayContainer storedRays;
842
843        bool mUseKdPvs;
844        bool mUseKdPvsAfterFiltering;
845
846        MixtureDistribution *mMixtureDistribution;
847
848        //////////////////
849        //-- visualization options
850       
851        /// color code for view cells visualization
852        bool mShowVisualization;
853        int mColorCode;
854        bool mExportGeometry;
855        bool mExportRays;
856        bool mViewCellsFinished;
857        bool mEvaluateViewCells;
858
859        /// if pvs should be exported with view cells
860        bool mExportPvs;
861
862        static int sRenderCostEvaluationType;
863
864        /// if view cells geometry should be used from other sources
865        bool mUsePredefinedViewCells;
866
867 
868  vector<PerViewCellStat> mPerViewCellStat;
869  SamplesStatistics mSamplesStat;
870};
871
872
873/** Manages different higher order operations on the view cells.
874*/
875class BspViewCellsManager: public ViewCellsManager
876{
877
878public:
879        /** Constructor taking the bsp tree and the number of samples
880                used to construct the bsp tree.
881        */
882        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
883
884        ~BspViewCellsManager();
885
886        int ConstructSubdivision(const ObjectContainer &objects,
887                                  const VssRayContainer &rays);
888
889        int PostProcess(const ObjectContainer &objects,
890                                        const VssRayContainer &rays);
891
892        void Visualize(const ObjectContainer &objects,
893                                   const VssRayContainer &sampleRays);
894
895        int GetType() const;
896       
897        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
898
899        bool ViewCellsConstructed() const;
900
901        //void PrintStatistics(ostream &s) const;
902
903        int CastLineSegment(const Vector3 &origin,
904                                                const Vector3 &termination,
905                                                ViewCellContainer &viewcells);
906       
907        /** Returns the probability that the view point lies
908                in this view cells.
909        */
910        float GetProbability(ViewCell *viewCell);
911
912        /** Get a viewcell containing the specified point.
913        */
914        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
915
916        /** Creates mesh for this view cell.
917        */
918        void CreateMesh(ViewCell *vc);
919
920        void ExportViewCellGeometry(Exporter *exporter,
921                                                                ViewCell *vc,
922                                                                const AxisAlignedBox3 *box,
923                                                                const AxisAlignedPlane *clipPlane = NULL
924                                                                ) const;
925       
926        void CollectMergeCandidates(const VssRayContainer &rays,
927                                                                vector<MergeCandidate> &candidates);
928
929        void Finalize(ViewCell *viewCell, const bool createMesh);
930
931        bool ExportViewCells(const string filename,
932                                                 const bool exportPvs,
933                                                 const ObjectContainer &objects);
934
935        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
936        */
937        ViewCell *ConstructSpatialMergeTree(BspNode *root);
938
939        void ExportSingleViewCells(const ObjectContainer &objects,
940                                                           const int maxViewCells,
941                                                           const bool sortViewCells,
942                                                           const bool exportPvs,
943                                                           const bool exportRays,
944                                                           const int maxRays,
945                                                           const string prefix,
946                                                           VssRayContainer *visRays = NULL);
947
948        bool LineSegmentIntersects(const Vector3 &origin,
949                                                           const Vector3 &termination,
950                                                           ViewCell *viewCell);
951
952protected:
953
954        void CollectViewCells();
955       
956        /// the BSP tree.
957        BspTree *mBspTree;
958        vector<BspRay *> mBspRays;
959
960private:
961
962        /** Constructs a spatial merge tree only 2 levels deep.
963        */
964        ViewCell *ConstructDummyMergeTree(BspNode *root);
965
966        /** Exports visualization of the BSP splits.
967        */
968        void ExportSplits(const ObjectContainer &objects);
969
970        /** test if subdivision is valid in terms of volume / area.
971        */
972        void TestSubdivision();
973};
974
975
976/**
977        Manages different higher order operations on the KD type view cells.
978*/
979class KdViewCellsManager: public ViewCellsManager
980{
981
982public:
983
984        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
985
986        int ConstructSubdivision(const ObjectContainer &objects,
987                                                         const VssRayContainer &rays);
988
989        int CastLineSegment(const Vector3 &origin,
990                                                const Vector3 &termination,
991                                                ViewCellContainer &viewcells);
992
993        int PostProcess(const ObjectContainer &objects,
994                                        const VssRayContainer &rays);
995
996        void Visualize(const ObjectContainer &objects,
997                                   const VssRayContainer &sampleRays);
998
999        int GetType() const;
1000
1001        bool ViewCellsConstructed() const;
1002
1003        ViewCell *GenerateViewCell(Mesh *mesh) const;
1004
1005        /** Prints out statistics of this approach.
1006        */
1007        //  virtual void PrintStatistics(ostream &s) const;
1008        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1009        { return NULL; }
1010
1011        float GetProbability(ViewCell *viewCell);
1012       
1013        void CreateMesh(ViewCell *vc);
1014
1015        void ExportViewCellGeometry(Exporter *exporter,
1016                                                                ViewCell *vc,
1017                                                                const AxisAlignedBox3 *box,
1018                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1019
1020
1021        void CollectMergeCandidates(const VssRayContainer &rays,
1022                                                                vector<MergeCandidate> &candidates);
1023
1024        void ExportSingleViewCells(const ObjectContainer &objects,
1025                                                           const int maxViewCells,
1026                                                           const bool sortViewCells,
1027                                                           const bool exportPvs,
1028                                                           const bool exportRays,
1029                                                           const int maxRays,
1030                                                           const string prefix,
1031                                                           VssRayContainer *visRays = NULL);
1032
1033        bool LineSegmentIntersects(const Vector3 &origin,
1034                                                           const Vector3 &termination,
1035                                                           ViewCell *viewCell);
1036
1037protected:
1038
1039        /** Collects view cells from a hierarchy.
1040        */
1041        void CollectViewCells();
1042
1043        KdNode *GetNodeForPvs(KdLeaf *leaf);
1044
1045        ////////////////////////////////////////
1046
1047        /// the BSP tree.
1048        KdTree *mKdTree;
1049
1050        /// depth of the KD tree nodes with represent the view cells
1051        int mKdPvsDepth;
1052
1053
1054};
1055
1056
1057/** Manages different higher order operations on the view cells.
1058*/
1059class VspBspViewCellsManager: public ViewCellsManager
1060{
1061
1062public:
1063
1064        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
1065        ~VspBspViewCellsManager();
1066
1067        int ConstructSubdivision(const ObjectContainer &objects,
1068                                                         const VssRayContainer &rays);
1069
1070        int PostProcess(const ObjectContainer &objects,
1071                                        const VssRayContainer &rays);
1072
1073        void Visualize(const ObjectContainer &objects,
1074                                   const VssRayContainer &sampleRays);
1075
1076        int GetType() const;
1077       
1078        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1079
1080        bool ViewCellsConstructed() const;
1081       
1082        int CastLineSegment(const Vector3 &origin,
1083                                                const Vector3 &termination,
1084                                                ViewCellContainer &viewcells);
1085
1086        float GetProbability(ViewCell *viewCell);
1087       
1088        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1089
1090        bool GetViewPoint(Vector3 &viewPoint) const;
1091
1092        bool ViewPointValid(const Vector3 &viewPoint) const;
1093
1094        void CreateMesh(ViewCell *vc);
1095
1096        bool ExportViewCells(const string filename,
1097                                                 const bool exportPvs,
1098                                                 const ObjectContainer &objects);
1099
1100        int CastBeam(Beam &beam);
1101       
1102        void ExportViewCellGeometry(Exporter *exporter,
1103                                                                ViewCell *vc,
1104                                                                const AxisAlignedBox3 *box,
1105                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1106
1107
1108        void Finalize(ViewCell *viewCell, const bool createMesh);
1109
1110        void CollectMergeCandidates(const VssRayContainer &rays,
1111                                                                vector<MergeCandidate> &candidates);
1112
1113        void ExportSingleViewCells(const ObjectContainer &objects,
1114                                                           const int maxViewCells,
1115                                                           const bool sortViewCells,
1116                                                           const bool exportPvs,
1117                                                           const bool exportRays,               
1118                                                           const int maxRays,
1119                                                           const string prefix,
1120                                                           VssRayContainer *visRays = NULL);
1121
1122       
1123        bool LineSegmentIntersects(const Vector3 &origin,
1124                                                           const Vector3 &termination,
1125                                                           ViewCell *viewCell);
1126
1127protected:
1128
1129        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1130                                                                ViewCellContainer &viewCells) const;
1131       
1132        /** Merges view cells according to some criteria
1133        */
1134        void MergeViewCells(const VssRayContainer &rays,
1135                                                const ObjectContainer &objects);
1136       
1137        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
1138
1139        void CollectViewCells();
1140
1141        /** Returns maximal depth difference of view cell
1142                leaves in tree.
1143        */
1144        int GetMaxTreeDiff(ViewCell *vc) const;
1145
1146        /** Prepare view cells for use after loading them from disc.
1147        */
1148        void PrepareLoadedViewCells();
1149
1150        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1151        */
1152        ViewCell *ConstructSpatialMergeTree(BspNode *root);
1153
1154        /// HACK for testing visibility filter functionality
1155        void TestFilter(const ObjectContainer &objects);
1156
1157        /** Visualization of the pvs difference to exact visubility using
1158                from point queries.
1159        */
1160        void VisualizeWithFromPointQueries();
1161
1162        /** Evaluate from point queries for the current scene.
1163        */
1164        void EvalFromPointQueries();
1165
1166        /** Exports visualization of the BSP splits.
1167        */
1168        void ExportSplits(const ObjectContainer &objects,
1169                                          const VssRayContainer &rays);
1170
1171
1172        /////////////////////////
1173
1174        /// the view space partition BSP tree.
1175        VspBspTree *mVspBspTree;
1176
1177
1178private:
1179
1180        /** test if subdivision is valid in terms of volume / area.
1181        */
1182        void TestSubdivision();
1183};
1184
1185#define TEST_EVALUATION 0
1186
1187/**
1188        Manages different higher order operations on the view cells.
1189*/
1190class VspOspViewCellsManager: public ViewCellsManager
1191{
1192        friend class ViewCellsParseHandlers;
1193
1194public:
1195
1196        VspOspViewCellsManager(ViewCellsTree *vcTree, const string &hierarchyType);
1197       
1198        ~VspOspViewCellsManager();
1199
1200        int ConstructSubdivision(const ObjectContainer &objects,
1201                                                         const VssRayContainer &rays);
1202
1203        int PostProcess(const ObjectContainer &objects,
1204                                        const VssRayContainer &rays);
1205
1206        void Visualize(const ObjectContainer &objects,
1207                                   const VssRayContainer &sampleRays);
1208
1209        int GetType() const;
1210       
1211        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1212
1213        virtual Intersectable *GetIntersectable(
1214                const VssRay &ray, const bool isTermination) const;
1215
1216        bool ViewCellsConstructed() const;
1217
1218       
1219        int CastLineSegment(const Vector3 &origin,
1220                                                const Vector3 &termination,
1221                                                ViewCellContainer &viewcells);
1222
1223        bool LineSegmentIntersects(const Vector3 &origin,
1224                                                           const Vector3 &termination,
1225                                                           ViewCell *viewCell);
1226
1227        float GetProbability(ViewCell *viewCell);
1228       
1229        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1230
1231        bool GetViewPoint(Vector3 &viewPoint) const;
1232
1233        bool ViewPointValid(const Vector3 &viewPoint) const;
1234
1235        void CreateMesh(ViewCell *vc);
1236
1237        bool ExportViewCells(const string filename,
1238                                                 const bool exportPvs,
1239                                                 const ObjectContainer &objects);
1240
1241        int CastBeam(Beam &beam);
1242
1243        void Finalize(ViewCell *viewCell, const bool createMesh);
1244
1245        ViewCellsManager *LoadViewCells(const string &filename,
1246                                                                        ObjectContainer *objects,
1247                                                                        const bool finalizeViewCells,
1248                                                                        BoundingBoxConverter *bconverter);
1249
1250        void ExportSingleViewCells(const ObjectContainer &objects,
1251                                                           const int maxViewCells,
1252                                                           const bool sortViewCells,
1253                                                           const bool exportPvs,
1254                                                           const bool exportRays,
1255                                                           const int maxRays,
1256                                                           const string prefix,
1257                                                           VssRayContainer *visRays = NULL);
1258       
1259        float UpdateObjectCosts();
1260
1261protected:
1262
1263        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1264       
1265#if 1//TEST_EVALUATION
1266        virtual void EvalViewCellPartition();
1267#endif
1268
1269        /** Exports view cell geometry.
1270        */
1271        void ExportViewCellGeometry(Exporter *exporter,
1272                                                                ViewCell *vc,
1273                                                                const AxisAlignedBox3 *box,
1274                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1275
1276        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1277                                                                ViewCellContainer &viewCells) const;
1278       
1279        void CollectViewCells();
1280
1281        virtual void CompressViewCells();
1282
1283        /** Prepare view cells for use after loading them from disc.
1284        */
1285        void PrepareLoadedViewCells();
1286
1287        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1288        */
1289        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1290
1291        /** Exports visualization of the PVS.
1292        */
1293        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
1294
1295        /** Returns a hierarchy manager of the given name.
1296        */
1297        static HierarchyManager *CreateHierarchyManager(const string &name);
1298
1299        //void ExportStats(const string &mergeStats);
1300
1301        /** collect objects intersecting a given spatial box
1302        */
1303        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
1304
1305
1306        /////////////////////////////////////////
1307
1308
1309        bool mCompressObjects;
1310
1311        HierarchyManager *mHierarchyManager;
1312};
1313
1314
1315}
1316
1317#endif
Note: See TracBrowser for help on using the repository browser.