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 |
|
---|
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 ¤tRay);
|
---|
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 ¤tRay,
|
---|
160 | const Triangle3 &hitTriangle,
|
---|
161 | const VssRay &oldRay);
|
---|
162 |
|
---|
163 | bool ReverseSampling2(const VssRay ¤tRay,
|
---|
164 | const Triangle3 &hitTriangle,
|
---|
165 | const VssRay &oldRay,
|
---|
166 | VssRayContainer &reverseRays);
|
---|
167 |
|
---|
168 | /** Returns true if we sampled a closer triangle than with the previous ray.
|
---|
169 | Does reverse sampling if gap found.
|
---|
170 | */
|
---|
171 | int CheckDiscontinuity(const VssRay ¤tRay,
|
---|
172 | const Triangle3 &hitTriangle,
|
---|
173 | const VssRay &oldRay);
|
---|
174 |
|
---|
175 | /** Adds new samples to the ray queue and classifies them
|
---|
176 | with respect to the previous ray.
|
---|
177 | */
|
---|
178 | void EnqueueRays(VssRayContainer &samples);
|
---|
179 |
|
---|
180 | /** Hepler function for adaptive border sampling. It finds
|
---|
181 | new sample points around a triangle in a eps environment
|
---|
182 | */
|
---|
183 | void EnlargeTriangle(VertexContainer &vertices,
|
---|
184 | const Triangle3 &hitTriangle,
|
---|
185 | const VssRay &ray) const;
|
---|
186 |
|
---|
187 | int SubdivideEdge(const Triangle3 &hitTriangle,
|
---|
188 | const Vector3 &p1,
|
---|
189 | const Vector3 &p2,
|
---|
190 | const VssRay &ray1,
|
---|
191 | const VssRay &ray2,
|
---|
192 | const VssRay &oldRay);
|
---|
193 |
|
---|
194 | void Visualize();
|
---|
195 |
|
---|
196 | void CreateDisplacedVertices(VertexContainer &vertices,
|
---|
197 | const Triangle3 &hitTriangle,
|
---|
198 | const VssRay &ray,
|
---|
199 | const int index) const;
|
---|
200 |
|
---|
201 | Vector3 CalcPredictedHitPoint(const VssRay &newRay,
|
---|
202 | const Triangle3 &hitTriangle,
|
---|
203 | const VssRay &oldRay) const;
|
---|
204 |
|
---|
205 | bool GetPassingPoint(const VssRay ¤tRay,
|
---|
206 | const Triangle3 &occluder,
|
---|
207 | const VssRay &oldRay,
|
---|
208 | Vector3 &newPoint) const;
|
---|
209 |
|
---|
210 | ViewCell *NextViewCell();
|
---|
211 |
|
---|
212 | void GlobalComputation();
|
---|
213 |
|
---|
214 | /** Loops over aall view cellls.
|
---|
215 | */
|
---|
216 | void PerViewCellComputation();
|
---|
217 | void PerViewCellComputation2();
|
---|
218 |
|
---|
219 | void VisualizeViewCells();
|
---|
220 |
|
---|
221 | void VisualizeViewCell(ViewCell *vc);
|
---|
222 | void VisualizeViewCell(const ObjectContainer &objects);
|
---|
223 |
|
---|
224 | /** Exchanges view cell triangle pvs with bvh leaf pvs.
|
---|
225 | */
|
---|
226 | void UpdatePvs(ViewCell *currentViewCell);
|
---|
227 |
|
---|
228 | void ClearRayQueue();
|
---|
229 |
|
---|
230 | /** Create a list of view cells gvs is run on.
|
---|
231 | */
|
---|
232 | void CompileViewCellsList();
|
---|
233 |
|
---|
234 | void GetObjectPvs(ObjectContainer &trianglePvs) const;
|
---|
235 |
|
---|
236 | bool HasContribution(VssRay &ray);
|
---|
237 |
|
---|
238 | void ComputeViewCellGeometryIntersection();
|
---|
239 |
|
---|
240 | void DeterminePvsObjects(VssRayContainer &rays);
|
---|
241 |
|
---|
242 | void StorePvs(const ObjectContainer &objectPvs);
|
---|
243 |
|
---|
244 | /** Compute visibility for the current view cell using gvs
|
---|
245 | */
|
---|
246 | void ComputeViewCell();
|
---|
247 | /** Use this for qt visualization.
|
---|
248 | */
|
---|
249 | void UpdateStatsForVisualization(KdIntersectable *kdInt);
|
---|
250 |
|
---|
251 | void CompileViewCellsFromPointList();
|
---|
252 |
|
---|
253 | void ComputeStats();
|
---|
254 |
|
---|
255 | int ConvertObjectPvs();
|
---|
256 | /** Keep count of new objects for stats. Returns number of new pvs entries.
|
---|
257 | */
|
---|
258 | inline int CountObject(Intersectable *triObj);
|
---|
259 |
|
---|
260 | inline bool AddTriangleObject(Intersectable *triObj);
|
---|
261 |
|
---|
262 | inline void AddKdNodeToPvs(const Vector3 &termination);
|
---|
263 |
|
---|
264 |
|
---|
265 | //////////////////////
|
---|
266 |
|
---|
267 | bool mUseKdPvs;
|
---|
268 | int mInitialSamples;
|
---|
269 |
|
---|
270 | RayQueue mRayQueue;
|
---|
271 | ViewCellBasedDistribution *mDistribution;
|
---|
272 |
|
---|
273 | //AxisAlignedBox3 mViewSpaceBox;
|
---|
274 | float mEps;
|
---|
275 | float mThreshold;
|
---|
276 | VssRayContainer mVssRays;
|
---|
277 |
|
---|
278 |
|
---|
279 | ObjectContainer mKdPvs;
|
---|
280 |
|
---|
281 | ///////////
|
---|
282 | // stats
|
---|
283 |
|
---|
284 | ofstream mGvsStatsStream;
|
---|
285 | GvsStatistics mGvsStats;
|
---|
286 |
|
---|
287 | bool mPerViewCell;
|
---|
288 | bool mOnlyRandomSampling;
|
---|
289 |
|
---|
290 | ViewCell *mCurrentViewCell;
|
---|
291 |
|
---|
292 | int mProcessedViewCells;
|
---|
293 |
|
---|
294 | int mMinContribution;
|
---|
295 |
|
---|
296 | ViewCellContainer mViewCells;
|
---|
297 |
|
---|
298 | int mMaxViewCells;
|
---|
299 |
|
---|
300 | int mGvsSamplesPerPass;
|
---|
301 |
|
---|
302 | bool mComputeRenderError;
|
---|
303 |
|
---|
304 | bool mEvaluatePixelError;
|
---|
305 |
|
---|
306 | Vector3 mCurrentViewPoint;
|
---|
307 | };
|
---|
308 |
|
---|
309 | };
|
---|
310 |
|
---|
311 | #endif
|
---|