Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Math.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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  constexpr float Pi = 3.14159265358979323846f;
39 
44  constexpr float Pi2 = Pi / 2;
45 
50  constexpr float Pi4 = Pi / 4;
51 
56  constexpr float Sqrt2 = 1.41421356237309504880f;
57 
62  constexpr float InvSqrt2 = 1 / Sqrt2;
63 
68  constexpr float Sqrt3 = 1.7320508075688772935f;
69 
74  constexpr float Epsilon = std::numeric_limits<float>::epsilon();
75 
86  template<typename T>
87  inline
88  bool almostEquals(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) {
89  if (a == b) {
90  return true;
91  }
92 
93  T diff = std::abs(a - b);
94 
95  if (a == 0 || b == 0 || diff < std::numeric_limits<T>::denorm_min()) {
96  return diff < (epsilon * std::numeric_limits<T>::denorm_min());
97  }
98 
99  T sum = std::abs(a) + std::abs(b);
100  sum = (sum < std::numeric_limits<T>::max()) ? sum : std::numeric_limits<T>::max();
101 
102  return (diff / sum) < epsilon;
103  }
104 
113  constexpr float degreesToRadians(float degrees) {
114  return degrees * Pi / 180.0f;
115  }
116 
125  constexpr float radiansToDegrees(float radians) {
126  return radians * 180.0f / Pi;
127  }
128 
152  template<typename T>
153  using Step = T (*)(T);
154 
163  template<typename T>
164  constexpr
166  return t;
167  }
168 
184  template<typename T>
185  constexpr
186  T cubicStep(T t) {
187  return (-2 * t + 3) * t * t;
188  }
189 
207  template<typename T>
208  constexpr
210  return ((6 * t - 15) * t + 10) * t * t * t;
211  }
212 
221  template<typename T>
222  inline
224  return (1 - std::cos(Pi * t)) * 0.5;
225  }
226 
240  template<typename T, typename U>
241  inline
242  T lerp(T lhs, T rhs, U t) {
243  return (1 - t) * lhs + t * rhs;
244  }
245 
258  template<typename T>
259  constexpr
260  T clamp(T val, T lo, T hi) {
261  return val < lo ? lo : (val > hi ? hi : val);
262  }
263 
273  template<typename T>
274  constexpr
275  T square(T val) {
276  return val * val;
277  }
278 
293  template<typename T>
294  constexpr
295  int sign(T val) {
296  return (val > T(0)) - (val < T(0));
297  }
298 
311  template<typename T>
312  constexpr
313  T absdiff(T lhs, T rhs) {
314  return lhs > rhs ? lhs - rhs : rhs - lhs;
315  }
316 
317 #ifndef DOXYGEN_SHOULD_SKIP_THIS
318 }
319 #endif
320 }
321 
322 #endif // GF_MATH_H
constexpr float Pi2
The constant.
Definition: Math.h:44
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:74
constexpr float radiansToDegrees(float radians)
Convert radians to degrees.
Definition: Math.h:125
constexpr float InvSqrt2
The constant.
Definition: Math.h:62
constexpr float degreesToRadians(float degrees)
Convert degrees to radians.
Definition: Math.h:113
T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:242
constexpr T absdiff(T lhs, T rhs)
Absolute difference of two values.
Definition: Math.h:313
constexpr T square(T val)
Square function.
Definition: Math.h:275
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:260
bool almostEquals(T a, T b, T epsilon=std::numeric_limits< T >::epsilon())
Compare two floats.
Definition: Math.h:88
constexpr float Sqrt2
The constant.
Definition: Math.h:56
constexpr T cubicStep(T t)
Cubic step (smoothstep)
Definition: Math.h:186
The namespace for gf classes.
Definition: Action.h:34
T cosineStep(T t)
Cosine step.
Definition: Math.h:223
constexpr float Pi
The constant.
Definition: Math.h:38
constexpr T linearStep(T t)
Linear step.
Definition: Math.h:165
constexpr float Sqrt3
The constant.
Definition: Math.h:68
constexpr int sign(T val)
Sign function.
Definition: Math.h:295
T(*)(T) Step
A step is a function with special features.
Definition: Math.h:153
constexpr T quinticStep(T t)
Quintic step (smootherstep)
Definition: Math.h:209
constexpr float Pi4
The constant.
Definition: Math.h:50