Gamedev Framework (gf)  0.17.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 Pi3 = Pi / 3;
61 
66  constexpr float Pi4 = Pi / 4;
67 
72  constexpr float Pi6 = Pi / 6;
73 
78  constexpr float Sqrt2 = 1.41421356237309504880f;
79 
84  constexpr float InvSqrt2 = 1 / Sqrt2;
85 
90  constexpr float Sqrt3 = 1.7320508075688772935f;
91 
96  constexpr float Epsilon = std::numeric_limits<float>::epsilon();
97 
108  template<typename T>
109  constexpr
110  bool almostEquals(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) {
111  if (a == b) {
112  return true;
113  }
114 
115  T diff = std::abs(a - b);
116 
117  if (a == 0 || b == 0 || diff < std::numeric_limits<T>::denorm_min()) {
118  return diff < (epsilon * std::numeric_limits<T>::denorm_min());
119  }
120 
121  T sum = std::abs(a) + std::abs(b);
122  sum = (sum < std::numeric_limits<T>::max()) ? sum : std::numeric_limits<T>::max();
123 
124  return (diff / sum) < epsilon;
125  }
126 
135  template<typename T>
136  constexpr
137  T degreesToRadians(T degrees) {
138  return degrees * gf::pi<T>() / T(180);
139  }
140 
149  template<typename T>
150  constexpr
151  T radiansToDegrees(T radians) {
152  return radians * T(180) / gf::pi<T>();
153  }
154 
178  template<typename T>
179  using Step = T (*)(T);
180 
189  template<typename T>
190  constexpr
192  return t;
193  }
194 
210  template<typename T>
211  constexpr
212  T cubicStep(T t) {
213  return (-T(2) * t + T(3)) * t * t;
214  }
215 
233  template<typename T>
234  constexpr
236  return ((T(6) * t - T(15)) * t + T(10)) * t * t * t;
237  }
238 
247  template<typename T>
248  inline
250  return (T(1) - std::cos(gf::pi<T>() * t)) * T(0.5);
251  }
252 
266  template<typename T, typename U>
267  constexpr
268  T lerp(T lhs, T rhs, U t) {
269  return (U(1) - t) * lhs + t * rhs;
270  }
271 
284  template<typename T>
285  constexpr
286  T clamp(T val, T lo, T hi) {
287  return val < lo ? lo : (val > hi ? hi : val);
288  }
289 
299  template<typename T>
300  constexpr
301  T square(T val) {
302  return val * val;
303  }
304 
314  template<typename T>
315  constexpr
316  T cube(T val) {
317  return val * val * val;
318  }
319 
334  template<typename T>
335  constexpr
336  int sign(T val) {
337  return (val > T(0)) - (val < T(0));
338  }
339 
352  template<typename T>
353  constexpr
354  T absdiff(T lhs, T rhs) {
355  return lhs > rhs ? lhs - rhs : rhs - lhs;
356  }
357 
358 #ifndef DOXYGEN_SHOULD_SKIP_THIS
359 }
360 #endif
361 }
362 
363 #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:268
constexpr float Epsilon
Machine epsilon.
Definition: Math.h:96
constexpr T cube(T val)
Cube function.
Definition: Math.h:316
constexpr float Pi6
The constant.
Definition: Math.h:72
constexpr float InvSqrt2
The constant.
Definition: Math.h:84
constexpr T absdiff(T lhs, T rhs)
Absolute difference of two values.
Definition: Math.h:354
constexpr T square(T val)
Square function.
Definition: Math.h:301
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:286
constexpr float Sqrt2
The constant.
Definition: Math.h:78
constexpr T radiansToDegrees(T radians)
Convert radians to degrees.
Definition: Math.h:151
constexpr T cubicStep(T t)
Cubic step (smoothstep)
Definition: Math.h:212
The namespace for gf classes.
Definition: Action.h:35
T cosineStep(T t)
Cosine step.
Definition: Math.h:249
constexpr T degreesToRadians(T degrees)
Convert degrees to radians.
Definition: Math.h:137
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:110
constexpr T linearStep(T t)
Linear step.
Definition: Math.h:191
constexpr float Sqrt3
The constant.
Definition: Math.h:90
constexpr int sign(T val)
Sign function.
Definition: Math.h:336
constexpr float Pi3
The constant.
Definition: Math.h:60
T(*)(T) Step
A step is a function with special features.
Definition: Math.h:179
constexpr T quinticStep(T t)
Quintic step (smootherstep)
Definition: Math.h:235
constexpr T pi()
Templated value of .
Definition: Math.h:40
constexpr float Pi4
The constant.
Definition: Math.h:66