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