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

Revision 1581, 30.3 KB checked in by bittner, 18 years ago (diff)

Qtglwidget changes - second view + trackball - ray casting seems not functional

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