Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Math.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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 
26 #include "Portability.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
33  /**
34  * @ingroup core
35  * @brief The @f$ \pi @f$ constant
36  */
37  constexpr float Pi = 3.14159265358979323846f;
38 
39  /**
40  * @ingroup core
41  * @brief The @f$ \frac{\pi}{2} @f$ constant
42  */
43  constexpr float Pi2 = Pi / 2;
44 
45  /**
46  * @ingroup core
47  * @brief The @f$ \frac{\pi}{4} @f$ constant
48  */
49  constexpr float Pi4 = Pi / 4;
50 
51  /**
52  * @ingroup core
53  * @brief The @f$ \sqrt{2} @f$ constant
54  */
55  constexpr float Sqrt2 = 1.41421356237309504880f;
56 
57  /**
58  * @ingroup core
59  * @brief The @f$ \frac{1}{\sqrt{2}} @f$ constant
60  */
61  constexpr float InvSqrt2 = 1 / Sqrt2;
62 
63 
64  /**
65  * @ingroup core
66  * @brief A step is a function with special features.
67  *
68  * A step @f$ f @f$ is a (mathematical) function which has the following properties:
69  *
70  * - @f$ f(0) = 0 @f$
71  * - @f$ f(1) = 1 @f$
72  *
73  * It can be used to smooth a linear interpolation.
74  *
75  * ~~~{.cc}
76  * gf::Vector2f point1 = ...;
77  * gf::Vector2f point2 = ...;
78  * float t = ...;
79  * gf::Step<float> step = gf::cubicStep<float>;
80  *
81  * gf::Vector2f interpolatedPoint = gf::lerp(point1, point2, t);
82  * gf::Vector2f smoothedInterpolatedPoint = gf::lerp(point1, point2, step(t));
83  * ~~~
84  *
85  * @sa linearStep(), cubicStep(), quinticStep(), cosineStep()
86  */
87  template<typename T>
88  using Step = T (*)(T);
89 
90  /**
91  * @ingroup core
92  * @brief Linear step
93  *
94  * @f[ f(t) = t @f]
95  *
96  * @sa gf::Step
97  */
98  template<typename T>
99  constexpr
100  T linearStep(T t) {
101  return t;
102  }
103 
104  /**
105  * @ingroup core
106  * @brief Cubic step (smoothstep)
107  *
108  * @f[ f(t) = -2 * t^3 + 3 * t^2 @f]
109  *
110  * Compared to a step, it has the following properties:
111  *
112  * - @f$ f'(0) = 0 @f$
113  * - @f$ f'(1) = 0 @f$
114  *
115  * This function is also known as [smoothstep](https://en.wikipedia.org/wiki/Smoothstep)
116  *
117  * @sa gf::Step
118  */
119  template<typename T>
120  constexpr
121  T cubicStep(T t) {
122  return (-2 * t + 3) * t * t;
123  }
124 
125  /**
126  * @ingroup core
127  * @brief Quintic step (smootherstep)
128  *
129  * @f[ f(t) = 6 * t^5 - 15 * t^4 + 10 * t^3 @f]
130  *
131  * Compared to a step, it has the following properties:
132  *
133  * - @f$ f'(0) = 0 @f$
134  * - @f$ f'(1) = 0 @f$
135  * - @f$ f''(0) = 0 @f$
136  * - @f$ f''(1) = 0 @f$
137  *
138  * This function is also known as [smootherstep](https://en.wikipedia.org/wiki/Smoothstep#Variations)
139  *
140  * @sa gf::Step
141  */
142  template<typename T>
143  constexpr
144  T quinticStep(T t) {
145  return ((6 * t - 15) * t + 10) * t * t * t;
146  }
147 
148  /**
149  * @ingroup core
150  * @brief Cosine step
151  *
152  * @f[ f(t) = (1 - \cos(\pi * t)) * 0.5 @f]
153  *
154  * @sa gf::Step
155  */
156  template<typename T>
157  inline
158  T cosineStep(T t) {
159  return (1 - std::cos(Pi * t)) * 0.5;
160  }
161 
162  /**
163  * @ingroup core
164  * @brief Linear interpolation function
165  *
166  * This functions returns an interpolated value between two values `lhs` and
167  * `rhs` according to a parameter @f$ t @f$. When @f$ t = 0 @f$ then `lhs`
168  * is returned, and when @f$ t = 1 @f$ then `rhs` is returned.
169  *
170  * @param lhs The first value
171  * @param rhs The second value
172  * @param t The interpolation parameter, generally in the interval @f$ [0,1] @f$
173  * @returns A value between the first and second value
174  */
175  template<typename T, typename U>
176  inline
177  T lerp(T lhs, T rhs, U t) {
178  return (1 - t) * lhs + t * rhs;
179  }
180 
181  /**
182  * @ingroup core
183  * @brief Clamping function
184  *
185  * This function takes a value and returns it if it is in a specified range.
186  * If not, the returned value is the nearest value in the range.
187  *
188  * @param val The value to clamp
189  * @param lo The minimum accepted value
190  * @param hi The maximum accepted value
191  * @returns The clamped value
192  */
193  template<typename T>
194  constexpr
195  T clamp(T val, T lo, T hi) {
196  return val < lo ? lo : (val > hi ? hi : val);
197  }
198 
199  /**
200  * @ingroup core
201  * @brief Square function
202  *
203  * For a value @f$ x @f$, the square value is @f$ x^2 @f$.
204  *
205  * @param val A value
206  * @returns The square of the value
207  */
208  template<typename T>
209  constexpr
210  T square(T val) {
211  return val * val;
212  }
213 
214 #ifndef DOXYGEN_SHOULD_SKIP_THIS
215 }
216 #endif
217 }
218 
219 #endif // GF_MATH_H
constexpr float Pi2
The constant.
Definition: Math.h:43
constexpr float InvSqrt2
The constant.
Definition: Math.h:61
T lerp(T lhs, T rhs, U t)
Linear interpolation function.
Definition: Math.h:177
constexpr T square(T val)
Square function.
Definition: Math.h:210
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:195
constexpr float Sqrt2
The constant.
Definition: Math.h:55
constexpr T cubicStep(T t)
Cubic step (smoothstep)
Definition: Math.h:121
Definition: Action.h:34
T cosineStep(T t)
Cosine step.
Definition: Math.h:158
constexpr float Pi
The constant.
Definition: Math.h:37
constexpr T linearStep(T t)
Linear step.
Definition: Math.h:100
constexpr T quinticStep(T t)
Quintic step (smootherstep)
Definition: Math.h:144
constexpr float Pi4
The constant.
Definition: Math.h:49