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

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