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

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

fixed bug in mergueue to find root of merge and sort out doube view cells

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        ViewCellsStatistics mViewCellsStats;
439        /// the scene bounding box
440        AxisAlignedBox3 mViewSpaceBox;
441        /// holds the view cell meshes
442        MeshContainer mMeshContainer;
443        /// if view cells should be exported
444        bool mExportViewCells;
445
446        bool mOnlyValidViewCells;
447
448        /// if rays should be used to collect merge candidates
449        bool mUseRaysForMerge;
450       
451
452        //-- visualization options
453       
454        /// color code for view cells
455        int mColorCode;
456        bool mExportGeometry;
457        bool mExportRays;
458
459        bool mViewCellsFinished;
460};
461
462
463
464/**
465        Manages different higher order operations on the view cells.
466*/
467class BspViewCellsManager: public ViewCellsManager
468{
469
470public:
471        /** Constructor taking the bsp tree and the number of samples
472                used to construct the bsp tree.
473        */
474        BspViewCellsManager(BspTree *tree);
475
476        ~BspViewCellsManager();
477
478        int ConstructSubdivision(const ObjectContainer &objects,
479                                  const VssRayContainer &rays);
480
481        int PostProcess(const ObjectContainer &objects,
482                                        const VssRayContainer &rays);
483
484        void Visualize(const ObjectContainer &objects,
485                                   const VssRayContainer &sampleRays);
486
487        int GetType() const;
488       
489        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
490
491        bool ViewCellsConstructed() const;
492
493        //void PrintStatistics(ostream &s) const;
494
495        int CastLineSegment(const Vector3 &origin,
496                                                const Vector3 &termination,
497                                                ViewCellContainer &viewcells);
498       
499        float GetProbability(ViewCell *viewCell);
500        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
501
502        /** Get a viewcell containing the specified point */
503        ViewCell *GetViewCell(const Vector3 &point) const;
504
505        void CreateMesh(ViewCell *vc);
506
507        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
508       
509        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
510
511protected:
512
513        void CollectViewCells();
514
515        void ConstructBspRays(const VssRayContainer &rays,
516                                                  const int numSamples);
517
518        void ExportColor(Exporter *exporter, ViewCell *vc) const;
519       
520
521
522        /// the BSP tree.
523        BspTree *mBspTree;
524       
525        vector<BspRay *> mBspRays;
526
527private:
528
529        /** Exports visualization of the BSP splits.
530        */
531        void ExportSplits(const ObjectContainer &objects);
532
533        /** Exports visualization of the BSP PVS.
534        */
535        void ExportBspPvs(const ObjectContainer &objects);
536
537};
538
539/**
540        Manages different higher order operations on the KD type view cells.
541*/
542class KdViewCellsManager: public ViewCellsManager
543{
544
545public:
546
547        KdViewCellsManager(KdTree *tree);
548
549        int ConstructSubdivision(const ObjectContainer &objects,
550                                  const VssRayContainer &rays);
551
552        int CastLineSegment(const Vector3 &origin,
553                                                const Vector3 &termination,
554                                                ViewCellContainer &viewcells);
555
556        int PostProcess(const ObjectContainer &objects,
557                                        const VssRayContainer &rays);
558
559        void Visualize(const ObjectContainer &objects,
560                                   const VssRayContainer &sampleRays);
561
562        int GetType() const;
563
564        bool ViewCellsConstructed() const;
565
566        ViewCell *GenerateViewCell(Mesh *mesh) const;
567
568        /** Prints out statistics of this approach.
569        */
570        //  virtual void PrintStatistics(ostream &s) const;
571        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
572
573        float GetProbability(ViewCell *viewCell);
574        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
575
576        void CreateMesh(ViewCell *vc);
577
578        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
579
580        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
581
582protected:
583
584        /** Collects view cells from a hierarchy.
585        */
586        void CollectViewCells();
587
588        KdNode *GetNodeForPvs(KdLeaf *leaf);
589
590        void ExportColor(Exporter *exporter, ViewCell *vc) const;
591
592
593        /// the BSP tree.
594        KdTree *mKdTree;
595
596        /// depth of the KD tree nodes with represent the view cells
597        int mKdPvsDepth;
598};
599
600/**
601        Manages different higher order operations on the view cells
602        for vsp kd tree view cells.
603*/
604class VspKdViewCellsManager: public ViewCellsManager
605{
606
607public:
608
609        VspKdViewCellsManager(VspKdTree *vspKdTree);
610
611        int ConstructSubdivision(const ObjectContainer &objects,
612                                  const VssRayContainer &rays);
613
614
615        int PostProcess(const ObjectContainer &objects,
616                                        const VssRayContainer &rays);
617
618        void Visualize(const ObjectContainer &objects,
619                                   const VssRayContainer &sampleRays);
620
621        int GetType() const;
622       
623        bool ViewCellsConstructed() const;
624
625        //virtual void PrintStatistics(ostream &s) const;
626
627        ViewCell *GenerateViewCell(Mesh *mesh) const;
628
629
630    int CastLineSegment(const Vector3 &origin,
631                                                const Vector3 &termination,
632                                                ViewCellContainer &viewcells);
633
634        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
635
636        float GetProbability(ViewCell *viewCell);
637        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
638
639        void CreateMesh(ViewCell *vc);
640
641        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
642
643        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
644
645protected:
646
647        void ExportLeaves(const ObjectContainer &objects,
648                                          const VssRayContainer &sampleRays);
649
650        void CollectViewCells();
651
652        void ExportColor(Exporter *exporter, ViewCell *vc) const;
653
654
655
656        /// the BSP tree.
657        VspKdTree *mVspKdTree;
658};
659
660
661
662/**
663        Manages different higher order operations on the view cells.
664*/
665class VspBspViewCellsManager: public ViewCellsManager
666{
667
668public:
669
670        VspBspViewCellsManager(VspBspTree *tree);
671        ~VspBspViewCellsManager();
672
673        int ConstructSubdivision(const ObjectContainer &objects,
674                                  const VssRayContainer &rays);
675
676        int PostProcess(const ObjectContainer &objects,
677                                        const VssRayContainer &rays);
678
679        void Visualize(const ObjectContainer &objects,
680                                   const VssRayContainer &sampleRays);
681
682        int GetType() const;
683       
684        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
685
686        bool ViewCellsConstructed() const;
687
688       
689        int CastLineSegment(const Vector3 &origin,
690                                                const Vector3 &termination,
691                                                ViewCellContainer &viewcells);
692
693        float GetProbability(ViewCell *viewCell);
694        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
695       
696        ViewCell *GetViewCell(const Vector3 &point) const;
697
698        bool GetViewPoint(Vector3 &viewPoint) const;
699
700        bool ViewPointValid(const Vector3 &viewPoint) const;
701
702        void CreateMesh(ViewCell *vc);
703
704        //bool LoadViewCellsGeometry(const string filename, ObjectContainer *objects);
705        bool ExportViewCells(const string filename);
706
707        int CastBeam(Beam &beam);
708
709        void ExportViewCellGeometry(Exporter *exporter, ViewCell *vc) const;
710
711        //float GetVolume(ViewCell *viewCell) const;
712
713        void Finalize(ViewCell *viewCell, const bool createMesh);
714
715        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
716
717protected:
718
719        /** Merges the view cells.
720        */
721        void MergeViewCells(const VssRayContainer &rays,
722                                                const ObjectContainer &objects);
723       
724        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
725
726        void CollectViewCells();
727
728        /** Returns maximal depth difference of view cell
729                leaves in tree.
730        */
731        int GetMaxTreeDiff(ViewCell *vc) const;
732       
733
734        void ExportColor(Exporter *exporter, ViewCell *vc) const;
735
736        void PrepareLoadedViewCells();
737
738        /// the view space partition BSP tree.
739        VspBspTree *mVspBspTree;
740
741private:
742
743        /** Exports visualization of the BSP splits.
744        */
745        void ExportSplits(const ObjectContainer &objects,
746                                          const VssRayContainer &rays);
747
748        /** Exports visualization of the BSP PVS.
749        */
750        void ExportBspPvs(const ObjectContainer &objects,
751                                          const VssRayContainer &rays);
752
753};
754
755
756class ViewCellsManagerFactory
757{
758
759public:
760
761        ViewCellsManager *Create(const string mName);
762
763};
764
765#endif
Note: See TracBrowser for help on using the repository browser.