6#ifndef EGT_DETAIL_MATH_H
7#define EGT_DETAIL_MATH_H
33 return std::acos(-T(1));
42 return std::acos(-T(0));
49 return a - std::floor(a / n) * n;
54constexpr T
angle_diff(T angle_start, T angle_stop,
bool clockwise =
true)
58 angle_stop -= angle_start;
59 if (angle_stop < 0.0f)
64 angle_stop -= angle_start;
65 return mmod(360.0f - angle_stop, 360.0f);
74 return radians * (180.0f / pi<T>());
84 return degrees * (pi<T>() / 180.0f);
89constexpr const T&
clamp(
const T& v,
const T& lo,
const T& hi)
91 return assert(!(hi < lo)),
92 (v < lo) ? lo : (hi < v) ? hi : v;
99 typename std::enable_if<std::is_floating_point<T>::value>::type* =
nullptr)
103 throw std::runtime_error(
"can't make sense of floating point value");
109 constexpr auto epsilon = 1.0e-05f;
110 if (
fabs(f1 - f2) <= epsilon)
112 return fabs(f1 - f2) <= epsilon * std::max(
fabs(f1),
fabs(f2));
118 constexpr auto epsilon = 1.0e-9;
119 if (
fabs(f1 - f2) <= epsilon)
121 return fabs(f1 - f2) <= epsilon * std::max(
fabs(f1),
fabs(f2));
135constexpr T
normalize(T value, T min, T max, T target_min, T target_max)
138 const auto r = ((value - min) / (max - min)) *
139 (target_max - target_min) + target_min;
140 assert(!std::isnan(r));
156constexpr T
normalize_to_angle(T value, T min, T max, T angle_start, T angle_stop,
bool clockwise =
true)
160 const auto a = normalize<T>(value, min, max, 0,
angle_diff(angle_start, angle_stop, clockwise));
161 assert(!std::isnan(a));
162 return clockwise ? angle_start + a : angle_start - a;
constexpr T angle_diff(T angle_start, T angle_stop, bool clockwise=true)
Compute the angle different between two angles.
Definition math.h:54
constexpr T mmod(T a, T n)
Floating point safe modulus function.
Definition math.h:47
constexpr T pi_2()
Returns the value of PI/2, accurate to the type used.
Definition math.h:40
constexpr T pi()
Returns the value of PI, accurate to the type used.
Definition math.h:31
constexpr T normalize(T value, T min, T max, T target_min, T target_max)
Normalize a value, given its min and max, to a different target min and max.
Definition math.h:135
constexpr T fabs(T x, typename std::enable_if< std::is_floating_point< T >::value >::type *=nullptr)
constexpr version of fabs/fabsf
Definition math.h:97
constexpr T to_radians(T zero, T degrees)
Convert from degrees to radians.
Definition math.h:81
constexpr bool float_equal(const float f1, const float f2)
Safe equal comparison of float values.
Definition math.h:107
constexpr T normalize_to_angle(T value, T min, T max, T angle_start, T angle_stop, bool clockwise=true)
Normalize a value, given its min and max, to a different target angle min and max.
Definition math.h:156
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Clamp a value between a hi and low value.
Definition math.h:89
constexpr T to_degrees(T radians)
Convert from radians to degrees.
Definition math.h:72
EGT framework namespace.
Definition animation.h:24