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

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