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