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

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