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

Revision 570, 16.9 KB checked in by mattausch, 18 years ago (diff)

construction method for view cells

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;
28class Preprocessor;
29
30struct BspRay;
31
32/**
33        Manages different higher order operations on the view cells.
34*/
35class ViewCellsManager
36{
37
38public:
39  struct PvsStatistics {
40        int minPvs;
41        int maxPvs;
42        float avgPvs;
43        int viewcells;
44  };
45 
46  /// view cell container types
47  enum {BSP, KD, VSP_KD, VSP_BSP};
48 
49        /** Constructor taking the maximal number of samples used for construction
50        */
51        ViewCellsManager(const int constructionSamples);
52
53        ViewCellsManager();
54
55        virtual ~ViewCellsManager();
56
57        /** Constructs view cells container taking a preprocessor
58                @returns the output rays (if not null)
59        */
60        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
61
62        /** Constructs view cell container with a given number of samples.
63        */
64        virtual int Construct(const ObjectContainer &objects,
65                                                  const VssRayContainer &rays) = 0;
66
67        /** Computes sample contributions of the rays to the view cells PVS.
68       
69                @param rays bundle of rays used to find intersections with view cells and
70                adding the contribution
71                @param addRays true if rays should be added to the PVSs of the viewcells they
72                intersect */
73  float ComputeSampleContributions(const VssRayContainer &rays,
74                                                                   const bool addContributions = true);
75
76  /** Add sample contributions to the viewcells they intersect */
77  void AddSampleContributions(const VssRayContainer &rays);
78 
79
80        /** Computes sample contribution of a simgle ray to the view cells PVS.
81                @param ray finds intersections with view cells and holds the contribution
82                @param castRay true if ray should be cast to gain the information, false if ray
83                is already holding the information and need not be recast.
84         
85                @returns number of sample contributions
86        */
87        virtual float ComputeSampleContributions(VssRay &ray, const bool addRays = true);
88
89  virtual void AddSampleContributions(VssRay &ray);
90
91        /** Prints out statistics of the view cells.
92        */
93        virtual void PrintStatistics(ostream &s) const;
94
95        /** Post processes view cells givemŽa number of rays.
96        */
97        virtual int PostProcess(const ObjectContainer &objects,
98                                                        const VssRayContainer &rays) = 0;
99
100        /** Show visualization of the view cells.
101        */
102        virtual void Visualize(const ObjectContainer &objects,
103                                                   const VssRayContainer &sampleRays) = 0;
104
105        /** type of the view cell container.
106        */
107        virtual int GetType() const = 0;
108
109        /** Load the input viewcells. The input viewcells should be given as a collection
110                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
111                the viewcell. The method then builds a BSP tree of these view cells.
112               
113                @param filename file to load
114                @return true on success
115    */
116    virtual bool LoadViewCells(const string filename, ObjectContainer *objects);
117
118        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
119                @param the base triangle
120                @param the height of the newly created view cell
121        */
122        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
123
124        /** Merges two view cells.
125                @note the piercing rays of the front and back will be ordered   
126                @returns new view cell based on the merging.
127        */
128        ViewCell *MergeViewCells(ViewCell &front, ViewCell &back) const;
129       
130        /** Generates view cell of type specified by this manager
131        */
132        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
133
134        /** Adds a new view cell to the list of predefined view cells.
135        */
136        void AddViewCell(ViewCell *viewCell);
137
138        /** Derive view cells from objects.
139        */
140        void DeriveViewCells(const ObjectContainer &objects,
141                                                 ViewCellContainer &viewCells,
142                                                 const int maxViewCells) const;
143
144        /** Sets maximal number of samples used for the
145                construction of the view cells.
146        */
147        void SetVisualizationSamples(const int visSamples);
148
149        /** Sets maximal number of samples used for the visualization of the view cells.
150        */
151        void SetConstructionSamples(const int constructionSamples);
152
153        /** Sets maximal number of samples used for the post processing of the view cells.
154        */
155        void SetPostProcessSamples(const int postProcessingSamples);
156
157        /** See set.
158        */
159        int GetVisualizationSamples() const;
160
161        /** See set.
162        */
163        int GetConstructionSamples() const;
164
165        /** See set.
166        */
167        int GetPostProcessSamples() const;
168
169        /** Returns true if view cells wer already constructed.
170        */
171        virtual bool ViewCellsConstructed() const = 0;
172
173        /** cast line segment to get a list of unique viewcells which are intersected
174                by this line segment
175        */
176 
177        virtual int CastLineSegment(const Vector3 &origin,
178                                                          const Vector3 &termination,
179                                                          ViewCellContainer &viewcells
180                                                          ) = 0;
181
182        virtual void GetPvsStatistics(PvsStatistics &stat);
183
184        /** Get a viewcell containing the specified point */
185        virtual ViewCell *GetViewCell(const Vector3 &point) const = 0;
186 
187        virtual void PrintPvsStatistics(ostream &s);
188
189        /** Returns probability that view point lies in one view cell.
190        */
191        virtual float GetProbability(ViewCell *viewCell) = 0;
192
193        /** Returns render cost of a single view cell given the render cost of an object.
194        */
195        virtual float GetRendercost(ViewCell *viewCell, float objRendercost) const = 0;
196
197        /** Returns vector of loaded / generated view cells.
198        */
199        ViewCellContainer &GetViewCells();
200
201        /** Helper function used to split ray sets uniformly
202                into one that is currently used and the other that
203                is saved for later processing.
204                @param sourceRays the input ray set
205                @param maxSize the maximal number of rays that will be used
206                @param usedRays returns the used ray set
207                @param savedRays if not null, returns the saved ray set
208        */
209        void GetRaySets(const VssRayContainer &sourceRays,
210                                        const int maxSize,
211                                        VssRayContainer &usedRays,
212                                        VssRayContainer *savedRays = NULL) const;
213       
214        /** Returns accumulated area of all view cells.
215        */
216        float GetAccVcArea();
217
218        /** Returns area of one view cell.
219        */
220        virtual float GetArea(ViewCell *viewCell) const;
221
222        /** Returns volume of view cell.
223        */
224        virtual float GetVolume(ViewCell *viewCell) const;
225
226        /** Sets the current renderer mainly for view cells statistics.
227        */
228        void SetRenderer(Renderer *renderer);
229
230        /** Computes a (random) view point in the valid view space.
231                @returns true if valid view point was found
232        */
233        virtual bool GetViewPoint(Vector3 &viewPoint) const;
234
235        /** Returns true if this view point is in the valid view space.
236        */
237        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
238
239        /** Sets a view space boundary.
240        */
241        void SetViewSpaceBox(const AxisAlignedBox3 &box);
242
243        AxisAlignedBox3 GetViewSpaceBox() const;
244
245        /** Creates mesh for this view cell.
246        */
247        virtual void CreateMesh(ViewCell *vc) = NULL;
248
249        /** Writes view cells to disc.
250        */
251        virtual bool ExportViewCells(const string filename);
252
253        /** Casts beam to collect view cells.
254        */
255        virtual int CastBeam(Beam &beam);
256
257        /** Checks if view cell is considered as valid.
258        */
259        virtual bool CheckValidity(ViewCell *vc,
260                                                           int minPvsSize,
261                                                           int maxPvsSize) const;
262
263       
264        /** Sets validity of view cell
265        */
266        virtual void SetValidity(ViewCell *vc,
267                                                         int minPvsSize,
268                                                         int maxPvsSize) const;
269
270  /** sets validy of all viewcells */
271        virtual void SetValidity(
272                                                         int minPvsSize,
273                                                         int maxPvsSize) const;
274
275  /** set valid viewcells in the range of pvs. sorts the viewcells
276          according to the pvs and then pickups those in the ranges */
277 
278  void
279  SetValidityPercentage(
280                                                const float minValid,
281                                                const float maxValid
282                                                );
283
284  int
285  CountValidViewcells() const;
286
287        /** Returns maximal allowed pvs size.
288        */
289        int GetMaxPvsSize() const;
290
291        /** Returns maximal allowed pvs size.
292        */
293        int GetMinPvsSize() const;
294
295        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
296                where pvs is still considered valid.
297        */
298        float GetMaxPvsRatio() const;
299
300        /** Exports view cell geometry.
301        */
302        virtual void ExportVcGeometry(Exporter *exporter, ViewCell *vc) const = 0;
303
304        virtual void FinalizeViewCells(const bool createMesh);
305
306protected:
307
308        void ParseEnvironment();
309
310        /** Creates unique view cell ids.
311        */
312        void CreateUniqueViewCellIds();
313
314        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
315        */
316        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
317
318        /** Recollects view cells and resets statistics.
319        */
320        void ResetViewCells();
321       
322        /** Collects the view cells in the view cell container.
323        */
324        virtual void CollectViewCells() = 0;
325       
326        /** Evaluates view cells statistics and stores it in
327                mViewCellsStatistics.
328        */
329        void EvaluateViewCellsStats();
330
331        //-- helper functions for view cell visualization
332
333        /** Exports the view cell partition.
334        */
335        void ExportViewCellsForViz(Exporter *exporter) const;
336
337        /** Sets exporter color.
338        */
339        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
340
341
342        virtual float GetViewSpaceVolume();
343       
344        /** Creates meshes from the view cells.
345        */
346        void CreateViewCellMeshes();
347
348        /**
349                Exports single view cell.
350                NOTE: should be in exporter!!
351        */
352        void ExportViewCell(ViewCell *viewCell, ofstream &stream);
353
354        /// the view cell corresponding to space ouside the valid view space
355        //ViewCell *mOutOfBoundsCell;
356
357        /// Renders the view cells.
358        Renderer *mRenderer;
359
360        /// Loaded view cells
361        ViewCellContainer mViewCells;
362
363        /// maximum number of samples taken for construction of the view cells
364        int mConstructionSamples;
365        int mPostProcessSamples;
366        int mVisualizationSamples;
367
368        float mTotalAreaValid;
369        float mTotalArea;
370
371        int mMaxPvsSize;
372        int mMinPvsSize;
373        float mMaxPvsRatio;
374
375        ViewCellsStatistics mViewCellsStats;
376        /// the scene bounding box
377        AxisAlignedBox3 mViewSpaceBox;
378        /// holds the view cell meshes
379        MeshContainer mMeshContainer;
380        /// if view cells should be exported
381        bool mExportViewCells;
382
383        bool mOnlyValidViewCells;
384
385        //-- visualization options
386       
387        /// color code for view cells
388        int mColorCode;
389        bool mExportGeometry;
390        bool mExportRays;
391
392        bool mViewCellsFinished;
393};
394
395
396
397/**
398        Manages different higher order operations on the view cells.
399*/
400class BspViewCellsManager: public ViewCellsManager
401{
402
403public:
404        /** Constructor taking the bsp tree and the number of samples
405                used to construct the bsp tree.
406        */
407        BspViewCellsManager(BspTree *tree, int constructionSamples);
408
409        ~BspViewCellsManager();
410
411        int Construct(const ObjectContainer &objects,
412                                  const VssRayContainer &rays);
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.