All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CatmullRomSpline.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "ISpline.h"
6 #include "VectorMath.h"
7 
8 #include <vector>
9 #include <string>
10 
11 namespace Eegeo
12 {
13  namespace Geometry
14  {
15  namespace TimeParameterizationMethod
16  {
17  enum Values
18  {
19  Uniform = 0, // Parameters for points are equidistant, regardless of Euclidean distances. This causes fast/slow sections depending on distance between points.
20  Chordal, // Parameters for points are based using Euclidean distances. This reduces the effect of looping & fast/slow sections
21  Centripetal // Parameters for points are based using Sqr Root Euclidean distances. As with chordal, but produces a much stiffer curve.
22  };
23 
24  bool TryParseTimeParameterizationMethod(const std::string& s, TimeParameterizationMethod::Values& out_value);
25  std::string ToString(const TimeParameterizationMethod::Values value);
26  }
27 
29  {
31  : Position(dv3::Zero())
32  , T(0)
33  {}
34 
35  CatmullRomSplinePoint(dv3& position)
36  : Position(position)
37  , T(0)
38  {}
39 
40  dv3 Position;
41  double T;
42  };
43 
44  class CatmullRomSpline : public ISpline
45  {
46  public:
48 
49  void SetTimeParameterizationMethod(TimeParameterizationMethod::Values method);
50 
51  void GetInterpolatedPositionInPlace(double t, dv3& out_position) const;
52  bool IsValid() const;
53 
54  int GetNumberOfPoints() const { return static_cast<int>(m_points.size()); }
55  const std::vector<CatmullRomSplinePoint>& GetPoints() const { return m_points; }
56 
57  void AddPoint(dv3 position);
58  void RemovePoint(int index);
59  void RemoveLastPoint();
60  void Clear();
61 
62  private:
63 
64  void RecalculateTimeParameters();
65  void RecalculateUniformTimeParameters();
66  void RecalculateEuclidianDistanceTimeParameters(bool useCentripetal);
67  int GetPointIndexForT(double t) const;
68  void GetRelevantPointsForTime(double t,
69  CatmullRomSplinePoint& out_p0,
70  CatmullRomSplinePoint& out_p1,
71  CatmullRomSplinePoint& out_p2,
72  CatmullRomSplinePoint& out_p3,
73  double& localT) const;
74 
75  TimeParameterizationMethod::Values m_timeParameterizationMethod;
76  std::vector<CatmullRomSplinePoint> m_points;
77 
78  std::vector<double> m_scratchDoubles;
79  };
80  }
81 }