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

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