Blender V4.5
math_float3.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2013 Intel Corporation
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#pragma once
7
8#include "util/math_base.h"
9#include "util/math_float4.h"
10#include "util/types_float3.h"
11#include "util/types_float4.h"
12
14
16{
17#ifdef __KERNEL_SSE__
18 return float3(_mm_setzero_ps());
19#else
20 return make_float3(0.0f, 0.0f, 0.0f);
21#endif
22}
23
25{
26 return make_float3(1.0f, 1.0f, 1.0f);
27}
28
33
35{
36#ifdef __KERNEL_SSE__
37 /* Don't use _mm_rcp_ps due to poor precision. */
38 return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
39#else
40 return make_float3(1.0f / a.x, 1.0f / a.y, 1.0f / a.z);
41#endif
42}
43
44#ifndef __KERNEL_METAL__
45
47{
48# ifdef __KERNEL_SSE__
49 return float3(_mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
50# else
51 return make_float3(-a.x, -a.y, -a.z);
52# endif
53}
54
56{
57# ifdef __KERNEL_SSE__
58 return float3(_mm_mul_ps(a.m128, b.m128));
59# else
60 return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
61# endif
62}
63
65{
66# ifdef __KERNEL_SSE__
67 return float3(_mm_mul_ps(a.m128, _mm_set1_ps(f)));
68# else
69 return make_float3(a.x * f, a.y * f, a.z * f);
70# endif
71}
72
74{
75# if defined(__KERNEL_SSE__)
76 return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
77# else
78 return make_float3(a.x * f, a.y * f, a.z * f);
79# endif
80}
81
83{
84# if defined(__KERNEL_SSE__)
85 return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
86# else
87 return make_float3(f / a.x, f / a.y, f / a.z);
88# endif
89}
90
92{
93# if defined(__KERNEL_SSE__)
94 return float3(_mm_div_ps(a.m128, _mm_set1_ps(f)));
95# else
96 float invf = 1.0f / f;
97 return make_float3(a.x * invf, a.y * invf, a.z * invf);
98# endif
99}
100
102{
103# if defined(__KERNEL_SSE__)
104 return float3(_mm_div_ps(a.m128, b.m128));
105# else
106 return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
107# endif
108}
109
111{
112# ifdef __KERNEL_SSE__
113 return float3(_mm_add_ps(a.m128, b.m128));
114# else
115 return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
116# endif
117}
118
120{
121 return a + make_float3(f);
122}
123
125{
126# ifdef __KERNEL_SSE__
127 return float3(_mm_sub_ps(a.m128, b.m128));
128# else
129 return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
130# endif
131}
132
134{
135 return a - make_float3(f);
136}
137
139{
140 return a = a + b;
141}
142
144{
145 return a = a - b;
146}
147
149{
150 return a = a * b;
151}
152
154{
155 return a = a * f;
156}
157
159{
160 return a = a / b;
161}
162
164{
165 const float invf = 1.0f / f;
166 return a = a * invf;
167}
168
169# if !(defined(__KERNEL_CUDA__) || defined(__KERNEL_HIP__) || defined(__KERNEL_ONEAPI__))
171{
172 a = float3(a) * b;
173 return a;
174}
175
177{
178 a = float3(a) * f;
179 return a;
180}
181
183{
184 a = float3(a) / b;
185 return a;
186}
187
189{
190 a = float3(a) / f;
191 return a;
192}
193# endif
194
196{
197# ifdef __KERNEL_SSE__
198 return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
199# else
200 return (a.x == b.x && a.y == b.y && a.z == b.z);
201# endif
202}
203
205{
206 return !(a == b);
207}
208
210{
211# ifdef __KERNEL_SSE__
212 return int3(_mm_castps_si128(_mm_cmpge_ps(a.m128, b.m128)));
213# else
214 return make_int3(a.x >= b.x, a.y >= b.y, a.z >= b.z);
215# endif
216}
217
218ccl_device_inline float dot(const float3 a, const float3 b)
219{
220# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
221 return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
222# else
223 return a.x * b.x + a.y * b.y + a.z * b.z;
224# endif
225}
226
227#endif
228
230{
231#if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
232 return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a, b), b));
233#else
234 return a.x * b.x + a.y * b.y;
235#endif
236}
237
239{
240#if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
241 return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(a.m128, a.m128, 0x7F)));
242#else
243 return sqrtf(dot(a, a));
244#endif
245}
246
248{
249 return min(min(a.x, a.y), a.z);
250}
251
253{
254 return max(max(a.x, a.y), a.z);
255}
256
258{
259 return dot(a, a);
260}
261
262#ifndef __KERNEL_METAL__
263
265{
266 return len(a - b);
267}
268
270{
271# ifdef __KERNEL_SSE__
272 const float4 x = float4(a.m128);
273 const float4 y = shuffle<1, 2, 0, 3>(float4(b.m128));
274 const float4 z = float4(_mm_mul_ps(shuffle<1, 2, 0, 3>(float4(a.m128)), float4(b.m128)));
275
276 return float3(shuffle<1, 2, 0, 3>(msub(x, y, z)).m128);
277# else
278 return make_float3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
279# endif
280}
281
283{
284# if defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
285 const __m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
286 return float3(_mm_div_ps(a.m128, norm));
287# else
288 return a / len(a);
289# endif
290}
291
293{
294# ifdef __KERNEL_SSE__
295 return float3(_mm_min_ps(a.m128, b.m128));
296# else
297 return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
298# endif
299}
300
302{
303# ifdef __KERNEL_SSE__
304 return float3(_mm_max_ps(a.m128, b.m128));
305# else
306 return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
307# endif
308}
309
310ccl_device_inline float3 clamp(const float3 a, const float3 mn, const float3 mx)
311{
312 return min(max(a, mn), mx);
313}
314
316{
317# ifdef __KERNEL_SSE__
318# ifdef __KERNEL_NEON__
319 return float3(vabsq_f32(a.m128));
320# else
321 __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
322 return float3(_mm_and_ps(a.m128, mask));
323# endif
324# else
325 return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
326# endif
327}
328
329/* The floating-point remainder of the division operation a / b calculated by this function is
330 * exactly the value a - iquot * b, where iquot is a / b with its fractional part truncated.
331 *
332 * The returned value has the same sign as a and is less than b in magnitude. */
333ccl_device_inline float3 fmod(const float3 a, const float b)
334{
335# if defined(__KERNEL_NEON__)
336 /* Use native Neon instructions.
337 * The logic is the same as the SSE code below, but on Apple M2 Ultra this seems to be faster.
338 * Possibly due to some runtime checks in _mm_round_ps which do not get properly inlined. */
339 const float32x4_t iquot = vrndq_f32(a / b);
340 return float3(vsubq_f32(a, vmulq_f32(iquot, vdupq_n_f32(b))));
341# elif defined(__KERNEL_SSE42__) && defined(__KERNEL_SSE__)
342 const __m128 iquot = _mm_round_ps(a / b, _MM_FROUND_TRUNC);
343 return float3(_mm_sub_ps(a, _mm_mul_ps(iquot, _mm_set1_ps(b))));
344# else
345 return make_float3(fmodf(a.x, b), fmodf(a.y, b), fmodf(a.z, b));
346# endif
347}
348
350{
351# ifdef __KERNEL_SSE__
352 return float3(_mm_sqrt_ps(a));
353# else
354 return make_float3(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z));
355# endif
356}
357
359{
360# ifdef __KERNEL_SSE__
361 return float3(_mm_floor_ps(a));
362# else
363 return make_float3(floorf(a.x), floorf(a.y), floorf(a.z));
364# endif
365}
366
368{
369# ifdef __KERNEL_SSE__
370 return float3(_mm_ceil_ps(a));
371# else
372 return make_float3(ceilf(a.x), ceilf(a.y), ceilf(a.z));
373# endif
374}
375
376ccl_device_inline float3 mix(const float3 a, const float3 b, const float t)
377{
378 return a + t * (b - a);
379}
380
385
387{
388 return make_float3(expf(v.x), expf(v.y), expf(v.z));
389}
390
392{
393 return make_float3(logf(v.x), logf(v.y), logf(v.z));
394}
395
397{
398 return make_float3(cosf(v.x), cosf(v.y), cosf(v.z));
399}
400
401ccl_device_inline float3 reflect(const float3 incident, const float3 unit_normal)
402{
403 return incident - 2.0f * unit_normal * dot(incident, unit_normal);
404}
405
406ccl_device_inline float3 refract(const float3 incident, const float3 normal, const float eta)
407{
408 const float k = 1.0f - eta * eta * (1.0f - dot(normal, incident) * dot(normal, incident));
409 if (k < 0.0f) {
410 return zero_float3();
411 }
412 return eta * incident - (eta * dot(normal, incident) + sqrt(k)) * normal;
413}
414
416 const float3 incident,
417 const float3 reference)
418{
419 return (dot(reference, incident) < 0.0f) ? vector : -vector;
420}
421#endif
422
424{
425 const float len_squared = dot(v_proj, v_proj);
426 return (len_squared != 0.0f) ? (dot(v, v_proj) / len_squared) * v_proj : zero_float3();
427}
428
430{
431 *t = len(a);
432 const float x = 1.0f / *t;
433 return a * x;
434}
435
437{
438 const float t = len(a);
439 return (t != 0.0f) ? a * (1.0f / t) : a;
440}
441
443{
444 const float t = len(a);
445 return (t != 0.0f) ? a * (1.0f / t) : fallback;
446}
447
449{
450 *t = len(a);
451 return (*t != 0.0f) ? a / (*t) : a;
452}
453
455{
456 return make_float3((b.x != 0.0f) ? a.x / b.x : 0.0f,
457 (b.y != 0.0f) ? a.y / b.y : 0.0f,
458 (b.z != 0.0f) ? a.z / b.z : 0.0f);
459}
460
462{
463 return (b != 0.0f) ? a / b : zero_float3();
464}
465
466ccl_device_inline float3 interp(const float3 a, const float3 b, const float t)
467{
468 return a + t * (b - a);
469}
470
472{
473 return a * a;
474}
475
477{
478#ifdef __KERNEL_SSE__
479 return a == make_float3(0.0f);
480#else
481 return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f);
482#endif
483}
484
486{
487#if defined(__KERNEL_SSE__) && defined(__KERNEL_NEON__)
488 __m128 t = a.m128;
489 t = vsetq_lane_f32(0.0f, t, 3);
490 return vaddvq_f32(t);
491#else
492 return (a.x + a.y + a.z);
493#endif
494}
495
497{
498 return reduce_add(a) * (1.0f / 3.0f);
499}
500
502{
503#if defined(__KERNEL_METAL__)
504 return all(a == b);
505#else
506 return a == b;
507#endif
508}
509
510template<class MaskType>
511ccl_device_inline float3 select(const MaskType mask, const float3 a, const float3 b)
512{
513#if defined(__KERNEL_METAL__)
514 return metal::select(b, a, bool3(mask));
515#elif defined(__KERNEL_SSE__)
516# ifdef __KERNEL_SSE42__
517 return float3(_mm_blendv_ps(b.m128, a.m128, _mm_castsi128_ps(mask.m128)));
518# else
519 return float4(
520 _mm_or_ps(_mm_and_ps(_mm_castsi128_ps(mask), a), _mm_andnot_ps(_mm_castsi128_ps(mask), b)));
521# endif
522#else
523 return make_float3((mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z);
524#endif
525}
526
527template<class MaskType> ccl_device_inline float3 mask(const MaskType mask, const float3 a)
528{
529 /* Replace elements of x with zero where mask isn't set. */
530 return select(mask, a, zero_float3());
531}
532
533/* Consistent name for this would be pow, but HIP compiler crashes in name mangling. */
535{
536 return make_float3(powf(v.x, e), powf(v.y, e), powf(v.z, e));
537}
538
540{
541 return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z);
542}
543
545{
546 float3 r = v;
547 if (!isfinite_safe(r.x)) {
548 r.x = 0.0f;
549 }
550 if (!isfinite_safe(r.y)) {
551 r.y = 0.0f;
552 }
553 if (!isfinite_safe(r.z)) {
554 r.z = 0.0f;
555 }
556 return r;
557}
558
559/* Triangle */
560
562 const ccl_private float3 &v2,
563 const ccl_private float3 &v3)
564{
565 return len(cross(v3 - v2, v1 - v2)) * 0.5f;
566}
567
568/* Orthonormal vectors */
569
573{
574#if 0
575 if (fabsf(N.y) >= 0.999f) {
576 *a = make_float3(1, 0, 0);
577 *b = make_float3(0, 0, 1);
578 return;
579 }
580 if (fabsf(N.z) >= 0.999f) {
581 *a = make_float3(1, 0, 0);
582 *b = make_float3(0, 1, 0);
583 return;
584 }
585#endif
586
587 if (N.x != N.y || N.x != N.z) {
588 *a = make_float3(N.z - N.y, N.x - N.z, N.y - N.x); //(1,1,1)x N
589 }
590 else {
591 *a = make_float3(N.z - N.y, N.x + N.z, -N.y - N.x); //(-1,1,1)x N
592 }
593
594 *a = normalize(*a);
595 *b = cross(N, *a);
596}
597
598/* Rotation of point around axis and angle */
599
601{
602 const float costheta = cosf(angle);
603 const float sintheta = sinf(angle);
604 float3 r;
605
606 r.x = ((costheta + (1 - costheta) * axis.x * axis.x) * p.x) +
607 (((1 - costheta) * axis.x * axis.y - axis.z * sintheta) * p.y) +
608 (((1 - costheta) * axis.x * axis.z + axis.y * sintheta) * p.z);
609
610 r.y = (((1 - costheta) * axis.x * axis.y + axis.z * sintheta) * p.x) +
611 ((costheta + (1 - costheta) * axis.y * axis.y) * p.y) +
612 (((1 - costheta) * axis.y * axis.z - axis.x * sintheta) * p.z);
613
614 r.z = (((1 - costheta) * axis.x * axis.z - axis.y * sintheta) * p.x) +
615 (((1 - costheta) * axis.y * axis.z + axis.x * sintheta) * p.y) +
616 ((costheta + (1 - costheta) * axis.z * axis.z) * p.z);
617
618 return r;
619}
620
621/* Calculate the angle between the two vectors a and b.
622 * The usual approach `acos(dot(a, b))` has severe precision issues for small angles,
623 * which are avoided by this method.
624 * Based on "Mangled Angles" from https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf
625 */
627{
628 return 2.0f * atan2f(len(a - b), len(a + b));
629}
630
631/* Tangent of the angle between vectors a and b. */
633{
634 return len(cross(a, b)) / dot(a, b);
635}
636
637/* projections */
639{
640 float len;
641 float u;
642 float v;
643 len = sqrtf(co.x * co.x + co.y * co.y);
644 if (len > 0.0f) {
645 u = (1.0f - (atan2f(co.x / len, co.y / len) / M_PI_F)) * 0.5f;
646 v = (co.z + 1.0f) * 0.5f;
647 }
648 else {
649 u = v = 0.0f;
650 }
651 return make_float2(u, v);
652}
653
655{
656 const float l = dot(co, co);
657 float u;
658 float v;
659 if (l > 0.0f) {
660 if (UNLIKELY(co.x == 0.0f && co.y == 0.0f)) {
661 u = 0.0f; /* Otherwise domain error. */
662 }
663 else {
664 u = (0.5f - atan2f(co.x, co.y) * M_1_2PI_F);
665 }
666 v = 1.0f - safe_acosf(co.z / sqrtf(l)) * M_1_PI_F;
667 }
668 else {
669 u = v = 0.0f;
670 }
671 return make_float2(u, v);
672}
673
MINLINE float safe_acosf(float a)
#define UNLIKELY(x)
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
#define ccl_private
#define ccl_device_inline
#define M_1_PI_F
#define M_1_2PI_F
#define ccl_device_template_spec
#define M_PI_F
#define logf(x)
#define sinf(x)
#define cosf(x)
#define expf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
#define saturatef(x)
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define atan2f(x, y)
#define ceilf(x)
#define fmodf(x, y)
#define floorf(x)
ccl_device_forceinline float2 make_float2(const float x, const float y)
ccl_device_forceinline int3 make_int3(const int x, const int y, const int z)
#define fabsf(x)
#define sqrtf(x)
#define log
#define exp
VecBase< float, 4 > float4
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
VecBase< int, 3 > int3
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< bool, 3 > bool3
VecBase< T, D > faceforward(VecOp< T, D >, VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, 3 > float3
#define select(A, B, C)
#define floor
#define ceil
bool all(VecOp< bool, D >) RET
#define sqrt
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
#define mix(a, b, c)
Definition hash.h:35
ccl_device_inline float len_squared(const float2 a)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline float3 safe_normalize(const float3 a)
ccl_device_inline float3 reciprocal(const float3 a)
Definition math_float3.h:34
ccl_device_inline bool is_zero(const float3 a)
ccl_device_inline float precise_angle(const float3 a, const float3 b)
ccl_device_inline float3 operator*(const float3 a, const float3 b)
Definition math_float3.h:55
ccl_device_inline bool isequal(const float3 a, const float3 b)
ccl_device_inline float3 power(const float3 v, const float e)
ccl_device_inline float3 safe_normalize_fallback(const float3 a, const float3 fallback)
ccl_device_inline float3 refract(const float3 incident, const float3 normal, const float eta)
ccl_device_inline int3 operator>=(const float3 a, const float3 b)
ccl_device_inline float3 one_float3()
Definition math_float3.h:24
ccl_device_inline float3 ensure_finite(const float3 v)
ccl_device_inline float3 normalize_len(const float3 a, ccl_private float *t)
ccl_device_inline float3 clamp(const float3 a, const float3 mn, const float3 mx)
ccl_device_inline float3 project(const float3 v, const float3 v_proj)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:15
ccl_device_template_spec float3 make_zero()
Definition math_float3.h:29
ccl_device_inline float3 operator/=(float3 &a, const float3 b)
ccl_device_inline float3 interp(const float3 a, const float3 b, const float t)
ccl_device_inline float dot_xy(const float3 a, const float3 b)
ccl_device_inline float3 fmod(const float3 a, const float b)
ccl_device_inline float3 fabs(const float3 a)
ccl_device_inline float reduce_add(const float3 a)
ccl_device_inline float3 rotate_around_axis(const float3 p, const float3 axis, const float angle)
ccl_device_inline float2 map_to_sphere(const float3 co)
ccl_device_inline bool isfinite_safe(const float3 v)
ccl_device_inline float3 mask(const MaskType mask, const float3 a)
ccl_device_inline float reduce_min(const float3 a)
ccl_device_inline float3 operator+(const float3 a, const float3 b)
ccl_device_inline float average(const float3 a)
ccl_device_inline float2 map_to_tube(const float3 co)
ccl_device_inline float3 operator*=(float3 &a, const float3 b)
ccl_device_inline float3 operator/(const float f, const float3 a)
Definition math_float3.h:82
ccl_device_inline bool operator==(const float3 a, const float3 b)
ccl_device_inline float distance(const float3 a, const float3 b)
ccl_device_inline float3 safe_divide(const float3 a, const float3 b)
ccl_device_inline float triangle_area(const ccl_private float3 &v1, const ccl_private float3 &v2, const ccl_private float3 &v3)
ccl_device_inline float3 operator-(const float3 &a)
Definition math_float3.h:46
ccl_device_inline float reduce_max(const float3 a)
ccl_device_inline float3 safe_normalize_len(const float3 a, ccl_private float *t)
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
ccl_device_inline float3 operator-=(float3 &a, const float3 b)
ccl_device_inline bool operator!=(const float3 a, const float3 b)
ccl_device_inline float3 operator+=(float3 &a, const float3 b)
ccl_device_inline float tan_angle(const float3 a, const float3 b)
ccl_device_inline float dot(const float3 a, const float3 b)
ccl_device_inline float len_squared(const float3 a)
ccl_device_inline float3 sqr(const float3 a)
ccl_device_inline float4 msub(const float4 a, const float4 b, const float4 c)
#define N
#define saturate(a)
Definition smaa.cc:315
#define min(a, b)
Definition sort.cc:36
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
max
Definition text_draw.cc:251
uint len