source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsManager.h @ 587

Revision 587, 19.4 KB checked in by mattausch, 18 years ago (diff)

updated vspkdtree for regular sudbivision

Line 
1#ifndef _ViewCellsManager_H__
2#define _ViewCellsManager_H__
3
4#include "Ray.h"
5#include "VssRay.h"
6#include "Containers.h"
7#include "ViewCell.h"
8
9class ViewCell;
10class Intersectable;
11class RenderSimulator;
12class Renderer;
13class Mesh;
14struct Triangle3;
15class SimulationStatistics;
16class BspTree;
17class KdTree;
18class VspKdTree;
19class VspBspTree;
20class KdNode;
21class KdLeaf;
22class VspKdTree;
23class AxisAlignedBox3;
24class BspLeaf;
25class ViewCellsStatistics;
26class Exporter;
27class Beam;
28class Preprocessor;
29class ViewCellsTree;
30class MergeCandidate;
31
32struct BspRay;
33
34/**
35        Manages different higher order operations on the view cells.
36*/
37class ViewCellsManager
38{
39
40public:
41  struct PvsStatistics {
42        int minPvs;
43        int maxPvs;
44        float avgPvs;
45        int viewcells;
46  };
47 
48  /// view cell container types
49  enum {BSP, KD, VSP_KD, VSP_BSP};
50 
51        /** Constructor taking the initial and construction samples.
52                @param initialSamples the maximal number of samples used for creating the hierarchy
53                of view cells
54                @param constructionSamples the maximal number of samples used for construction
55        */
56        ViewCellsManager(const int initialSamples, const int constructionSamples);
57
58        ViewCellsManager();
59
60        virtual ~ViewCellsManager();
61
62        /** Constructs view cells container taking a preprocessor
63                @returns the output rays (if not null)
64        */
65        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
66
67        /** Constructs view cell container with a given number of samples.
68        */
69        virtual int ConstructSubdivision(const ObjectContainer &objects,
70                                                  const VssRayContainer &rays) = 0;
71
72        /** Computes sample contributions of the rays to the view cells PVS.
73       
74                @param rays bundle of rays used to find intersections with view cells and
75                adding the contribution
76                @param addRays true if rays should be added to the PVSs of the viewcells they
77                intersect
78                @param storeViewCells true if view cells should be stored in the ray
79        */
80  float ComputeSampleContributions(const VssRayContainer &rays,
81                                                                   const bool addContributions,
82                                                                   const bool storeViewCells);
83
84  /** Add sample contributions to the viewcells they intersect */
85  void AddSampleContributions(const VssRayContainer &rays);
86 
87
88        /** Computes sample contribution of a simgle ray to the view cells PVS.
89                @param ray finds intersections with view cells and holds the contribution
90                @param castRay true if ray should be cast to gain the information, false if ray
91                is already holding the information and need not be recast.
92         
93                @returns number of sample contributions
94        */
95        virtual float ComputeSampleContributions(VssRay &ray, const bool addRays, const bool storeViewCells);
96
97  virtual void AddSampleContributions(VssRay &ray);
98
99        /** Prints out statistics of the view cells.
100        */
101        virtual void PrintStatistics(ostream &s) const;
102
103        /** Post processes view cells givemŽa number of rays.
104        */
105        virtual int PostProcess(const ObjectContainer &objects,
106                                                        const VssRayContainer &rays) = 0;
107
108        /** Show visualization of the view cells.
109        */
110        virtual void Visualize(const ObjectContainer &objects,
111                                                   const VssRayContainer &sampleRays) = 0;
112
113        /** type of the view cell container.
114        */
115        virtual int GetType() const = 0;
116
117        /** Load the input viewcells. The input viewcells should be given as a collection
118                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
119                the viewcell. The method then builds a BSP tree of these view cells.
120               
121                @param filename file to load
122                @return true on success
123    */
124    virtual bool LoadViewCellsGeometry(const string filename);
125
126       
127        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
128                @param the base triangle
129                @param the height of the newly created view cell
130        */
131        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
132
133        /** Merges two view cells.
134                @note the piercing rays of the front and back will be ordered   
135                @returns new view cell based on the merging.
136        */
137        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
138
139        /** Merges a container of view cells.
140                @returns new view cell based on the merging.
141        */
142        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
143       
144        /** Generates view cell of type specified by this manager
145        */
146        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
147
148        /** Adds a new view cell to the list of predefined view cells.
149        */
150        void AddViewCell(ViewCell *viewCell);
151
152        /** Derive view cells from objects.
153        */
154        void DeriveViewCells(const ObjectContainer &objects,
155                                                 ViewCellContainer &viewCells,
156                                                 const int maxViewCells) const;
157
158        /** Sets maximal number of samples used for the
159                construction of the view cells.
160        */
161        void SetVisualizationSamples(const int visSamples);
162
163        /** Sets maximal number of samples used for the construction of the view cells.
164        */
165        void SetConstructionSamples(const int constructionSamples);
166
167        /** Sets maximal number of samples used for the visualization of the view cells.
168        */
169        void SetInitialSamples(const int initialSamples);
170
171        /** Sets maximal number of samples used for the post processing of the view cells.
172        */
173        void SetPostProcessSamples(const int postProcessingSamples);
174
175        /** See set.
176        */
177        int GetVisualizationSamples() const;
178
179        /** See set.
180        */
181        int GetConstructionSamples() const;
182
183        /** See set.
184        */
185        int GetPostProcessSamples() const;
186
187        /** Returns true if view cells wer already constructed.
188        */
189        virtual bool ViewCellsConstructed() const = 0;
190
191        /** cast line segment to get a list of unique viewcells which are intersected
192                by this line segment
193        */
194 
195        virtual int CastLineSegment(const Vector3 &origin,
196                                                          const Vector3 &termination,
197                                                          ViewCellContainer &viewcells
198                                                          ) = 0;
199
200        virtual void GetPvsStatistics(PvsStatistics &stat);
201
202        /** Get a viewcell containing the specified point */
203        virtual ViewCell *GetViewCell(const Vector3 &point) const = 0;
204 
205        virtual void PrintPvsStatistics(ostream &s);
206
207        /** Returns probability that view point lies in one view cell.
208        */
209        virtual float GetProbability(ViewCell *viewCell) = 0;
210
211        /** Returns render cost of a single view cell given the render cost of an object.
212        */
213        virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
214
215        /** Returns container of loaded / generated view cells.
216        */
217        ViewCellContainer &GetViewCells();
218
219        /** Helper function used to split ray sets uniformly
220                into one that is currently used and the other that
221                is saved for later processing.
222                @param sourceRays the input ray set
223                @param maxSize the maximal number of rays that will be used
224                @param usedRays returns the used ray set
225                @param savedRays if not null, returns the saved ray set
226        */
227        void GetRaySets(const VssRayContainer &sourceRays,
228                                        const int maxSize,
229                                        VssRayContainer &usedRays,
230                                        VssRayContainer *savedRays = NULL) const;
231       
232        /** Returns accumulated area of all view cells.
233        */
234        float GetAccVcArea();
235
236        /** Returns area of one view cell.
237        */
238        virtual float GetArea(ViewCell *viewCell) const;
239
240        /** Returns volume of view cell.
241        */
242        virtual float GetVolume(ViewCell *viewCell) const;
243
244        /** Sets the current renderer mainly for view cells statistics.
245        */
246        void SetRenderer(Renderer *renderer);
247
248        /** Computes a (random) view point in the valid view space.
249                @returns true if valid view point was found
250        */
251        virtual bool GetViewPoint(Vector3 &viewPoint) const;
252
253        /** Returns true if this view point is in the valid view space.
254        */
255        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
256
257        /** Sets a view space boundary.
258        */
259        void SetViewSpaceBox(const AxisAlignedBox3 &box);
260
261        AxisAlignedBox3 GetViewSpaceBox() const;
262
263        /** Creates mesh for this view cell.
264        */
265        virtual void CreateMesh(ViewCell *vc) = NULL;
266
267        /** Writes view cells to disc.
268        */
269        virtual bool ExportViewCells(const string filename);
270
271        /** Casts beam to collect view cells.
272        */
273        virtual int CastBeam(Beam &beam);
274
275        /** Checks if view cell is considered as valid.
276        */
277        virtual bool CheckValidity(ViewCell *vc,
278                                                           int minPvsSize,
279                                                           int maxPvsSize) const;
280
281       
282        /** Sets validity of view cell
283        */
284        virtual void SetValidity(ViewCell *vc,
285                                                         int minPvsSize,
286                                                         int maxPvsSize) const;
287
288  /** sets validy of all viewcells */
289        virtual void SetValidity(
290                                                         int minPvsSize,
291                                                         int maxPvsSize) const;
292
293  /** set valid viewcells in the range of pvs. sorts the viewcells
294          according to the pvs and then pickups those in the ranges */
295 
296  void
297  SetValidityPercentage(
298                                                const float minValid,
299                                                const float maxValid
300                                                );
301
302  int
303  CountValidViewcells() const;
304
305        /** Returns maximal allowed pvs size.
306        */
307        int GetMaxPvsSize() const;
308
309        /** Returns maximal allowed pvs size.
310        */
311        int GetMinPvsSize() const;
312
313        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
314                where pvs is still considered valid.
315        */
316        float GetMaxPvsRatio() const;
317
318        /** Exports view cell geometry.
319        */
320        virtual void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const = 0;
321
322        virtual void FinalizeViewCells(const bool createMesh);
323
324
325        /** Loads view cells from file. The view cells manager is created with respect to the loaded
326                view cells.
327
328                @returns the view cells manager if loading was successful, false otherwise
329        */
330        static ViewCellsManager *LoadViewCells(const string filename, ObjectContainer *objects);
331
332        /** Evaluates statistics values on view cells.
333        */
334        void EvaluateRenderStatistics(float &totalRenderCost,
335                                                                  float &expectedRenderCost,
336                                                                  float &deviation,
337                                                                  float &variance,
338                                                                  int &totalPvs,
339                                                                  float &avgRenderCost);
340
341
342        /** Returns hierarchy of the view cells.
343        */
344        ViewCellsTree *GetViewCellsTree();
345
346        virtual void CollectMergeCandidates(const VssRayContainer &rays,
347                                                                                vector<MergeCandidate> &candidates) = 0;
348
349       
350
351protected:
352
353        /**
354                if the view cells tree was already constructed or not.
355        */
356        bool ViewCellsTreeConstructed() const;
357
358        int CastPassSamples(const int samplesPerPass,
359                                                const int sampleType,
360                                                VssRayContainer &vssRays) const;
361
362        void ParseEnvironment();
363
364        /** Creates unique view cell ids.
365        */
366        void CreateUniqueViewCellIds();
367
368        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
369        */
370        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
371
372        /** Recollects view cells and resets statistics.
373        */
374        void ResetViewCells();
375       
376        /** Collects the view cells in the view cell container.
377        */
378        virtual void CollectViewCells() = 0;
379       
380        /** Evaluates view cells statistics and stores it in
381                mViewCellsStatistics.
382        */
383        void EvaluateViewCellsStats();
384
385        //-- helper functions for view cell visualization
386
387        /** Exports the view cell partition.
388        */
389        void ExportViewCellsForViz(Exporter *exporter) const;
390
391        /** Sets exporter color.
392        */
393        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
394
395
396        virtual float GetViewSpaceVolume();
397       
398        /** Creates meshes from the view cells.
399        */
400        void CreateViewCellMeshes();
401
402        /**
403                Exports single view cell.
404                NOTE: should be in exporter!!
405        */
406        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
407
408
409        /**
410                Takes different measures to prepares the view cells after loading them from disc.
411        */
412        virtual void PrepareLoadedViewCells() {};
413
414        /// Renders the view cells.
415        Renderer *mRenderer;
416
417        /// Loaded view cells
418        ViewCellContainer mViewCells;
419
420        ViewCellsTree *mViewCellsTree;
421
422        /// maximum number of samples taken for construction of the view cells
423        int mConstructionSamples;
424        int mSamplesPerPass;
425        int mInitialSamples;
426        int mPostProcessSamples;
427        int mVisualizationSamples;
428
429        float mTotalAreaValid;
430        float mTotalArea;
431
432        int mMaxPvsSize;
433        int mMinPvsSize;
434        float mMaxPvsRatio;
435
436        int mNumMergedViewCells;
437
438        bool mCompressViewCells;
439
440        ViewCellsStatistics mViewCellsStats;
441        /// the scene bounding box
442        AxisAlignedBox3 mViewSpaceBox;
443        /// holds the view cell meshes
444        MeshContainer mMeshContainer;
445        /// if view cells should be exported
446        bool mExportViewCells;
447
448        bool mOnlyValidViewCells;
449
450        /// if rays should be used to collect merge candidates
451        bool mUseRaysForMerge;
452       
453
454        //-- visualization options
455       
456        /// color code for view cells
457        int mColorCode;
458        bool mExportGeometry;
459        bool mExportRays;
460
461        bool mViewCellsFinished;
462};
463
464
465
466/**
467        Manages different higher order operations on the view cells.
468*/
469class BspViewCellsManager: public ViewCellsManager
470{
471
472public:
473        /** Constructor taking the bsp tree and the number of samples
474                used to construct the bsp tree.
475        */
476        BspViewCellsManager(BspTree *tree);
477
478        ~BspViewCellsManager();
479
480        int ConstructSubdivision(const ObjectContainer &objects,
481                                  const VssRayContainer &rays);
482
483        int PostProcess(const ObjectContainer &objects,
484                                        const VssRayContainer &rays);
485
486        void Visualize(const ObjectContainer &objects,
487                                   const VssRayContainer &sampleRays);
488
489        int GetType() const;
490       
491        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
492
493        bool ViewCellsConstructed() const;
494
495        //void PrintStatistics(ostream &s) const;
496
497        int CastLineSegment(const Vector3 &origin,
498                                                const Vector3 &termination,
499                                                ViewCellContainer &viewcells);
500       
501        float GetProbability(ViewCell *viewCell);
502        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
503
504        /** Get a viewcell containing the specified point */
505        ViewCell *GetViewCell(const Vector3 &point) const;
506
507        void CreateMesh(ViewCell *vc);
508
509        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
510       
511        void CollectMergeCandidates(const VssRayContainer &rays,
512                                                                vector<MergeCandidate> &candidates);
513
514        void Finalize(ViewCell *viewCell, const bool createMesh);
515
516protected:
517
518        void CollectViewCells();
519
520        void ExportColor(Exporter *exporter, ViewCell *vc) const;
521       
522
523
524        /// the BSP tree.
525        BspTree *mBspTree;
526       
527        vector<BspRay *> mBspRays;
528
529private:
530
531        /** Exports visualization of the BSP splits.
532        */
533        void ExportSplits(const ObjectContainer &objects);
534
535        /** Exports visualization of the BSP PVS.
536        */
537        void ExportBspPvs(const ObjectContainer &objects);
538
539};
540
541/**
542        Manages different higher order operations on the KD type view cells.
543*/
544class KdViewCellsManager: public ViewCellsManager
545{
546
547public:
548
549        KdViewCellsManager(KdTree *tree);
550
551        int ConstructSubdivision(const ObjectContainer &objects,
552                                  const VssRayContainer &rays);
553
554        int CastLineSegment(const Vector3 &origin,
555                                                const Vector3 &termination,
556                                                ViewCellContainer &viewcells);
557
558        int PostProcess(const ObjectContainer &objects,
559                                        const VssRayContainer &rays);
560
561        void Visualize(const ObjectContainer &objects,
562                                   const VssRayContainer &sampleRays);
563
564        int GetType() const;
565
566        bool ViewCellsConstructed() const;
567
568        ViewCell *GenerateViewCell(Mesh *mesh) const;
569
570        /** Prints out statistics of this approach.
571        */
572        //  virtual void PrintStatistics(ostream &s) const;
573        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
574
575        float GetProbability(ViewCell *viewCell);
576        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
577
578        void CreateMesh(ViewCell *vc);
579
580        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
581
582        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
583
584protected:
585
586        /** Collects view cells from a hierarchy.
587        */
588        void CollectViewCells();
589
590        KdNode *GetNodeForPvs(KdLeaf *leaf);
591
592        void ExportColor(Exporter *exporter, ViewCell *vc) const;
593
594
595        /// the BSP tree.
596        KdTree *mKdTree;
597
598        /// depth of the KD tree nodes with represent the view cells
599        int mKdPvsDepth;
600};
601
602/**
603        Manages different higher order operations on the view cells
604        for vsp kd tree view cells.
605*/
606class VspKdViewCellsManager: public ViewCellsManager
607{
608
609public:
610
611        VspKdViewCellsManager(VspKdTree *vspKdTree);
612
613        int ConstructSubdivision(const ObjectContainer &objects,
614                                  const VssRayContainer &rays);
615
616
617        int PostProcess(const ObjectContainer &objects,
618                                        const VssRayContainer &rays);
619
620        void Visualize(const ObjectContainer &objects,
621                                   const VssRayContainer &sampleRays);
622
623        int GetType() const;
624       
625        bool ViewCellsConstructed() const;
626
627        //virtual void PrintStatistics(ostream &s) const;
628
629        ViewCell *GenerateViewCell(Mesh *mesh) const;
630
631
632    int CastLineSegment(const Vector3 &origin,
633                                                const Vector3 &termination,
634                                                ViewCellContainer &viewcells);
635
636        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
637
638        float GetProbability(ViewCell *viewCell);
639        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
640
641        void CreateMesh(ViewCell *vc);
642
643        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
644
645        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
646
647protected:
648
649        void ExportLeaves(const ObjectContainer &objects,
650                                          const VssRayContainer &sampleRays);
651
652        void CollectViewCells();
653
654        void ExportColor(Exporter *exporter, ViewCell *vc) const;
655
656
657
658        /// the BSP tree.
659        VspKdTree *mVspKdTree;
660};
661
662
663
664/**
665        Manages different higher order operations on the view cells.
666*/
667class VspBspViewCellsManager: public ViewCellsManager
668{
669
670public:
671
672        VspBspViewCellsManager(VspBspTree *tree);
673        ~VspBspViewCellsManager();
674
675        int ConstructSubdivision(const ObjectContainer &objects,
676                                  const VssRayContainer &rays);
677
678        int PostProcess(const ObjectContainer &objects,
679                                        const VssRayContainer &rays);
680
681        void Visualize(const ObjectContainer &objects,
682                                   const VssRayContainer &sampleRays);
683
684        int GetType() const;
685       
686        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
687
688        bool ViewCellsConstructed() const;
689
690       
691        int CastLineSegment(const Vector3 &origin,
692                                                const Vector3 &termination,
693                                                ViewCellContainer &viewcells);
694
695        float GetProbability(ViewCell *viewCell);
696        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
697       
698        ViewCell *GetViewCell(const Vector3 &point) const;
699
700        bool GetViewPoint(Vector3 &viewPoint) const;
701
702        bool ViewPointValid(const Vector3 &viewPoint) const;
703
704        void CreateMesh(ViewCell *vc);
705
706        //bool LoadViewCellsGeometry(const string filename, ObjectContainer *objects);
707        bool ExportViewCells(const string filename);
708
709        int CastBeam(Beam &beam);
710
711        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
712
713        //float GetVolume(ViewCell *viewCell) const;
714
715        void Finalize(ViewCell *viewCell, const bool createMesh);
716
717        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
718
719protected:
720
721        /** Merges the view cells.
722        */
723        void MergeViewCells(const VssRayContainer &rays,
724                                                const ObjectContainer &objects);
725       
726        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
727
728        void CollectViewCells();
729
730        /** Returns maximal depth difference of view cell
731                leaves in tree.
732        */
733        int GetMaxTreeDiff(ViewCell *vc) const;
734       
735
736        void ExportColor(Exporter *exporter, ViewCell *vc) const;
737
738        void PrepareLoadedViewCells();
739
740        /// the view space partition BSP tree.
741        VspBspTree *mVspBspTree;
742
743private:
744
745        /** Exports visualization of the BSP splits.
746        */
747        void ExportSplits(const ObjectContainer &objects,
748                                          const VssRayContainer &rays);
749
750        /** Exports visualization of the BSP PVS.
751        */
752        void ExportBspPvs(const ObjectContainer &objects,
753                                          const VssRayContainer &rays);
754
755};
756
757
758class ViewCellsManagerFactory
759{
760
761public:
762
763        ViewCellsManager *Create(const string mName);
764
765};
766
767#endif
Note: See TracBrowser for help on using the repository browser.