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

Revision 1787, 32.5 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;
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        /** Returns true if this view point is in the valid view space.
311        */
312        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
313
314        /** Sets a view space boundary.
315        */
316        void SetViewSpaceBox(const AxisAlignedBox3 &box);
317
318        AxisAlignedBox3 GetViewSpaceBox() const;
319
320        /** Creates mesh for this view cell.
321        */
322        virtual void CreateMesh(ViewCell *vc) = NULL;
323
324        /** Writes view cells to disc.
325        */
326        virtual bool ExportViewCells(const string filename,
327                                                                 const bool exportPvs,
328                                                                 const ObjectContainer &objects);
329
330        /** Casts beam to collect view cells.
331        */
332        virtual int CastBeam(Beam &beam);
333
334        /** Checks if view cell is considered as valid.
335        */
336        virtual bool CheckValidity(ViewCell *vc,
337                                                           int minPvsSize,
338                                                           int maxPvsSize) const;
339
340       
341        /** Sets validity of view cell
342        */
343        virtual void SetValidity(ViewCell *vc,
344                                                         int minPvsSize,
345                                                         int maxPvsSize) const;
346
347        /** sets validy of all viewcells
348        */
349        virtual void SetValidity(int minPvsSize,
350                                                         int maxPvsSize) const;
351
352
353        /** set valid viewcells in the range of pvs. sorts the viewcells
354          according to the pvs and then pickups those in the ranges
355          */
356        void SetValidityPercentage(const float minValid, const float maxValid);
357
358        /** Returns number of valid view cells.
359        */
360    int CountValidViewcells() const;
361
362        /** Returns maximal allowed pvs size.
363        */
364        int GetMaxPvsSize() const;
365
366        /** Returns maximal allowed pvs size.
367        */
368        int GetMinPvsSize() const;
369        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
370                where pvs is still considered valid.
371        */
372        float GetMaxPvsRatio() const;
373        /** Exports view cell geometry.
374        */
375        virtual void ExportViewCellGeometry(
376                Exporter *exporter,
377                ViewCell *vc,
378                const AxisAlignedBox3 *box,
379                const AxisAlignedPlane *clipPlane = NULL
380                ) const = 0;
381
382        /** Brings the view cells into their final state, computes meshes and volume.
383        */
384        virtual void FinalizeViewCells(const bool createMesh);
385
386        /** Evaluates statistics values on view cells.
387        */
388        void EvaluateRenderStatistics(float &totalRenderCost,
389                                                                  float &expectedRenderCost,
390                                                                  float &deviation,
391                                                                  float &variance,
392                                                                  float &totalPvs,
393                                                                  float &avgRenderCost);
394
395
396        /** Returns hierarchy of the view cells.
397        */
398        ViewCellsTree *GetViewCellsTree();
399
400        /** Collect candidates for the view cell merge.
401        */
402        virtual void CollectMergeCandidates(const VssRayContainer &rays,
403                                                                                vector<MergeCandidate> &candidates);
404
405        /** Collects n view cells and stores it as the active view cells.
406        */
407        void CollectViewCells(const int n);
408
409        /** Sets current view cells set to active,
410                i.e., the sampling is done in this view cell set.
411        */
412        void SetViewCellsActive();
413
414        /** Evaluates render cost statistics of current view cell hierarchy.
415        */
416        virtual void EvalViewCellPartition();
417
418        /** Sets maximal size of a view cell filter.
419        */
420        void SetMaxFilterSize(const int size);
421
422        /** Returns maximal filter size.
423        */
424        int GetMaxFilterSize() const;
425
426        /** Deletes interior nodes from the tree which have negative
427                merge cost set (local merge).
428        */ 
429        void DeleteLocalMergeTree(ViewCell *vc) const;
430 
431        /** Evaluautes histogram for a given number of view cells.
432        */
433        void EvalViewCellHistogram(const string filename, const int nViewCells);
434
435        /** Evaluautes histogram for a given number of view cells.
436        */
437        void EvalViewCellHistogramForPvsSize(const string filename, const int nViewCells);
438
439        /** Evaluates the render cost of a view cell.
440        */
441        static float EvalRenderCost(Intersectable *obj);
442   
443        /** Sets pvs size of view cell as a scalar. Used when storing pvs only in the leaves
444                of the hierarchy.
445        */
446        void UpdateScalarPvsSize(ViewCell *vc, const float pvsCost, const int entriesInPvs) const;
447
448        /** Returns bounding box of a view cell.
449        */
450        AxisAlignedBox3 GetViewCellBox(ViewCell *vc);
451
452        /** Exports bounding boxes of objects to file.
453        */
454        bool ExportBoundingBoxes(const string filename, const ObjectContainer &objects) const;
455   
456        /** Load the bounding boxes into the container.
457        */
458        bool LoadBoundingBoxes(const string filename, IndexedBoundingBoxContainer &boxes) const;
459
460        /** Returns true if pvs should be exported together with the view cells.
461        */
462        bool GetExportPvs() const;
463
464        /** Efficiently merges the view cells in the container.
465        */
466        void MergeViewCellsRecursivly(ObjectPvs &pvs,
467                                                                  const ViewCellContainer &viewCells) const;
468
469
470        /////////////////////////////
471        // static members
472       
473        /** Loads view cells from file. The view cells manager is created with
474                respect to the loaded view cells.
475
476                @param filename the filename of the view cells
477                @param objects the scene objects
478                @param finalizeViewCells if the view cells should be post processed, i.e.
479                        a mesh is created representing the geometry
480                @param bconverter a conversion routine working with the similarities of bounding
481                boxes: if there is a certain similarity of overlap between bounding boxes, two tested
482                candidate objects are considered to be the same objects
483                @returns the view cells manager if loading was successful, false otherwise
484        */
485        static ViewCellsManager *LoadViewCells(const string &filename,
486                                                                                   ObjectContainer *objects,
487                                                                                   bool finalizeViewCells = false,
488                                                                                   BoundingBoxConverter *bconverter = NULL);
489
490
491        ////////////////////////////////////////////////////////7
492        // visiblity filter options
493
494        // TODO: write own visibiltiy filter class
495  void ApplyFilter(KdTree *kdTree,
496                                         const float viewSpaceFilterSize,
497                                         const float spatialFilterSize);
498
499  // new adaptive version of the filter
500  PvsFilterStatistics
501  ApplyFilter2(ViewCell *viewCell,
502                           const bool useViewSpaceFilter,
503                           const float filterSize,
504                           ObjectPvs &pvs
505                           );
506
507        void ApplySpatialFilter(KdTree *kdTree,
508                                                        const float spatialFilterSize,
509                                                        ObjectPvs &pvs);
510
511         void ApplyFilter(ViewCell *viewCell,
512                                          KdTree *kdTree,
513                                          const float viewSpaceFilterSize,
514                                          const float spatialFilterSize,
515                                          ObjectPvs &pvs);
516
517        float GetFilterWidth();
518
519        float GetAbsFilterWidth();
520
521        /** Returns the bounding box of filter width.
522        */
523        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
524
525        //////////////////////////////////////////////////////////////////
526
527        /** If true, the kd nodes are stored instead of the object pvs.
528        */
529        void SetStoreKdPvs(const bool storeKdPvs);
530
531        /** Returns true if the kd nodes are stored instead of the object pvs.
532        **/
533        bool GetStoreKdPVs() const;
534
535        /** Exports single view cells for visualization.
536                @param objects the scene objects
537                @param limit the maximal number of output view cells
538                @param sortViewCells if the view cells should be sorted by pvs size
539                @param exportPvs if the pvs should also be exported
540                @param exportRays if sample rays should be exported as well
541                @param maxRays maximum number of rays to export
542                @param prefix the prefix for the output file
543                @param visRays additional rays
544        */
545        virtual void ExportSingleViewCells(const ObjectContainer &objects,
546                                                                           const int maxViewCells,
547                                                                           const bool sortViewCells,
548                                                                           const bool exportPvs,
549                                                                           const bool exportRays,
550                                                                           const int maxRays,
551                                                                           const string prefix,
552                                                                           VssRayContainer *visRays = NULL) = NULL;
553
554       
555        void ViewCellsManager::ResetPvs();
556 
557  Preprocessor *GetPreprocessor() const {
558        return mPreprocessor;
559  }
560
561  void SetPreprocessor(Preprocessor *p) {
562        mPreprocessor = p;
563  }
564
565  void UpdatePvsForEvaluation();
566
567protected:
568
569        void MergeViewCellsRecursivly(ObjectPvs &pvs,
570                                                                  const ViewCellContainer &viewCells,
571                                                                  const int leftIdx,
572                                                                  const int rightIdx) const;
573
574        /** Intersects box with the tree and returns the number of intersected boxes.
575                @returns number of view cells found
576        */
577        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
578                                                                                ViewCellContainer &viewCells) const;
579
580        /** Tests the visibility filter functionality.
581        */
582        virtual void TestFilter(const ObjectContainer &objects) {};
583
584        /** If the view cells tree was already constructed or not.
585        */
586        bool ViewCellsTreeConstructed() const;
587
588        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
589        */
590        int CastPassSamples(const int samplesPerPass,
591                                                const vector<int> &strategies,
592                                                VssRayContainer &vssRays) const;
593
594        /** Parse the options from the environment file.
595        */
596        void ParseEnvironment();
597
598        /** Creates unique view cell ids.
599        */
600        void CreateUniqueViewCellIds();
601
602        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
603        */
604        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
605
606        /** Recollects view cells and resets statistics.
607        */
608        void ResetViewCells();
609       
610        /** Sets this view cell to active.
611        */
612        void SetViewCellActive(ViewCell *vc) const;
613
614        /** Collects the view cells in the view cell container.
615        */
616        virtual void CollectViewCells() = 0;
617       
618        /** Evaluates view cells statistics and stores it in
619                mViewCellsStatistics.
620        */
621        void EvaluateViewCellsStats();
622
623       
624        ///////////////////////
625        //-- helper functions for view cell visualization
626
627        /** Exports the view cell partition.
628        */
629        void ExportViewCellsForViz(Exporter *exporter,
630                                                           const AxisAlignedBox3 *box,
631                                                           const bool colorCode,
632                                                           const AxisAlignedPlane *clipPlane
633                                                           ) const;
634
635        /** Sets exporter color.
636        */
637        virtual void ExportColor(Exporter *exporter,
638                                                         ViewCell *vc,
639                                                         const bool colorCode) const;
640       
641        /** Creates meshes from the view cells.
642        */
643        void CreateViewCellMeshes();
644
645    /** Creates clip plane for visualization.
646        */
647        void CreateClipPlane();
648
649        AxisAlignedPlane *GetClipPlane();
650       
651        void ExportMergedViewCells(const ObjectContainer &objects);
652
653        ///////////////////////
654
655        /** Returns volume of the view space.
656        */
657        virtual float GetViewSpaceVolume();
658        /** Prepares the view cells for sampling after loading them from disc.
659        */
660        virtual void PrepareLoadedViewCells() {};
661        /** Constructs local view cell merge hierarchy.
662        */
663        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
664                                                                          const ViewCellContainer &viewCells);
665
666        /** Constructs local view cell merge hierarchy based solely on similarity with the
667                current viewcell
668        */
669        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
670                                                                           const ViewCellContainer &viewCells);
671 
672        /** Updates pvs of all view cells for statistical evaluation after some more sampling
673        */
674        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
675
676        virtual void ExportStats(const string &mergeStats);
677
678 
679        ////////////////////////////////////////////////
680
681        Preprocessor *mPreprocessor;
682        /// if bounding boxes should be exported together with the view cells
683        bool mExportBboxesForPvs;
684        /// the clip plane for visualization
685        AxisAlignedPlane mClipPlaneForViz;
686        /// if the visualization is using the clip plane
687        bool mUseClipPlaneForViz;
688        /// Renders the view cells.
689        Renderer *mRenderer;
690        /// Loaded view cells
691        ViewCellContainer mViewCells;
692        /// the view cell hierarchy (i.e., the logical description of view cells)
693        ViewCellsTree *mViewCellsTree;
694       
695        std::vector<int> mStrategies;
696
697        /** if the values in the view cell leaves and the interiors are up to date
698                this is meant for lazy storing of the pvs, where only a scalar indicating
699                pvs size is stored in interiors and not the pvs itself.
700        */
701        bool mViewCellPvsIsUpdated;
702
703        /// maximum number of samples for the view cell construction
704        int mConstructionSamples;
705        int mSamplesPerPass;
706        int mInitialSamples;
707        int mPostProcessSamples;
708        int mVisualizationSamples;
709
710        float mTotalAreaValid;
711        float mTotalArea;
712
713        int mMaxPvsSize;
714        int mMinPvsSize;
715        float mMaxPvsRatio;
716
717        int mSamplingType;
718        int mEvaluationSamplingType;
719        int mNumActiveViewCells;
720        bool mCompressViewCells;
721
722        /// holds the current view cell statistics
723        ViewCellsStatistics mCurrentViewCellsStats;
724        /// the scene bounding box
725        AxisAlignedBox3 mViewSpaceBox;
726       
727        /// if view cells should be exported
728        bool mExportViewCells;
729
730        // if only valid view cells should be considered for processing
731        bool mOnlyValidViewCells;
732
733        /// if rays should be used to collect merge candidates
734        bool mUseRaysForMerge;
735
736        /// if there should be an additional merge step after the subdivision
737        bool mMergeViewCells;
738
739        /// the width of the box filter
740        float mFilterWidth;
741        /// Maximal size of the filter in terms of contributing view cells
742        int mMaxFilterSize;
743       
744        // only for debugging
745        VssRayContainer storedRays;
746
747        //////////////////
748        //-- visualization options
749       
750        /// color code for view cells visualization
751        bool mShowVisualization;
752        int mColorCode;
753        bool mExportGeometry;
754        bool mExportRays;
755        bool mViewCellsFinished;
756        bool mEvaluateViewCells;
757
758        /// if pvs should be exported with view cells
759        bool mExportPvs;
760
761        static int sRenderCostEvaluationType;
762
763        /// if view cells geometry should be used from other sources
764        bool mUsePredefinedViewCells;
765};
766
767
768/** Manages different higher order operations on the view cells.
769*/
770class BspViewCellsManager: public ViewCellsManager
771{
772
773public:
774        /** Constructor taking the bsp tree and the number of samples
775                used to construct the bsp tree.
776        */
777        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
778
779        ~BspViewCellsManager();
780
781        int ConstructSubdivision(const ObjectContainer &objects,
782                                  const VssRayContainer &rays);
783
784        int PostProcess(const ObjectContainer &objects,
785                                        const VssRayContainer &rays);
786
787        void Visualize(const ObjectContainer &objects,
788                                   const VssRayContainer &sampleRays);
789
790        int GetType() const;
791       
792        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
793
794        bool ViewCellsConstructed() const;
795
796        //void PrintStatistics(ostream &s) const;
797
798        int CastLineSegment(const Vector3 &origin,
799                                                const Vector3 &termination,
800                                                ViewCellContainer &viewcells);
801       
802        /** Returns the probability that the view point lies
803                in this view cells.
804        */
805        float GetProbability(ViewCell *viewCell);
806
807        /** Get a viewcell containing the specified point.
808        */
809        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
810
811        /** Creates mesh for this view cell.
812        */
813        void CreateMesh(ViewCell *vc);
814
815        void ExportViewCellGeometry(Exporter *exporter,
816                                                                ViewCell *vc,
817                                                                const AxisAlignedBox3 *box,
818                                                                const AxisAlignedPlane *clipPlane = NULL
819                                                                ) const;
820       
821        void CollectMergeCandidates(const VssRayContainer &rays,
822                                                                vector<MergeCandidate> &candidates);
823
824        void Finalize(ViewCell *viewCell, const bool createMesh);
825
826        bool ExportViewCells(const string filename,
827                                                 const bool exportPvs,
828                                                 const ObjectContainer &objects);
829
830        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
831        */
832        ViewCell *ConstructSpatialMergeTree(BspNode *root);
833
834        void ExportSingleViewCells(const ObjectContainer &objects,
835                                                           const int maxViewCells,
836                                                           const bool sortViewCells,
837                                                           const bool exportPvs,
838                                                           const bool exportRays,
839                                                           const int maxRays,
840                                                           const string prefix,
841                                                           VssRayContainer *visRays = NULL);
842
843protected:
844
845        void CollectViewCells();
846       
847        /// the BSP tree.
848        BspTree *mBspTree;
849        vector<BspRay *> mBspRays;
850
851private:
852
853        /** Constructs a spatial merge tree only 2 levels deep.
854        */
855        ViewCell *ConstructDummyMergeTree(BspNode *root);
856
857        /** Exports visualization of the BSP splits.
858        */
859        void ExportSplits(const ObjectContainer &objects);
860
861        /** test if subdivision is valid in terms of volume / area.
862        */
863        void TestSubdivision();
864};
865
866
867/**
868        Manages different higher order operations on the KD type view cells.
869*/
870class KdViewCellsManager: public ViewCellsManager
871{
872
873public:
874
875        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
876
877        int ConstructSubdivision(const ObjectContainer &objects,
878                                                         const VssRayContainer &rays);
879
880        int CastLineSegment(const Vector3 &origin,
881                                                const Vector3 &termination,
882                                                ViewCellContainer &viewcells);
883
884        int PostProcess(const ObjectContainer &objects,
885                                        const VssRayContainer &rays);
886
887        void Visualize(const ObjectContainer &objects,
888                                   const VssRayContainer &sampleRays);
889
890        int GetType() const;
891
892        bool ViewCellsConstructed() const;
893
894        ViewCell *GenerateViewCell(Mesh *mesh) const;
895
896        /** Prints out statistics of this approach.
897        */
898        //  virtual void PrintStatistics(ostream &s) const;
899        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
900        { return NULL; }
901
902        float GetProbability(ViewCell *viewCell);
903       
904        void CreateMesh(ViewCell *vc);
905
906        void ExportViewCellGeometry(Exporter *exporter,
907                                                                ViewCell *vc,
908                                                                const AxisAlignedBox3 *box,
909                                                                const AxisAlignedPlane *clipPlane = NULL) const;
910
911
912        void CollectMergeCandidates(const VssRayContainer &rays,
913                                                                vector<MergeCandidate> &candidates);
914
915        void ExportSingleViewCells(const ObjectContainer &objects,
916                                                           const int maxViewCells,
917                                                           const bool sortViewCells,
918                                                           const bool exportPvs,
919                                                           const bool exportRays,
920                                                           const int maxRays,
921                                                           const string prefix,
922                                                           VssRayContainer *visRays = NULL);
923
924protected:
925
926        /** Collects view cells from a hierarchy.
927        */
928        void CollectViewCells();
929
930        KdNode *GetNodeForPvs(KdLeaf *leaf);
931
932        ////////////////////////////////////////
933
934        /// the BSP tree.
935        KdTree *mKdTree;
936
937        /// depth of the KD tree nodes with represent the view cells
938        int mKdPvsDepth;
939
940
941};
942
943
944/** Manages different higher order operations on the view cells.
945*/
946class VspBspViewCellsManager: public ViewCellsManager
947{
948
949public:
950
951        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
952        ~VspBspViewCellsManager();
953
954        int ConstructSubdivision(const ObjectContainer &objects,
955                                                         const VssRayContainer &rays);
956
957        int PostProcess(const ObjectContainer &objects,
958                                        const VssRayContainer &rays);
959
960        void Visualize(const ObjectContainer &objects,
961                                   const VssRayContainer &sampleRays);
962
963        int GetType() const;
964       
965        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
966
967        bool ViewCellsConstructed() const;
968
969       
970        int CastLineSegment(const Vector3 &origin,
971                                                const Vector3 &termination,
972                                                ViewCellContainer &viewcells);
973
974        float GetProbability(ViewCell *viewCell);
975       
976        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
977
978        bool GetViewPoint(Vector3 &viewPoint) const;
979
980        bool ViewPointValid(const Vector3 &viewPoint) const;
981
982        void CreateMesh(ViewCell *vc);
983
984        bool ExportViewCells(const string filename,
985                                                 const bool exportPvs,
986                                                 const ObjectContainer &objects);
987
988        int CastBeam(Beam &beam);
989       
990        void ExportViewCellGeometry(Exporter *exporter,
991                                                                ViewCell *vc,
992                                                                const AxisAlignedBox3 *box,
993                                                                const AxisAlignedPlane *clipPlane = NULL) const;
994
995
996        void Finalize(ViewCell *viewCell, const bool createMesh);
997
998        void CollectMergeCandidates(const VssRayContainer &rays,
999                                                                vector<MergeCandidate> &candidates);
1000
1001        void ExportSingleViewCells(const ObjectContainer &objects,
1002                                                           const int maxViewCells,
1003                                                           const bool sortViewCells,
1004                                                           const bool exportPvs,
1005                                                           const bool exportRays,               
1006                                                           const int maxRays,
1007                                                           const string prefix,
1008                                                           VssRayContainer *visRays = NULL);
1009
1010
1011protected:
1012
1013        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1014                                                                ViewCellContainer &viewCells) const;
1015       
1016        /** Merges view cells according to some criteria
1017        */
1018        void MergeViewCells(const VssRayContainer &rays,
1019                                                const ObjectContainer &objects);
1020       
1021        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
1022
1023        void CollectViewCells();
1024
1025        /** Returns maximal depth difference of view cell
1026                leaves in tree.
1027        */
1028        int GetMaxTreeDiff(ViewCell *vc) const;
1029
1030        /** Prepare view cells for use after loading them from disc.
1031        */
1032        void PrepareLoadedViewCells();
1033
1034        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1035        */
1036        ViewCell *ConstructSpatialMergeTree(BspNode *root);
1037
1038        /// HACK for testing visibility filter functionality
1039        void TestFilter(const ObjectContainer &objects);
1040
1041        /** Visualization of the pvs difference to exact visubility using
1042                from point queries.
1043        */
1044        void VisualizeWithFromPointQueries();
1045
1046        /** Evaluate from point queries for the current scene.
1047        */
1048        void EvalFromPointQueries();
1049
1050        /** Exports visualization of the BSP splits.
1051        */
1052        void ExportSplits(const ObjectContainer &objects,
1053                                          const VssRayContainer &rays);
1054
1055
1056        /////////////////////////
1057
1058        /// the view space partition BSP tree.
1059        VspBspTree *mVspBspTree;
1060
1061
1062private:
1063
1064        /** test if subdivision is valid in terms of volume / area.
1065        */
1066        void TestSubdivision();
1067};
1068
1069#define TEST_EVALUATION 0
1070
1071/**
1072        Manages different higher order operations on the view cells.
1073*/
1074class VspOspViewCellsManager: public ViewCellsManager
1075{
1076        friend class ViewCellsParseHandlers;
1077public:
1078
1079        VspOspViewCellsManager(ViewCellsTree *vcTree, const string &hierarchyType);
1080       
1081        ~VspOspViewCellsManager();
1082
1083        int ConstructSubdivision(const ObjectContainer &objects,
1084                                                         const VssRayContainer &rays);
1085
1086        int PostProcess(const ObjectContainer &objects,
1087                                        const VssRayContainer &rays);
1088
1089        void Visualize(const ObjectContainer &objects,
1090                                   const VssRayContainer &sampleRays);
1091
1092        int GetType() const;
1093       
1094        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1095
1096  virtual Intersectable *
1097  GetIntersectable(const VssRay &ray, const bool isTermination) const;
1098
1099        bool ViewCellsConstructed() const;
1100
1101       
1102        int CastLineSegment(const Vector3 &origin,
1103                                                const Vector3 &termination,
1104                                                ViewCellContainer &viewcells);
1105
1106        float GetProbability(ViewCell *viewCell);
1107       
1108        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1109
1110        bool GetViewPoint(Vector3 &viewPoint) const;
1111
1112        bool ViewPointValid(const Vector3 &viewPoint) const;
1113
1114        void CreateMesh(ViewCell *vc);
1115
1116        bool ExportViewCells(const string filename,
1117                                                 const bool exportPvs,
1118                                                 const ObjectContainer &objects);
1119
1120        int CastBeam(Beam &beam);
1121
1122        void Finalize(ViewCell *viewCell, const bool createMesh);
1123
1124        /** Stores sample contribution for kd cells or objects.
1125        */
1126        virtual float ComputeSampleContribution(VssRay &ray,
1127                                                                                        const bool addRays,
1128                                                                                        const bool storeViewCells);
1129
1130        ViewCellsManager *LoadViewCells(const string &filename,
1131                                                                        ObjectContainer *objects,
1132                                                                        const bool finalizeViewCells,
1133                                                                        BoundingBoxConverter *bconverter);
1134
1135        void ExportSingleViewCells(const ObjectContainer &objects,
1136                                                           const int maxViewCells,
1137                                                           const bool sortViewCells,
1138                                                           const bool exportPvs,
1139                                                           const bool exportRays,
1140                                                           const int maxRays,
1141                                                           const string prefix,
1142                                                           VssRayContainer *visRays = NULL);
1143
1144protected:
1145
1146        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1147       
1148#if 1//TEST_EVALUATION
1149        virtual void EvalViewCellPartition();
1150#endif
1151
1152        /** Exports view cell geometry.
1153        */
1154        void ExportViewCellGeometry(Exporter *exporter,
1155                                                                ViewCell *vc,
1156                                                                const AxisAlignedBox3 *box,
1157                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1158
1159        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1160                                                                ViewCellContainer &viewCells) const;
1161       
1162        void CollectViewCells();
1163
1164        /** Prepare view cells for use after loading them from disc.
1165        */
1166        void PrepareLoadedViewCells();
1167
1168        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1169        */
1170        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1171
1172        /** Exports visualization of the PVS.
1173        */
1174        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
1175
1176        /** Returns a hierarchy manager of the given name.
1177        */
1178        static HierarchyManager *CreateHierarchyManager(const string &name);
1179
1180        //void ExportStats(const string &mergeStats);
1181
1182        /////////////////////////////////////////
1183
1184    /** collect objects intersecting a given spatial box */
1185  virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
1186
1187        HierarchyManager *mHierarchyManager;
1188};
1189
1190
1191}
1192
1193#endif
Note: See TracBrowser for help on using the repository browser.