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

Revision 2743, 9.3 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#include "Preprocessor.h"
7#include "FlexibleHeap.h"
8
9
10
11namespace GtpVisibilityPreprocessor {
12
13class Exporter;
14class VssRay;
15class TriangleIntersectable;
16class KdIntersectable;
17class ViewCellBasedDistribution;
18class ViewCellBorderBasedDistribution;
19
20
21/** View space partition statistics.
22*/
23class GvsStatistics: public StatisticsBase
24{
25public:
26       
27        /// Constructor
28        GvsStatistics()
29        {
30                Reset();
31        }
32       
33        void Reset()
34        {
35                mPass = 0;
36                mTotalSamples = 0;
37                mPassContribution = 0;
38                mTotalContribution = 0;
39                mReverseSamples = 0;
40                mBorderSamples = 0;
41                mGvsRuns = 0;
42
43                mRandomSamples = 0;
44                mGvsSamples = 0;
45                mTotalPvs = 0;
46                mViewCells = 0;
47                mPerViewCellSamples = 0;
48                mPerViewCellPvs = 0;
49                mTrianglePvs = 0;
50                mViewCellId = 0;
51                mTotalTime = 0;
52                mTimePerViewCell = 0;
53                mTotalTrianglePvs = 0;
54                mPvsCost = 0;
55        }
56
57
58public:
59
60        int mRandomSamples;
61        int mGvsSamples;
62        int mPass;
63        int mTotalSamples;
64        int mPassContribution;
65        int mTotalContribution;
66        int mReverseSamples;
67        int mBorderSamples;
68        int mGvsRuns;
69
70        int mTotalPvs;
71        int mViewCells;
72        int mPerViewCellSamples;
73        int mPerViewCellPvs;
74        int mTrianglePvs;
75        int mTotalTrianglePvs;
76        int mViewCellId;
77
78        float mTimePerViewCell;
79        float mTotalTime;
80       
81        float mPvsCost;
82
83        ObjectContainer mKdPvs;
84
85        float RaysPerSec() const
86        {
87                if (!mTotalTime) return 0;
88                return (float)(mTotalSamples / mTotalTime) * 1e-6f;
89        }
90
91        void Print(ostream &app) const;
92
93        friend ostream &operator<<(ostream &s, const GvsStatistics &stat)
94        {
95                stat.Print(s);
96                return s;
97        }
98};
99
100
101struct ProbablyVisibleItem
102{
103        KdNode *mNode;
104        ObjectContainer mTriangles;
105};
106
107
108typedef FlexibleHeap<ProbablyVisibleItem *> ProbablyVisibleQueue;
109
110
111
112/** Sampling based visibility preprocessing. The implementation is
113        based on heuristical sampling of view space.
114*/
115class GvsPreprocessor : public Preprocessor
116{
117
118public:
119
120        GvsPreprocessor();
121        ~GvsPreprocessor();
122
123        virtual bool ComputeVisibility();
124       
125
126protected:
127
128        typedef stack<VssRay *> RayQueue;
129
130        /** Runs the adaptive sampling until the ray queue is empty.
131                The method starts with a number of random rays given
132                by the queue and continues as long it finds new visible geometry
133                (i.e., the queue is     not empty).
134                @returns the number of samples cast.
135        */
136        int ProcessQueue();
137        /** Generates the rays starting the adaptive visibility sampling process.
138        */
139        int CastInitialSamples(int numSamples);
140        /** Uses the information gained from the ray for doing adaptive border sampling.
141                This function tries to find the border of the triangle found visible by the
142                current ray. New rays are generated which sample this border.
143               
144                We use the following strategies:
145
146                a) if new triangle was found: adaptive border sampling
147                b) if triangle was found reverse sampling
148        */
149        inline bool HandleRay(VssRay *ray);
150        /** The adaptive border sampling step. It aims to find neighbouring
151                triangles of the one hit by the current ray.
152        */     
153        int AdaptiveBorderSampling(const VssRay &currentRay);
154
155        int AdaptiveBorderSamplingOpt(const VssRay &currentRay);
156        /** The reverse sampling step. It is started once the cast
157                ray finds a discontinuity, i.e., a closer triangle.
158                Then the process tries to find a ray from the old
159                triangle passing through a gap.
160        */
161        bool ComputeReverseRay(const VssRay &currentRay,
162                                 const Triangle3 &hitTriangle,
163                                                         const VssRay &oldRay,
164                                                         SimpleRay &reverseRay);
165        /** Returns true if we sampled a closer triangle than with the previous ray.
166                Does reverse sampling if gap found.
167        */
168        int CheckDiscontinuity(const VssRay &currentRay,
169                                                   const Triangle3 &hitTriangle,
170                                                   const VssRay &oldRay);
171        /** Returns true if we sampled a closer triangle than with the previous ray.
172                Does reverse sampling if gap found.
173        */
174        bool CheckDiscontinuity2(const VssRay &currentRay,
175                                     const Triangle3 &hitTriangle,
176                                                         const VssRay &oldRay,
177                                                         SimpleRay &simpleRay);
178        /** Adds new samples to the ray queue and classifies them
179                with respect to the previous ray.
180        */
181        void EnqueueRays(const VssRayContainer &samples);
182        /** Hepler function for adaptive border sampling. It finds
183                new sample points around a triangle in a eps environment
184        */
185        void EnlargeTriangle(VertexContainer &vertices,
186                                                 const Triangle3 &hitTriangle,
187                                                 const VssRay &ray) const;
188
189        int SubdivideEdge(const Triangle3 &hitTriangle,
190                                          const Vector3 &p1,
191                                          const Vector3 &p2,
192                                          const VssRay &ray1,
193                      const VssRay &ray2,
194                                          const VssRay &oldRay,
195                                          int level);
196
197        void CreateDisplacedVertices(VertexContainer &vertices,
198                                                                 const Triangle3 &hitTriangle,
199                                                                 const VssRay &ray,
200                                                                 const int index) const;
201
202        Vector3 CalcPredictedHitPoint(const VssRay &newRay,
203                                                                  const Triangle3 &hitTriangle,
204                                                                  const VssRay &oldRay) const;
205
206        bool GetPassingPoint(const VssRay &currentRay,
207                                                 const Triangle3 &occluder,
208                                                 const VssRay &oldRay,
209                                                 Vector3 &newPoint) const;
210
211        ViewCell *NextViewCell();
212
213        void GlobalComputation();
214
215        /** Loops over aall view cellls.
216        */
217        void PerViewCellComputation();
218        void PerViewCellComputation2();
219
220        void VisualizeViewCells();
221
222        void VisualizeViewCell(ViewCell *vc);
223        void VisualizeViewCell(const ObjectContainer &objects);
224
225        /** Exchanges view cell triangle pvs with bvh leaf pvs.
226        */
227        void UpdatePvs(ViewCell *currentViewCell);
228
229        void ClearRayQueue();
230
231        /** Create a list of view cells gvs is run on.
232        */
233        void CompileViewCellsList();
234
235        void GetObjectPvs(ObjectContainer &trianglePvs) const;
236
237        bool HasContribution(VssRay &ray);
238
239        void ComputeViewCellGeometryIntersection();
240
241        void DeterminePvsObjects(VssRayContainer &rays);
242
243        void StorePvs(const ObjectContainer &objectPvs);
244
245        /** Compute visibility for the current view cell using gvs
246        */
247        void ComputeViewCell();
248        /** Use this for qt visualization.
249        */
250        void UpdateStatsForVisualization(KdIntersectable *kdInt);
251       
252        void CompileViewCellsFromPointList();
253
254        void ComputeStats();
255
256        int ConvertObjectPvs();
257        /** Keep count of new objects for stats. Returns number of new pvs entries.
258        */
259        inline int CountObject(Intersectable *triObj);
260
261        inline bool AddTriangleObject(Intersectable *triObj);
262
263        inline void AddKdNodeToPvs(const Vector3 &termination);
264        /** For all rayys of the ray container, generates a ray bundle of 4 jittered rays
265                and casts them using optimized 4 eye ray casting.
266        */
267        void CastRayBundles4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
268       
269        void CastRayBundles16(const SimpleRayContainer &rays, VssRayContainer &vssRays, float scale);
270
271        /** Generates a ray bundle of 4 jittered rays and casts them using optimized 4 eye ray casting.
272        */
273        void CastRayBundle4(const SimpleRay &ray, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
274        /**  Cast rays using the optimized 4 eye ray casting routine.
275        */
276        void CastRays4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
277        /** Wrapper for optized ray casting routine taking 4 eye rays and a bounding box for ray clipping.
278        */
279        void Cast4Rays(float *dist, Vector3 *dirs, Vector3 *origs, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
280
281        void CastRayBundle16(const SimpleRay &ray, VssRayContainer &vssRays, float scale);
282
283        virtual void ComputeRenderError();
284
285        bool GenerateImportanceSample(const VssRay &ray, const Triangle3 &triangle, SimpleRay &sray);
286
287        void GenerateImportanceSamples(const VssRay &ray,
288                                           const Triangle3 &triangle,
289                                                                   int numSamples,
290                                                                   SimpleRayContainer &simpleRays);
291
292        /** Collects triangles which are probably visible. Warning: returns
293                only unmailed objects.
294        */
295        void CollectProbablyVisibleTriangles(ObjectContainer &triangles);
296
297        void PrepareProbablyVisibleSampling();
298       
299        void CreateRandomizedReverseRays(const VssRay &ray,
300                                             const Vector3 &predictedPoint,
301                                                                         SimpleRayContainer &rays,
302                                                                         int number,
303                                                                         const Triangle3 &triangle);
304
305        bool CreateRandomizedReverseRay(const VssRay &ray,
306                                            const Vector3 &predictedPoint,
307                                                                        SimpleRay &simpleray,
308                                                                         const Triangle3 &triangle);
309
310        inline bool IntersectsViewCell(Vector3 &origin, const Vector3 &dir) const;
311
312        void CollectProbablyVisibleItems(ProbablyVisibleQueue &vqueue);
313
314        //////////////////////
315
316        bool mUseKdPvs;
317        int mInitialSamples;
318
319        RayQueue mRayQueue;
320        ViewCellBasedDistribution *mDistribution;
321
322        //AxisAlignedBox3 mViewSpaceBox;
323        float mEps;
324        float mThreshold;
325        VssRayContainer mVssRays;
326       
327
328        ObjectContainer mKdPvs;
329
330        ///////////
331        // stats
332
333        ofstream mGvsStatsStream;
334        GvsStatistics mGvsStats;
335
336        bool mPerViewCell;
337        bool mOnlyRandomSampling;
338
339        ViewCell *mCurrentViewCell;
340
341        int mProcessedViewCells;
342
343        int mMinContribution;
344
345        ViewCellContainer mViewCells;
346
347        int mMaxViewCells;
348
349        int mGvsSamplesPerPass;
350
351        bool mComputeRenderError;
352
353        bool mEvaluatePixelError;
354
355        Vector3 mCurrentViewPoint;
356
357        /// container for probably visible triangles
358        ObjectContainer mProbablyVisibleTriangles;
359
360        bool mUseProbablyVisibleSampling;
361
362        float mInitialJitter;
363
364        bool mUseDeterministicGvs;
365
366        float mRadiusOfInfluence;
367};
368
369
370}
371
372#endif
Note: See TracBrowser for help on using the repository browser.