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