CCatmullRomAdvancedSplineInterpolator.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ISplineInterpolator.h"
5 
6 
7 namespace ion
8 {
9  namespace Animation
10  {
11 
12  template <typename T>
14  {
15 
16  public:
17 
18  enum EMode
19  {
20  Uniform = 0,
22  Chordal = 2
23  };
24 
25  EMode Mode = EMode::Uniform;
26 
27  virtual T Interpolate(ISpline<T> const & Path, int const Index, float const Mu);
28 
29  };
30 
31  template <typename T>
32  T CCatmullRomAdvancedSplineInterpolator<T>::Interpolate(ISpline<T> const & Path, int const Index, float const Mu)
33  {
34  // Uses notation from the Centripetal Catmull-Rom Spline article on Wikipedia.org:
35  // https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
36 
37  T const P0 = Path.GetNode(Index - 1);
38  T const P1 = Path.GetNode(Index + 0);
39  T const P2 = Path.GetNode(Index + 1);
40  T const P3 = Path.GetNode(Index + 2);
41 
42  float Alpha = 0;
43  if (Mode == EMode::Uniform)
44  Alpha = 0;
45  else if (Mode == EMode::Centripetal)
46  Alpha = 0.5f;
47  else if (Mode == EMode::Chordal)
48  Alpha = 1;
49 
50  // Define a minimum gap between knots to prevent divide-by-zero
51  static float const MinimumDistance = 0.00001f;;
52  float const P1P0 = Max((P1 - P0).Length(), MinimumDistance);
53  float const P2P1 = Max((P2 - P1).Length(), MinimumDistance);
54  float const P3P2 = Max((P3 - P2).Length(), MinimumDistance);
55 
56  float const t0 = 0;
57  float const t1 = pow(P1P0, Alpha) + t0;
58  float const t2 = pow(P2P1, Alpha) + t1;
59  float const t3 = pow(P3P2, Alpha) + t2;
60 
61  float const t = Mu * (t2 - t1) + t1;
62 
63  T const A1 = P0 * ((t1 - t) / (t1 - t0)) + P1 * ((t - t0) / (t1 - t0));
64  T const A2 = P1 * ((t2 - t) / (t2 - t1)) + P2 * ((t - t1) / (t2 - t1));
65  T const A3 = P2 * ((t3 - t) / (t3 - t2)) + P3 * ((t - t2) / (t3 - t2));
66 
67  T const B1 = A1 * ((t2 - t) / (t2 - t0)) + A2 * ((t - t0) / (t2 - t0));
68  T const B2 = A2 * ((t3 - t) / (t3 - t1)) + A3 * ((t - t1) / (t3 - t1));
69 
70  return B1 * ((t2 - t) / (t2 - t1)) + B2 * ((t - t1) / (t2 - t1));
71  }
72 
73  }
74 }
Definition: CCatmullRomAdvancedSplineInterpolator.h:21
Definition: CCatmullRomAdvancedSplineInterpolator.h:22
Definition: ISplineInterpolator.h:14
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
EMode
Definition: CCatmullRomAdvancedSplineInterpolator.h:18
Definition: CCatmullRomAdvancedSplineInterpolator.h:13
Definition: CCatmullRomAdvancedSplineInterpolator.h:20
Definition: ISpline.h:13
virtual TSplineNode const & GetNode(int const Index) const =0
virtual T Interpolate(ISpline< T > const &Path, int const Index, float const Mu)
Definition: CCatmullRomAdvancedSplineInterpolator.h:32
EMode Mode
Definition: CCatmullRomAdvancedSplineInterpolator.h:25