-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasingFunctions.h
More file actions
230 lines (206 loc) · 9.07 KB
/
EasingFunctions.h
File metadata and controls
230 lines (206 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** --------------------------------------------------------
*
* EASING FUNCTIONS
*
* This class contains the mathematical equations, used for
* the Motion class.
*
* Easing visual examples at: https://easings.net/
*
-------------------------------------------------------- **/
#ifndef EASING_FUNCTIONS_H
#define EASING_FUNCTIONS_H
#include <cmath>
#include <limits>
#include <functional>
namespace Motion
{
/** See https://easings.net/ to choose a correct easing type **/
enum class Type : uint8_t
{
LINEAR, // Linear
POW, // Raised to arbitrary power ( modifier is said power )
QUAD, // Quadratic
CUBIC, // Cubic
BACK, // Go slightly back and then forwards
CIRCULAR, // Circular motion
ELASTIC, // Elastic motion ( modifier controls number of wobbles )
BOUNCE, // Bouncing motion ( modifier controls number of bounces )
SINE, // Sinusoidal
EXPONENTIAL, // Exponential ( modifier controls the steepness of the curve )
};
/** Acceleration of the selected motion type */
enum class Acceleration : uint8_t
{
IN, // Acceleration
OUT, // Deceleration
};
using Easing = std::function<double (double, double, double)>;
namespace EasingFunction
{
static Easing Linear = []( double x, double slope, double ) -> double
{
return x*slope;
};
static Easing Pow = []( double x, double power, double ) -> double
{
return std::pow( x, power );
};
static Easing Sine = []( double x, double, double ) -> double
{
const auto h_pi = M_PI*0.5;
return 1 + std::sin( h_pi*x - h_pi );
};
static Easing Back = []( double x, double, double ) -> double
{
return std::pow(x, 3) - x*std::sin( x*M_PI );
};
static Easing Circular = []( double x, double, double ) -> double
{
const auto inv = 1-x;
return 1 - std::sqrt( (2 - inv) * inv );
};
static Easing Elastic = []( double x, double wobbles, double gravity ) -> double
{
const auto arg = wobbles * M_PI * (1 - x);
return std::pow( M_E, (x - 1) * gravity ) * std::sin(arg)/arg;
};
static Easing Bounce = []( double x, double bounces, double gravity ) -> double
{
const auto arg = bounces * M_PI * (1 - x);
return std::abs(std::pow( M_E, (x - 1) * gravity ) * std::sin(arg)/arg);
};
static Easing Exponential = []( double x, double steepness, double ) -> double
{
return std::pow( M_E, (x - 1) * steepness );
};
}
class EasingFunctions
{
public:
/** Get normalized function value */
inline static Easing Normalized( double from, double to, const Easing& func, bool inverted = false )
{
return Easing(
[&]( double val, double modifier = 1, double gravity = 6 )
{
const auto l = to-from;
const auto f = ( from == 0.0 ? 0.0 : ( inverted ? 1-func(1-from, modifier, gravity) : func(from, modifier, gravity) ) );
const auto t = ( to == 1.0 ? 1.0 : ( inverted ? 1-func(1-to, modifier, gravity) : func(to, modifier, gravity) ) );
const auto c = ( inverted ? 1-func(1-(val*l+from), modifier, gravity) : func(val*l+from, modifier, gravity) );
auto d = t-f;
const auto delta = std::numeric_limits<decltype(d)>::epsilon();
if( d < delta ) d = delta;
return (c - f) / d;
}
);
}
/** Get easing function value by enum */
inline static double GetFunctionValue( double val,
double from,
double to,
Type type,
Acceleration accel = Acceleration::IN,
double modifier = 6.0,
double gravity = 6.0 )
{
// Calculate current step
if( from == 0.0 && to == 1.0 )
{
switch( type )
{
case Type::POW:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Pow( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Pow( 1-val, modifier, gravity );
}
}
case Type::QUAD:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Pow( val, 2, gravity );
case Acceleration::OUT: return 1-EasingFunction::Pow( 1-val, 2, gravity );
}
}
case Type::CUBIC:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Pow( val, 3, gravity );
case Acceleration::OUT: return 1-EasingFunction::Pow( 1-val, 3, gravity );
}
}
case Type::SINE:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Sine( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Sine( 1-val, modifier, gravity );
}
}
case Type::BACK:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Back( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Back( 1-val, modifier, gravity );
}
}
case Type::CIRCULAR:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Circular( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Circular( 1-val, modifier, gravity );
}
}
case Type::ELASTIC:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Elastic( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Elastic( 1-val, modifier, gravity );
}
}
case Type::BOUNCE:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Bounce( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Bounce( 1-val, modifier, gravity );
}
}
case Type::EXPONENTIAL:
{
switch( accel )
{
case Acceleration::IN: return EasingFunction::Exponential( val, modifier, gravity );
case Acceleration::OUT: return 1-EasingFunction::Exponential( 1-val, modifier, gravity );
}
}
default: return EasingFunction::Linear( val, 1.0, 1.0 );
}
}
else
{
switch( type )
{
case Type::POW: return Normalized( from, to, EasingFunction::Pow, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::QUAD: return Normalized( from, to, EasingFunction::Pow, (accel == Acceleration::OUT) )( val, 2, gravity );
case Type::CUBIC: return Normalized( from, to, EasingFunction::Pow, (accel == Acceleration::OUT) )( val, 3, gravity );
case Type::SINE: return Normalized( from, to, EasingFunction::Sine, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::BACK: return Normalized( from, to, EasingFunction::Back, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::CIRCULAR: return Normalized( from, to, EasingFunction::Circular, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::ELASTIC: return Normalized( from, to, EasingFunction::Elastic, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::BOUNCE: return Normalized( from, to, EasingFunction::Bounce, (accel == Acceleration::OUT) )( val, modifier, gravity );
case Type::EXPONENTIAL: return Normalized( from, to, EasingFunction::Exponential, (accel == Acceleration::OUT) )( val, modifier, gravity );
default: return EasingFunction::Linear( val, 1.0, 1.0 );
}
}
}
};
} // namespace egt
#endif /** EASING_FUNCTIONS_H */