1 | #ifndef _GvsPreprocessor_H__
|
---|
2 | #define _GvsPreprocessor_H__
|
---|
3 |
|
---|
4 | #include <fstream>
|
---|
5 | #include <stack>
|
---|
6 |
|
---|
7 | #include "Preprocessor.h"
|
---|
8 |
|
---|
9 | namespace GtpVisibilityPreprocessor {
|
---|
10 |
|
---|
11 | class Exporter;
|
---|
12 | class VssRay;
|
---|
13 |
|
---|
14 | /** View space partition statistics.
|
---|
15 | */
|
---|
16 | class GvsStatistics: public StatisticsBase
|
---|
17 | {
|
---|
18 | public:
|
---|
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 |
|
---|
48 | public:
|
---|
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 | */
|
---|
86 | class GvsPreprocessor : public Preprocessor {
|
---|
87 |
|
---|
88 | public:
|
---|
89 |
|
---|
90 | GvsPreprocessor();
|
---|
91 | ~GvsPreprocessor();
|
---|
92 |
|
---|
93 | virtual bool ComputeVisibility();
|
---|
94 |
|
---|
95 |
|
---|
96 | protected:
|
---|
97 | #if 0
|
---|
98 | struct PendingRay
|
---|
99 | {
|
---|
100 | PendingRay(VssRay *ray, const bool d)
|
---|
101 | : mRay(ray), mFoundDiscontinuity(d)
|
---|
102 | {}
|
---|
103 |
|
---|
104 | VssRay *mRay;
|
---|
105 | bool mFoundDiscontinuity;
|
---|
106 | };
|
---|
107 |
|
---|
108 | typedef stack<PendingRay> PendingQueue;
|
---|
109 | #endif
|
---|
110 | typedef stack<VssRay *> RayQueue;
|
---|
111 |
|
---|
112 | /** Runs the adaptive sampling until the ray queue is empty.
|
---|
113 | The method starts with a number of random rays given
|
---|
114 | by the queue and continues as long it finds new visible geometry
|
---|
115 | (i.e., the queue is not empty).
|
---|
116 |
|
---|
117 | @returns the number of samples cast.
|
---|
118 | */
|
---|
119 | int ProcessQueue();
|
---|
120 |
|
---|
121 | /** Generates the rays starting the adaptive visibility sampling process.
|
---|
122 | */
|
---|
123 | int CastInitialSamples(const int numSamples, const int sampleType);
|
---|
124 |
|
---|
125 | /** Uses the information gained from the ray for doing adaptive border sampling.
|
---|
126 | This function tries to find the border of the triangle found visible by the
|
---|
127 | current ray. New rays are generated which sample this border.
|
---|
128 |
|
---|
129 | We use the following strategies:
|
---|
130 |
|
---|
131 | a) if new triangle was found: adaptive border sampling
|
---|
132 | b) if triangle was found reverse sampling
|
---|
133 | */
|
---|
134 | bool HandleRay(VssRay *ray);
|
---|
135 |
|
---|
136 | /** The adaptive border sampling step. It aims to find neighbouring
|
---|
137 | triangles of the one hit by the current ray.
|
---|
138 | */
|
---|
139 | int AdaptiveBorderSampling(const VssRay ¤tRay);
|
---|
140 |
|
---|
141 | /** The reverse sampling step. It is started once the cast
|
---|
142 | ray finds a discontinuity, i.e., a closer triangle.
|
---|
143 | Then the process tries to find a ray from the old
|
---|
144 | triangle passing through a gap.
|
---|
145 | */
|
---|
146 | VssRay *ReverseSampling(const VssRay ¤tRay,
|
---|
147 | const Triangle3 &hitTriangle,
|
---|
148 | const VssRay &oldRay);
|
---|
149 |
|
---|
150 | /** Returns true if we sampled a closer triangle than with the previous ray.
|
---|
151 | Does reverse sampling if gap found.
|
---|
152 | */
|
---|
153 | int CheckDiscontinuity(const VssRay ¤tRay,
|
---|
154 | const Triangle3 &hitTriangle,
|
---|
155 | const VssRay &oldRay);
|
---|
156 |
|
---|
157 | /** Adds new samples to the ray queue and classifies them
|
---|
158 | with respect to the previous ray.
|
---|
159 | */
|
---|
160 | void EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples);
|
---|
161 |
|
---|
162 | /** Hepler function for adaptive border sampling. It finds
|
---|
163 | new sample points around a triangle in a eps environment
|
---|
164 | */
|
---|
165 | void EnlargeTriangle(VertexContainer &vertices,
|
---|
166 | const Triangle3 &hitTriangle,
|
---|
167 | const VssRay &ray) const;
|
---|
168 |
|
---|
169 | int SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
170 | const Vector3 &p1,
|
---|
171 | const Vector3 &p2,
|
---|
172 | const VssRay &ray1,
|
---|
173 | const VssRay &ray2,
|
---|
174 | const VssRay &oldRay);
|
---|
175 |
|
---|
176 | void Visualize();
|
---|
177 |
|
---|
178 | void CreateDisplacedVertices(VertexContainer &vertices,
|
---|
179 | const Triangle3 &hitTriangle,
|
---|
180 | const VssRay &ray,
|
---|
181 | const int index) const;
|
---|
182 |
|
---|
183 | Vector3 CalcPredictedHitPoint(const VssRay &newRay,
|
---|
184 | const Triangle3 &hitTriangle,
|
---|
185 | const VssRay &oldRay) const;
|
---|
186 |
|
---|
187 | bool GetPassingPoint(const VssRay ¤tRay,
|
---|
188 | const Triangle3 &occluder,
|
---|
189 | const VssRay &oldRay,
|
---|
190 | Vector3 &newPoint) const;
|
---|
191 |
|
---|
192 | bool NextViewCell();
|
---|
193 |
|
---|
194 | void GlobalComputation();
|
---|
195 |
|
---|
196 | void PerViewCellComputation();
|
---|
197 |
|
---|
198 | void VisualizeViewCells();
|
---|
199 |
|
---|
200 | void VisualizeViewCell(ViewCell *vc);
|
---|
201 | void VisualizeViewCell(const ObjectContainer &objects);
|
---|
202 |
|
---|
203 | /** Exchanges view cell triangle pvs with bvh leaf pvs.
|
---|
204 | */
|
---|
205 | void UpdatePvs(ViewCell *currentViewCell);
|
---|
206 |
|
---|
207 | void ProcessViewCell();
|
---|
208 | void ClearRayQueue();
|
---|
209 |
|
---|
210 | void CompileViewCellsList();
|
---|
211 |
|
---|
212 | void GetObjectPvs(ObjectContainer &trianglePvs) const;
|
---|
213 |
|
---|
214 | bool HasContribution(VssRay &ray);
|
---|
215 |
|
---|
216 | void IntersectWithViewCell();
|
---|
217 |
|
---|
218 | void DeterminePvsObjects(VssRayContainer &rays);
|
---|
219 |
|
---|
220 | //virtual void ComputeRenderError();
|
---|
221 |
|
---|
222 | void StorePvs(const ObjectContainer &objectPvs);
|
---|
223 |
|
---|
224 |
|
---|
225 | //////////////////////
|
---|
226 |
|
---|
227 | bool mUseKdPvs;
|
---|
228 | int mInitialSamples;
|
---|
229 |
|
---|
230 | RayQueue mRayQueue;
|
---|
231 | int mSamplingType;
|
---|
232 |
|
---|
233 | //AxisAlignedBox3 mViewSpaceBox;
|
---|
234 | float mEps;
|
---|
235 | float mThreshold;
|
---|
236 | VssRayContainer mVssRays;
|
---|
237 |
|
---|
238 |
|
---|
239 | ObjectContainer mKdPvs;
|
---|
240 |
|
---|
241 | ///////////
|
---|
242 | // stats
|
---|
243 |
|
---|
244 | ofstream mGvsStatsStream;
|
---|
245 | GvsStatistics mGvsStats;
|
---|
246 |
|
---|
247 | bool mPerViewCell;
|
---|
248 | bool mOnlyRandomSampling;
|
---|
249 |
|
---|
250 | ViewCell *mCurrentViewCell;
|
---|
251 |
|
---|
252 | int mProcessedViewCells;
|
---|
253 |
|
---|
254 | int mMinContribution;
|
---|
255 |
|
---|
256 | ViewCellContainer mViewCells;
|
---|
257 |
|
---|
258 | int mMaxViewCells;
|
---|
259 |
|
---|
260 | ObjectContainer mTrianglePvs;
|
---|
261 |
|
---|
262 | // HACK
|
---|
263 | int mGvsSamplesPerPass;
|
---|
264 |
|
---|
265 | bool mComputeRenderError;
|
---|
266 |
|
---|
267 | bool mEvaluatePixelError;
|
---|
268 | };
|
---|
269 |
|
---|
270 | };
|
---|
271 |
|
---|
272 | #endif
|
---|