9 #ifndef TurtleBrains_Interpolation_hpp
10 #define TurtleBrains_Interpolation_hpp
12 #include <turtle_brains/math/tb_math.hpp>
13 #include <turtle_brains/math/tb_constants.hpp>
19 namespace Interpolation
22 enum class InterpolationMode
67 inline constexpr
float Linear(
float percentage)
85 template <
typename Type> constexpr Type Linear(
float percentage,
const Type& start,
const Type& end)
87 return static_cast<Type
>(((end - start) * percentage) + start);
98 inline constexpr
float Squared(
float percentage)
100 return percentage * percentage;
115 template <
typename Type> Type Squared(
float percentage,
const Type& start,
const Type& end)
117 return ((end - start) * Squared(percentage)) + start;
126 inline constexpr
float Cubic(
float percentage)
128 return percentage * percentage * percentage;
143 template <
typename Type> Type Cubic(
float percentage,
const Type& start,
const Type& end)
145 return ((end - start) * Cubic(percentage)) + start;
154 inline constexpr
float Quartic(
float percentage)
156 return percentage * percentage * percentage * percentage;
168 template <
typename Type> Type Quartic(
float percentage,
const Type& start,
const Type& end)
170 return ((end - start) * Quartic(percentage)) + start;
179 inline constexpr
float Quintic(
float percentage)
181 return percentage * percentage * percentage * percentage * percentage;
193 template <
typename Type> Type Quintic(
float percentage,
const Type& start,
const Type& end)
195 return ((end - start) * Quintic(percentage)) + start;
204 inline float Exponential(
float percentage)
206 return pow(2.0f, 10.0f * (percentage - 1.0f));
218 template <
typename Type> Type Exponential(
float percentage,
const Type& start,
const Type& end)
220 return ((end - start) * Exponential(percentage)) + start;
229 inline float Sine(
float percentage)
231 return 1.0f - cos(percentage * tbMath::kPi / 2.0f);
246 template <
typename Type> Type Sine(
float percentage,
const Type& start,
const Type& end)
248 return ((end - start) * Sine(percentage)) + start;
259 inline float Elastic(
float percentage)
261 const float amplitude = 1.0f;
262 const float period = 0.3f;
263 return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
266 inline float Elastic(
float percentage,
float amplitude,
float period)
268 return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
282 template <
typename Type> Type Elastic(
float percentage,
const Type& start,
const Type& end)
284 return ((end - start) * Elastic(percentage)) + start;
293 inline float Bounce(
const float percentage)
295 const float a(7.5625f);
296 const float b(1.0f / 2.75f);
299 tbMath::Minimum(a * (percentage - 1.5f * b) * (percentage - 1.5f * b) + 0.75f,
300 tbMath::Minimum(a * (percentage - 2.25f * b) * (percentage - 2.25f * b) + 0.9375f,
301 a * (percentage - 2.625f * b) * (percentage - 2.625f * b) + 0.984375f)));
316 template <
typename Type> Type Bounce(
float percentage,
const Type& start,
const Type& end)
318 return ((end - start) * Bounce(percentage)) + start;
322 typedef float(*InterpolationFunction)(float);
324 inline float Out(
float percentage, InterpolationFunction interpolation)
326 return 1.0f - interpolation(1.0f - percentage);
329 template <
typename Type>
float Out(
float percentage,
const Type& start,
330 const Type& end, InterpolationFunction interpolation)
332 return ((end - start) * Out(percentage, interpolation)) + start;
336 inline float InOut(
float percentage, InterpolationFunction interpolation)
338 return 0.5f * ((percentage < 0.5f) ? interpolation(2.0f * percentage) :
339 1.0f + Out(2.0f * percentage - 1.0f, interpolation));
342 template <
typename Type>
float InOut(
float percentage,
const Type& start,
343 const Type& end, InterpolationFunction interpolation)
345 return ((end - start) * InOut(percentage, interpolation)) + start;
348 inline float Interpolate(
float percentage,
const InterpolationMode& mode)
352 case InterpolationMode::Linear:
return Linear(percentage);
353 case InterpolationMode::InSquared:
return Squared(percentage);
354 case InterpolationMode::OutSquared:
return Out(percentage, Squared);
355 case InterpolationMode::InOutSquared:
return InOut(percentage, &Squared);
357 case InterpolationMode::InCubic:
return Cubic(percentage);
358 case InterpolationMode::OutCubic:
return Out(percentage, Cubic);
359 case InterpolationMode::InOutCubic:
return InOut(percentage, &Cubic);
361 case InterpolationMode::InQuartic:
return Quartic(percentage);
362 case InterpolationMode::OutQuartic:
return Out(percentage, Quartic);
363 case InterpolationMode::InOutQuartic:
return InOut(percentage, &Quartic);
365 case InterpolationMode::InQuintic:
return Quintic(percentage);
366 case InterpolationMode::OutQuintic:
return Out(percentage, Quintic);
367 case InterpolationMode::InOutQuintic:
return InOut(percentage, &Quintic);
369 case InterpolationMode::InExponential:
return Exponential(percentage);
370 case InterpolationMode::OutExponential:
return Out(percentage, Exponential);
371 case InterpolationMode::InOutExponential:
return InOut(percentage, &Exponential);
373 case InterpolationMode::InSine:
return Sine(percentage);
374 case InterpolationMode::OutSine:
return Out(percentage, Sine);
375 case InterpolationMode::InOutSine:
return InOut(percentage, &Sine);
377 case InterpolationMode::InElastic:
return Elastic(percentage);
378 case InterpolationMode::OutElastic:
return Out(percentage, Elastic);
379 case InterpolationMode::InOutElastic:
return InOut(percentage, &Elastic);
381 case InterpolationMode::InBounce:
return Bounce(percentage);
382 case InterpolationMode::OutBounce:
return Out(percentage, Bounce);
383 case InterpolationMode::InOutBounce:
return InOut(percentage, &Bounce);
386 tb_error(
"tbInternalError: Unhandled case for InterpolationMode: %d\n", mode);
390 template<
typename Type> Type Interpolate(
float percentage,
const Type& start,
const Type& end,
const InterpolationMode& mode)
392 return ((end - start) * Interpolate(percentage, mode)) + start;
404 inline float SmoothStep(
float percentage)
406 return (percentage * percentage * (3.0f - 2.0f * percentage));
422 template <
typename Type> Type SmoothStep(
float percentage,
const Type& start,
const Type& end)
424 const float smoothPercentage = SmoothStep(percentage);
425 return (start * smoothPercentage) + (end * (1.0f - smoothPercentage));
446 template <
typename Type> Type CubicBezier(
float percentage,
const Type& a,
const Type& b,
const Type& c,
const Type& d)
451 const Type tempA = Linear(percentage, a, b);
452 const Type tempB = Linear(percentage, b, c);
453 const Type tempC = Linear(percentage, c, d);
455 const Type tempAA = Linear(percentage, tempA, tempB);
456 const Type tempBB = Linear(percentage, tempB, tempC);
458 return Linear(percentage, tempAA, tempBB);
475 template <
typename Type> Type CubicBezierTangent(
float percentage,
const Type& a,
const Type& b,
const Type& c,
const Type& d)
478 const Type c1(d - (3.0f * c) + (3.0f * b) - a);
479 const Type c2((3.0f * c) - (6.0f * b) + (3.0f * a));
480 const Type c3((3.0f * b) - (3.0f * a));
483 return ((c1 * (3.0f * percentage * percentage)) + (c2 * (2.0f * percentage)) + c3);
Contains objects and functions for dealing with Vector and Matrix math.
#define tb_error(message,...)
Definition: tb_error.hpp:23
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:21
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.hpp:89