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

Revision 1867, 32.7 KB checked in by bittner, 18 years ago (diff)

merge, global lines, rss sampling updates

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