9 #ifndef _TurtleBrains_Interpolation_h_
10 #define _TurtleBrains_Interpolation_h_
16 namespace Interpolation
19 enum class InterpolationMode
64 inline constexpr
float Linear(
float percentage)
82 template <
typename Type> constexpr Type Linear(
float percentage,
const Type& start,
const Type& end)
84 return ((end - start) * percentage) + start;
95 inline constexpr
float Squared(
float percentage)
97 return percentage * percentage;
112 template <
typename Type> Type Squared(
float percentage,
const Type& start,
const Type& end)
114 return ((end - start) * Squared(percentage)) + start;
123 inline constexpr
float Cubic(
float percentage)
125 return percentage * percentage * percentage;
140 template <
typename Type> Type Cubic(
float percentage,
const Type& start,
const Type& end)
142 return ((end - start) * Cubic(percentage)) + start;
151 inline constexpr
float Quartic(
float percentage)
153 return percentage * percentage * percentage * percentage;
165 template <
typename Type> Type Quartic(
float percentage,
const Type& start,
const Type& end)
167 return ((end - start) * Quartic(percentage)) + start;
176 inline constexpr
float Quintic(
float percentage)
178 return percentage * percentage * percentage * percentage * percentage;
190 template <
typename Type> Type Quintic(
float percentage,
const Type& start,
const Type& end)
192 return ((end - start) * Quintic(percentage)) + start;
201 inline float Exponential(
float percentage)
203 return pow(2.0f, 10.0f * (percentage - 1.0f));
215 template <
typename Type> Type Exponential(
float percentage,
const Type& start,
const Type& end)
217 return ((end - start) * Exponential(percentage)) + start;
226 inline float Sine(
float percentage)
228 return 1.0f - cos(percentage * tbMath::kPi / 2.0f);
243 template <
typename Type> Type Sine(
float percentage,
const Type& start,
const Type& end)
245 return ((end - start) * Sine(percentage)) + start;
256 inline float Elastic(
float percentage)
258 const float amplitude = 1.0f;
259 const float period = 0.3f;
260 return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
263 inline float Elastic(
float percentage,
float amplitude,
float period)
265 return (-amplitude * sin(tbMath::kTwoPi / period * (percentage - 1.0f) - asin(1.0f / amplitude))) * pow(2.0f, (10.0f * (percentage - 1.0f)));
279 template <
typename Type> Type Elastic(
float percentage,
const Type& start,
const Type& end)
281 return ((end - start) * Elastic(percentage)) + start;
290 inline float Bounce(
const float percentage)
292 const float a(7.5625f);
293 const float b(1.0f / 2.75f);
296 tbMath::Minimum(a * (percentage - 1.5f * b) * (percentage - 1.5f * b) + 0.75f,
297 tbMath::Minimum(a * (percentage - 2.25f * b) * (percentage - 2.25f * b) + 0.9375f,
298 a * (percentage - 2.625f * b) * (percentage - 2.625f * b) + 0.984375f)));
313 template <
typename Type> Type Bounce(
float percentage,
const Type& start,
const Type& end)
315 return ((end - start) * Bounce(percentage)) + start;
319 typedef float(*InterpolationFunction)(float);
321 inline float Out(
float percentage, InterpolationFunction interpolation)
323 return 1.0f - interpolation(1.0f - percentage);
326 template <
typename Type>
float Out(
float percentage,
const Type& start,
327 const Type& end, InterpolationFunction interpolation)
329 return ((end - start) * Out(percentage, interpolation)) + start;
333 inline float InOut(
float percentage, InterpolationFunction interpolation)
335 return 0.5f * ((percentage < 0.5f) ? interpolation(2.0f * percentage) :
336 1.0f + Out(2.0f * percentage - 1.0f, interpolation));
339 template <
typename Type>
float InOut(
float percentage,
const Type& start,
340 const Type& end, InterpolationFunction interpolation)
342 return ((end - start) * InOut(percentage, interpolation)) + start;
345 inline float Interpolate(
float percentage,
const InterpolationMode& mode)
349 case InterpolationMode::Linear:
return Linear(percentage);
350 case InterpolationMode::InSquared:
return Squared(percentage);
351 case InterpolationMode::OutSquared:
return Out(percentage, Squared);
352 case InterpolationMode::InOutSquared:
return InOut(percentage, &Squared);
354 case InterpolationMode::InCubic:
return Cubic(percentage);
355 case InterpolationMode::OutCubic:
return Out(percentage, Cubic);
356 case InterpolationMode::InOutCubic:
return InOut(percentage, &Cubic);
358 case InterpolationMode::InQuartic:
return Quartic(percentage);
359 case InterpolationMode::OutQuartic:
return Out(percentage, Quartic);
360 case InterpolationMode::InOutQuartic:
return InOut(percentage, &Quartic);
362 case InterpolationMode::InQuintic:
return Quintic(percentage);
363 case InterpolationMode::OutQuintic:
return Out(percentage, Quintic);
364 case InterpolationMode::InOutQuintic:
return InOut(percentage, &Quintic);
366 case InterpolationMode::InExponential:
return Exponential(percentage);
367 case InterpolationMode::OutExponential:
return Out(percentage, Exponential);
368 case InterpolationMode::InOutExponential:
return InOut(percentage, &Exponential);
370 case InterpolationMode::InSine:
return Sine(percentage);
371 case InterpolationMode::OutSine:
return Out(percentage, Sine);
372 case InterpolationMode::InOutSine:
return InOut(percentage, &Sine);
374 case InterpolationMode::InElastic:
return Elastic(percentage);
375 case InterpolationMode::OutElastic:
return Out(percentage, Elastic);
376 case InterpolationMode::InOutElastic:
return InOut(percentage, &Elastic);
378 case InterpolationMode::InBounce:
return Bounce(percentage);
379 case InterpolationMode::OutBounce:
return Out(percentage, Bounce);
380 case InterpolationMode::InOutBounce:
return InOut(percentage, &Bounce);
383 tb_error(
"tbInternalError: Unhandled case for InterpolationMode: %d\n", mode);
387 template<
typename Type> Type Interpolate(
float percentage,
const Type& start,
const Type& end,
const InterpolationMode& mode)
389 return ((end - start) * Interpolate(percentage, mode)) + start;
401 inline float SmoothStep(
float percentage)
403 return (percentage * percentage * (3.0f - 2.0f * percentage));
419 template <
typename Type> Type SmoothStep(
float percentage,
const Type& start,
const Type& end)
421 const float smoothPercentage = SmoothStep(percentage);
422 return (start * smoothPercentage) + (end * (1.0f - smoothPercentage));
443 template <
typename Type> Type CubicBezier(
float percentage,
const Type& a,
const Type& b,
const Type& c,
const Type& d)
445 const Type tempA = Linear(percentage, a, c);
446 const Type tempB = Linear(percentage, c, d);
447 const Type tempC = Linear(percentage, d, b);
449 const Type tempAA = Linear(percentage, tempA, tempB);
450 const Type tempBB = Linear(percentage, tempB, tempC);
452 return Linear(percentage, tempAA, tempBB);
469 template <
typename Type> Type CubicBezierTangent(
float percentage,
const Type& a,
const Type& b,
const Type& c,
const Type& d)
472 const Type c1(d - (3.0f * c) + (3.0f * b) - a);
473 const Type c2((3.0f * c) - (6.0f * b) + (3.0f * a));
474 const Type c3((3.0f * b) - (3.0f * a));
477 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.h:23
Here is some information about the primary namespace.
Definition: tb_application_dialog.h:21
constexpr const T & Minimum(const T &leftValue, const T &rightValue) noexcept
Definition: tb_math.h:81