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

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