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

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