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

Revision 563, 16.3 KB checked in by bittner, 18 years ago (diff)

rss sampling changes, preprocessor::GenerateRays?

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