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

Revision 2105, 35.8 KB checked in by bittner, 17 years ago (diff)

simple ray separated

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