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

Revision 1764, 31.9 KB checked in by mattausch, 18 years ago (diff)

removed error in sample registration

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