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

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