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

Revision 564, 16.4 KB checked in by mattausch, 18 years ago (diff)
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        bool mOnlyValidViewCells;
366
367        //-- visualization options
368       
369        /// color code for view cells
370        int mColorCode;
371        bool mExportGeometry;
372        bool mExportRays;
373
374        bool mViewCellsFinished;
375};
376
377
378
379/**
380        Manages different higher order operations on the view cells.
381*/
382class BspViewCellsManager: public ViewCellsManager
383{
384
385public:
386        /** Constructor taking the bsp tree and the number of samples
387                used to construct the bsp tree.
388        */
389        BspViewCellsManager(BspTree *tree, int constructionSamples);
390
391        ~BspViewCellsManager();
392
393        int Construct(const ObjectContainer &objects,
394                                  const VssRayContainer &rays);
395
396
397        int PostProcess(const ObjectContainer &objects,
398                                        const VssRayContainer &rays);
399
400        void Visualize(const ObjectContainer &objects,
401                                   const VssRayContainer &sampleRays);
402
403        int GetType() const;
404       
405        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
406
407        bool ViewCellsConstructed() const;
408
409        //void PrintStatistics(ostream &s) const;
410
411        int CastLineSegment(const Vector3 &origin,
412                                                const Vector3 &termination,
413                                                ViewCellContainer &viewcells);
414       
415        float GetProbability(ViewCell *viewCell);
416        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
417
418        /** Get a viewcell containing the specified point */
419        ViewCell *GetViewCell(const Vector3 &point);
420
421        void CreateMesh(ViewCell *vc);
422
423        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
424
425protected:
426
427        void CollectViewCells();
428
429        void ConstructBspRays(const VssRayContainer &rays,
430                                                  const int numSamples);
431
432        void ExportColor(Exporter *exporter, ViewCell *vc) const;
433       
434
435
436        /// the BSP tree.
437        BspTree *mBspTree;
438       
439        vector<BspRay *> mBspRays;
440
441private:
442
443        /** Exports visualization of the BSP splits.
444        */
445        void ExportSplits(const ObjectContainer &objects);
446
447        /** Exports visualization of the BSP PVS.
448        */
449        void ExportBspPvs(const ObjectContainer &objects);
450
451};
452
453/**
454        Manages different higher order operations on the KD type view cells.
455*/
456class KdViewCellsManager: public ViewCellsManager
457{
458
459public:
460
461        KdViewCellsManager(KdTree *tree);
462
463        int Construct(const ObjectContainer &objects,
464                                  const VssRayContainer &rays);
465
466        int CastLineSegment(const Vector3 &origin,
467                                                const Vector3 &termination,
468                                                ViewCellContainer &viewcells);
469
470        int PostProcess(const ObjectContainer &objects,
471                                        const VssRayContainer &rays);
472
473        void Visualize(const ObjectContainer &objects,
474                                   const VssRayContainer &sampleRays);
475
476        int GetType() const;
477
478        bool ViewCellsConstructed() const;
479
480
481        /** Prints out statistics of this approach.
482        */
483        //  virtual void PrintStatistics(ostream &s) const;
484        ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
485
486        float GetProbability(ViewCell *viewCell);
487        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
488
489        void CreateMesh(ViewCell *vc);
490
491        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
492
493protected:
494
495        void CollectViewCells();
496        KdNode *GetNodeForPvs(KdLeaf *leaf);
497
498        void ExportColor(Exporter *exporter, ViewCell *vc) const;
499
500
501        /// the BSP tree.
502        KdTree *mKdTree;
503
504        /// depth of the KD tree nodes with represent the view cells
505        int mKdPvsDepth;
506};
507
508/**
509        Manages different higher order operations on the view cells
510        for vsp kd tree view cells.
511*/
512class VspKdViewCellsManager: public ViewCellsManager
513{
514
515public:
516
517        VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
518
519        int Construct(const ObjectContainer &objects,
520                                  const VssRayContainer &rays);
521
522
523        int PostProcess(const ObjectContainer &objects,
524                                        const VssRayContainer &rays);
525
526        void Visualize(const ObjectContainer &objects,
527                                   const VssRayContainer &sampleRays);
528
529        int GetType() const;
530       
531        bool ViewCellsConstructed() const;
532
533        //virtual void PrintStatistics(ostream &s) const;
534
535        ViewCell *GenerateViewCell(Mesh *mesh) const;
536
537
538    int CastLineSegment(const Vector3 &origin,
539                                                const Vector3 &termination,
540                                                ViewCellContainer &viewcells);
541
542        ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
543
544        float GetProbability(ViewCell *viewCell);
545        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
546
547        void CreateMesh(ViewCell *vc);
548
549        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
550
551protected:
552
553        void ExportLeaves(const ObjectContainer &objects,
554                                          const VssRayContainer &sampleRays);
555
556        void CollectViewCells();
557
558        void ExportColor(Exporter *exporter, ViewCell *vc) const;
559
560
561
562        /// the BSP tree.
563        VspKdTree *mVspKdTree;
564};
565
566
567
568/**
569        Manages different higher order operations on the view cells.
570*/
571class VspBspViewCellsManager: public ViewCellsManager
572{
573
574public:
575
576        VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
577        ~VspBspViewCellsManager();
578
579        int Construct(const ObjectContainer &objects,
580                                  const VssRayContainer &rays);
581
582        int PostProcess(const ObjectContainer &objects,
583                                        const VssRayContainer &rays);
584
585        void Visualize(const ObjectContainer &objects,
586                                   const VssRayContainer &sampleRays);
587
588        int GetType() const;
589       
590        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
591
592        bool ViewCellsConstructed() const;
593
594       
595        int CastLineSegment(const Vector3 &origin,
596                                                const Vector3 &termination,
597                                                ViewCellContainer &viewcells);
598
599        float GetProbability(ViewCell *viewCell);
600        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
601       
602        ViewCell *GetViewCell(const Vector3 &point);
603
604        bool GetViewPoint(Vector3 &viewPoint) const;
605
606        bool ViewPointValid(const Vector3 &viewPoint) const;
607
608        void CreateMesh(ViewCell *vc);
609
610        bool LoadViewCells(const string filename, ObjectContainer *objects);
611        bool ExportViewCells(const string filename);
612
613        int CastBeam(Beam &beam);
614
615        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
616
617        //float GetVolume(ViewCell *viewCell) const;
618
619        void Finalize(ViewCell *viewCell, const bool createMesh);
620
621protected:
622
623        /** Merges the view cells.
624        */
625        void MergeViewCells(const VssRayContainer &rays,
626                                                const ObjectContainer &objects);
627       
628        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
629
630        void CollectViewCells();
631
632        /** Returns maximal depth difference of view cell
633                leaves in tree.
634        */
635        int GetMaxTreeDiff(ViewCell *vc) const;
636       
637
638        void ExportColor(Exporter *exporter, ViewCell *vc) const;
639
640        /// the view space partition BSP tree.
641        VspBspTree *mVspBspTree;
642
643private:
644
645        /** Exports visualization of the BSP splits.
646        */
647        void ExportSplits(const ObjectContainer &objects,
648                                          const VssRayContainer &rays);
649
650        /** Exports visualization of the BSP PVS.
651        */
652        void ExportBspPvs(const ObjectContainer &objects,
653                                          const VssRayContainer &rays);
654
655};
656
657#endif
Note: See TracBrowser for help on using the repository browser.