source: GTP/trunk/Lib/Vis/Preprocessing/src/GvsPreprocessor.h @ 2053

Revision 2053, 5.9 KB checked in by mattausch, 17 years ago (diff)
  • Property svn:executable set to *
Line 
1#ifndef _GvsPreprocessor_H__
2#define _GvsPreprocessor_H__
3
4#include <fstream>
5#include <stack>
6using namespace std;
7
8#include "Preprocessor.h"
9
10namespace GtpVisibilityPreprocessor {
11
12class Exporter;
13class VssRay;
14
15/** View space partition statistics.
16*/
17class GvsStatistics: public StatisticsBase
18{
19public:
20       
21        /// Constructor
22        GvsStatistics()
23        {
24                Reset();
25        }
26       
27        void Reset()
28        {
29                mPass = 0;
30                mTotalSamples = 0;
31                mPassContribution = 0;
32                mTotalContribution = 0;
33                mReverseSamples = 0;
34                mBorderSamples = 0;
35                mGvsPass = 0;
36
37                mTotalPvs = 0;
38                mViewCells = 0;
39                mPerViewCellSamples = 0;
40                mPerViewCellPvs = 0;
41                mTrianglePvs = 0;
42                mViewCellId = 0;
43                mTotalTime = 0;
44                mTimePerViewCell = 0;
45                mTotalTrianglePvs = 0;
46        }
47
48
49public:
50
51        int mPass;
52        int mTotalSamples;
53        int mPassContribution;
54        int mTotalContribution;
55        int mReverseSamples;
56        int mBorderSamples;
57        int mGvsPass;
58
59        int mTotalPvs;
60        int mViewCells;
61        int mPerViewCellSamples;
62        int mPerViewCellPvs;
63        int mTrianglePvs;
64        int mTotalTrianglePvs;
65        int mViewCellId;
66
67        float mTimePerViewCell;
68        float mTotalTime;
69
70        float RaysPerSec() const { if (!mTotalTime) return 0; return (float)mTotalSamples / mTotalTime * 1e-6f; }
71
72        void Print(ostream &app) const;
73
74        friend ostream &operator<<(ostream &s, const GvsStatistics &stat)
75        {
76                stat.Print(s);
77                return s;
78        }
79};
80
81
82/** Sampling based visibility preprocessing. The implementation is
83        based on heuristical sampling of view space.
84*/
85class GvsPreprocessor : public Preprocessor {
86
87public:
88
89        GvsPreprocessor();
90        ~GvsPreprocessor();
91
92        virtual bool ComputeVisibility();
93       
94
95protected:
96#if 0
97        struct PendingRay
98        {
99                PendingRay(VssRay *ray, const bool d)
100                        : mRay(ray), mFoundDiscontinuity(d)
101                {}
102
103                VssRay *mRay;
104                bool mFoundDiscontinuity;
105        };
106
107        typedef stack<PendingRay> PendingQueue;
108#endif
109        typedef stack<VssRay *> RayQueue;
110
111        /** Runs the adaptive sampling until the ray queue is empty.
112                The method starts with a number of random rays given
113                by the queue and continues as long it finds new visible geometry
114                (i.e., the queue is     not empty).
115
116                @returns the number of samples cast.
117        */
118        int ProcessQueue();
119
120        /** Generates the rays starting the adaptive visibility sampling process.
121        */
122        int CastInitialSamples(const int numSamples, const int sampleType);
123
124        /** Uses the information gained from the ray for doing adaptive border sampling.
125                This function tries to find the border of the triangle found visible by the
126                current ray. New rays are generated which sample this border.
127               
128                We use the following strategies:
129
130                a) if new triangle was found: adaptive border sampling
131                b) if triangle was found reverse sampling
132        */
133        bool HandleRay(VssRay *ray);
134
135        /** The adaptive border sampling step. It aims to find neighbouring
136                triangles of the one hit by the current ray.
137        */     
138        int AdaptiveBorderSampling(const VssRay &currentRay);
139       
140        /** The reverse sampling step. It is started once the cast
141                ray finds a discontinuity, i.e., a closer triangle.
142                Then the process tries to find a ray from the old
143                triangle passing through a gap.
144        */
145        VssRay *ReverseSampling(const VssRay &currentRay,
146                                                        const Triangle3 &hitTriangle,
147                                                        const VssRay &oldRay);
148
149        /** Returns true if we sampled a closer triangle than with the previous ray.
150                Does reverse sampling if gap found.
151        */
152        int CheckDiscontinuity(const VssRay &currentRay,
153                                                   const Triangle3 &hitTriangle,
154                                                   const VssRay &oldRay);
155
156        /** Adds new samples to the ray queue and classifies them
157                with respect to the previous ray.
158        */
159        void EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples);
160
161        /** Hepler function for adaptive border sampling. It finds
162                new sample points around a triangle in a eps environment
163        */
164        void EnlargeTriangle(VertexContainer &vertices,
165                                                 const Triangle3 &hitTriangle,
166                                                 const VssRay &ray) const;
167
168        int SubdivideEdge(const Triangle3 &hitTriangle,
169                                          const Vector3 &p1,
170                                          const Vector3 &p2,
171                                          const VssRay &ray1,
172                      const VssRay &ray2,
173                                          const VssRay &oldRay);
174
175        void Visualize();
176
177        void CreateDisplacedVertices(VertexContainer &vertices,
178                                                                 const Triangle3 &hitTriangle,
179                                                                 const VssRay &ray,
180                                                                 const int index) const;
181
182        Vector3 CalcPredictedHitPoint(const VssRay &newRay,
183                                                                  const Triangle3 &hitTriangle,
184                                                                  const VssRay &oldRay) const;
185
186        bool GetPassingPoint(const VssRay &currentRay,
187                                                 const Triangle3 &occluder,
188                                                 const VssRay &oldRay,
189                                                 Vector3 &newPoint) const;
190
191        bool NextViewCell();
192
193        void GlobalComputation();
194
195        void PerViewCellComputation();
196
197        void VisualizeViewCells();
198
199        void VisualizeViewCell(ViewCell *vc);
200        void VisualizeViewCell(const ObjectContainer &objects);
201
202        /** Exchanges view cell triangle pvs with bvh leaf pvs.
203        */
204        void UpdatePvs(ViewCell *currentViewCell);
205
206        void ProcessViewCell();
207        void ClearRayQueue();
208
209        void CompileViewCellsList();
210
211        void GetObjectPvs(ObjectContainer &trianglePvs) const;
212
213        bool HasContribution(VssRay &ray);
214
215        void IntersectWithViewCell();
216
217        void DeterminePvsObjects(VssRayContainer &rays);
218
219        //virtual void ComputeRenderError();
220
221        void StorePvs(const ObjectContainer &objectPvs);
222
223
224        //////////////////////
225
226        //int mSamplesPerPass;
227        //int mTotalSamples;
228        int mInitialSamples;
229
230        RayQueue mRayQueue;
231        int mSamplingType;
232
233        //AxisAlignedBox3 mViewSpaceBox;
234        float mEps;
235        float mThreshold;
236        VssRayContainer mVssRays;
237       
238
239
240        ///////////
241        // stats
242
243        ofstream mGvsStatsStream;
244        GvsStatistics mGvsStats;
245
246        bool mPerViewCell;
247        bool mOnlyRandomSampling;
248
249        ViewCell *mCurrentViewCell;
250
251        int mProcessedViewCells;
252
253        int mMinContribution;
254
255        ViewCellContainer mViewCells;
256
257        int mMaxViewCells;
258
259        ObjectContainer mTrianglePvs;
260
261        // HACK
262        int mGvsSamplesPerPass;
263
264        bool mComputeRenderError;
265
266        bool mEvaluatePixelError;
267};
268
269};
270
271#endif
Note: See TracBrowser for help on using the repository browser.