-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotionCore.h
372 lines (304 loc) · 9.44 KB
/
MotionCore.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/** --------------------------------------------------------
*
* MOTION
*
* This class is intended for use with various parameters,
* animation coordinates for example, to create smooth
* movements based on easing mathematical functions.
*
* The class is capable of modifying all numerical types,
* be it integer coordinates or floating point values.
*
* Easing visual examples at: https://easings.net/
*
-------------------------------------------------------- **/
#ifndef MOTION_CORE_H
#define MOTION_CORE_H
#include <vector>
#include <deque>
#include <fstream>
#include "EasingFunctions.h"
namespace Motion
{
using TimeType = uint32_t;
/** Singme motion parameters */
template<typename ValueType>
struct MotionParameters
{
/** Type of easing */
Type motion_type;
/** Type of easing */
Acceleration accel_type;
/** Duration of the animation (fraction of the total) */
double duration {0.5};
/** Length of the animation (fraction of the total) */
double length {0.5};
/** Starting value of the function */
ValueType start_value {0};
/** Ending value of the function */
ValueType end_value {1};
/** Extra modifier for Bounce/Elastic/Pow/Exponential */
double modifier {};
/** Gravity modifier for Bounce/Elastic */
double gravity {};
/** Elapsed time */
TimeType elapsed_time {};
};
/** Motion parameters queue type */
template<typename ValueType>
using MotionQueue = std::vector<MotionParameters<ValueType>>;
/** Easing class */
template<typename ValueType>
class MotionCore
{
using MotionIdx = uint8_t;
using Interpolated = std::deque<ValueType>;
private:
/** Starting value */
ValueType start_value {};
/** Target value */
ValueType end_value {};
/** Total duration in frames */
TimeType total_duration {};
/** Queue of animations */
MotionQueue<ValueType> motion_queue;
/** Queue of interpolated values */
Interpolated interpolated_values;
/** Current value */
ValueType current_value {};
/** Current animation starting point */
double current_start_value {};
/** Current animation ending point */
double current_end_value {};
/** Runtime calculation flag */
bool runtime_calculation {};
public:
/** Constructor */
explicit MotionCore( bool runtime_calculation = true )
: runtime_calculation( runtime_calculation )
{}
/** Reset animation */
inline void Reset()
{
current_value = start_value;
current_start_value = start_value;
current_end_value = end_value;
}
/** Check if animation has finished */
inline bool HasFinished()
{
if( runtime_calculation )
{
return motion_queue.empty();
}
else
{
return interpolated_values.empty();
}
}
/** Advance to next frame */
void AdvanceToNext()
{
if( runtime_calculation )
{
CalculateNext();
}
else
{
if( !interpolated_values.empty() )
{
current_value = interpolated_values.front();
interpolated_values.pop_front();
}
}
}
/** Calculate next value */
void CalculateNext()
{
if( motion_queue.empty() )
{
return;
}
motion_queue.back().elapsed_time++;
if( CalculateCurrentEasingValue() )
{
motion_queue.pop_back();
if( !motion_queue.empty() )
{
motion_queue.back().elapsed_time = 0;
current_start_value = current_end_value;
current_end_value += (end_value-start_value) * motion_queue.back().length;
}
}
}
/** Fill interpolation vector */
void FillInterpolationVector()
{
interpolated_values.clear();
// Calculate easing values
for( decltype(total_duration) t = 0; t < total_duration; ++t )
{
CalculateNext();
interpolated_values.emplace_back( current_value );
}
Reset();
}
/** ACCESSORS */
/** Set current value */
inline void SetCurrentValue( ValueType currentValue )
{
current_value = currentValue;
}
/** Get current value */
inline ValueType GetCurrentValue()
{
return current_value;
}
/** Set starting value */
inline void SetStartingValue( ValueType startingValue )
{
start_value = startingValue;
}
/** Get starting value */
inline ValueType GetStartingValue()
{
return start_value;
}
/** Set ending value */
inline void SetEndingValue( ValueType endingValue )
{
end_value = endingValue;
}
/** Get starting value */
inline double GetEndingValue()
{
return end_value;
}
/** Set frame duration value */
inline void SetFrameDuration( TimeType frameDuration )
{
total_duration = frameDuration;
}
/** Get frame duration */
inline TimeType GetFrameDuration()
{
return total_duration;
}
/** Set motion parameters queue */
inline void SetMotionQueue( MotionQueue<ValueType>&& params )
{
motion_queue = MotionQueue<ValueType>( std::move(params) );
// Calculate new ending value
current_start_value = start_value;
current_end_value = start_value + (end_value-start_value) * motion_queue.back().length;
if( std::fabs(current_start_value - current_end_value) <= std::numeric_limits<decltype(current_start_value)>::epsilon() )
{
motion_queue.clear();
interpolated_values.clear();
Reset();
return;
}
if( !runtime_calculation )
{
FillInterpolationVector();
}
}
/** Get motion parameters queue */
inline MotionQueue<ValueType>& GetMotionQueue()
{
return motion_queue;
}
/** Get interpolated values queue */
inline const Interpolated& GetInterpolatedValues()
{
return interpolated_values;
}
/** Set parameters */
inline void SetParameters(
ValueType start_value,
ValueType end_value,
TimeType frame_duration,
MotionQueue<ValueType> params )
{
SetStartingValue( start_value );
SetEndingValue( end_value );
SetFrameDuration( frame_duration );
SetCurrentValue( start_value );
SetMotionQueue( std::move(params) );
}
/** Set linear parameters */
inline void SetParameters(
ValueType start_value,
ValueType end_value,
TimeType frame_duration,
Type type = Type::SINE,
double duration_split = 0.5,
double modifier = 4,
double gravity = 2 )
{
SetStartingValue( start_value );
SetEndingValue( end_value );
SetFrameDuration( frame_duration );
SetCurrentValue( start_value );
SetMotionQueue(
{
{type, Acceleration::OUT, 1-duration_split, 0.5, 0, 1, modifier, gravity},
{type, Acceleration::IN, duration_split, 0.5, 0, 1, modifier, gravity},
}
);
}
/** Set runtime calculation flag */
inline void SetRuntimeCalculation( bool runtime )
{
runtime_calculation = runtime;
}
/** Dump interpolated values to file for plotting */
inline void DumpToFile( const std::string& path )
{
std::ofstream file( path == std::string() ? "motion_plot.xls" : path );
for( const auto value : interpolated_values )
{
file << value << std::endl;
}
file.close();
}
/** Calculate current animation value */
bool CalculateCurrentEasingValue()
{
const auto& element = motion_queue.back();
// Calculate current motion progress
auto progress = static_cast<double>(element.elapsed_time) / (total_duration * element.duration);
// Check for progress completion
if( std::fabs(1.0 - progress) <= 0.1 )
{
current_value = current_end_value;
return true;
}
else if( progress < std::numeric_limits<decltype(progress)>::epsilon() )
{
progress = std::numeric_limits<decltype(progress)>::epsilon();
}
// Calculate current step
double step = EasingFunctions::GetFunctionValue( progress,
static_cast<double>(element.start_value),
static_cast<double>(element.end_value),
element.motion_type,
element.accel_type,
element.modifier,
element.gravity );
// Calculate new value
auto length = std::fabs(current_end_value - current_start_value);
step *= length;
if( current_start_value > current_end_value )
{
current_value = (length - step) + current_end_value;
}
else
{
current_value = step + current_start_value;
}
return false;
}
}; // class MotionCore
} // namespace egt
#endif /** MOTION_H */