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

Revision 2113, 35.7 KB checked in by mattausch, 17 years ago (diff)

warning: debug version

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 view 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 &pvsObjects,
574                                                                                   ObjectContainer &preprocessorObjects,
575                                                                                   bool finalizeViewCells = false,
576                                                                                   BoundingBoxConverter *bconverter = NULL);
577
578
579        ///////////////////////
580        // visiblity filter options
581
582        // TODO: write own visibiltiy filter class
583        void ApplyFilter(KdTree *kdTree,
584                                         const float viewSpaceFilterSize,
585                                         const float spatialFilterSize);
586
587        // new adaptive version of the filter
588        PvsFilterStatistics ApplyFilter2(ViewCell *viewCell,
589                                                                         const bool useViewSpaceFilter,
590                                                                         const float filterSize,
591                                                                         ObjectPvs &pvs,
592                                                                         vector<AxisAlignedBox3> *filteredBoxes = NULL);
593
594        void ApplySpatialFilter(KdTree *kdTree,
595                                                        const float spatialFilterSize,
596                                                        ObjectPvs &pvs);
597
598         void ApplyFilter(ViewCell *viewCell,
599                                          KdTree *kdTree,
600                                          const float viewSpaceFilterSize,
601                                          const float spatialFilterSize,
602                                          ObjectPvs &pvs);
603
604        float GetFilterWidth();
605
606        float GetAbsFilterWidth();
607
608        /** Returns the bounding box of filter width.
609        */
610        AxisAlignedBox3 GetFilterBBox(const Vector3 &viewPoint, const float width) const;
611
612        //////////////////////////////////////////////////////////////////
613
614
615        /** If true, the kd nodes are stored instead of the object pvs.
616        */
617        void SetStoreKdPvs(const bool storeKdPvs);
618
619        /** Returns true if the kd nodes are stored instead of the object pvs.
620        **/
621        bool GetStoreKdPVs() const;
622
623        /** Exports single view cells for visualization.
624                @param objects the scene objects
625                @param limit the maximal number of output view cells
626                @param sortViewCells if the view cells should be sorted by pvs size
627                @param exportPvs if the pvs should also be exported
628                @param exportRays if sample rays should be exported as well
629                @param maxRays maximum number of rays to export
630                @param prefix the prefix for the output file
631                @param visRays additional rays
632        */
633        virtual void ExportSingleViewCells(const ObjectContainer &objects,
634                                                                           const int maxViewCells,
635                                                                           const bool sortViewCells,
636                                                                           const bool exportPvs,
637                                                                           const bool exportRays,
638                                                                           const int maxRays,
639                                                                           const string prefix,
640                                                                           VssRayContainer *visRays = NULL) = NULL;
641
642
643        void ResetPvs();
644
645        Preprocessor *GetPreprocessor() const {
646                return mPreprocessor;
647        }
648
649        void SetPreprocessor(Preprocessor *p) {
650                mPreprocessor = p;
651        }
652
653        vector<ViewCellPoints *> *GetViewCellPoints()
654        {
655                return &mViewCellPoints;
656        }
657
658        void UpdatePvsForEvaluation();
659
660protected:
661
662        void ComputeViewCellContribution(ViewCell *viewCell,
663                                                                         VssRay &ray,
664                                                                         Intersectable *obj,
665                                                                         const Vector3 &pt,
666                                                                         const bool addRays);
667
668        void MergeViewCellsRecursivly(ObjectPvs &pvs,
669                                                                  const ViewCellContainer &viewCells,
670                                                                  const int leftIdx,
671                                                                  const int rightIdx) const;
672
673        /** Intersects box with the tree and returns the number of intersected boxes.
674        @returns number of view cells found
675        */
676        virtual int ComputeBoxIntersections(const AxisAlignedBox3 &box,
677                                                                                ViewCellContainer &viewCells) const;
678
679        /** Tests the visibility filter functionality.
680        */
681        virtual void TestFilter(const ObjectContainer &objects) {};
682
683        /** If the view cells tree was already constructed or not.
684        */
685        bool ViewCellsTreeConstructed() const;
686
687        /** Requests preprocessor to cast samplesPerPass samples of a specific type.
688        */
689        int CastPassSamples(const int samplesPerPass,
690                                                const vector<int> &strategies,
691                                                VssRayContainer &vssRays) const;
692
693        int CastPassSamples2(const int samplesPerPass,
694                                                 VssRayContainer &passSamples) const;
695
696        /** Parse the options from the environment file.
697        */
698        void ParseEnvironment();
699
700        /** Creates unique view cell ids.
701        */
702        void CreateUniqueViewCellIds();
703
704        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
705        */
706        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
707
708        /** Recollects view cells and resets statistics.
709        */
710        void ResetViewCells();
711
712        /** Sets this view cell to active.
713        */
714        void SetViewCellActive(ViewCell *vc) const;
715
716        /** Collects the view cells in the view cell container.
717        */
718        virtual void CollectViewCells() = 0;
719
720        /** Evaluates view cells statistics and stores it in
721        mViewCellsStatistics.
722        */
723        void EvaluateViewCellsStats();
724
725
726        ///////////////////////
727        //-- helper functions for view cell visualization
728
729        /** Exports the view cell partition.
730        */
731        void ExportViewCellsForViz(Exporter *exporter,
732                                                           const AxisAlignedBox3 *box,
733                                                           const bool colorCode,
734                                                           const AxisAlignedPlane *clipPlane) const;
735
736        /** Sets exporter color.
737        */
738        virtual void ExportColor(Exporter *exporter,
739                                                         ViewCell *vc,
740                                                         const bool colorCode) const;
741
742        /** Creates meshes from the view cells.
743        */
744        void CreateViewCellMeshes();
745
746        /** Creates clip plane for visualization.
747        */
748        void CreateClipPlane();
749
750        AxisAlignedPlane *GetClipPlane();
751
752        void ExportMergedViewCells(const ObjectContainer &objects);
753
754
755        ///////////////////////
756
757        /** Returns volume of the view space.
758        */
759        virtual float GetViewSpaceVolume();
760
761        /** Prepares the view cells for sampling after loading them from disc.
762        */
763        virtual void PrepareLoadedViewCells() {};
764       
765        /** Constructs local view cell merge hierarchy.
766        */
767        ViewCell *ConstructLocalMergeTree(ViewCell *currentViewCell,
768                const ViewCellContainer &viewCells);
769
770        /** Constructs local view cell merge hierarchy based solely on similarity with the
771                current viewcell
772        */
773        ViewCell *ConstructLocalMergeTree2(ViewCell *currentViewCell,
774                                                                           const ViewCellContainer &viewCells);
775 
776        /** Updates pvs of all view cells for statistical evaluation after some more sampling
777        */
778        void UpdatePvsForEvaluation(ViewCell *root, ObjectPvs &pvs);
779
780        virtual void ExportStats(const string &mergeStats);
781
782
783        ////////////////////////////////////////////////
784
785
786        Preprocessor *mPreprocessor;
787       
788        /// if bounding boxes should be exported together with the view cells
789        bool mExportBboxesForPvs;
790       
791        /// the clip plane for visualization
792        AxisAlignedPlane mClipPlaneForViz;
793       
794        /// if the visualization is using the clip plane
795        bool mUseClipPlaneForViz;
796       
797        /// Renders the view cells.
798        Renderer *mRenderer;
799       
800        /// Loaded view cells
801        ViewCellContainer mViewCells;
802       
803        /// the view cell hierarchy (i.e., the logical description of view cells)
804        ViewCellsTree *mViewCellsTree;
805       
806        std::vector<int> mStrategies;
807
808        /** if the values in the view cell leaves and the interiors are up to date
809                this is meant for lazy storing of the pvs, where only a scalar indicating
810                pvs size is stored in interiors and not the pvs itself.
811        */
812        bool mViewCellPvsIsUpdated;
813
814        /// maximum number of samples for the view cell construction
815        int mConstructionSamples;
816        int mSamplesPerPass;
817        int mInitialSamples;
818        int mPostProcessSamples;
819        int mVisualizationSamples;
820
821        float mTotalAreaValid;
822        float mTotalArea;
823
824        int mMaxPvsSize;
825        int mMinPvsSize;
826        float mMaxPvsRatio;
827
828        int mSamplingType;
829        int mEvaluationSamplingType;
830        int mNumActiveViewCells;
831        bool mCompressViewCells;
832
833        vector<ViewCellPoints *> mViewCellPoints;
834
835        /// holds the current view cell statistics
836        ViewCellsStatistics mCurrentViewCellsStats;
837
838        /// the scene bounding box
839        AxisAlignedBox3 mViewSpaceBox;
840       
841        /// if view cells should be exported
842        bool mExportViewCells;
843
844        // if only valid view cells should be considered for processing
845        bool mOnlyValidViewCells;
846
847        /// if rays should be used to collect merge candidates
848        bool mUseRaysForMerge;
849
850        /// if there should be an additional merge step after the subdivision
851        bool mMergeViewCells;
852
853        /// the width of the box filter
854        float mFilterWidth;
855        /// Maximal size of the filter in terms of contributing view cells
856        int mMaxFilterSize;
857       
858        // only for debugging
859        VssRayContainer storedRays;
860
861        bool mUseKdPvs;
862        bool mUseKdPvsAfterFiltering;
863
864        MixtureDistribution *mMixtureDistribution;
865
866
867        //////////////////
868        //-- visualization options
869       
870        /// color code for view cells visualization
871        bool mShowVisualization;
872        int mColorCode;
873        bool mExportGeometry;
874        bool mExportRays;
875        bool mViewCellsFinished;
876        bool mEvaluateViewCells;
877
878        /// if pvs should be exported with view cells
879        bool mExportPvs;
880
881        static int sRenderCostEvaluationType;
882
883        /// if view cells geometry should be used from other sources
884        bool mUsePredefinedViewCells;
885
886 
887  vector<PerViewCellStat> mPerViewCellStat;
888  SamplesStatistics mSamplesStat;
889};
890
891
892/** Manages different higher order operations on the view cells.
893*/
894class BspViewCellsManager: public ViewCellsManager
895{
896
897public:
898        /** Constructor taking the bsp tree and the number of samples
899                used to construct the bsp tree.
900        */
901        BspViewCellsManager(ViewCellsTree *viewCellsTree, BspTree *tree);
902
903        ~BspViewCellsManager();
904
905        int ConstructSubdivision(const ObjectContainer &objects,
906                                  const VssRayContainer &rays);
907
908        int PostProcess(const ObjectContainer &objects,
909                                        const VssRayContainer &rays);
910
911        void Visualize(const ObjectContainer &objects,
912                                   const VssRayContainer &sampleRays);
913
914        int GetType() const;
915       
916        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
917
918        bool ViewCellsConstructed() const;
919
920        //void PrintStatistics(ostream &s) const;
921
922        int CastLineSegment(const Vector3 &origin,
923                                                const Vector3 &termination,
924                                                ViewCellContainer &viewcells);
925       
926        /** Returns the probability that the view point lies
927                in this view cells.
928        */
929        float GetProbability(ViewCell *viewCell);
930
931        /** Get a viewcell containing the specified point.
932        */
933        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
934
935        /** Creates mesh for this view cell.
936        */
937        void CreateMesh(ViewCell *vc);
938
939        void ExportViewCellGeometry(Exporter *exporter,
940                                                                ViewCell *vc,
941                                                                const AxisAlignedBox3 *box,
942                                                                const AxisAlignedPlane *clipPlane = NULL
943                                                                ) const;
944       
945        void CollectMergeCandidates(const VssRayContainer &rays,
946                                                                vector<MergeCandidate> &candidates);
947
948        void Finalize(ViewCell *viewCell, const bool createMesh);
949
950        bool ExportViewCells(const string filename,
951                                                 const bool exportPvs,
952                                                 const ObjectContainer &objects);
953
954        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
955        */
956        ViewCell *ConstructSpatialMergeTree(BspNode *root);
957
958        void ExportSingleViewCells(const ObjectContainer &objects,
959                                                           const int maxViewCells,
960                                                           const bool sortViewCells,
961                                                           const bool exportPvs,
962                                                           const bool exportRays,
963                                                           const int maxRays,
964                                                           const string prefix,
965                                                           VssRayContainer *visRays = NULL);
966
967        bool LineSegmentIntersects(const Vector3 &origin,
968                                                           const Vector3 &termination,
969                                                           ViewCell *viewCell);
970
971protected:
972
973        void CollectViewCells();
974       
975        /// the BSP tree.
976        BspTree *mBspTree;
977        vector<BspRay *> mBspRays;
978
979private:
980
981        /** Constructs a spatial merge tree only 2 levels deep.
982        */
983        ViewCell *ConstructDummyMergeTree(BspNode *root);
984
985        /** Exports visualization of the BSP splits.
986        */
987        void ExportSplits(const ObjectContainer &objects);
988
989        /** test if subdivision is valid in terms of volume / area.
990        */
991        void TestSubdivision();
992};
993
994
995/**
996        Manages different higher order operations on the KD type view cells.
997*/
998class KdViewCellsManager: public ViewCellsManager
999{
1000
1001public:
1002
1003        KdViewCellsManager(ViewCellsTree *viewCellsTree, KdTree *tree);
1004
1005        int ConstructSubdivision(const ObjectContainer &objects,
1006                                                         const VssRayContainer &rays);
1007
1008        int CastLineSegment(const Vector3 &origin,
1009                                                const Vector3 &termination,
1010                                                ViewCellContainer &viewcells);
1011
1012        int PostProcess(const ObjectContainer &objects,
1013                                        const VssRayContainer &rays);
1014
1015        void Visualize(const ObjectContainer &objects,
1016                                   const VssRayContainer &sampleRays);
1017
1018        int GetType() const;
1019
1020        bool ViewCellsConstructed() const;
1021
1022        ViewCell *GenerateViewCell(Mesh *mesh) const;
1023
1024        /** Prints out statistics of this approach.
1025        */
1026        //  virtual void PrintStatistics(ostream &s) const;
1027        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const
1028        { return NULL; }
1029
1030        float GetProbability(ViewCell *viewCell);
1031       
1032        void CreateMesh(ViewCell *vc);
1033
1034        void ExportViewCellGeometry(Exporter *exporter,
1035                                                                ViewCell *vc,
1036                                                                const AxisAlignedBox3 *box,
1037                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1038
1039
1040        void CollectMergeCandidates(const VssRayContainer &rays,
1041                                                                vector<MergeCandidate> &candidates);
1042
1043        void ExportSingleViewCells(const ObjectContainer &objects,
1044                                                           const int maxViewCells,
1045                                                           const bool sortViewCells,
1046                                                           const bool exportPvs,
1047                                                           const bool exportRays,
1048                                                           const int maxRays,
1049                                                           const string prefix,
1050                                                           VssRayContainer *visRays = NULL);
1051
1052        bool LineSegmentIntersects(const Vector3 &origin,
1053                                                           const Vector3 &termination,
1054                                                           ViewCell *viewCell);
1055
1056protected:
1057
1058        /** Collects view cells from a hierarchy.
1059        */
1060        void CollectViewCells();
1061
1062        KdNode *GetNodeForPvs(KdLeaf *leaf);
1063
1064        ////////////////////////////////////////
1065
1066        /// the BSP tree.
1067        KdTree *mKdTree;
1068
1069        /// depth of the KD tree nodes with represent the view cells
1070        int mKdPvsDepth;
1071
1072
1073};
1074
1075
1076/** Manages different higher order operations on the view cells.
1077*/
1078class VspBspViewCellsManager: public ViewCellsManager
1079{
1080
1081public:
1082
1083        VspBspViewCellsManager(ViewCellsTree *viewCellsTree, VspBspTree *tree);
1084        ~VspBspViewCellsManager();
1085
1086        int ConstructSubdivision(const ObjectContainer &objects,
1087                                                         const VssRayContainer &rays);
1088
1089        int PostProcess(const ObjectContainer &objects,
1090                                        const VssRayContainer &rays);
1091
1092        void Visualize(const ObjectContainer &objects,
1093                                   const VssRayContainer &sampleRays);
1094
1095        int GetType() const;
1096       
1097        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1098
1099        bool ViewCellsConstructed() const;
1100       
1101        int CastLineSegment(const Vector3 &origin,
1102                                                const Vector3 &termination,
1103                                                ViewCellContainer &viewcells);
1104
1105        float GetProbability(ViewCell *viewCell);
1106       
1107        ViewCell *GetViewCell(const Vector3 &point, const bool active = false) const;
1108
1109        bool GetViewPoint(Vector3 &viewPoint) const;
1110
1111        bool ViewPointValid(const Vector3 &viewPoint) const;
1112
1113        void CreateMesh(ViewCell *vc);
1114
1115        bool ExportViewCells(const string filename,
1116                                                 const bool exportPvs,
1117                                                 const ObjectContainer &objects);
1118
1119        int CastBeam(Beam &beam);
1120       
1121        void ExportViewCellGeometry(Exporter *exporter,
1122                                                                ViewCell *vc,
1123                                                                const AxisAlignedBox3 *box,
1124                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1125
1126
1127        void Finalize(ViewCell *viewCell, const bool createMesh);
1128
1129        void CollectMergeCandidates(const VssRayContainer &rays,
1130                                                                vector<MergeCandidate> &candidates);
1131
1132        void ExportSingleViewCells(const ObjectContainer &objects,
1133                                                           const int maxViewCells,
1134                                                           const bool sortViewCells,
1135                                                           const bool exportPvs,
1136                                                           const bool exportRays,               
1137                                                           const int maxRays,
1138                                                           const string prefix,
1139                                                           VssRayContainer *visRays = NULL);
1140
1141       
1142        bool LineSegmentIntersects(const Vector3 &origin,
1143                                                           const Vector3 &termination,
1144                                                           ViewCell *viewCell);
1145
1146protected:
1147
1148        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1149                                                                ViewCellContainer &viewCells) const;
1150       
1151        /** Merges view cells according to some criteria
1152        */
1153        void MergeViewCells(const VssRayContainer &rays,
1154                                                const ObjectContainer &objects);
1155       
1156        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
1157
1158        void CollectViewCells();
1159
1160        /** Returns maximal depth difference of view cell
1161                leaves in tree.
1162        */
1163        int GetMaxTreeDiff(ViewCell *vc) const;
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(BspNode *root);
1172
1173        /// HACK for testing visibility filter functionality
1174        void TestFilter(const ObjectContainer &objects);
1175
1176        /** Visualization of the pvs difference to exact visubility using
1177                from point queries.
1178        */
1179        void VisualizeWithFromPointQueries();
1180
1181        /** Evaluate from point queries for the current scene.
1182        */
1183        void EvalFromPointQueries();
1184
1185        /** Exports visualization of the BSP splits.
1186        */
1187        void ExportSplits(const ObjectContainer &objects,
1188                                          const VssRayContainer &rays);
1189
1190
1191        /////////////////////////
1192
1193        /// the view space partition BSP tree.
1194        VspBspTree *mVspBspTree;
1195
1196
1197private:
1198
1199        /** test if subdivision is valid in terms of volume / area.
1200        */
1201        void TestSubdivision();
1202};
1203
1204#define TEST_EVALUATION 0
1205
1206/**
1207        Manages different higher order operations on the view cells.
1208*/
1209class VspOspViewCellsManager: public ViewCellsManager
1210{
1211        friend class ViewCellsParseHandlers;
1212
1213public:
1214
1215        VspOspViewCellsManager(ViewCellsTree *vcTree,
1216                                                   const string &hierarchyType);
1217       
1218        ~VspOspViewCellsManager();
1219
1220        int ConstructSubdivision(const ObjectContainer &objects,
1221                                                         const VssRayContainer &rays);
1222
1223        int PostProcess(const ObjectContainer &objects,
1224                                        const VssRayContainer &rays);
1225
1226        void Visualize(const ObjectContainer &objects,
1227                                   const VssRayContainer &sampleRays);
1228
1229        int GetType() const;
1230       
1231        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
1232
1233        virtual Intersectable *GetIntersectable(
1234                const VssRay &ray, const bool isTermination) const;
1235
1236        bool ViewCellsConstructed() const;
1237
1238       
1239        int CastLineSegment(const Vector3 &origin,
1240                                                const Vector3 &termination,
1241                                                ViewCellContainer &viewcells);
1242
1243        bool LineSegmentIntersects(const Vector3 &origin,
1244                                                           const Vector3 &termination,
1245                                                           ViewCell *viewCell);
1246
1247        float GetProbability(ViewCell *viewCell);
1248       
1249        ViewCell *GetViewCell(const Vector3 &point,
1250                                                  const bool active = false) const;
1251
1252        bool GetViewPoint(Vector3 &viewPoint) const;
1253
1254        bool ViewPointValid(const Vector3 &viewPoint) const;
1255
1256        void CreateMesh(ViewCell *vc);
1257
1258        bool ExportViewCells(const string filename,
1259                                                 const bool exportPvs,
1260                                                 const ObjectContainer &objects);
1261
1262        int CastBeam(Beam &beam);
1263
1264        void Finalize(ViewCell *viewCell, const bool createMesh);
1265
1266        void ExportSingleViewCells(const ObjectContainer &objects,
1267                                                           const int maxViewCells,
1268                                                           const bool sortViewCells,
1269                                                           const bool exportPvs,
1270                                                           const bool exportRays,
1271                                                           const int maxRays,
1272                                                           const string prefix,
1273                                                           VssRayContainer *visRays = NULL);
1274       
1275        float UpdateObjectCosts();
1276
1277protected:
1278
1279        VspOspViewCellsManager(ViewCellsTree *vcTree, HierarchyManager *hm);
1280       
1281#if 1//TEST_EVALUATION
1282        virtual void EvalViewCellPartition();
1283#endif
1284
1285        /** Exports view cell geometry.
1286        */
1287        void ExportViewCellGeometry(Exporter *exporter,
1288                                                                ViewCell *vc,
1289                                                                const AxisAlignedBox3 *box,
1290                                                                const AxisAlignedPlane *clipPlane = NULL) const;
1291
1292        int ComputeBoxIntersections(const AxisAlignedBox3 &box,
1293                                                                ViewCellContainer &viewCells) const;
1294       
1295        void CollectViewCells();
1296
1297        virtual void CompressViewCells();
1298
1299        /** Prepare view cells for use after loading them from disc.
1300        */
1301        void PrepareLoadedViewCells();
1302
1303        /** Constructs merge hierarchy which corresponds to the spatial hierarchy.
1304        */
1305        ViewCell *ConstructSpatialMergeTree(VspNode *root);
1306
1307        /** Exports visualization of the PVS.
1308        */
1309        void ExportPvs(const ObjectContainer &objects, const VssRayContainer &rays);
1310
1311        /** Returns a hierarchy manager of the given name.
1312        */
1313        static HierarchyManager *CreateHierarchyManager(const string &name);
1314
1315        //void ExportStats(const string &mergeStats);
1316
1317        /** collect objects intersecting a given spatial box
1318        */
1319        virtual void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
1320
1321
1322        /////////////////////////////////////////
1323
1324        bool mCompressObjects;
1325
1326        HierarchyManager *mHierarchyManager;
1327};
1328
1329
1330}
1331
1332#endif
Note: See TracBrowser for help on using the repository browser.