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

Revision 508, 15.5 KB checked in by mattausch, 18 years ago (diff)

implemented view cells exporting / loading
improved vsp bsp tree (only axis aligbed until a level), reuse results from Plane
testing, collectmergeneighbors
implemented view cell meshes

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