Go to the documentation of this file. 1 #ifndef EAGINE_MATH_FUNCTIONS_HPP
9 #define EAGINE_MATH_FUNCTIONS_HPP
11 #include "../memory/span.hpp"
12 #include "../valid_if/decl.hpp"
16 #include <type_traits>
24 -> std::enable_if_t<std::is_integral_v<T>,
bool> {
25 using U = std::make_unsigned_t<T>;
26 return (value > 0) && ((U(value) & (U(value) - 1)) == 0);
33 -> std::enable_if_t<std::is_integral_v<T>, T> {
40 static constexpr
auto signum(T x) noexcept {
41 return (x < 0) ? T(-1) : T(1);
47 static constexpr
auto minimum(T a, T b) noexcept {
53 template <
typename T,
typename... P>
54 static constexpr
auto minimum(T a, T b, T c, P... d) noexcept {
61 static constexpr
auto maximum(T a, T b) noexcept {
67 template <
typename T,
typename... P>
68 static constexpr
auto maximum(T a, T b, T c, P... d) noexcept {
76 if(b > T(0) || (b < T(0))) {
87 if(abs(x) > std::numeric_limits<T>::epsilon()) {
88 return {T(1) / x,
true};
95 template <
typename T,
typename Min,
typename Max>
96 static constexpr
auto clamp(T x, Min min, Max max) noexcept {
97 return x < T(min) ? T(min) : x > T(max) ? T(max) : x;
102 template <
typename T,
typename S,
typename E>
103 static constexpr
auto ramp(T x, S start, E end) noexcept {
104 return (
clamp(x, start, end) - start) / (end - start);
109 template <
typename T,
typename A>
110 static constexpr
auto blend(T v1, T v2, A alpha) noexcept {
111 return v1 * alpha + v2 * (1 - alpha);
116 template <
typename T>
119 return log(x) - log(1 - x);
124 template <
typename T>
127 return 1 / (1 + exp(-x));
133 template <
typename T,
typename C>
141 template <
typename T>
149 template <
typename T>
154 EAGINE_DIAG_OFF(
double-promotion)
157 return (1 - cos(x *
pi)) / 2;
166 template <
typename T>
169 return (sin(2 *
pi * x) + 1) / 2;
175 template <
typename T>
178 return (cos(2 *
pi * x) + 1) / 2;
183 template <
typename T,
typename U = T>
184 static constexpr
auto saw(T x, U u = T(1)) noexcept {
191 template <
typename T>
193 -> std::enable_if_t<std::is_integral_v<T>, T> {
199 template <
typename T>
201 -> std::enable_if_t<std::is_integral_v<T>, T> {
202 return ((n >= 0) && (k >= 0) && (k <= n))
213 template <
typename Type,
typename Parameter,
int N>
216 static constexpr
auto _coef(
int m,
int i, Parameter t) noexcept {
218 return binomial(m, i) * pow(t, i) * pow(1 - t, m - i);
221 static constexpr
auto _calc(
int,
int, Parameter) noexcept {
225 template <
typename... P>
226 static constexpr
auto
227 _calc(
int m,
int i, Parameter t, Type first, P... rest) noexcept -> Type {
228 return first * _coef(m, i, t) + _calc(m, i + 1, t, rest...);
231 template <
typename P,
typename S, std::size_t... I>
232 static constexpr
auto _calc(
237 std::index_sequence<I...>) noexcept -> Type {
238 return _calc(m, i, t, p[I]...);
245 typename = std::enable_if_t<
sizeof...(Points) == N>>
246 constexpr
auto operator()(Parameter t, Points&&... p)
const noexcept {
247 return _calc(N - 1, 0, t, std::forward<Points>(p)...);
251 template <
typename P,
typename S>
254 return _calc(N - 1, 0, t, p, std::make_index_sequence<N>());
260 #endif // EAGINE_MATH_FUNCTIONS_HPP
static constexpr auto clamp(T x, Min min, Max max) noexcept
Clamps x to be between min and max.
Definition: functions.hpp:96
Primary template for conditionally valid values.
Definition: decl.hpp:49
static constexpr auto reciprocal(T x) noexcept -> optionally_valid< T >
Returns the reciprocal of x if x is not zero.
Definition: functions.hpp:85
static constexpr auto ramp(T x, S start, E end) noexcept
Normalizes x to (0, 1), where start = 0 and end = 1.
Definition: functions.hpp:103
Helper class for bezier curve segment calculations.
Definition: functions.hpp:214
static constexpr const auto pi
The pi constant.
Definition: constants.hpp:23
static constexpr auto factorial(T n) noexcept -> std::enable_if_t< std::is_integral_v< T >, T >
Calculates factorial of n.
Definition: functions.hpp:192
static constexpr auto sine_wave01(T x) noexcept
Calculates sine of x, mapped to interval (0, 1).
Definition: functions.hpp:167
static auto sine_sigmoid01(T x)
Calculates goniometric sigmoid (cos in interval (0, 1)) of x.
Definition: functions.hpp:150
static constexpr auto sigmoid01(T x, C c) noexcept
Calculates the sigmoid of x. The value c controls steepness.
Definition: functions.hpp:134
static constexpr auto binomial(T n, T k) noexcept -> std::enable_if_t< std::is_integral_v< T >, T >
Calculates binomial coefficient of n over k.
Definition: functions.hpp:200
Non-owning view of a contiguous range of memory with ValueType elements.
Definition: flatten_fwd.hpp:16
static constexpr auto ratio(T a, T b) noexcept -> optionally_valid< T >
Returns a divided by b if b is not zero.
Definition: functions.hpp:75
Math-related code is placed in this namespace.
Definition: eagine.hpp:48
static constexpr auto maximum(T a, T b) noexcept
Returns the maximum value of a and b.
Definition: functions.hpp:61
static constexpr auto inverse_logistic(T x) noexcept
Calculates the inverse logistic (log(x) - log(1 - x)) of x.
Definition: functions.hpp:117
static constexpr auto blend(T v1, T v2, A alpha) noexcept
Blends v1 and v2, using alpha as the blending factor.
Definition: functions.hpp:110
static constexpr auto cosine_wave01(T x) noexcept
Calculates cosine of x, mapped to interval (0, 1).
Definition: functions.hpp:176
static constexpr auto logistic(T x) noexcept
Calculates the logistic (1 / (1 + exp(-x))) of x.
Definition: functions.hpp:125
auto operator()(Parameter t, memory::basic_span< const Type, P, S > p) const noexcept
Interpolate from control points in span p at position t.
Definition: functions.hpp:252
static constexpr auto greatest_common_divisor(T l, T r) noexcept -> std::enable_if_t< std::is_integral_v< T >, T >
Returns the greates common divisor of arguments l and r.
Definition: functions.hpp:32
static constexpr auto saw(T x, U u=T(1)) noexcept
Calculates floating-point modulo of x in intervals of u.
Definition: functions.hpp:184
static constexpr auto minimum(T a, T b) noexcept
Returns the minimum value of a and b.
Definition: functions.hpp:47
static constexpr auto is_positive_power_of_2(T value) noexcept -> std::enable_if_t< std::is_integral_v< T >, bool >
Indicates if value is a positive integral power of two.
Definition: functions.hpp:23
static constexpr auto signum(T x) noexcept
Returns 1 if x is non-negative, returns -1 otherwise.
Definition: functions.hpp:40