Blender V5.0
sky_math.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifndef __SKY_MATH_H__
10#define __SKY_MATH_H__
11
12#ifdef WITH_TBB
13# include <tbb/parallel_for.h>
14#else
15# include <algorithm>
16#endif
17
18/* Minimal math implementation for sky model. */
19
20#include <cmath>
21
22#ifndef M_PI_F
23# define M_PI_F (3.1415926535897932f) /* pi */
24#endif
25#ifndef M_PI_2_F
26# define M_PI_2_F (1.5707963267948966f) /* pi/2 */
27#endif
28#ifndef M_2PI_F
29# define M_2PI_F (6.2831853071795864f) /* 2*pi */
30#endif
31#ifndef M_1_PI_F
32# define M_1_PI_F (0.3183098861837067f) /* 1/pi */
33#endif
34#ifndef M_4PI_F
35# define M_4PI_F (12.566370614359172f) /* 4*pi */
36#endif
37#ifndef M_1_4PI_F
38# define M_1_4PI_F (0.0795774715459476f) /* 1/(4*pi) */
39#endif
40
41/* float2 */
42struct float2 {
43 float x, y;
44
45 float2() = default;
46
47 float2(const float *ptr) : x{ptr[0]}, y{ptr[1]} {}
48
49 float2(const float (*ptr)[2]) : float2((const float *)ptr) {}
50
51 explicit float2(float value) : x(value), y(value) {}
52
53 explicit float2(int value) : x(value), y(value) {}
54
55 float2(float x, float y) : x{x}, y{y} {}
56
57 operator const float *() const
58 {
59 return &x;
60 }
61
62 operator float *()
63 {
64 return &x;
65 }
66
67 friend float2 operator*(const float2 &a, float b)
68 {
69 return {a.x * b, a.y * b};
70 }
71
72 friend float2 operator*(float b, const float2 &a)
73 {
74 return {a.x * b, a.y * b};
75 }
76
77 friend float2 operator/(const float2 &a, float b)
78 {
79 return {a.x / b, a.y / b};
80 }
81
82 friend float2 operator/(float b, const float2 &a)
83 {
84 return {a.x / b, a.y / b};
85 }
86
87 friend float2 operator/(const float2 &a, const float2 &b)
88 {
89 return {a.x / b.x, a.y / b.y};
90 }
91
92 friend float2 operator-(const float2 &a, const float2 &b)
93 {
94 return {a.x - b.x, a.y - b.y};
95 }
96
97 friend float2 operator-(const float2 &a)
98 {
99 return {-a.x, -a.y};
100 }
101
102 float length_squared() const
103 {
104 return x * x + y * y;
105 }
106
107 float length() const
108 {
109 return sqrt(length_squared());
110 }
111
112 static float distance(const float2 &a, const float2 &b)
113 {
114 return (a - b).length();
115 }
116
117 friend float2 operator+(const float2 &a, const float2 &b)
118 {
119 return {a.x + b.x, a.y + b.y};
120 }
121
122 void operator+=(const float2 &b)
123 {
124 this->x += b.x;
125 this->y += b.y;
126 }
127
128 friend float2 operator*(const float2 &a, const float2 &b)
129 {
130 return {a.x * b.x, a.y * b.y};
131 }
132};
133
134/* float3 */
135struct float3 {
136 float x, y, z;
137
138 float3() = default;
139
140 float3(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]} {}
141
142 float3(const float (*ptr)[3]) : float3((const float *)ptr) {}
143
144 explicit float3(float value) : x(value), y(value), z(value) {}
145
146 explicit float3(int value) : x(value), y(value), z(value) {}
147
148 float3(float x, float y, float z) : x{x}, y{y}, z{z} {}
149
150 operator const float *() const
151 {
152 return &x;
153 }
154
155 operator float *()
156 {
157 return &x;
158 }
159
160 friend float3 operator*(const float3 &a, float b)
161 {
162 return {a.x * b, a.y * b, a.z * b};
163 }
164
165 friend float3 operator*(float b, const float3 &a)
166 {
167 return {a.x * b, a.y * b, a.z * b};
168 }
169
170 friend float3 operator/(const float3 &a, float b)
171 {
172 return {a.x / b, a.y / b, a.z / b};
173 }
174
175 friend float3 operator/(float b, const float3 &a)
176 {
177 return {a.x / b, a.y / b, a.z / b};
178 }
179
180 friend float3 operator-(const float3 &a, const float3 &b)
181 {
182 return {a.x - b.x, a.y - b.y, a.z - b.z};
183 }
184
185 friend float3 operator-(const float3 &a)
186 {
187 return {-a.x, -a.y, -a.z};
188 }
189
190 float length_squared() const
191 {
192 return x * x + y * y + z * z;
193 }
194
195 float length() const
196 {
197 return sqrt(length_squared());
198 }
199
200 static float distance(const float3 &a, const float3 &b)
201 {
202 return (a - b).length();
203 }
204
205 friend float3 operator+(const float3 &a, const float3 &b)
206 {
207 return {a.x + b.x, a.y + b.y, a.z + b.z};
208 }
209
210 void operator+=(const float3 &b)
211 {
212 this->x += b.x;
213 this->y += b.y;
214 this->z += b.z;
215 }
216
217 friend float3 operator*(const float3 &a, const float3 &b)
218 {
219 return {a.x * b.x, a.y * b.y, a.z * b.z};
220 }
221};
222
223/* float4 */
224struct float4 {
225 float x, y, z, w;
226
227 float4() = default;
228
229 float4(const float *ptr) : x{ptr[0]}, y{ptr[1]}, z{ptr[2]}, w{ptr[3]} {}
230
231 float4(const float (*ptr)[4]) : float4((const float *)ptr) {}
232
233 explicit float4(float value) : x(value), y(value), z(value), w(value) {}
234
235 explicit float4(int value) : x(value), y(value), z(value), w(value) {}
236
237 float4(float x, float y, float z, float w) : x{x}, y{y}, z{z}, w{w} {}
238
239 operator const float *() const
240 {
241 return &x;
242 }
243
244 operator float *()
245 {
246 return &x;
247 }
248
249 friend float4 operator*(const float4 &a, float b)
250 {
251 return {a.x * b, a.y * b, a.z * b, a.w * b};
252 }
253
254 friend float4 operator*(float b, const float4 &a)
255 {
256 return {a.x * b, a.y * b, a.z * b, a.w * b};
257 }
258
259 friend float4 operator/(const float4 &a, float b)
260 {
261 return {a.x / b, a.y / b, a.z / b, a.w / b};
262 }
263
264 friend float4 operator/(float b, const float4 &a)
265 {
266 return {a.x / b, a.y / b, a.z / b, a.w / b};
267 }
268
269 friend float4 operator*(const float4 &a, const float4 &b)
270 {
271 return {a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w};
272 }
273
274 friend float4 operator/(const float4 &a, const float4 &b)
275 {
276 return {a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w};
277 }
278
279 friend float4 operator-(const float4 &a, const float4 &b)
280 {
281 return {a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
282 }
283
284 friend float4 operator-(const float4 &a)
285 {
286 return {-a.x, -a.y, -a.z, -a.w};
287 }
288
289 float length_squared() const
290 {
291 return x * x + y * y + z * z + w * w;
292 }
293
294 float length() const
295 {
296 return sqrt(length_squared());
297 }
298
299 static float distance(const float4 &a, const float4 &b)
300 {
301 return (a - b).length();
302 }
303
304 friend float4 operator+(const float4 &a, const float4 &b)
305 {
306 return {a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
307 }
308
309 void operator+=(const float4 &b)
310 {
311 this->x += b.x;
312 this->y += b.y;
313 this->z += b.z;
314 this->w += b.w;
315 }
316
317 void operator*=(const float4 &b)
318 {
319 this->x *= b.x;
320 this->y *= b.y;
321 this->z *= b.z;
322 this->w *= b.w;
323 }
324};
325
326inline float sqr(float a)
327{
328 return a * a;
329}
330
331inline float safe_sqrtf(const float f)
332{
333 return sqrt(fmax(f, 0.0f));
334}
335
336inline float2 make_float2(float x, float y)
337{
338 return float2(x, y);
339}
340
341inline float dot(const float2 &a, const float2 &b)
342{
343 return a.x * b.x + a.y * b.y;
344}
345
346inline float distance(const float2 &a, const float2 &b)
347{
348 return float2::distance(a, b);
349}
350
351inline float len_squared(float2 f)
352{
353 return f.length_squared();
354}
355
356inline float len(float2 f)
357{
358 return f.length();
359}
360
361inline float reduce_add(float2 f)
362{
363 return f.x + f.y;
364}
365
366inline float3 make_float3(float x, float y, float z)
367{
368 return float3(x, y, z);
369}
370
371inline float dot(const float3 &a, const float3 &b)
372{
373 return a.x * b.x + a.y * b.y + a.z * b.z;
374}
375
376inline float distance(const float3 &a, const float3 &b)
377{
378 return float3::distance(a, b);
379}
380
381inline float len_squared(float3 f)
382{
383 return f.length_squared();
384}
385
386inline float len(float3 f)
387{
388 return f.length();
389}
390
391inline float reduce_add(float3 f)
392{
393 return f.x + f.y + f.z;
394}
395
396inline float4 make_float4(float x, float y, float z, float w)
397{
398 return float4(x, y, z, w);
399}
400
401inline float dot(const float4 a, const float4 b)
402{
403 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
404}
405
406inline float distance(const float4 &a, const float4 &b)
407{
408 return float4::distance(a, b);
409}
410
411inline float len_squared(float4 f)
412{
413 return f.length_squared();
414}
415
416inline float len(float4 f)
417{
418 return f.length();
419}
420
421inline float reduce_add(float4 f)
422{
423 return f.x + f.y + f.z + f.w;
424}
425
427{
428 return make_float4(expf(a.x), expf(a.y), expf(a.z), expf(a.w));
429}
430
431inline float4 max(float4 a, float b)
432{
433 return make_float4(fmax(a.x, b), fmax(a.y, b), fmax(a.z, b), fmax(a.w, b));
434}
435
436inline float clamp(float x, float min, float max)
437{
438 if (x < min) {
439 return min;
440 }
441 if (x > max) {
442 return max;
443 }
444 return x;
445}
446
447inline float saturate(const float a)
448{
449 return clamp(a, 0.0f, 1.0f);
450}
451
452template<typename T> inline T mix(T x, T y, float a)
453{
454 return x + a * (y - x);
455}
456
457inline float3 sun_direction(float sun_cos_theta)
458{
459 return make_float3(-sqrtf(1.0f - sun_cos_theta * sun_cos_theta), 0.0f, sun_cos_theta);
460}
461
462inline float ray_sphere_intersection(float3 pos, float3 dir, float radius)
463{
464 float b = dot(pos, dir);
465 float c = dot(pos, pos) - radius * radius;
466 if (c > 0.0f && b > 0.0f) {
467 return -1.0f;
468 }
469 float d = b * b - c;
470 if (d < 0) {
471 return -1.0f;
472 }
473 if (d >= b * b) {
474 return -b + sqrtf(d);
475 }
476 return -b - sqrtf(d);
477}
478
479/* Minimal parallel for implementation. */
480
481template<typename Function>
482inline void SKY_parallel_for(const size_t begin,
483 const size_t end,
484 const size_t grainsize,
485 const Function &function)
486{
487#ifdef WITH_TBB
488 tbb::parallel_for(
489 tbb::blocked_range<size_t>(begin, end, grainsize),
490 [function](const tbb::blocked_range<size_t> &r) { function(r.begin(), r.end()); });
491#else
492 for (size_t i = begin; i < end; i += grainsize) {
493 function(i, std::min(i + grainsize, end));
494 }
495 (void)grainsize;
496#endif
497}
498
499#endif /* __SKY_MATH_H__ */
iter begin(iter)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
nullptr float
#define expf(x)
uint pos
#define exp
#define sqrt
VecBase< float, 2 > float2
VecBase< float, 4 > float4
VecBase< float, 3 > float3
#define T
#define mix
#define sqr
#define sqrtf
#define make_float2
#define make_float4
float distance(const float2 &a, const float2 &b)
Definition sky_math.h:346
float safe_sqrtf(const float f)
Definition sky_math.h:331
float3 make_float3(float x, float y, float z)
Definition sky_math.h:366
float reduce_add(float2 f)
Definition sky_math.h:361
void SKY_parallel_for(const size_t begin, const size_t end, const size_t grainsize, const Function &function)
Definition sky_math.h:482
float ray_sphere_intersection(float3 pos, float3 dir, float radius)
Definition sky_math.h:462
float3 sun_direction(float sun_cos_theta)
Definition sky_math.h:457
float clamp(float x, float min, float max)
Definition sky_math.h:436
float len_squared(float2 f)
Definition sky_math.h:351
float dot(const float2 &a, const float2 &b)
Definition sky_math.h:341
#define saturate(a)
Definition smaa.cc:315
#define min(a, b)
Definition sort.cc:36
friend float2 operator-(const float2 &a)
Definition sky_math.h:97
float x
float y
friend float2 operator-(const float2 &a, const float2 &b)
Definition sky_math.h:92
float2(const float(*ptr)[2])
Definition sky_math.h:49
friend float2 operator*(const float2 &a, float b)
Definition sky_math.h:67
float2(const float *ptr)
Definition sky_math.h:47
float2(int value)
Definition sky_math.h:53
float2(float value)
Definition sky_math.h:51
float2()=default
friend float2 operator+(const float2 &a, const float2 &b)
Definition sky_math.h:117
friend float2 operator/(float b, const float2 &a)
Definition sky_math.h:82
friend float2 operator*(const float2 &a, const float2 &b)
Definition sky_math.h:128
void operator+=(const float2 &b)
Definition sky_math.h:122
friend float2 operator*(float b, const float2 &a)
Definition sky_math.h:72
float2(float x, float y)
Definition sky_math.h:55
friend float2 operator/(const float2 &a, float b)
Definition sky_math.h:77
static float distance(const float2 &a, const float2 &b)
Definition sky_math.h:112
friend float2 operator/(const float2 &a, const float2 &b)
Definition sky_math.h:87
float length_squared() const
Definition sky_math.h:102
float length() const
Definition sky_math.h:107
friend float3 operator*(const float3 &a, float b)
Definition sky_math.h:160
friend float3 operator-(const float3 &a, const float3 &b)
Definition sky_math.h:180
float3(const float *ptr)
Definition sky_math.h:140
float3(float x, float y, float z)
Definition sky_math.h:148
float3(int value)
Definition sky_math.h:146
void operator+=(const float3 &b)
Definition sky_math.h:210
float length_squared() const
Definition sky_math.h:190
friend float3 operator+(const float3 &a, const float3 &b)
Definition sky_math.h:205
float3(const float(*ptr)[3])
Definition sky_math.h:142
float length() const
Definition sky_math.h:195
friend float3 operator/(const float3 &a, float b)
Definition sky_math.h:170
float z
Definition sky_math.h:136
float3()=default
float y
Definition sky_math.h:136
float3(float value)
Definition sky_math.h:144
friend float3 operator/(float b, const float3 &a)
Definition sky_math.h:175
static float distance(const float3 &a, const float3 &b)
Definition sky_math.h:200
friend float3 operator*(const float3 &a, const float3 &b)
Definition sky_math.h:217
float x
Definition sky_math.h:136
friend float3 operator-(const float3 &a)
Definition sky_math.h:185
friend float3 operator*(float b, const float3 &a)
Definition sky_math.h:165
void operator+=(const float4 &b)
Definition sky_math.h:309
friend float4 operator-(const float4 &a)
Definition sky_math.h:284
friend float4 operator/(const float4 &a, float b)
Definition sky_math.h:259
float4(float value)
Definition sky_math.h:233
float4(int value)
Definition sky_math.h:235
float4()=default
friend float4 operator/(const float4 &a, const float4 &b)
Definition sky_math.h:274
void operator*=(const float4 &b)
Definition sky_math.h:317
float y
Definition sky_math.h:225
static float distance(const float4 &a, const float4 &b)
Definition sky_math.h:299
float z
Definition sky_math.h:225
friend float4 operator-(const float4 &a, const float4 &b)
Definition sky_math.h:279
friend float4 operator/(float b, const float4 &a)
Definition sky_math.h:264
float length_squared() const
Definition sky_math.h:289
friend float4 operator*(const float4 &a, const float4 &b)
Definition sky_math.h:269
float4(float x, float y, float z, float w)
Definition sky_math.h:237
float length() const
Definition sky_math.h:294
float x
Definition sky_math.h:225
float4(const float *ptr)
Definition sky_math.h:229
friend float4 operator+(const float4 &a, const float4 &b)
Definition sky_math.h:304
friend float4 operator*(float b, const float4 &a)
Definition sky_math.h:254
float w
Definition sky_math.h:225
float4(const float(*ptr)[4])
Definition sky_math.h:231
friend float4 operator*(const float4 &a, float b)
Definition sky_math.h:249
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
uint len
PointerRNA * ptr
Definition wm_files.cc:4238