1 | #ifndef __VSS_RAY_H
|
---|
2 | #define __VSS_RAY_H
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 |
|
---|
6 | #include "Vector3.h"
|
---|
7 | #include "Containers.h"
|
---|
8 |
|
---|
9 | #ifdef USE_SSE
|
---|
10 | #include <xmmintrin.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 |
|
---|
14 | namespace GtpVisibilityPreprocessor {
|
---|
15 |
|
---|
16 | class AxisAlignedBox3;
|
---|
17 | class Intersectable;
|
---|
18 | class KdNode;
|
---|
19 | class Ray;
|
---|
20 |
|
---|
21 | #define ABS_CONTRIBUTION_WEIGHT 0.0f
|
---|
22 | #define VSS_STORE_VIEWCELLS 1
|
---|
23 |
|
---|
24 |
|
---|
25 |
|
---|
26 | class VssRay {
|
---|
27 | public:
|
---|
28 |
|
---|
29 | // various flags
|
---|
30 | enum {
|
---|
31 | FPosDirX = 1, // the direction of ray in X-axis is positive
|
---|
32 | FPosDirY = 2, // the direction of ray in Y-axis is positive
|
---|
33 | FPosDirZ = 4, // the direction of ray in Z-axis is positive
|
---|
34 | BorderSample = 8,// if this ray is an adaptive border ray
|
---|
35 | ReverseSample = 16, // if this ray is a reverse sample
|
---|
36 | Valid = 32 // this ray is a valid ray
|
---|
37 | //(with respect to detect empty viewspace)
|
---|
38 | };
|
---|
39 |
|
---|
40 | // Id of the generating SimpleRay
|
---|
41 | int mGeneratorId;
|
---|
42 |
|
---|
43 | static int mailID;
|
---|
44 | int mMailbox;
|
---|
45 |
|
---|
46 | // inverse of the ray size
|
---|
47 | float mInvSize;
|
---|
48 |
|
---|
49 | // counter of references to this ray
|
---|
50 | short mRefCount;
|
---|
51 |
|
---|
52 | // various flags
|
---|
53 | char mFlags;
|
---|
54 |
|
---|
55 | Vector3 mOrigin;
|
---|
56 | Vector3 mTermination;
|
---|
57 |
|
---|
58 | /// Termination object for the ray
|
---|
59 | /// only the termination object is actually used
|
---|
60 | Intersectable *mOriginObject;
|
---|
61 | Intersectable *mTerminationObject;
|
---|
62 |
|
---|
63 | #if VSS_STORE_VIEWCELLS
|
---|
64 | ViewCellContainer mViewCells;
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | ////////////////////////
|
---|
68 | // members related to importance sampling
|
---|
69 | // sampling pass in which this ray was generated
|
---|
70 | short mPass;
|
---|
71 |
|
---|
72 | /// Distribution used to generate this ray
|
---|
73 | short mDistribution;
|
---|
74 |
|
---|
75 | /// number of cells where this ray made a contribution to the PVS
|
---|
76 | int mPvsContribution;
|
---|
77 |
|
---|
78 | /// sum of relative ray contributions per object
|
---|
79 | float mRelativePvsContribution;
|
---|
80 |
|
---|
81 | /// weighted contribution to the pvs (based on the pass the ray was casted at)
|
---|
82 | /// computed by the prperocessor
|
---|
83 | float mWeightedPvsContribution;
|
---|
84 |
|
---|
85 | /// probability of this ray
|
---|
86 | float mPdf;
|
---|
87 |
|
---|
88 | //////////////////////////////
|
---|
89 |
|
---|
90 |
|
---|
91 | /// the kd node holding the termination point
|
---|
92 | KdNode *mTerminationNode;
|
---|
93 | /// the kd node holding the origin point
|
---|
94 | KdNode *mOriginNode;
|
---|
95 |
|
---|
96 | VssRay() {}
|
---|
97 |
|
---|
98 | VssRay(
|
---|
99 | const Vector3 &origin,
|
---|
100 | const Vector3 &termination,
|
---|
101 | Intersectable *originObject,
|
---|
102 | Intersectable *terminationObject,
|
---|
103 | const int pass = 0,
|
---|
104 | const float pdf = 1.0f
|
---|
105 | );
|
---|
106 |
|
---|
107 | VssRay(const Ray &ray);
|
---|
108 |
|
---|
109 |
|
---|
110 | void Precompute();
|
---|
111 |
|
---|
112 | void Mail() { mMailbox = mailID; }
|
---|
113 | static void NewMail() { mailID++; }
|
---|
114 | bool Mailed() const { return mMailbox == mailID; }
|
---|
115 |
|
---|
116 | bool Mailed(const int mail) {
|
---|
117 | return mMailbox >= mailID + mail;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int HitCount() const {
|
---|
121 | #if BIDIRECTIONAL_RAY
|
---|
122 | if (mOriginObject && mTerminationObject)
|
---|
123 | return 2;
|
---|
124 | if (mOriginObject || mTerminationObject)
|
---|
125 | return 1;
|
---|
126 | return 0;
|
---|
127 | #else
|
---|
128 | return (mTerminationObject) ? 1 : 0;
|
---|
129 | #endif
|
---|
130 | }
|
---|
131 |
|
---|
132 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
133 | Vector3 GetTermination() const { return mTermination; }
|
---|
134 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
135 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
136 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
137 |
|
---|
138 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
139 |
|
---|
140 | Vector3 Extrap(const float t) const {
|
---|
141 | return GetOrigin() + t * GetDir();
|
---|
142 | }
|
---|
143 |
|
---|
144 | float GetDirParametrization(const int axis) const;
|
---|
145 | float GetOpositeDirParametrization(const int axis) const;
|
---|
146 |
|
---|
147 | static float GetDirParam(const int axis, const Vector3 dir);
|
---|
148 | static Vector3 GetInvDirParam(const float alpha, const float beta);
|
---|
149 |
|
---|
150 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
151 | float GetInvSize() const { return mInvSize; }
|
---|
152 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
153 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
154 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
155 | float GetNormalizedDir(const int axis) const {
|
---|
156 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool
|
---|
160 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
161 | float &tmin,
|
---|
162 | float &tmax) const;
|
---|
163 |
|
---|
164 | bool
|
---|
165 | Intersects(const AxisAlignedBox3 &box,
|
---|
166 | float &tmin,
|
---|
167 | float &tmax) const;
|
---|
168 |
|
---|
169 | bool
|
---|
170 | IntersectsSphere(const Vector3 ¢er,
|
---|
171 | const float sqrRadius,
|
---|
172 | Vector3 &point,
|
---|
173 | float &t) const;
|
---|
174 |
|
---|
175 | void
|
---|
176 | Translate(const Vector3 &translation) {
|
---|
177 | mOrigin += translation;
|
---|
178 | mTermination += translation;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void SetupEndPoints(const Vector3 &origin,
|
---|
182 | const Vector3 &termination)
|
---|
183 | {
|
---|
184 | mOrigin = origin;
|
---|
185 | mTermination = termination;
|
---|
186 | Precompute();
|
---|
187 | }
|
---|
188 |
|
---|
189 | inline bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
190 |
|
---|
191 | char Flags() const { return mFlags;} void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
192 |
|
---|
193 | bool IsActive() const { return mRefCount>0; }
|
---|
194 |
|
---|
195 | // reference counting for leaf nodes
|
---|
196 | int RefCount() const { return mRefCount; }
|
---|
197 | int Ref() { return mRefCount++; }
|
---|
198 |
|
---|
199 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
200 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
201 | void Unref() {
|
---|
202 | if (mRefCount > 0)
|
---|
203 | mRefCount--;
|
---|
204 | else
|
---|
205 | if (mRefCount < 0)
|
---|
206 | mRefCount++;
|
---|
207 | else {
|
---|
208 | std::cerr<<"Trying to unref already deleted ray!"<<std::endl;
|
---|
209 | exit(1);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | static Vector3
|
---|
214 | GetDirection(const float a, const float b) {
|
---|
215 | return GetInvDirParam(a, b);
|
---|
216 | //return Vector3(sin(a), sin(b), cos(a));
|
---|
217 | }
|
---|
218 |
|
---|
219 | friend bool GreaterWeightedPvsContribution(const VssRay * a,
|
---|
220 | const VssRay *b) {
|
---|
221 | return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
|
---|
222 | }
|
---|
223 |
|
---|
224 | float SqrDistance(const Vector3 &point) const {
|
---|
225 | Vector3 diff = point - mOrigin;
|
---|
226 | float t = DotProd(diff, GetDir());
|
---|
227 |
|
---|
228 | if ( t <= 0.0f ) {
|
---|
229 | t = 0.0f;
|
---|
230 | } else {
|
---|
231 | if (t >= 1.0f) {
|
---|
232 | t = 1.0f;
|
---|
233 | } else {
|
---|
234 | t /= SqrMagnitude(GetDir());
|
---|
235 | }
|
---|
236 | diff -= t*GetDir();
|
---|
237 | }
|
---|
238 | return SqrMagnitude(diff);
|
---|
239 | }
|
---|
240 |
|
---|
241 | /** Returns the data sampled on either the ray origin or termination.
|
---|
242 | */
|
---|
243 | void GetSampleData(
|
---|
244 | const bool isTerminaton,
|
---|
245 | Vector3 &pt,
|
---|
246 | Intersectable **obj,
|
---|
247 | KdNode **node) const;
|
---|
248 |
|
---|
249 | friend std::ostream& operator<< (std::ostream &s, const VssRay &vssRay);
|
---|
250 |
|
---|
251 | };
|
---|
252 |
|
---|
253 |
|
---|
254 | inline void VssRay::GetSampleData(const bool isTermination,
|
---|
255 | Vector3 &pt,
|
---|
256 | Intersectable **obj,
|
---|
257 | KdNode **node) const
|
---|
258 | {
|
---|
259 | if (isTermination)
|
---|
260 | {
|
---|
261 | pt = mTermination;
|
---|
262 | *obj = mTerminationObject;
|
---|
263 | *node = mTerminationNode;
|
---|
264 | }
|
---|
265 | else
|
---|
266 | {
|
---|
267 | pt = mOrigin;
|
---|
268 | *obj = mOriginObject;
|
---|
269 | *node = mOriginNode;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | void
|
---|
275 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
276 | float &w2,
|
---|
277 | float &w3,
|
---|
278 | const float overlap
|
---|
279 | );
|
---|
280 |
|
---|
281 | void
|
---|
282 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
283 | float &w2,
|
---|
284 | const float overlap
|
---|
285 | );
|
---|
286 |
|
---|
287 | // Overload << operator for C++-style output
|
---|
288 | inline std::ostream&
|
---|
289 | operator<< (std::ostream &s, const VssRay &vssRay)
|
---|
290 | {
|
---|
291 | return s
|
---|
292 | << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
|
---|
293 | << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
|
---|
294 | }
|
---|
295 |
|
---|
296 | // --------------------------------------------------------------
|
---|
297 | // For sorting rays
|
---|
298 | // --------------------------------------------------------------
|
---|
299 | struct SortableEntry
|
---|
300 | {
|
---|
301 | enum EType {
|
---|
302 | ERayMin,
|
---|
303 | ERayMax
|
---|
304 | };
|
---|
305 |
|
---|
306 | int type;
|
---|
307 | float value;
|
---|
308 | void *data;
|
---|
309 |
|
---|
310 | SortableEntry() {}
|
---|
311 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
312 | value(v),
|
---|
313 | data(d) {}
|
---|
314 |
|
---|
315 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
316 | return a.value < b.value;
|
---|
317 | }
|
---|
318 | };
|
---|
319 |
|
---|
320 | struct VssRayContainer : public vector<VssRay *>
|
---|
321 | {
|
---|
322 | void PrintStatistics(std::ostream &s);
|
---|
323 | int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
|
---|
324 | int GetContributingRays(VssRayContainer &selected, const int minPass) const;
|
---|
325 |
|
---|
326 | };
|
---|
327 |
|
---|
328 |
|
---|
329 | struct RayPacket
|
---|
330 | {
|
---|
331 | RayPacket(const VssRayContainer &rays)
|
---|
332 | {
|
---|
333 | for (int i = 0; i < 4; ++ i)
|
---|
334 | mOriginX[i] = rays[i]->mOrigin[0];
|
---|
335 |
|
---|
336 | for (int i = 0; i < 4; ++ i)
|
---|
337 | mOriginY[i] = rays[i]->mOrigin[1];
|
---|
338 |
|
---|
339 | for (int i = 0; i < 4; ++ i)
|
---|
340 | mOriginZ[i] = rays[i]->mOrigin[2];
|
---|
341 | }
|
---|
342 | #ifdef USE_SSE
|
---|
343 | union { float mOriginX[4]; __m128 mOriginX4; };
|
---|
344 | union { float mOriginY[4]; __m128 mOriginY4; };
|
---|
345 | union { float mOriginZ[4]; __m128 mOriginZ4; };
|
---|
346 |
|
---|
347 | union { float mTerminationX[4]; __m128 mTerminationX4; };
|
---|
348 | union { float mTerminationY[4]; __m128 mTerminationY4; };
|
---|
349 | union { float mTerminationZ[4]; __m128 mTerminationZ4; };
|
---|
350 | #else
|
---|
351 | float mOriginX[4];
|
---|
352 | float mOriginY[4];
|
---|
353 | float mOriginZ[4];
|
---|
354 |
|
---|
355 | float mTerminationX[4];
|
---|
356 | float mTerminationY[4];
|
---|
357 | float mTerminationZ[4];
|
---|
358 | #endif
|
---|
359 | ViewCellContainer mViewCells[4];
|
---|
360 | };
|
---|
361 |
|
---|
362 |
|
---|
363 | };
|
---|
364 |
|
---|
365 | #endif
|
---|