Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Math.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_MATH_H
22 #define GF_MATH_H
23 
24 #include <cmath>
25 #include <limits>
26 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
38  template<typename T>
39  constexpr
40  T pi() {
41  return T(3.141592653589793238462643383279502884197169399L);
42  }
43 
48  constexpr float Pi = pi<float>();
49 
54  constexpr float Pi2 = Pi / 2;
55 
60  constexpr float Pi4 = Pi / 4;
61 
66  constexpr float Sqrt2 = 1.41421356237309504880f;
67 
72  constexpr float InvSqrt2 = 1 / Sqrt2;
73 
78  constexpr float Sqrt3 = 1.7320508075688772935f;
79 
84  constexpr float Epsilon = std::numeric_limits<float>::epsilon();
85 
96  template<typename T>
97  constexpr
98  bool almostEquals(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) {
99  if (a == b) {
100  return true;
101  }
102 
103  T diff = std::abs(a - b);
104 
105  if (a == 0 || b == 0 || diff < std::numeric_limits<T>::denorm_min()) {
106  return diff < (epsilon * std::numeric_limits<T>::denorm_min());
107  }
108 
109  T sum = std::abs(a) + std::abs(b);
110  sum = (sum < std::numeric_limits<T>::max()) ? sum : std::numeric_limits<T>::max();
111 
112  return (diff / sum) < epsilon;
113  }
114 
123  template<typename T>
124  constexpr
125  T degreesToRadians(T degrees) {
126  return degrees * gf::pi<T>() / T(180);
127  }
128 
137  template<typename T>
138  constexpr
139  T radiansToDegrees(T radians) {
140  return radians * T(180) / gf::pi<T>();
141  }
142 
166  template<typename T>
167  using Step = T (*)(T);
168 
177  template<typename T>
178  constexpr
180  return t;
181  }
182 
198  template<typename T>
199  constexpr
200  T cubicStep(T t) {
201  return (-T(2) * t + T(3)) * t * t;
202  }
203 
221  template<typename T>
222  constexpr
224  return ((T(6) * t - T(15)) * t + T(10)) * t * t * t;
225  }
226 
235  template<typename T>
236  inline
238  return (T(1) - std::cos(gf::pi<T>() * t)) * T(0.5);
239  }
240 
254  template<typename T, typename U>
255  constexpr
256  T lerp(T lhs, T rhs, U t) {
257  return (U(1) - t) * lhs + t * rhs;
258  }
259 
272  template<typename T>
273  constexpr
274  T clamp(T val, T lo, T hi) {
275  return val < lo ? lo : (val > hi ? hi : val);
276  }
277 
287  template<typename T>
288  constexpr
289  T square(T val) {
290  return val * val;
291  }
292 
302  template<typename T>
303  constexpr
304  T cube(T val) {
305  return val * val * val;
306  }
307 
322  template<typename T>
323  constexpr
324  int sign(T val) {
325  return (val > T(0)) - (val < T(0));
326  }
327 
340  template<typename T>
341  constexpr
342  T absdiff(T lhs, T rhs) {
343  return lhs > rhs ? lhs - rhs : rhs - lhs;
344  }
345 
346 #ifndef DOXYGEN_SHOULD_SKIP_THIS
347 }
348 #endif
349 }
350 
351 #endif // GF_MATH_H
constexpr float Pi2
The constant.
Definition: Math.h:54
constexpr T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:256
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:84
constexpr T cube(T val)
Cube function.
Definition: Math.h:304
constexpr float InvSqrt2
The constant.
Definition: Math.h:72
constexpr T absdiff(T lhs, T rhs)
Absolute difference of two values.
Definition: Math.h:342
constexpr T square(T val)
Square function.
Definition: Math.h:289
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:274
constexpr float Sqrt2
The constant.
Definition: Math.h:66
constexpr T radiansToDegrees(T radians)
Convert radians to degrees.
Definition: Math.h:139
constexpr T cubicStep(T t)
Cubic step (smoothstep)
Definition: Math.h:200
The namespace for gf classes.
Definition: Action.h:35
T cosineStep(T t)
Cosine step.
Definition: Math.h:237
constexpr T degreesToRadians(T degrees)
Convert degrees to radians.
Definition: Math.h:125
constexpr float Pi
The constant.
Definition: Math.h:48
constexpr bool almostEquals(T a, T b, T epsilon=std::numeric_limits< T >::epsilon())
Compare two floats.
Definition: Math.h:98
constexpr T linearStep(T t)
Linear step.
Definition: Math.h:179
constexpr float Sqrt3
The constant.
Definition: Math.h:78
constexpr int sign(T val)
Sign function.
Definition: Math.h:324
T(*)(T) Step
A step is a function with special features.
Definition: Math.h:167
constexpr T quinticStep(T t)
Quintic step (smootherstep)
Definition: Math.h:223
constexpr T pi()
Templated value of .
Definition: Math.h:40
constexpr float Pi4
The constant.
Definition: Math.h:60