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

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

using mutationsamples for evaluation

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