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

Revision 556, 16.3 KB checked in by bittner, 18 years ago (diff)

debug version looking for glrenderer bug...

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