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

Revision 569, 16.8 KB checked in by bittner, 18 years ago (diff)

viewcell validy setting functions extended

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