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

Revision 1983, 34.3 KB checked in by bittner, 17 years ago (diff)

merge

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