source: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsManager.h @ 677

Revision 677, 20.8 KB checked in by bittner, 18 years ago (diff)

changes for visualization of distant view space sampling

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#include "ViewCellBsp.h"
9
10class ViewCell;
11class Intersectable;
12class RenderSimulator;
13class Renderer;
14class Mesh;
15struct Triangle3;
16class SimulationStatistics;
17class BspTree;
18class KdTree;
19class VspKdTree;
20class VspBspTree;
21class KdNode;
22class KdLeaf;
23class VspKdTree;
24class AxisAlignedBox3;
25class BspLeaf;
26class ViewCellsStatistics;
27class Exporter;
28class Beam;
29class Preprocessor;
30class ViewCellsTree;
31class MergeCandidate;
32
33struct BspRay;
34
35
36/** Probably Visible Set */
37class PrVs {
38  /// viewcells
39  ViewCellContainer mVviewCells;
40  /// aggregatred pvs
41  ObjectPvs mPvs;
42
43  // input parameter is the render budget for the PrVS
44  float mRenderBudget;
45
46  /// some characteristic values could be here
47 
48};
49
50/**
51        Manages different higher order operations on the view cells.
52*/
53class ViewCellsManager
54{
55
56public:
57  struct PvsStatistics {
58        int minPvs;
59        int maxPvs;
60        float avgPvs;
61        int viewcells;
62  };
63 
64  /// view cell container types
65  enum {BSP, KD, VSP_KD, VSP_BSP};
66 
67        /** Constructor taking the initial and construction samples.
68                @param initialSamples the maximal number of samples used for creating the hierarchy
69                of view cells
70                @param constructionSamples the maximal number of samples used for construction
71        */
72        ViewCellsManager(const int initialSamples, const int constructionSamples);
73
74        ViewCellsManager();
75
76        virtual ~ViewCellsManager();
77
78        /** Constructs view cells container taking a preprocessor
79                @returns the output rays (if not null)
80        */
81        int Construct(Preprocessor *preprocessor, VssRayContainer *outRays = NULL);
82
83        /** Constructs view cell container with a given number of samples.
84        */
85        virtual int ConstructSubdivision(const ObjectContainer &objects,
86                                                  const VssRayContainer &rays) = 0;
87
88        /** Computes sample contributions of the rays to the view cells PVS.
89       
90                @param rays bundle of rays used to find intersections with view cells and
91                adding the contribution
92                @param addRays true if rays should be added to the PVSs of the viewcells they
93                intersect
94                @param storeViewCells true if view cells should be stored in the ray
95        */
96  float ComputeSampleContributions(const VssRayContainer &rays,
97                                                                   const bool addContributions,
98                                                                   const bool storeViewCells);
99
100  /** Add sample contributions to the viewcells they intersect */
101  void AddSampleContributions(const VssRayContainer &rays);
102 
103
104        /** Computes sample contribution of a simgle ray to the view cells PVS.
105                @param ray finds intersections with view cells and holds the contribution
106                @param castRay true if ray should be cast to gain the information, false if ray
107                is already holding the information and need not be recast.
108         
109                @returns number of sample contributions
110        */
111        virtual float ComputeSampleContributions(VssRay &ray,
112                                                                                         const bool addRays,
113                                                                                         const bool storeViewCells);
114
115        virtual void AddSampleContributions(VssRay &ray);
116
117        /** Prints out statistics of the view cells.
118        */
119        virtual void PrintStatistics(ostream &s) const;
120
121        /** Post processes view cells givemŽa number of rays.
122        */
123        virtual int PostProcess(const ObjectContainer &objects,
124                                                        const VssRayContainer &rays) = 0;
125
126        /** Show visualization of the view cells.
127        */
128        virtual void Visualize(const ObjectContainer &objects,
129                                                   const VssRayContainer &sampleRays) = 0;
130
131        /** type of the view cell container.
132        */
133        virtual int GetType() const = 0;
134
135        /** Load the input viewcells. The input viewcells should be given as a collection
136                of meshes. Each mesh is assume to form a bounded polyhedron defining the interior of
137                the viewcell. The method then builds a BSP tree of these view cells.
138               
139                @param filename file to load
140                @return true on success
141    */
142    virtual bool LoadViewCellsGeometry(const string filename);
143
144       
145        /** Constructs view cell from base triangle. The ViewCell is extruded along the normal vector.
146                @param the base triangle
147                @param the height of the newly created view cell
148        */
149        ViewCell *ExtrudeViewCell(const Triangle3 &baseTri, const float height) const;
150
151        /** Merges two view cells.
152                @note the piercing rays of the front and back will be ordered   
153                @returns new view cell based on the merging.
154        */
155        ViewCellInterior *MergeViewCells(ViewCell *front, ViewCell *back) const;
156
157        /** Merges a container of view cells.
158                @returns new view cell based on the merging.
159        */
160        ViewCellInterior *MergeViewCells(ViewCellContainer &children) const;
161       
162        /** Generates view cell of type specified by this manager
163        */
164        virtual ViewCell *GenerateViewCell(Mesh *mesh = NULL) const = 0;
165
166        /** Adds a new view cell to the list of predefined view cells.
167        */
168        void AddViewCell(ViewCell *viewCell);
169
170        /** Derive view cells from objects.
171        */
172        void DeriveViewCells(const ObjectContainer &objects,
173                                                 ViewCellContainer &viewCells,
174                                                 const int maxViewCells) const;
175
176        /** Sets maximal number of samples used for the
177                construction of the view cells.
178        */
179        void SetVisualizationSamples(const int visSamples);
180
181        /** Sets maximal number of samples used for the construction of the view cells.
182        */
183        void SetConstructionSamples(const int constructionSamples);
184
185        /** Sets maximal number of samples used for the visualization of the view cells.
186        */
187        void SetInitialSamples(const int initialSamples);
188
189        /** Sets maximal number of samples used for the post processing of the view cells.
190        */
191        void SetPostProcessSamples(const int postProcessingSamples);
192
193        /** See set.
194        */
195        int GetVisualizationSamples() const;
196
197        /** See set.
198        */
199        int GetConstructionSamples() const;
200
201        /** See set.
202        */
203        int GetPostProcessSamples() const;
204
205        /** Returns true if view cells wer already constructed.
206        */
207        virtual bool ViewCellsConstructed() const = 0;
208
209        /** cast line segment to get a list of unique viewcells which are intersected
210                by this line segment
211        */
212 
213        virtual int CastLineSegment(const Vector3 &origin,
214                                                          const Vector3 &termination,
215                                                          ViewCellContainer &viewcells
216                                                          ) = 0;
217
218        virtual void GetPvsStatistics(PvsStatistics &stat);
219
220  virtual void GetPrVS(const Vector3 &viewPoint,
221                                           PrVs &prvs
222                                           ) {}
223 
224  /** Get a viewcell containing the specified point */
225        virtual ViewCell *GetViewCell(const Vector3 &point) const = 0;
226 
227        virtual void PrintPvsStatistics(ostream &s);
228
229        /** Returns probability that view point lies in one view cell.
230        */
231        virtual float GetProbability(ViewCell *viewCell) = 0;
232
233        /** Returns render cost of a single view cell given the render cost of an object.
234        */
235        float GetRendercost(ViewCell *viewCell, float objRendercost) const;
236
237        /** Returns container of loaded / generated view cells.
238        */
239        ViewCellContainer &GetViewCells();
240
241        /** Helper function used to split ray sets uniformly
242                into one that is currently used and the other that
243                is saved for later processing.
244                @param sourceRays the input ray set
245                @param maxSize the maximal number of rays that will be used
246                @param usedRays returns the used ray set
247                @param savedRays if not null, returns the saved ray set
248        */
249        void GetRaySets(const VssRayContainer &sourceRays,
250                                        const int maxSize,
251                                        VssRayContainer &usedRays,
252                                        VssRayContainer *savedRays = NULL) const;
253       
254        /** Returns accumulated area of all view cells.
255        */
256        float GetAccVcArea();
257
258        /** Returns area of one view cell.
259        */
260        virtual float GetArea(ViewCell *viewCell) const;
261
262        /** Returns volume of view cell.
263        */
264        virtual float GetVolume(ViewCell *viewCell) const;
265
266        /** Sets the current renderer mainly for view cells statistics.
267        */
268        void SetRenderer(Renderer *renderer);
269
270        /** Computes a (random) view point in the valid view space.
271                @returns true if valid view point was found
272        */
273        virtual bool GetViewPoint(Vector3 &viewPoint) const;
274
275        /** Returns true if this view point is in the valid view space.
276        */
277        virtual bool ViewPointValid(const Vector3 &viewPoint) const;
278
279        /** Sets a view space boundary.
280        */
281        void SetViewSpaceBox(const AxisAlignedBox3 &box);
282
283        AxisAlignedBox3 GetViewSpaceBox() const;
284
285        /** Creates mesh for this view cell.
286        */
287        virtual void CreateMesh(ViewCell *vc) = NULL;
288
289        /** Writes view cells to disc.
290        */
291        virtual bool ExportViewCells(const string filename);
292
293        /** Casts beam to collect view cells.
294        */
295        virtual int CastBeam(Beam &beam);
296
297        /** Checks if view cell is considered as valid.
298        */
299        virtual bool CheckValidity(ViewCell *vc,
300                                                           int minPvsSize,
301                                                           int maxPvsSize) const;
302
303       
304        /** Sets validity of view cell
305        */
306        virtual void SetValidity(ViewCell *vc,
307                                                         int minPvsSize,
308                                                         int maxPvsSize) const;
309
310  /** sets validy of all viewcells */
311        virtual void SetValidity(
312                                                         int minPvsSize,
313                                                         int maxPvsSize) const;
314
315
316        /** set valid viewcells in the range of pvs. sorts the viewcells
317          according to the pvs and then pickups those in the ranges */
318
319   
320        void SetValidityPercentage(const float minValid,        const float maxValid);
321
322    int CountValidViewcells() const;
323
324        /** Returns maximal allowed pvs size.
325        */
326        int GetMaxPvsSize() const;
327
328        /** Returns maximal allowed pvs size.
329        */
330        int GetMinPvsSize() const;
331
332        /** Returns maximal ratio. i.e., currentPVs / maxPvs,
333                where pvs is still considered valid.
334        */
335        float GetMaxPvsRatio() const;
336
337        /** Exports view cell geometry.
338        */
339        virtual void ExportViewCellGeometry(Exporter *exporter,
340                                                                                ViewCell *vc,
341                                                                                const Plane3 *clipPlane = NULL) const = 0;
342
343        virtual void FinalizeViewCells(const bool createMesh);
344
345
346        /** Loads view cells from file. The view cells manager is created with
347                respect to the loaded view cells.
348
349                @returns the view cells manager if loading was successful, false otherwise
350        */
351        static ViewCellsManager *LoadViewCells(const string filename,
352                                                                                   ObjectContainer *objects);
353
354        /** Evaluates statistics values on view cells.
355        */
356        void EvaluateRenderStatistics(float &totalRenderCost,
357                                                                  float &expectedRenderCost,
358                                                                  float &deviation,
359                                                                  float &variance,
360                                                                  int &totalPvs,
361                                                                  float &avgRenderCost);
362
363
364        /** Returns hierarchy of the view cells.
365        */
366        ViewCellsTree *GetViewCellsTree();
367
368        virtual void CollectMergeCandidates(const VssRayContainer &rays,
369                                                                                vector<MergeCandidate> &candidates) = 0;
370
371        void CollectViewCells(const int n);
372
373        /** Returns true if this (logical) view cell is equal to a spatial node.
374        */
375        virtual bool EqualToSpatialNode(ViewCell *viewCell) const { return false;}
376
377        /** Sets current view cells set to active, i.e., the sampling is done in this view cell set.
378        */
379        void SetViewCellsActive();
380
381        /** Evaluates worth of current view cell hierarchy.
382        */
383        void EvalViewCellPartition(Preprocessor *preprocessor);
384
385protected:
386
387
388        /**
389                if the view cells tree was already constructed or not.
390        */
391        bool ViewCellsTreeConstructed() const;
392
393        int CastPassSamples(const int samplesPerPass,
394                                                const int sampleType,
395                                                VssRayContainer &vssRays) const;
396
397        void ParseEnvironment();
398
399        /** Creates unique view cell ids.
400        */
401        void CreateUniqueViewCellIds();
402
403        /** Finalizes, i.e., creates mesh, volume, area etc. for the view cell.
404        */
405        virtual void Finalize(ViewCell *viewCell, const bool createMesh);
406
407        /** Recollects view cells and resets statistics.
408        */
409        void ResetViewCells();
410       
411        /** Collects the view cells in the view cell container.
412        */
413        virtual void CollectViewCells() = 0;
414       
415        /** Evaluates view cells statistics and stores it in
416                mViewCellsStatistics.
417        */
418        void EvaluateViewCellsStats();
419
420        //-- helper functions for view cell visualization
421
422        /** Exports the view cell partition.
423        */
424        void ExportViewCellsForViz(Exporter *exporter) const;
425
426        /** Sets exporter color.
427        */
428        virtual void ExportColor(Exporter *exporter, ViewCell *vc) const = 0;
429
430
431        virtual float GetViewSpaceVolume();
432       
433        /** Creates meshes from the view cells.
434        */
435        void CreateViewCellMeshes();
436
437        /** Takes different measures to prepares the view cells after loading them from disc.
438        */
439        virtual void PrepareLoadedViewCells() {};
440
441        /** Creates clip plane for visualization.
442        */
443        void CreateClipPlane();
444
445        Plane3 mClipPlane;
446        bool mUseClipPlaneForViz;
447
448
449        /// Renders the view cells.
450        Renderer *mRenderer;
451
452        /// Loaded view cells
453        ViewCellContainer mViewCells;
454
455        ViewCellsTree *mViewCellsTree;
456
457        /// maximum number of samples taken for construction of the view cells
458        int mConstructionSamples;
459        int mSamplesPerPass;
460        int mInitialSamples;
461        int mPostProcessSamples;
462        int mVisualizationSamples;
463
464        float mTotalAreaValid;
465        float mTotalArea;
466
467        int mMaxPvsSize;
468        int mMinPvsSize;
469        float mMaxPvsRatio;
470
471        int mSamplingType;
472        int mNumActiveViewCells;
473        bool mCompressViewCells;
474
475        ViewCellsStatistics mCurrentViewCellsStats;
476        /// the scene bounding box
477        AxisAlignedBox3 mViewSpaceBox;
478        /// holds the view cell meshes
479        MeshContainer mMeshContainer;
480        /// if view cells should be exported
481        bool mExportViewCells;
482
483        //bool mMarchTree);
484        bool mOnlyValidViewCells;
485
486        /// if rays should be used to collect merge candidates
487        bool mUseRaysForMerge;
488       
489        bool mMergeViewCells;
490       
491        //-- visualization options
492       
493        /// color code for view cells
494        int mColorCode;
495        bool mExportGeometry;
496        bool mExportRays;
497
498        bool mViewCellsFinished;
499
500        bool mEvaluateViewCells;
501
502        bool mShowVisualization;       
503};
504
505
506
507/**
508        Manages different higher order operations on the view cells.
509*/
510class BspViewCellsManager: public ViewCellsManager
511{
512
513public:
514        /** Constructor taking the bsp tree and the number of samples
515                used to construct the bsp tree.
516        */
517        BspViewCellsManager(BspTree *tree);
518
519        ~BspViewCellsManager();
520
521        int ConstructSubdivision(const ObjectContainer &objects,
522                                  const VssRayContainer &rays);
523
524        int PostProcess(const ObjectContainer &objects,
525                                        const VssRayContainer &rays);
526
527        void Visualize(const ObjectContainer &objects,
528                                   const VssRayContainer &sampleRays);
529
530        int GetType() const;
531       
532        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
533
534        bool ViewCellsConstructed() const;
535
536        //void PrintStatistics(ostream &s) const;
537
538        int CastLineSegment(const Vector3 &origin,
539                                                const Vector3 &termination,
540                                                ViewCellContainer &viewcells);
541       
542        float GetProbability(ViewCell *viewCell);
543       
544
545        /** Get a viewcell containing the specified point */
546        ViewCell *GetViewCell(const Vector3 &point) const;
547
548        void CreateMesh(ViewCell *vc);
549
550        void ExportViewCellGeometry(Exporter *exporter,
551                                                                ViewCell *vc,
552                                                                const Plane3 *clipPlane = NULL) const;
553       
554        void CollectMergeCandidates(const VssRayContainer &rays,
555                                                                vector<MergeCandidate> &candidates);
556
557        void Finalize(ViewCell *viewCell, const bool createMesh);
558
559        bool ExportViewCells(const string filename);
560
561        ViewCell *ConstructSpatialMergeTree(BspNode *root);
562
563
564protected:
565
566
567        /** HACK
568        */
569        void AddCurrentViewCellsToHierarchy();
570
571        void CollectViewCells();
572
573        void ExportColor(Exporter *exporter, ViewCell *vc) const;
574       
575
576
577        /// the BSP tree.
578        BspTree *mBspTree;
579       
580        vector<BspRay *> mBspRays;
581
582private:
583
584        /** Exports visualization of the BSP splits.
585        */
586        void ExportSplits(const ObjectContainer &objects);
587
588        /** Exports visualization of the BSP PVS.
589        */
590        void ExportBspPvs(const ObjectContainer &objects);
591
592};
593
594/**
595        Manages different higher order operations on the KD type view cells.
596*/
597class KdViewCellsManager: public ViewCellsManager
598{
599
600public:
601
602        KdViewCellsManager(KdTree *tree);
603
604        int ConstructSubdivision(const ObjectContainer &objects,
605                                  const VssRayContainer &rays);
606
607        int CastLineSegment(const Vector3 &origin,
608                                                const Vector3 &termination,
609                                                ViewCellContainer &viewcells);
610
611        int PostProcess(const ObjectContainer &objects,
612                                        const VssRayContainer &rays);
613
614        void Visualize(const ObjectContainer &objects,
615                                   const VssRayContainer &sampleRays);
616
617        int GetType() const;
618
619        bool ViewCellsConstructed() const;
620
621        ViewCell *GenerateViewCell(Mesh *mesh) const;
622
623        /** Prints out statistics of this approach.
624        */
625        //  virtual void PrintStatistics(ostream &s) const;
626        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
627
628        float GetProbability(ViewCell *viewCell);
629       
630
631        void CreateMesh(ViewCell *vc);
632
633        void ExportViewCellGeometry(Exporter *exporter,
634                                                                ViewCell *vc,
635                                                                const Plane3 *clipPlane = NULL) const;
636
637        void CollectMergeCandidates(const VssRayContainer &rays,
638                                                                vector<MergeCandidate> &candidates);
639
640protected:
641
642        /** Collects view cells from a hierarchy.
643        */
644        void CollectViewCells();
645
646        KdNode *GetNodeForPvs(KdLeaf *leaf);
647
648        void ExportColor(Exporter *exporter, ViewCell *vc) const;
649
650
651        /// the BSP tree.
652        KdTree *mKdTree;
653
654        /// depth of the KD tree nodes with represent the view cells
655        int mKdPvsDepth;
656};
657
658/**
659        Manages different higher order operations on the view cells
660        for vsp kd tree view cells.
661*/
662class VspKdViewCellsManager: public ViewCellsManager
663{
664
665public:
666
667        VspKdViewCellsManager(VspKdTree *vspKdTree);
668
669        int ConstructSubdivision(const ObjectContainer &objects,
670                                  const VssRayContainer &rays);
671
672
673        int PostProcess(const ObjectContainer &objects,
674                                        const VssRayContainer &rays);
675
676        void Visualize(const ObjectContainer &objects,
677                                   const VssRayContainer &sampleRays);
678
679        int GetType() const;
680       
681        bool ViewCellsConstructed() const;
682
683        //virtual void PrintStatistics(ostream &s) const;
684
685        ViewCell *GenerateViewCell(Mesh *mesh) const;
686
687
688    int CastLineSegment(const Vector3 &origin,
689                                                const Vector3 &termination,
690                                                ViewCellContainer &viewcells);
691
692        ViewCell *GetViewCell(const Vector3 &point) const { return NULL; }
693
694        float GetProbability(ViewCell *viewCell);
695
696
697        void CreateMesh(ViewCell *vc);
698
699        void ExportViewCellGeometry(Exporter *exporter,
700                                                                ViewCell *vc,
701                                                                const Plane3 *clipPlane = NULL) const;
702
703        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
704
705protected:
706
707        void ExportLeaves(const ObjectContainer &objects,
708                                          const VssRayContainer &sampleRays);
709
710        void CollectViewCells();
711
712        void ExportColor(Exporter *exporter, ViewCell *vc) const;
713
714
715
716        /// the BSP tree.
717        VspKdTree *mVspKdTree;
718};
719
720
721
722/**
723        Manages different higher order operations on the view cells.
724*/
725class VspBspViewCellsManager: public ViewCellsManager
726{
727
728public:
729
730        VspBspViewCellsManager(VspBspTree *tree);
731        ~VspBspViewCellsManager();
732
733        int ConstructSubdivision(const ObjectContainer &objects,
734                                  const VssRayContainer &rays);
735
736        int PostProcess(const ObjectContainer &objects,
737                                        const VssRayContainer &rays);
738
739        void Visualize(const ObjectContainer &objects,
740                                   const VssRayContainer &sampleRays);
741
742        int GetType() const;
743       
744        ViewCell *GenerateViewCell(Mesh *mesh = NULL) const;
745
746        bool ViewCellsConstructed() const;
747
748       
749        int CastLineSegment(const Vector3 &origin,
750                                                const Vector3 &termination,
751                                                ViewCellContainer &viewcells);
752
753        float GetProbability(ViewCell *viewCell);
754       
755        ViewCell *GetViewCell(const Vector3 &point) const;
756
757        bool GetViewPoint(Vector3 &viewPoint) const;
758
759        bool ViewPointValid(const Vector3 &viewPoint) const;
760
761        void CreateMesh(ViewCell *vc);
762
763        //bool LoadViewCellsGeometry(const string filename, ObjectContainer *objects);
764        bool ExportViewCells(const string filename);
765
766        int CastBeam(Beam &beam);
767
768        void ExportViewCellGeometry(Exporter *exporter,
769                                                                ViewCell *vc,
770                                                                const Plane3 *clipPlane = NULL) const;
771
772        //float GetVolume(ViewCell *viewCell) const;
773
774        void Finalize(ViewCell *viewCell, const bool createMesh);
775
776        void CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
777
778        bool EqualToSpatialNode(ViewCell *viewCell) const;
779
780protected:
781
782       
783        /** Returns node of the spatial hierarchy corresponding to the view cell
784                if such a node exists.
785        */
786        BspNode *GetSpatialNode(ViewCell *viewCell) const;
787
788        /** HACK
789        */
790        void AddCurrentViewCellsToHierarchy();
791
792        /** Merges the view cells.
793        */
794        void MergeViewCells(const VssRayContainer &rays,
795                                                const ObjectContainer &objects);
796       
797        void RefineViewCells(const VssRayContainer &rays, const ObjectContainer &objects);
798
799        void CollectViewCells();
800
801        /** Returns maximal depth difference of view cell
802                leaves in tree.
803        */
804        int GetMaxTreeDiff(ViewCell *vc) const;
805       
806
807        void ExportColor(Exporter *exporter, ViewCell *vc) const;
808
809        void PrepareLoadedViewCells();
810
811        ViewCell *ConstructSpatialMergeTree(BspNode *root);
812
813        /// the view space partition BSP tree.
814        VspBspTree *mVspBspTree;
815
816
817private:
818
819        /** Exports visualization of the BSP splits.
820        */
821        void ExportSplits(const ObjectContainer &objects,
822                                          const VssRayContainer &rays);
823
824        /** Exports visualization of the BSP PVS.
825        */
826        void ExportBspPvs(const ObjectContainer &objects,
827                                          const VssRayContainer &rays);
828
829};
830
831
832class ViewCellsManagerFactory
833{
834
835public:
836
837        ViewCellsManager *Create(const string mName);
838
839};
840
841#endif
Note: See TracBrowser for help on using the repository browser.