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

Revision 562, 16.3 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 cell.
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
253        /** Checks if view cell is considered as valid.
254        */
255        virtual bool CheckValidity(ViewCell *vc,
256                                                           int minPvsSize,
257                                                           int maxPvsSize) const;
258
259       
260        /** Sets validity of view cell
261        */
262        virtual void SetValidity(ViewCell *vc,
263                                                         int minPvsSize,
264                                                         int maxPvsSize) const;
265
266        /** Returns maximal allowed pvs size.
267        */
268        int GetMaxPvsSize() const;
269
270        /** Returns maximal allowed pvs size.
271        */
272        int GetMinPvsSize() const;
273
274        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
275                where pvs is still considered valid.
276        */
277        float GetMaxPvsRatio() const;
278
279        /** Exports view cell geometry.
280        */
281        virtual void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const = 0;
282
283        virtual void FinalizeViewCells(const bool createMesh);
284
285protected:
286
287        void ParseEnvironment();
288
289        /** Creates unique view cell ids.
290        */
291        void CreateUniqueViewCellIds();
292
293        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
294        */
295        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
296
297        /** Recollects view cells and resets statistics.
298        */
299        void ResetViewCells();
300       
301        /** Collects the view cells in the view cell container.
302        */
303        virtual void CollectViewCells() = 0;
304       
305        /** Evaluates view cells statistics and stores it in
306                mViewCellsStatistics.
307        */
308        void EvaluateViewCellsStats();
309
310        //-- helper functions for view cell visualization
311
312        /** Exports the view cell partition.
313        */
314        void ExportViewCellsForViz(Exporter *exporter) const;
315
316        /** Sets exporter color.
317        */
318        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
319
320
321        virtual float GetViewSpaceVolume();
322       
323        /** Creates meshes from the view cells.
324        */
325        void CreateViewCellMeshes();
326
327        /**
328                Exports single view cell.
329                NOTE: should be in exporter!!
330        */
331        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
332
333        /// the view cell corresponding to space ouside the valid view space
334        //ViewCell *mOutOfBoundsCell;
335
336        /// Renders the view cells.
337        Renderer *mRenderer;
338
339        /// Loaded view cells
340        ViewCellContainer mViewCells;
341
342        /// maximum number of samples taken for construction of the view cells
343        int mConstructionSamples;
344        int mPostProcessSamples;
345        int mVisualizationSamples;
346
347        float mTotalAreaValid;
348        float mTotalArea;
349
350        int mMaxPvsSize;
351        int mMinPvsSize;
352        float mMaxPvsRatio;
353
354        ViewCellsStatistics mViewCellsStats;
355        /// the scene bounding box
356        AxisAlignedBox3 mViewSpaceBox;
357        /// holds the view cell meshes
358        MeshContainer mMeshContainer;
359        /// if view cells should be exported
360        bool mExportViewCells;
361
362
363        //-- visualization options
364       
365        /// color code for view cells
366        int mColorCode;
367        bool mExportGeometry;
368        bool mExportRays;
369
370        bool mViewCellsFinished;
371};
372
373
374
375/**
376        Manages different higher order operations on the view cells.
377*/
378class BspViewCellsManager: public ViewCellsManager
379{
380
381public:
382        /** Constructor taking the bsp tree and the number of samples
383                used to construct the bsp tree.
384        */
385        BspViewCellsManager(BspTree *tree, int constructionSamples);
386
387        ~BspViewCellsManager();
388
389        int Construct(const ObjectContainer &objects,
390                                  const VssRayContainer &rays);
391
392
393        int PostProcess(const ObjectContainer &objects,
394                                        const VssRayContainer &rays);
395
396        void Visualize(const ObjectContainer &objects,
397                                   const VssRayContainer &sampleRays);
398
399        int GetType() const;
400       
401        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
402
403        bool ViewCellsConstructed() const;
404
405        //void PrintStatistics(ostream &s) const;
406
407        int CastLineSegment(const Vector3 &origin,
408                                                const Vector3 &termination,
409                                                ViewCellContainer &viewcells);
410       
411        float GetProbability(ViewCell *viewCell);
412        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
413
414        /** Get a viewcell containing the specified point */
415        ViewCell *GetViewCell(const Vector3 &point);
416
417        void CreateMesh(ViewCell *vc);
418
419        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
420
421protected:
422
423        void CollectViewCells();
424
425        void ConstructBspRays(const VssRayContainer &rays,
426                                                  const int numSamples);
427
428        void ExportColor(Exporter *exporter, ViewCell *vc) const;
429       
430
431
432        /// the BSP tree.
433        BspTree *mBspTree;
434       
435        vector<BspRay *> mBspRays;
436
437private:
438
439        /** Exports visualization of the BSP splits.
440        */
441        void ExportSplits(const ObjectContainer &objects);
442
443        /** Exports visualization of the BSP PVS.
444        */
445        void ExportBspPvs(const ObjectContainer &objects);
446
447};
448
449/**
450        Manages different higher order operations on the KD type view cells.
451*/
452class KdViewCellsManager: public ViewCellsManager
453{
454
455public:
456
457        KdViewCellsManager(KdTree *tree);
458
459        int Construct(const ObjectContainer &objects,
460                                  const VssRayContainer &rays);
461
462        int CastLineSegment(const Vector3 &origin,
463                                                const Vector3 &termination,
464                                                ViewCellContainer &viewcells);
465
466        int PostProcess(const ObjectContainer &objects,
467                                        const VssRayContainer &rays);
468
469        void Visualize(const ObjectContainer &objects,
470                                   const VssRayContainer &sampleRays);
471
472        int GetType() const;
473
474        bool ViewCellsConstructed() const;
475
476
477        /** Prints out statistics of this approach.
478        */
479        //  virtual void PrintStatistics(ostream &s) const;
480        ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
481
482        float GetProbability(ViewCell *viewCell);
483        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
484
485        void CreateMesh(ViewCell *vc);
486
487        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
488
489protected:
490
491        void CollectViewCells();
492        KdNode *GetNodeForPvs(KdLeaf *leaf);
493
494        void ExportColor(Exporter *exporter, ViewCell *vc) const;
495
496
497        /// the BSP tree.
498        KdTree *mKdTree;
499
500        /// depth of the KD tree nodes with represent the view cells
501        int mKdPvsDepth;
502};
503
504/**
505        Manages different higher order operations on the view cells
506        for vsp kd tree view cells.
507*/
508class VspKdViewCellsManager: public ViewCellsManager
509{
510
511public:
512
513        VspKdViewCellsManager(VspKdTree *vspKdTree, int constructionSamples);
514
515        int Construct(const ObjectContainer &objects,
516                                  const VssRayContainer &rays);
517
518
519        int PostProcess(const ObjectContainer &objects,
520                                        const VssRayContainer &rays);
521
522        void Visualize(const ObjectContainer &objects,
523                                   const VssRayContainer &sampleRays);
524
525        int GetType() const;
526       
527        bool ViewCellsConstructed() const;
528
529        //virtual void PrintStatistics(ostream &s) const;
530
531        ViewCell *GenerateViewCell(Mesh *mesh) const;
532
533
534    int CastLineSegment(const Vector3 &origin,
535                                                const Vector3 &termination,
536                                                ViewCellContainer &viewcells);
537
538        ViewCell *GetViewCell(const Vector3 &point) { return NULL; }
539
540        float GetProbability(ViewCell *viewCell);
541        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
542
543        void CreateMesh(ViewCell *vc);
544
545        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
546
547protected:
548
549        void ExportLeaves(const ObjectContainer &objects,
550                                          const VssRayContainer &sampleRays);
551
552        void CollectViewCells();
553
554        void ExportColor(Exporter *exporter, ViewCell *vc) const;
555
556
557
558        /// the BSP tree.
559        VspKdTree *mVspKdTree;
560};
561
562
563
564/**
565        Manages different higher order operations on the view cells.
566*/
567class VspBspViewCellsManager: public ViewCellsManager
568{
569
570public:
571
572        VspBspViewCellsManager(VspBspTree *tree, int constructionSamples);
573        ~VspBspViewCellsManager();
574
575        int Construct(const ObjectContainer &objects,
576                                  const VssRayContainer &rays);
577
578        int PostProcess(const ObjectContainer &objects,
579                                        const VssRayContainer &rays);
580
581        void Visualize(const ObjectContainer &objects,
582                                   const VssRayContainer &sampleRays);
583
584        int GetType() const;
585       
586        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
587
588        bool ViewCellsConstructed() const;
589
590       
591        int CastLineSegment(const Vector3 &origin,
592                                                const Vector3 &termination,
593                                                ViewCellContainer &viewcells);
594
595        float GetProbability(ViewCell *viewCell);
596        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
597       
598        ViewCell *GetViewCell(const Vector3 &point);
599
600        bool GetViewPoint(Vector3 &viewPoint) const;
601
602        bool ViewPointValid(const Vector3 &viewPoint) const;
603
604        void CreateMesh(ViewCell *vc);
605
606        bool LoadViewCells(const string filename, ObjectContainer *objects);
607        bool ExportViewCells(const string filename);
608
609        int CastBeam(Beam &beam);
610
611        void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const;
612
613        //float GetVolume(ViewCell *viewCell) const;
614
615        void Finalize(ViewCell *viewCell, const bool createMesh);
616
617protected:
618
619        /** Merges the view cells.
620        */
621        void MergeViewCells(const VssRayContainer &rays,
622                                                const ObjectContainer &objects);
623       
624        void RefineViewCells(const VssRayContainer &rays);
625
626        void CollectViewCells();
627
628        /** Returns maximal depth difference of view cell
629                leaves in tree.
630        */
631        int GetMaxTreeDiff(ViewCell *vc) const;
632       
633
634        void ExportColor(Exporter *exporter, ViewCell *vc) const;
635
636        /// the view space partition BSP tree.
637        VspBspTree *mVspBspTree;
638
639private:
640
641        /** Exports visualization of the BSP splits.
642        */
643        void ExportSplits(const ObjectContainer &objects,
644                                          const VssRayContainer &rays);
645
646        /** Exports visualization of the BSP PVS.
647        */
648        void ExportBspPvs(const ObjectContainer &objects,
649                                          const VssRayContainer &rays);
650
651};
652
653#endif
Note: See TracBrowser for help on using the repository browser.