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

Revision 2625, 6.1 KB checked in by mattausch, 16 years ago (diff)

added new view cell
deleted other view cell generation stuff (please use generate_viewcells.sh script)
added visualizaton method for gvs

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