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

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