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

Revision 2116, 35.9 KB checked in by mattausch, 17 years ago (diff)

implemented hashpvs

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