Blender V4.5
BLI_math_base.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <algorithm>
12#include <cmath>
13#include <type_traits>
14
15#include "BLI_math_numbers.hh"
16#include "BLI_utildefines.h"
17
18namespace blender::math {
19
20template<typename T> inline constexpr bool is_math_float_type = std::is_floating_point_v<T>;
21template<typename T> inline constexpr bool is_math_integral_type = std::is_integral_v<T>;
22
23template<typename T> inline bool is_zero(const T &a)
24{
25 return a == T(0);
26}
27
28template<typename T> inline bool is_any_zero(const T &a)
29{
30 return is_zero(a);
31}
32
33template<typename T> inline T abs(const T &a)
34{
35 return std::abs(a);
36}
37
38template<typename T> inline T sign(const T &a)
39{
40 return (T(0) < a) - (a < T(0));
41}
42
43template<typename T> inline T min(const T &a, const T &b)
44{
45 static_assert(std::is_arithmetic_v<T>, "math::min on non-arithmetic type is likely unintended");
46 return std::min(a, b);
47}
48
49template<typename T> inline T max(const T &a, const T &b)
50{
51 static_assert(std::is_arithmetic_v<T>, "math::max on non-arithmetic type is likely unintended");
52 return std::max(a, b);
53}
54
55template<typename T> inline void max_inplace(T &a, const T &b)
56{
57 static_assert(std::is_arithmetic_v<T>,
58 "math::max_inplace on non-arithmetic type is likely unintended");
59 a = std::max(a, b);
60}
61
62template<typename T> inline void min_inplace(T &a, const T &b)
63{
64 static_assert(std::is_arithmetic_v<T>,
65 "math::min_inplace on non-arithmetic type is likely unintended");
66 a = std::min(a, b);
67}
68
69template<typename T> inline T clamp(const T &a, const T &min, const T &max)
70{
71 return std::clamp(a, min, max);
72}
73
74template<typename T> inline T step(const T &edge, const T &value)
75{
76 return value < edge ? 0 : 1;
77}
78
79template<typename T> inline T mod(const T &a, const T &b)
80{
81 return std::fmod(a, b);
82}
83
84template<typename T> inline T safe_mod(const T &a, const T &b)
85{
86 return (b != 0) ? std::fmod(a, b) : 0;
87}
88
89template<typename T> inline void min_max(const T &value, T &min, T &max)
90{
91 static_assert(std::is_arithmetic_v<T>,
92 "math::min_max on non-arithmetic type is likely unintended");
93 min = std::min(value, min);
94 max = std::max(value, max);
95}
96
97template<typename T> inline T safe_divide(const T &a, const T &b)
98{
99 return (b != 0) ? a / b : T(0.0f);
100}
101
102template<typename T> inline T floor(const T &a)
103{
104 return std::floor(a);
105}
106
107template<typename T> inline T round(const T &a)
108{
109 return std::round(a);
110}
111
117template<typename T> inline T mod_periodic(const T &a, const T &b)
118{
119 BLI_assert(b != 0);
120 if constexpr (std::is_integral_v<T>) {
121 BLI_assert(std::numeric_limits<T>::max() - math::abs(a) >= b);
122 return ((a % b) + b) % b;
123 }
124
125 return a - (b * math::floor(a / b));
126}
127
128template<typename T> inline T ceil(const T &a)
129{
130 return std::ceil(a);
131}
132
133template<typename T> inline T distance(const T &a, const T &b)
134{
135 return std::abs(a - b);
136}
137
138template<typename T> inline T fract(const T &a)
139{
140 return a - std::floor(a);
141}
142
143template<typename T> inline T sqrt(const T &a)
144{
145 return std::sqrt(a);
146}
147
148/* Inverse value.
149 * If the input is zero the output is NaN. */
150template<typename T> inline T rcp(const T &a)
151{
152 return T(1) / a;
153}
154
155/* Inverse value.
156 * If the input is zero the output is zero. */
157template<typename T> inline T safe_rcp(const T &a)
158{
159 return a ? T(1) / a : T(0);
160}
161
162template<typename T> inline T cos(const T &a)
163{
164 return std::cos(a);
165}
166
167template<typename T> inline T sin(const T &a)
168{
169 return std::sin(a);
170}
171
172template<typename T> inline T tan(const T &a)
173{
174 return std::tan(a);
175}
176
177template<typename T> inline T acos(const T &a)
178{
179 return std::acos(a);
180}
181
182template<typename T> inline T pow(const T &x, const T &power)
183{
184 return std::pow(x, power);
185}
186
187template<typename T> inline T safe_pow(const T &x, const T &power)
188{
189 return (x < 0 || (x == 0 && power <= 0)) ? x : std::pow(x, power);
190}
191
192template<typename T> inline T fallback_pow(const T &x, const T &power, const T &fallback)
193{
194 return (x < 0 || (x == 0 && power <= 0)) ? fallback : std::pow(x, power);
195}
196
197template<typename T> inline T square(const T &a)
198{
199 return a * a;
200}
201
202template<typename T> inline T exp(const T &x)
203{
204 return std::exp(x);
205}
206
207template<typename T> inline T safe_acos(const T &a)
208{
209 if (UNLIKELY(a <= T(-1))) {
210 return T(numbers::pi);
211 }
212 if (UNLIKELY(a >= T(1))) {
213 return T(0);
214 }
215 return math::acos((a));
216}
217
219inline float safe_acos_approx(float x)
220{
221 const float f = std::abs(x);
222 /* Clamp and crush denormals. */
223 const float m = (f < 1.0f) ? 1.0f - (1.0f - f) : 1.0f;
224 /* Based on http://www.pouet.net/topic.php?which=9132&page=2
225 * 85% accurate (ULP 0)
226 * Examined 2130706434 values of `acos`:
227 * 15.2000597 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // without "denormal crush".
228 * Examined 2130706434 values of `acos`:
229 * 15.2007108 avg ULP diff, 4492 max ULP, 4.51803e-05 max error // with "denormal crush".
230 */
231 const float a = std::sqrt(1.0f - m) *
232 (1.5707963267f + m * (-0.213300989f + m * (0.077980478f + m * -0.02164095f)));
233 return x < 0.0f ? float(numbers::pi) - a : a;
234}
235
236template<typename T> inline T asin(const T &a)
237{
238 return std::asin(a);
239}
240
241template<typename T> inline T atan(const T &a)
242{
243 return std::atan(a);
244}
245
246template<typename T> inline T atan2(const T &y, const T &x)
247{
248 return std::atan2(y, x);
249}
250
251template<typename T> inline T hypot(const T &y, const T &x)
252{
253 return std::hypot(y, x);
254}
255
256template<typename T, typename FactorT>
257inline T interpolate(const T &a, const T &b, const FactorT &t)
258{
259 auto result = a * (1 - t) + b * t;
260 if constexpr (std::is_integral_v<T> && std::is_floating_point_v<FactorT>) {
261 result = std::round(result);
262 }
263 return result;
264}
265
266template<typename T> inline T midpoint(const T &a, const T &b)
267{
268 if constexpr (std::is_integral_v<T>) {
270 using Unsigned = std::make_unsigned_t<T>;
271 int sign = 1;
272 Unsigned smaller = a;
273 Unsigned larger = b;
274 if (a > b) {
275 sign = -1;
276 smaller = b;
277 larger = a;
278 }
279 return a + sign * T(Unsigned(larger - smaller) / 2);
280 }
281 else {
282 return (a + b) * T(0.5);
283 }
284}
285
286} // namespace blender::math
#define BLI_assert(a)
Definition BLI_assert.h:46
#define UNLIKELY(x)
#define atan
#define tan
#define sin
#define round
#define pow
#define exp
#define cos
#define abs
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
#define floor
#define ceil
#define sqrt
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define asin
#define acos
ccl_device_inline float2 power(const float2 v, const float e)
#define T
float safe_acos_approx(float x)
constexpr bool is_math_float_type
T clamp(const T &a, const T &min, const T &max)
T safe_rcp(const T &a)
T acos(const T &a)
bool is_any_zero(const T &a)
T safe_divide(const T &a, const T &b)
T sign(const T &a)
T floor(const T &a)
constexpr bool is_math_integral_type
T distance(const T &a, const T &b)
void min_inplace(T &a, const T &b)
T min(const T &a, const T &b)
bool is_zero(const T &a)
T midpoint(const T &a, const T &b)
T safe_mod(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
T fract(const T &a)
T safe_pow(const T &x, const T &power)
T fallback_pow(const T &x, const T &power, const T &fallback)
T atan2(const T &y, const T &x)
T rcp(const T &a)
T safe_acos(const T &a)
void min_max(const T &value, T &min, T &max)
T square(const T &a)
T mod_periodic(const T &a, const T &b)
T max(const T &a, const T &b)
T abs(const T &a)
T hypot(const T &y, const T &x)
void max_inplace(T &a, const T &b)
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251