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

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