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

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