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 template<
typename ScalarType>
inline constexpr ScalarType Linear(
const ScalarType percentage)
85 template<
typename Type,
typename ScalarType> constexpr Type Linear(
const ScalarType percentage,
const Type& start,
const Type& end)
87 return ((end - start) * percentage) + start;
99 template<
typename ScalarType> constexpr ScalarType Squared(
const ScalarType percentage)
101 return percentage * percentage;
116 template<
typename Type,
typename ScalarType> Type Squared(
const ScalarType percentage,
const Type& start,
const Type& end)
118 return ((end - start) * Squared(percentage)) + start;
127 template<
typename ScalarType> constexpr ScalarType Cubic(
const ScalarType percentage)
129 return percentage * percentage * percentage;
144 template<
typename Type,
typename ScalarType> Type Cubic(
const ScalarType percentage,
const Type& start,
const Type& end)
146 return ((end - start) * Cubic(percentage)) + start;
155 template<
typename ScalarType> constexpr ScalarType Quartic(
const ScalarType percentage)
157 return percentage * percentage * percentage * percentage;
169 template<
typename Type,
typename ScalarType> Type Quartic(
const ScalarType percentage,
const Type& start,
const Type& end)
171 return ((end - start) * Quartic(percentage)) + start;
180 template<
typename ScalarType> constexpr ScalarType Quintic(
const ScalarType percentage)
182 return percentage * percentage * percentage * percentage * percentage;
194 template<
typename Type,
typename ScalarType> Type Quintic(
const ScalarType percentage,
const Type& start,
const Type& end)
196 return ((end - start) * Quintic(percentage)) + start;
205 template<
typename ScalarType> ScalarType Exponential(
const ScalarType percentage)
207 return std::pow(ScalarType(2.0), ScalarType(10.0) * (percentage - ScalarType(1.0)));
219 template<
typename Type,
typename ScalarType> Type Exponential(
const ScalarType percentage,
const Type& start,
const Type& end)
221 return ((end - start) * Exponential(percentage)) + start;
230 template<
typename ScalarType> ScalarType Sine(
const ScalarType percentage)
232 return ScalarType(1.0) - cos(percentage * tbMath::Pi<ScalarType>() / ScalarType(2.0));
247 template<
typename Type,
typename ScalarType> Type Sine(
const ScalarType percentage,
const Type& start,
const Type& end)
249 return ((end - start) * Sine(percentage)) + start;
260 template<
typename ScalarType> ScalarType Elastic(
const ScalarType percentage)
262 const ScalarType amplitude = ScalarType(1.0);
263 const ScalarType period = ScalarType(0.3);
264 return (-amplitude * std::sin(tbMath::kTwoPi / period * (percentage - ScalarType(1.0)) -
265 std::asin(ScalarType(1.0) / amplitude))) * std::pow(ScalarType(2.0), (ScalarType(10.0) * (percentage - ScalarType(1.0))));
268 template<
typename ScalarType> ScalarType Elastic(
const ScalarType percentage,
const ScalarType amplitude,
const ScalarType period)
270 return (-amplitude * std::sin(tbMath::kTwoPi / period * (percentage - ScalarType(1.0)) -
271 std::asin(ScalarType(1.0) / amplitude))) * std::pow(ScalarType(2.0), (ScalarType(10.0) * (percentage - ScalarType(1.0))));
285 template<
typename Type,
typename ScalarType> Type Elastic(
const ScalarType percentage,
const Type& start,
const Type& end)
287 return ((end - start) * Elastic(percentage)) + start;
296 template<
typename ScalarType> ScalarType Bounce(
const ScalarType percentage)
298 const ScalarType a = ScalarType(7.5625);
299 const ScalarType b = ScalarType(1.0f / 2.75);
302 tbMath::Minimum(a * (percentage - ScalarType(1.5) * b) * (percentage - ScalarType(1.5) * b) + ScalarType(0.75),
303 tbMath::Minimum(a * (percentage - ScalarType(2.25) * b) * (percentage - ScalarType(2.25) * b) + ScalarType(0.9375),
304 a * (percentage - ScalarType(2.625) * b) * (percentage - ScalarType(2.625) * b) + ScalarType(0.984375))));
319 template<
typename Type,
typename ScalarType> Type Bounce(
const ScalarType percentage,
const Type& start,
const Type& end)
321 return ((end - start) * Bounce(percentage)) + start;
326 template<
typename ScalarType>
using InterpolationFunction = ScalarType(*)(ScalarType);
328 template<
typename ScalarType> ScalarType Out(
const ScalarType percentage, InterpolationFunction<ScalarType> interpolation)
330 return ScalarType(1.0) - interpolation(ScalarType(1.0) - percentage);
333 template<
typename Type,
typename ScalarType> Type Out(
const ScalarType percentage,
const Type& start,
334 const Type& end, InterpolationFunction<ScalarType> interpolation)
336 return ((end - start) * Out(percentage, interpolation)) + start;
339 template<
typename ScalarType> ScalarType InOut(
const ScalarType percentage, InterpolationFunction<ScalarType> interpolation)
341 return ScalarType(0.5) * ((percentage < ScalarType(0.5)) ? interpolation(ScalarType(2.0) * percentage) :
342 ScalarType(1.0) + Out(ScalarType(2.0) * percentage - ScalarType(1.0), interpolation));
345 template<
typename Type,
typename ScalarType> Type InOut(
const ScalarType percentage,
const Type& start,
346 const Type& end, InterpolationFunction<ScalarType> interpolation)
348 return ((end - start) * InOut(percentage, interpolation)) + start;
351 template<
typename ScalarType> ScalarType Interpolate(
const ScalarType percentage,
const InterpolationMode& mode)
355 case InterpolationMode::Linear:
return Linear(percentage);
356 case InterpolationMode::InSquared:
return Squared(percentage);
357 case InterpolationMode::OutSquared:
return Out(percentage, Squared);
358 case InterpolationMode::InOutSquared:
return InOut(percentage, &Squared);
360 case InterpolationMode::InCubic:
return Cubic(percentage);
361 case InterpolationMode::OutCubic:
return Out(percentage, Cubic);
362 case InterpolationMode::InOutCubic:
return InOut(percentage, &Cubic);
364 case InterpolationMode::InQuartic:
return Quartic(percentage);
365 case InterpolationMode::OutQuartic:
return Out(percentage, Quartic);
366 case InterpolationMode::InOutQuartic:
return InOut(percentage, &Quartic);
368 case InterpolationMode::InQuintic:
return Quintic(percentage);
369 case InterpolationMode::OutQuintic:
return Out(percentage, Quintic);
370 case InterpolationMode::InOutQuintic:
return InOut(percentage, &Quintic);
372 case InterpolationMode::InExponential:
return Exponential(percentage);
373 case InterpolationMode::OutExponential:
return Out(percentage, Exponential);
374 case InterpolationMode::InOutExponential:
return InOut(percentage, &Exponential);
376 case InterpolationMode::InSine:
return Sine(percentage);
377 case InterpolationMode::OutSine:
return Out(percentage, Sine);
378 case InterpolationMode::InOutSine:
return InOut(percentage, &Sine);
380 case InterpolationMode::InElastic:
return Elastic(percentage);
381 case InterpolationMode::OutElastic:
return Out(percentage, Elastic);
382 case InterpolationMode::InOutElastic:
return InOut(percentage, &Elastic);
384 case InterpolationMode::InBounce:
return Bounce(percentage);
385 case InterpolationMode::OutBounce:
return Out(percentage, Bounce);
386 case InterpolationMode::InOutBounce:
return InOut(percentage, &Bounce);
389 tb_error(
"tbInternalError: Unhandled case for InterpolationMode: %d\n", mode);
390 return ScalarType(0.0);
393 template<
typename Type,
typename ScalarType> Type Interpolate(
const ScalarType percentage,
394 const Type& start,
const Type& end,
const InterpolationMode& mode)
396 return ((end - start) * Interpolate(percentage, mode)) + start;
408 template<
typename ScalarType> ScalarType SmoothStep(
const ScalarType percentage)
410 return (percentage * percentage * (ScalarType(3.0) - ScalarType(2.0) * percentage));
426 template<
typename Type,
typename ScalarType> Type SmoothStep(
const ScalarType percentage,
const Type& start,
const Type& end)
428 const float smoothPercentage = SmoothStep(percentage);
429 return (start * smoothPercentage) + (end * (ScalarType(1.0) - smoothPercentage));
450 template<
typename Type,
typename ScalarType> Type CubicBezier(
const ScalarType percentage,
451 const Type& a,
const Type& b,
const Type& c,
const Type& d)
458 const Type tempA = Linear(percentage, a, b);
459 const Type tempB = Linear(percentage, b, c);
460 const Type tempC = Linear(percentage, c, d);
462 const Type tempAA = Linear(percentage, tempA, tempB);
463 const Type tempBB = Linear(percentage, tempB, tempC);
465 return Linear(percentage, tempAA, tempBB);
482 template<
typename Type,
typename ScalarType> Type CubicBezierTangent(
const ScalarType percentage,
483 const Type& a,
const Type& b,
const Type& c,
const Type& d)
485 const ScalarType kThree = ScalarType(3.0);
486 const Type c1(d - (kThree * c) + (kThree * b) - a);
487 const Type c2((kThree * c) - (ScalarType(6.0) * b) + (kThree * a));
488 const Type c3((kThree * b) - (kThree * a));
491 return ((c1 * (kThree * percentage * percentage)) + (c2 * (ScalarType(2.0) * percentage)) + c3);
#define tb_error(message,...)
Definition: tb_error.hpp:23
Contains objects and functions for dealing with Vector and Matrix math.
constexpr const Type & Minimum(const Type &leftValue, const Type &rightValue) noexcept
Definition: tb_math.hpp:42
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22