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 | class TriangleIntersectable;
|
---|
14 | class KdIntersectable;
|
---|
15 | class ViewCellBasedDistribution;
|
---|
16 | class ViewCellBorderBasedDistribution;
|
---|
17 |
|
---|
18 |
|
---|
19 | /** View space partition statistics.
|
---|
20 | */
|
---|
21 | class GvsStatistics: public StatisticsBase
|
---|
22 | {
|
---|
23 | public:
|
---|
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 |
|
---|
56 | public:
|
---|
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 | */
|
---|
98 | class GvsPreprocessor : public Preprocessor
|
---|
99 | {
|
---|
100 |
|
---|
101 | public:
|
---|
102 |
|
---|
103 | GvsPreprocessor();
|
---|
104 | ~GvsPreprocessor();
|
---|
105 |
|
---|
106 | virtual bool ComputeVisibility();
|
---|
107 |
|
---|
108 |
|
---|
109 | protected:
|
---|
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 | /** The adaptive border sampling step. It aims to find neighbouring
|
---|
149 | triangles of the one hit by the current ray.
|
---|
150 | */
|
---|
151 | int AdaptiveBorderSampling(const VssRay ¤tRay);
|
---|
152 | int AdaptiveBorderSamplingOpt(const VssRay ¤tRay);
|
---|
153 | /** The reverse sampling step. It is started once the cast
|
---|
154 | ray finds a discontinuity, i.e., a closer triangle.
|
---|
155 | Then the process tries to find a ray from the old
|
---|
156 | triangle passing through a gap.
|
---|
157 | */
|
---|
158 | bool ComputeReverseRay(const VssRay ¤tRay,
|
---|
159 | const Triangle3 &hitTriangle,
|
---|
160 | const VssRay &oldRay,
|
---|
161 | SimpleRay &reverseRay);
|
---|
162 | /** Returns true if we sampled a closer triangle than with the previous ray.
|
---|
163 | Does reverse sampling if gap found.
|
---|
164 | */
|
---|
165 | int CheckDiscontinuity(const VssRay ¤tRay,
|
---|
166 | const Triangle3 &hitTriangle,
|
---|
167 | const VssRay &oldRay);
|
---|
168 | /** Adds new samples to the ray queue and classifies them
|
---|
169 | with respect to the previous ray.
|
---|
170 | */
|
---|
171 | void EnqueueRays(VssRayContainer &samples);
|
---|
172 | /** Hepler function for adaptive border sampling. It finds
|
---|
173 | new sample points around a triangle in a eps environment
|
---|
174 | */
|
---|
175 | void EnlargeTriangle(VertexContainer &vertices,
|
---|
176 | const Triangle3 &hitTriangle,
|
---|
177 | const VssRay &ray) const;
|
---|
178 |
|
---|
179 | int SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
180 | const Vector3 &p1,
|
---|
181 | const Vector3 &p2,
|
---|
182 | const VssRay &ray1,
|
---|
183 | const VssRay &ray2,
|
---|
184 | const VssRay &oldRay);
|
---|
185 |
|
---|
186 | void Visualize();
|
---|
187 |
|
---|
188 | void CreateDisplacedVertices(VertexContainer &vertices,
|
---|
189 | const Triangle3 &hitTriangle,
|
---|
190 | const VssRay &ray,
|
---|
191 | const int index) const;
|
---|
192 |
|
---|
193 | Vector3 CalcPredictedHitPoint(const VssRay &newRay,
|
---|
194 | const Triangle3 &hitTriangle,
|
---|
195 | const VssRay &oldRay) const;
|
---|
196 |
|
---|
197 | bool GetPassingPoint(const VssRay ¤tRay,
|
---|
198 | const Triangle3 &occluder,
|
---|
199 | const VssRay &oldRay,
|
---|
200 | Vector3 &newPoint) const;
|
---|
201 |
|
---|
202 | ViewCell *NextViewCell();
|
---|
203 |
|
---|
204 | void GlobalComputation();
|
---|
205 |
|
---|
206 | /** Loops over aall view cellls.
|
---|
207 | */
|
---|
208 | void PerViewCellComputation();
|
---|
209 | void PerViewCellComputation2();
|
---|
210 |
|
---|
211 | void VisualizeViewCells();
|
---|
212 |
|
---|
213 | void VisualizeViewCell(ViewCell *vc);
|
---|
214 | void VisualizeViewCell(const ObjectContainer &objects);
|
---|
215 |
|
---|
216 | /** Exchanges view cell triangle pvs with bvh leaf pvs.
|
---|
217 | */
|
---|
218 | void UpdatePvs(ViewCell *currentViewCell);
|
---|
219 |
|
---|
220 | void ClearRayQueue();
|
---|
221 |
|
---|
222 | /** Create a list of view cells gvs is run on.
|
---|
223 | */
|
---|
224 | void CompileViewCellsList();
|
---|
225 |
|
---|
226 | void GetObjectPvs(ObjectContainer &trianglePvs) const;
|
---|
227 |
|
---|
228 | bool HasContribution(VssRay &ray);
|
---|
229 |
|
---|
230 | void ComputeViewCellGeometryIntersection();
|
---|
231 |
|
---|
232 | void DeterminePvsObjects(VssRayContainer &rays);
|
---|
233 |
|
---|
234 | void StorePvs(const ObjectContainer &objectPvs);
|
---|
235 |
|
---|
236 | /** Compute visibility for the current view cell using gvs
|
---|
237 | */
|
---|
238 | void ComputeViewCell();
|
---|
239 | /** Use this for qt visualization.
|
---|
240 | */
|
---|
241 | void UpdateStatsForVisualization(KdIntersectable *kdInt);
|
---|
242 |
|
---|
243 | void CompileViewCellsFromPointList();
|
---|
244 |
|
---|
245 | void ComputeStats();
|
---|
246 |
|
---|
247 | int ConvertObjectPvs();
|
---|
248 | /** Keep count of new objects for stats. Returns number of new pvs entries.
|
---|
249 | */
|
---|
250 | inline int CountObject(Intersectable *triObj);
|
---|
251 |
|
---|
252 | inline bool AddTriangleObject(Intersectable *triObj);
|
---|
253 |
|
---|
254 | inline void AddKdNodeToPvs(const Vector3 &termination);
|
---|
255 | /** For all rayys of the ray container, generates a ray bundle of 4 jittered rays
|
---|
256 | and casts them using optimized 4 eye ray casting.
|
---|
257 | */
|
---|
258 | void CastRayBundles4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
|
---|
259 |
|
---|
260 | void CastRayBundles16(const SimpleRayContainer &rays, VssRayContainer &vssRays);
|
---|
261 |
|
---|
262 | /** Generates a ray bundle of 4 jittered rays and casts them using optimized 4 eye ray casting.
|
---|
263 | */
|
---|
264 | void CastRayBundle4(const SimpleRay &ray, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
|
---|
265 | /** Cast rays using the optimized 4 eye ray casting routine.
|
---|
266 | */
|
---|
267 | void CastRays4(const SimpleRayContainer &rays, VssRayContainer &vssRays);
|
---|
268 | /** Wrapper for optized ray casting routine taking 4 eye rays and a bounding box for ray clipping.
|
---|
269 | */
|
---|
270 | void Cast4Rays(float *dist, Vector3 *dirs, Vector3 *origs, VssRayContainer &vssRays, const AxisAlignedBox3 &box);
|
---|
271 |
|
---|
272 | void CastRayBundle16(const SimpleRay &ray, VssRayContainer &vssRays);
|
---|
273 |
|
---|
274 | virtual void ComputeRenderError();
|
---|
275 |
|
---|
276 | bool GenerateImportanceSample(const VssRay &ray, const Triangle3 &triangle, SimpleRay &sray);
|
---|
277 |
|
---|
278 | void GenerateImportanceSamples(const VssRay &ray,
|
---|
279 | const Triangle3 &triangle,
|
---|
280 | int numSamples,
|
---|
281 | SimpleRayContainer &simpleRays);
|
---|
282 |
|
---|
283 | /** Collects triangles which are probably visible. Warning: returns
|
---|
284 | only unmailed objects.
|
---|
285 | */
|
---|
286 | void CollectProbablyVisibleTriangles(ObjectContainer &triangles);
|
---|
287 |
|
---|
288 | void PrepareProbablyVisibleSampling();
|
---|
289 |
|
---|
290 |
|
---|
291 | //////////////////////
|
---|
292 |
|
---|
293 | bool mUseKdPvs;
|
---|
294 | int mInitialSamples;
|
---|
295 |
|
---|
296 | RayQueue mRayQueue;
|
---|
297 | ViewCellBasedDistribution *mDistribution;
|
---|
298 |
|
---|
299 | //AxisAlignedBox3 mViewSpaceBox;
|
---|
300 | float mEps;
|
---|
301 | float mThreshold;
|
---|
302 | VssRayContainer mVssRays;
|
---|
303 |
|
---|
304 |
|
---|
305 | ObjectContainer mKdPvs;
|
---|
306 |
|
---|
307 | ///////////
|
---|
308 | // stats
|
---|
309 |
|
---|
310 | ofstream mGvsStatsStream;
|
---|
311 | GvsStatistics mGvsStats;
|
---|
312 |
|
---|
313 | bool mPerViewCell;
|
---|
314 | bool mOnlyRandomSampling;
|
---|
315 |
|
---|
316 | ViewCell *mCurrentViewCell;
|
---|
317 |
|
---|
318 | int mProcessedViewCells;
|
---|
319 |
|
---|
320 | int mMinContribution;
|
---|
321 |
|
---|
322 | ViewCellContainer mViewCells;
|
---|
323 |
|
---|
324 | int mMaxViewCells;
|
---|
325 |
|
---|
326 | int mGvsSamplesPerPass;
|
---|
327 |
|
---|
328 | bool mComputeRenderError;
|
---|
329 |
|
---|
330 | bool mEvaluatePixelError;
|
---|
331 |
|
---|
332 | Vector3 mCurrentViewPoint;
|
---|
333 |
|
---|
334 | /// container for probably visible triangles
|
---|
335 | ObjectContainer mProbablyVisibleTriangles;
|
---|
336 |
|
---|
337 | bool mUseProbablyVisibleSampling;
|
---|
338 | };
|
---|
339 |
|
---|
340 | };
|
---|
341 |
|
---|
342 | #endif
|
---|