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

Revision 1902, 32.8 KB checked in by mattausch, 18 years ago (diff)

added kd pvs as option to view cells manager to avoid errors when updating the svn from jiri

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