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

Revision 2232, 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                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        int mEvaluationSamples;
842
843        float mTotalAreaValid;
844        float mTotalArea;
845
846        int mMaxPvsSize;
847        int mMinPvsSize;
848        float mMaxPvsRatio;
849
850        int mSamplingType;
851        int mEvaluationSamplingType;
852        int mNumActiveViewCells;
853        bool mCompressViewCells;
854
855        vector<ViewCellPoints *> mViewCellPoints;
856
857        /// holds the current view cell statistics
858        ViewCellsStatistics mCurrentViewCellsStats;
859
860        /// the scene bounding box
861        AxisAlignedBox3 mViewSpaceBox;
862       
863        /// if view cells should be exported
864        bool mExportViewCells;
865
866        // if only valid view cells should be considered for processing
867        bool mOnlyValidViewCells;
868
869        /// if rays should be used to collect merge candidates
870        bool mUseRaysForMerge;
871
872        /// if there should be an additional merge step after the subdivision
873        bool mMergeViewCells;
874
875        /// the width of the box filter
876        float mFilterWidth;
877        /// Maximal size of the filter in terms of contributing view cells
878        int mMaxFilterSize;
879       
880        // only for debugging
881        VssRayContainer storedRays;
882
883        bool mUseKdPvs;
884        bool mUseKdPvsAfterFiltering;
885
886        MixtureDistribution *mMixtureDistribution;
887
888
889        //////////////////
890        //-- visualization options
891       
892        /// color code for view cells visualization
893        bool mShowVisualization;
894        int mColorCode;
895        bool mExportGeometry;
896        bool mExportRays;
897        bool mViewCellsFinished;
898        bool mEvaluateViewCells;
899
900        /// if pvs should be exported with view cells
901        bool mExportPvs;
902
903        static int sRenderCostEvaluationType;
904
905        /// if view cells geometry should be used from other sources
906        bool mUsePredefinedViewCells;
907
908 
909  vector<PerViewCellStat> mPerViewCellStat;
910  SamplesStatistics mSamplesStat;
911};
912
913
914/** Manages different higher order operations on the view cells.
915*/
916class BspViewCellsManager: public ViewCellsManager
917{
918
919public:
920        /** Constructor taking the bsp tree and the number of samples
921                used to construct the bsp tree.
922        */
923        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
924
925        ~BspViewCellsManager();
926
927        int ConstructSubdivision(const ObjectContainer &objects,
928                                  const VssRayContainer &rays);
929
930        int PostProcess(const ObjectContainer &objects,
931                                        const VssRayContainer &rays);
932
933        void Visualize(const ObjectContainer &objects,
934                                   const VssRayContainer &sampleRays);
935
936        int GetType() const;
937       
938        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
939
940        bool ViewCellsConstructed() const;
941
942        //void PrintStatistics(ostream &s) const;
943
944        int CastLineSegment(const Vector3 &origin,
945                                                const Vector3 &termination,
946                                                ViewCellContainer &viewcells);
947       
948        /** Returns the probability that the view point lies
949                in this view cells.
950        */
951        float GetProbability(ViewCell *viewCell);
952
953        /** Get a viewcell containing the specified point.
954        */
955        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
956
957        /** Creates mesh for this view cell.
958        */
959        void CreateMesh(ViewCell *vc);
960
961        void ExportViewCellGeometry(Exporter *exporter,
962                                                                ViewCell *vc,
963                                                                const AxisAlignedBox3 *box,
964                                                                const AxisAlignedPlane *clipPlane = NULL
965                                                                ) const;
966       
967        void CollectMergeCandidates(const VssRayContainer &rays,
968                                                                vector<MergeCandidate> &candidates);
969
970        void Finalize(ViewCell *viewCell, const bool createMesh);
971
972        bool ExportViewCells(const string filename,
973                                                 const bool exportPvs,
974                                                 const ObjectContainer &objects);
975
976        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
977        */
978        ViewCell *ConstructSpatialMergeTree(BspNode *root);
979
980        void ExportSingleViewCells(const ObjectContainer &objects,
981                                                           const int maxViewCells,
982                                                           const bool sortViewCells,
983                                                           const bool exportPvs,
984                                                           const bool exportRays,
985                                                           const int maxRays,
986                                                           const string prefix,
987                                                           VssRayContainer *visRays = NULL);
988
989        bool LineSegmentIntersects(const Vector3 &origin,
990                                                           const Vector3 &termination,
991                                                           ViewCell *viewCell);
992
993protected:
994
995        void CollectViewCells();
996       
997        /// the BSP tree.
998        BspTree *mBspTree;
999        vector<BspRay *> mBspRays;
1000
1001private:
1002
1003        /** Constructs a spatial merge tree only 2 levels deep.
1004        */
1005        ViewCell *ConstructDummyMergeTree(BspNode *root);
1006
1007        /** Exports visualization of the BSP splits.
1008        */
1009        void ExportSplits(const ObjectContainer &objects);
1010
1011        /** test if subdivision is valid in terms of volume / area.
1012        */
1013        void TestSubdivision();
1014};
1015
1016
1017/**
1018        Manages different higher order operations on the KD type view cells.
1019*/
1020class KdViewCellsManager: public ViewCellsManager
1021{
1022
1023public:
1024
1025        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
1026
1027        int ConstructSubdivision(const ObjectContainer &objects,
1028                                                         const VssRayContainer &rays);
1029
1030        int CastLineSegment(const Vector3 &origin,
1031                                                const Vector3 &termination,
1032                                                ViewCellContainer &viewcells);
1033
1034        int PostProcess(const ObjectContainer &objects,
1035                                        const VssRayContainer &rays);
1036
1037        void Visualize(const ObjectContainer &objects,
1038                                   const VssRayContainer &sampleRays);
1039
1040        int GetType() const;
1041
1042        bool ViewCellsConstructed() const;
1043
1044        ViewCell *GenerateViewCell(Mesh *mesh) const;
1045
1046        /** Prints out statistics of this approach.
1047        */
1048        //  virtual void PrintStatistics(ostream &s) const;
1049        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1050        { return NULL; }
1051
1052        float GetProbability(ViewCell *viewCell);
1053       
1054        void CreateMesh(ViewCell *vc);
1055
1056        void ExportViewCellGeometry(Exporter *exporter,
1057                                                                ViewCell *vc,
1058                                                                const AxisAlignedBox3 *box,
1059                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1060
1061
1062        void CollectMergeCandidates(const VssRayContainer &rays,
1063                                                                vector<MergeCandidate> &candidates);
1064
1065        void ExportSingleViewCells(const ObjectContainer &objects,
1066                                                           const int maxViewCells,
1067                                                           const bool sortViewCells,
1068                                                           const bool exportPvs,
1069                                                           const bool exportRays,
1070                                                           const int maxRays,
1071                                                           const string prefix,
1072                                                           VssRayContainer *visRays = NULL);
1073
1074        bool LineSegmentIntersects(const Vector3 &origin,
1075                                                           const Vector3 &termination,
1076                                                           ViewCell *viewCell);
1077
1078protected:
1079
1080        /** Collects view cells from a hierarchy.
1081        */
1082        void CollectViewCells();
1083
1084        KdNode *GetNodeForPvs(KdLeaf *leaf);
1085
1086        ////////////////////////////////////////
1087
1088        /// the BSP tree.
1089        KdTree *mKdTree;
1090
1091        /// depth of the KD tree nodes with represent the view cells
1092        int mKdPvsDepth;
1093
1094
1095};
1096
1097
1098/** Manages different higher order operations on the view cells.
1099*/
1100class VspBspViewCellsManager: public ViewCellsManager
1101{
1102
1103public:
1104
1105        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
1106        ~VspBspViewCellsManager();
1107
1108        int ConstructSubdivision(const ObjectContainer &objects,
1109                                                         const VssRayContainer &rays);
1110
1111        int PostProcess(const ObjectContainer &objects,
1112                                        const VssRayContainer &rays);
1113
1114        void Visualize(const ObjectContainer &objects,
1115                                   const VssRayContainer &sampleRays);
1116
1117        int GetType() const;
1118       
1119        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1120
1121        bool ViewCellsConstructed() const;
1122       
1123        int CastLineSegment(const Vector3 &origin,
1124                                                const Vector3 &termination,
1125                                                ViewCellContainer &viewcells);
1126
1127        float GetProbability(ViewCell *viewCell);
1128       
1129        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1130
1131        bool GetViewPoint(Vector3 &viewPoint) const;
1132
1133        bool ViewPointValid(const Vector3 &viewPoint) const;
1134
1135        void CreateMesh(ViewCell *vc);
1136
1137        bool ExportViewCells(const string filename,
1138                                                 const bool exportPvs,
1139                                                 const ObjectContainer &objects);
1140
1141        int CastBeam(Beam &beam);
1142       
1143        void ExportViewCellGeometry(Exporter *exporter,
1144                                                                ViewCell *vc,
1145                                                                const AxisAlignedBox3 *box,
1146                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1147
1148
1149        void Finalize(ViewCell *viewCell, const bool createMesh);
1150
1151        void CollectMergeCandidates(const VssRayContainer &rays,
1152                                                                vector<MergeCandidate> &candidates);
1153
1154        void ExportSingleViewCells(const ObjectContainer &objects,
1155                                                           const int maxViewCells,
1156                                                           const bool sortViewCells,
1157                                                           const bool exportPvs,
1158                                                           const bool exportRays,               
1159                                                           const int maxRays,
1160                                                           const string prefix,
1161                                                           VssRayContainer *visRays = NULL);
1162
1163       
1164        bool LineSegmentIntersects(const Vector3 &origin,
1165                                                           const Vector3 &termination,
1166                                                           ViewCell *viewCell);
1167
1168protected:
1169
1170        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1171                                                                ViewCellContainer &viewCells) const;
1172       
1173        /** Merges view cells according to some criteria
1174        */
1175        void MergeViewCells(const VssRayContainer &rays,
1176                                                const ObjectContainer &objects);
1177       
1178        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
1179
1180        void CollectViewCells();
1181
1182        /** Returns maximal depth difference of view cell
1183                leaves in tree.
1184        */
1185        int GetMaxTreeDiff(ViewCell *vc) const;
1186
1187        /** Prepare view cells for use after loading them from disc.
1188        */
1189        void PrepareLoadedViewCells();
1190
1191        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1192        */
1193        ViewCell *ConstructSpatialMergeTree(BspNode *root);
1194
1195        /// HACK for testing visibility filter functionality
1196        void TestFilter(const ObjectContainer &objects);
1197
1198        /** Visualization of the pvs difference to exact visubility using
1199                from point queries.
1200        */
1201        void VisualizeWithFromPointQueries();
1202
1203        /** Evaluate from point queries for the current scene.
1204        */
1205        void EvalFromPointQueries();
1206
1207        /** Exports visualization of the BSP splits.
1208        */
1209        void ExportSplits(const ObjectContainer &objects,
1210                                          const VssRayContainer &rays);
1211
1212
1213        /////////////////////////
1214
1215        /// the view space partition BSP tree.
1216        VspBspTree *mVspBspTree;
1217
1218
1219private:
1220
1221        /** test if subdivision is valid in terms of volume / area.
1222        */
1223        void TestSubdivision();
1224};
1225
1226#define TEST_EVALUATION 0
1227
1228/**
1229        Manages different higher order operations on the view cells.
1230*/
1231class VspOspViewCellsManager: public ViewCellsManager
1232{
1233        friend class ViewCellsParseHandlers;
1234
1235public:
1236
1237        VspOspViewCellsManager(ViewCellsTree *vcTree,
1238                                                   const string &hierarchyType);
1239       
1240        ~VspOspViewCellsManager();
1241
1242        int ConstructSubdivision(const ObjectContainer &objects,
1243                                                         const VssRayContainer &rays);
1244
1245        int PostProcess(const ObjectContainer &objects,
1246                                        const VssRayContainer &rays);
1247
1248        void Visualize(const ObjectContainer &objects,
1249                                   const VssRayContainer &sampleRays);
1250
1251        int GetType() const;
1252       
1253        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1254
1255        virtual Intersectable *GetIntersectable(
1256                const VssRay &ray, const bool isTermination) const;
1257
1258        bool ViewCellsConstructed() const;
1259
1260       
1261        int CastLineSegment(const Vector3 &origin,
1262                                                const Vector3 &termination,
1263                                                ViewCellContainer &viewcells);
1264
1265        bool LineSegmentIntersects(const Vector3 &origin,
1266                                                           const Vector3 &termination,
1267                                                           ViewCell *viewCell);
1268
1269        float GetProbability(ViewCell *viewCell);
1270       
1271        ViewCell *GetViewCell(const Vector3 &point,
1272                                                  const bool active = false) const;
1273
1274        bool GetViewPoint(Vector3 &viewPoint) const;
1275
1276        bool ViewPointValid(const Vector3 &viewPoint) const;
1277
1278        void CreateMesh(ViewCell *vc);
1279
1280        bool ExportViewCells(const string filename,
1281                                                 const bool exportPvs,
1282                                                 const ObjectContainer &objects);
1283
1284        int CastBeam(Beam &beam);
1285
1286        void Finalize(ViewCell *viewCell, const bool createMesh);
1287
1288        void ExportSingleViewCells(const ObjectContainer &objects,
1289                                                           const int maxViewCells,
1290                                                           const bool sortViewCells,
1291                                                           const bool exportPvs,
1292                                                           const bool exportRays,
1293                                                           const int maxRays,
1294                                                           const string prefix,
1295                                                           VssRayContainer *visRays = NULL);
1296       
1297        float UpdateObjectCosts();
1298
1299        virtual void FinalizeViewCells(const bool createMesh);
1300
1301protected:
1302
1303        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1304       
1305#if 1//TEST_EVALUATION
1306        virtual void EvalViewCellPartition();
1307#endif
1308
1309        /** Exports view cell geometry.
1310        */
1311        void ExportViewCellGeometry(Exporter *exporter,
1312                                                                ViewCell *vc,
1313                                                                const AxisAlignedBox3 *box,
1314                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1315
1316        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1317                                                                ViewCellContainer &viewCells) const;
1318       
1319        void CollectViewCells();
1320
1321        virtual void CompressViewCells();
1322
1323        /** Prepare view cells for use after loading them from disc.
1324        */
1325        void PrepareLoadedViewCells();
1326
1327        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1328        */
1329        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1330
1331        /** Exports visualization of the PVS.
1332        */
1333        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
1334
1335        /** Returns a hierarchy manager of the given name.
1336        */
1337        static HierarchyManager *CreateHierarchyManager(const string &name);
1338
1339        //void ExportStats(const string &mergeStats);
1340
1341        /** collect objects intersecting a given spatial box
1342        */
1343        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
1344
1345
1346        /////////////////////////////////////////
1347
1348        bool mCompressObjects;
1349
1350        HierarchyManager *mHierarchyManager;
1351};
1352
1353
1354}
1355
1356#endif
Note: See TracBrowser for help on using the repository browser.