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

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