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

Revision 574, 17.5 KB checked in by mattausch, 18 years ago (diff)

finished function for view cell construction
removed bsp rays from vspbspmanager

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