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

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