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

Revision 2176, 36.0 KB checked in by mattausch, 17 years ago (diff)

removed using namespace std from .h

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