OGLplus  (0.59.0) a C++ wrapper for rendering APIs

functions.hpp
Go to the documentation of this file.
1 #ifndef EAGINE_MATH_FUNCTIONS_HPP
9 #define EAGINE_MATH_FUNCTIONS_HPP
10 
11 #include "../memory/span.hpp"
12 #include "../valid_if/decl.hpp"
13 #include "constants.hpp"
14 #include <cassert>
15 #include <cmath>
16 #include <type_traits>
17 
18 namespace eagine::math {
19 //------------------------------------------------------------------------------
22 template <typename T>
23 static constexpr auto is_positive_power_of_2(T value) noexcept
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);
27 }
28 //------------------------------------------------------------------------------
31 template <typename T>
32 static constexpr auto greatest_common_divisor(T l, T r) noexcept
33  -> std::enable_if_t<std::is_integral_v<T>, T> {
34  return (r == T(0)) ? l : greatest_common_divisor(r, T(l % r));
35 }
36 //------------------------------------------------------------------------------
39 template <typename T>
40 static constexpr auto signum(T x) noexcept {
41  return (x < 0) ? T(-1) : T(1);
42 }
43 //------------------------------------------------------------------------------
46 template <typename T>
47 static constexpr auto minimum(T a, T b) noexcept {
48  return a < b ? a : b;
49 }
50 //------------------------------------------------------------------------------
53 template <typename T, typename... P>
54 static constexpr auto minimum(T a, T b, T c, P... d) noexcept {
55  return minimum(minimum(a, b), c, d...);
56 }
57 //------------------------------------------------------------------------------
60 template <typename T>
61 static constexpr auto maximum(T a, T b) noexcept {
62  return a > b ? a : b;
63 }
64 //------------------------------------------------------------------------------
67 template <typename T, typename... P>
68 static constexpr auto maximum(T a, T b, T c, P... d) noexcept {
69  return maximum(maximum(a, b), c, d...);
70 }
71 //------------------------------------------------------------------------------
74 template <typename T>
75 static constexpr auto ratio(T a, T b) noexcept -> optionally_valid<T> {
76  if(b > T(0) || (b < T(0))) {
77  return {a / b, true};
78  }
79  return {};
80 }
81 //------------------------------------------------------------------------------
84 template <typename T>
85 static constexpr auto reciprocal(T x) noexcept -> optionally_valid<T> {
86  using std::abs;
87  if(abs(x) > std::numeric_limits<T>::epsilon()) {
88  return {T(1) / x, true};
89  }
90  return {};
91 }
92 //------------------------------------------------------------------------------
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;
98 }
99 //------------------------------------------------------------------------------
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);
105 }
106 //------------------------------------------------------------------------------
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);
112 }
113 //------------------------------------------------------------------------------
116 template <typename T>
117 static constexpr auto inverse_logistic(T x) noexcept {
118  using std::log;
119  return log(x) - log(1 - x);
120 }
121 //------------------------------------------------------------------------------
124 template <typename T>
125 static constexpr auto logistic(T x) noexcept {
126  using std::exp;
127  return 1 / (1 + exp(-x));
128 }
129 //------------------------------------------------------------------------------
133 template <typename T, typename C>
134 static constexpr auto sigmoid01(T x, C c) noexcept {
135  return logistic(c * inverse_logistic(x));
136 }
137 //------------------------------------------------------------------------------
141 template <typename T>
142 static constexpr auto sigmoid01(T x) noexcept {
143  return sigmoid01(x, 2);
144 }
145 //------------------------------------------------------------------------------
149 template <typename T>
150 static inline auto sine_sigmoid01(T x) {
151 
152 #ifdef __clang__
153  EAGINE_DIAG_PUSH()
154  EAGINE_DIAG_OFF(double-promotion)
155 #endif
156  using std::cos;
157  return (1 - cos(x * pi)) / 2;
158 #ifdef __clang__
159  EAGINE_DIAG_POP()
160 #endif
161 }
162 //------------------------------------------------------------------------------
166 template <typename T>
167 static constexpr auto sine_wave01(T x) noexcept {
168  using std::sin;
169  return (sin(2 * pi * x) + 1) / 2;
170 }
171 //------------------------------------------------------------------------------
175 template <typename T>
176 static constexpr auto cosine_wave01(T x) noexcept {
177  using std::cos;
178  return (cos(2 * pi * x) + 1) / 2;
179 }
180 //------------------------------------------------------------------------------
183 template <typename T, typename U = T>
184 static constexpr auto saw(T x, U u = T(1)) noexcept {
185  using std::fmod;
186  return fmod(x, u);
187 }
188 //------------------------------------------------------------------------------
191 template <typename T>
192 static constexpr auto factorial(T n) noexcept
193  -> std::enable_if_t<std::is_integral_v<T>, T> {
194  return n > 0 ? n * factorial(n - 1) : 1;
195 }
196 //------------------------------------------------------------------------------
199 template <typename T>
200 static constexpr auto binomial(T n, T k) noexcept
201  -> std::enable_if_t<std::is_integral_v<T>, T> {
202  return ((n >= 0) && (k >= 0) && (k <= n))
203  ? (factorial(n) / (factorial(k) * factorial(n - k)))
204  : 0;
205 }
206 //------------------------------------------------------------------------------
213 template <typename Type, typename Parameter, int N>
214 struct bezier_t {
215 private:
216  static constexpr auto _coef(int m, int i, Parameter t) noexcept {
217  using std::pow;
218  return binomial(m, i) * pow(t, i) * pow(1 - t, m - i);
219  }
220 
221  static constexpr auto _calc(int, int, Parameter) noexcept {
222  return Type{0};
223  }
224 
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...);
229  }
230 
231  template <typename P, typename S, std::size_t... I>
232  static constexpr auto _calc(
233  int m,
234  int i,
235  Parameter t,
237  std::index_sequence<I...>) noexcept -> Type {
238  return _calc(m, i, t, p[I]...);
239  }
240 
241 public:
243  template <
244  typename... Points,
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)...);
248  }
249 
251  template <typename P, typename S>
253  const noexcept {
254  return _calc(N - 1, 0, t, p, std::make_index_sequence<N>());
255  }
256 };
257 //------------------------------------------------------------------------------
258 } // namespace eagine::math
259 
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

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).