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

Revision 1824, 32.5 KB checked in by bittner, 18 years ago (diff)

global lines support

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