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