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

Revision 2227, 36.1 KB checked in by mattausch, 17 years ago (diff)

added render cost bound for objects, improved undersampling compensation

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