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