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

Revision 1414, 28.9 KB checked in by mattausch, 18 years ago (diff)

fixed kd tree loading / exporting

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       
617        /// if the values in the view cell leaves and the interiors are up to date
618        /// this is meant for lazy storing of the pvs, where only a scalar indicating
619        /// pvs size is stored in interiors and not the pvs itself.
620        bool mViewCellPvsIsUpdated;
621
622        /// maximum number of samples taken for construction of the view cells
623        int mConstructionSamples;
624        int mSamplesPerPass;
625        int mInitialSamples;
626        int mPostProcessSamples;
627        int mVisualizationSamples;
628
629        float mTotalAreaValid;
630        float mTotalArea;
631
632        int mMaxPvsSize;
633        int mMinPvsSize;
634        float mMaxPvsRatio;
635
636        int mSamplingType;
637        int mEvaluationSamplingType;
638        int mNumActiveViewCells;
639        bool mCompressViewCells;
640
641        /// holds the current view cell statistics
642        ViewCellsStatistics mCurrentViewCellsStats;
643        /// the scene bounding box
644        AxisAlignedBox3 mViewSpaceBox;
645       
646        /// if view cells should be exported
647        bool mExportViewCells;
648
649        // if only valid view cells should be considered for processing
650        bool mOnlyValidViewCells;
651
652        /// if rays should be used to collect merge candidates
653        bool mUseRaysForMerge;
654
655        /// if there should be an additional merge step after the subdivision
656        bool mMergeViewCells;
657
658        /// the width of the box filter
659        float mFilterWidth;
660        int mMaxFilterSize;
661
662        //-- visualization options
663       
664        /// color code for view cells visualization
665        int mColorCode;
666        bool mExportGeometry;
667        bool mExportRays;
668
669        bool mViewCellsFinished;
670
671        bool mEvaluateViewCells;
672
673        bool mShowVisualization;
674
675        int mRenderCostEvaluationType;
676
677        /// if pvs should be exported with view cells
678        bool mExportPvs;
679
680        bool mStoreObjectPvs;
681
682        VssRayContainer storedRays;
683};
684
685
686/**
687        Manages different higher order operations on the view cells.
688*/
689class BspViewCellsManager: public ViewCellsManager
690{
691
692public:
693        /** Constructor taking the bsp tree and the number of samples
694                used to construct the bsp tree.
695        */
696        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
697
698        ~BspViewCellsManager();
699
700        int ConstructSubdivision(const ObjectContainer &objects,
701                                  const VssRayContainer &rays);
702
703        int PostProcess(const ObjectContainer &objects,
704                                        const VssRayContainer &rays);
705
706        void Visualize(const ObjectContainer &objects,
707                                   const VssRayContainer &sampleRays);
708
709        int GetType() const;
710       
711        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
712
713        bool ViewCellsConstructed() const;
714
715        //void PrintStatistics(ostream &s) const;
716
717        int CastLineSegment(const Vector3 &origin,
718                                                const Vector3 &termination,
719                                                ViewCellContainer &viewcells);
720       
721        /** Returns the probability that the view point lies
722                in this view cells.
723        */
724        float GetProbability(ViewCell *viewCell);
725
726        /** Get a viewcell containing the specified point.
727        */
728        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
729
730        /** Creates mesh for this view cell.
731        */
732        void CreateMesh(ViewCell *vc);
733
734        void ExportViewCellGeometry(Exporter *exporter,
735                                                                ViewCell *vc,
736                                                                const AxisAlignedPlane *clipPlane = NULL) const;
737       
738        void CollectMergeCandidates(const VssRayContainer &rays,
739                                                                vector<MergeCandidate> &candidates);
740
741        void Finalize(ViewCell *viewCell, const bool createMesh);
742
743        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
744
745        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
746        */
747        ViewCell *ConstructSpatialMergeTree(BspNode *root);
748
749       
750protected:
751
752        void CollectViewCells();
753
754       
755        /// the BSP tree.
756        BspTree *mBspTree;
757       
758        vector<BspRay *> mBspRays;
759
760private:
761
762        /** Exports visualization of the BSP splits.
763        */
764        void ExportSplits(const ObjectContainer &objects);
765
766        /** Exports visualization of the BSP PVS.
767        */
768        void ExportBspPvs(const ObjectContainer &objects);
769
770        /** test if subdivision is valid in terms of volume / area.
771        */
772        void TestSubdivision();
773};
774
775/**
776        Manages different higher order operations on the KD type view cells.
777*/
778class KdViewCellsManager: public ViewCellsManager
779{
780
781public:
782
783        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
784
785        int ConstructSubdivision(const ObjectContainer &objects,
786                                  const VssRayContainer &rays);
787
788        int CastLineSegment(const Vector3 &origin,
789                                                const Vector3 &termination,
790                                                ViewCellContainer &viewcells);
791
792        int PostProcess(const ObjectContainer &objects,
793                                        const VssRayContainer &rays);
794
795        void Visualize(const ObjectContainer &objects,
796                                   const VssRayContainer &sampleRays);
797
798        int GetType() const;
799
800        bool ViewCellsConstructed() const;
801
802        ViewCell *GenerateViewCell(Mesh *mesh) const;
803
804        /** Prints out statistics of this approach.
805        */
806        //  virtual void PrintStatistics(ostream &s) const;
807        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const { return NULL; }
808
809        float GetProbability(ViewCell *viewCell);
810       
811
812        void CreateMesh(ViewCell *vc);
813
814        void ExportViewCellGeometry(Exporter *exporter,
815                                                                ViewCell *vc,
816                                                                const AxisAlignedPlane *clipPlane = NULL) const;
817
818        void CollectMergeCandidates(const VssRayContainer &rays,
819                                                                vector<MergeCandidate> &candidates);
820
821
822protected:
823
824        /** Collects view cells from a hierarchy.
825        */
826        void CollectViewCells();
827
828        KdNode *GetNodeForPvs(KdLeaf *leaf);
829
830
831        /// the BSP tree.
832        KdTree *mKdTree;
833
834        /// depth of the KD tree nodes with represent the view cells
835        int mKdPvsDepth;
836
837
838};
839
840/**
841        Manages different higher order operations on the view cells.
842*/
843class VspBspViewCellsManager: public ViewCellsManager
844{
845
846public:
847
848        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
849        ~VspBspViewCellsManager();
850
851        int ConstructSubdivision(const ObjectContainer &objects,
852                                  const VssRayContainer &rays);
853
854        int PostProcess(const ObjectContainer &objects,
855                                        const VssRayContainer &rays);
856
857        void Visualize(const ObjectContainer &objects,
858                                   const VssRayContainer &sampleRays);
859
860        int GetType() const;
861       
862        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
863
864        bool ViewCellsConstructed() const;
865
866       
867        int CastLineSegment(const Vector3 &origin,
868                                                const Vector3 &termination,
869                                                ViewCellContainer &viewcells);
870
871        float GetProbability(ViewCell *viewCell);
872       
873        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
874
875        bool GetViewPoint(Vector3 &viewPoint) const;
876
877        bool ViewPointValid(const Vector3 &viewPoint) const;
878
879        void CreateMesh(ViewCell *vc);
880
881        bool ExportViewCells(const string filename, const bool exportPvs, const ObjectContainer &objects);
882
883        int CastBeam(Beam &beam);
884
885        void ExportViewCellGeometry(Exporter *exporter,
886                                                                ViewCell *vc,
887                                                                const AxisAlignedPlane *clipPlane = NULL) const;
888
889        //float GetVolume(ViewCell *viewCell) const;
890
891        void Finalize(ViewCell *viewCell, const bool createMesh);
892
893        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
894
895        /** Returns true if this view cell is equivavalent to a spatial node.
896        */
897        bool EqualToSpatialNode(ViewCell *viewCell) const;
898
899
900protected:
901
902        int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
903       
904        /** Returns node of the spatial hierarchy corresponding to the view cell
905                if such a node exists.
906        */
907        BspNode *GetSpatialNode(ViewCell *viewCell) const;
908
909        /** Merges view cells according to some criteria
910        */
911        void MergeViewCells(const VssRayContainer &rays,
912                                                const ObjectContainer &objects);
913       
914        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
915
916        void CollectViewCells();
917
918        /** Returns maximal depth difference of view cell
919                leaves in tree.
920        */
921        int GetMaxTreeDiff(ViewCell *vc) const;
922       
923
924        /** Prepare view cells for use after loading them from disc.
925        */
926        void PrepareLoadedViewCells();
927
928        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
929        */
930        ViewCell *ConstructSpatialMergeTree(BspNode *root);
931
932        /// HACK for testing visibility filter functionality
933        void TestFilter(const ObjectContainer &objects);
934
935        /** Visualization of the pvs difference to exact visubility using
936                from point queries.
937        */
938        void VisualizeWithFromPointQueries();
939
940        /** Evaluate from point queries for the current scene.
941        */
942        void EvalFromPointQueries();
943
944        /** Exports visualization of the BSP splits.
945        */
946        void ExportSplits(const ObjectContainer &objects,
947                                          const VssRayContainer &rays);
948
949        /** Exports visualization of the BSP PVS.
950        */
951        void ExportBspPvs(const ObjectContainer &objects,
952                                          const VssRayContainer &rays);
953
954
955        /// the view space partition BSP tree.
956        VspBspTree *mVspBspTree;
957
958
959private:
960
961        /** test if subdivision is valid in terms of volume / area.
962        */
963        void TestSubdivision();
964};
965
966#define TEST_EVALUATION 1
967
968/**
969        Manages different higher order operations on the view cells.
970*/
971class VspOspViewCellsManager: public ViewCellsManager
972{
973
974public:
975
976        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
977       
978        ~VspOspViewCellsManager();
979
980        int ConstructSubdivision(const ObjectContainer &objects,
981                                                         const VssRayContainer &rays);
982
983        int PostProcess(const ObjectContainer &objects,
984                                        const VssRayContainer &rays);
985
986        void Visualize(const ObjectContainer &objects,
987                                   const VssRayContainer &sampleRays);
988
989        int GetType() const;
990       
991        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
992
993        bool ViewCellsConstructed() const;
994
995       
996        int CastLineSegment(const Vector3 &origin,
997                                                const Vector3 &termination,
998                                                ViewCellContainer &viewcells);
999
1000        float GetProbability(ViewCell *viewCell);
1001       
1002        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1003
1004        bool GetViewPoint(Vector3 &viewPoint) const;
1005
1006        bool ViewPointValid(const Vector3 &viewPoint) const;
1007
1008        void CreateMesh(ViewCell *vc);
1009
1010        bool ExportViewCells(const string filename,
1011                                                 const bool exportPvs,
1012                                                 const ObjectContainer &objects);
1013
1014        int CastBeam(Beam &beam);
1015
1016        void Finalize(ViewCell *viewCell, const bool createMesh);
1017
1018        /** Stores sample contribution for kd cells or objects.
1019        */
1020        virtual float ComputeSampleContribution(VssRay &ray,
1021                                                                                        const bool addRays,
1022                                                                                        const bool storeViewCells);
1023
1024        ViewCellsManager *LoadViewCells(const string &filename,
1025                ObjectContainer *objects,
1026                const bool finalizeViewCells,
1027                BoundingBoxConverter *bconverter);
1028
1029        bool AddSampleToPvs(
1030                Intersectable *obj,
1031                const Vector3 &hitPoint,
1032                ViewCell *vc,
1033                const float pdf,
1034        float &contribution) const;
1035
1036protected:
1037
1038#if TEST_EVALUATION
1039        virtual void EvalViewCellPartition();
1040#endif
1041        /** Exports view cell geometry.
1042        */
1043        void ExportViewCellGeometry(Exporter *exporter,
1044                ViewCell *vc,
1045                const AxisAlignedPlane *clipPlane = NULL) const;
1046
1047        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1048                ViewCellContainer &viewCells) const;
1049       
1050        void CollectViewCells();
1051
1052
1053        /** Prepare view cells for use after loading them from disc.
1054        */
1055        void PrepareLoadedViewCells();
1056
1057        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1058        */
1059        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1060
1061        /** Exports visualization of the PVS.
1062        */
1063        void ExportPvs(
1064                const ObjectContainer &objects,
1065                const VssRayContainer &rays);
1066
1067
1068        /////////////////////////////////////////
1069
1070        /// the view space / object partition hierarchies
1071        //VspTree *mVspTree;
1072        //OspTree *mOspTree;
1073        //BvHierarchy *mBvHierarchy;
1074
1075        HierarchyManager *mHierarchyManager;
1076};
1077
1078
1079/*class ViewCellsManagerFactory
1080{
1081public:
1082        ViewCellsManager *Create(const string mName);
1083};*/
1084
1085}
1086
1087#endif
Note: See TracBrowser for help on using the repository browser.