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

Revision 1743, 32.3 KB checked in by bittner, 18 years ago (diff)

merge with new pvs

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