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

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