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

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