hermite  0.0.1
cubic_vec.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <array>
10 #include <cstddef>
11 #include <vector>
12 
14 #include "hermite/pose.hpp"
16 
17 namespace hermite {
18 using svector::Vector;
19 
25 template <std::size_t D> class CubicVec {
26 public:
35  CubicVec() = default;
36 
50  CubicVec(const std::vector<Pose<D>> &waypoints) {
51  const std::size_t n = waypoints.size();
52  m_ts.resize(n);
53 
54  // solve each dimension independently
55  for (std::size_t dim = 0; dim < D; dim++) {
56  m_ys[dim].resize(n);
57  m_accs[dim].resize(n);
58 
59  for (std::size_t i = 0; i < n; i++) {
60  m_ts[i] = waypoints[i].getTime();
61  m_ys[dim][i] = waypoints[i].getPos()[dim];
62  }
63 
64  const double yp1 = waypoints[0].getVel()[dim];
65  const double ypn = waypoints[n - 1].getVel()[dim];
66 
67  spline(m_ts.data(), m_ys[dim].data(), static_cast<int>(n), yp1, ypn,
68  m_accs[dim].data());
69  }
70  }
71 
75  CubicVec(const CubicVec<D> &other)
76  : m_ts{other.m_ts}, m_ys{other.m_ys}, m_accs{other.m_accs} {}
77 
82  // check if assigning to self
83  if (this == &other) {
84  return *this;
85  }
86 
87  m_ts = other.m_ts;
88  m_ys = other.m_ys;
89  m_accs = other.m_accs;
90 
91  return *this;
92  }
93 
97  ~CubicVec() = default;
98 
106  Vector<D> splpos(const double t) const {
107  const std::size_t n = m_ts.size();
108  Vector<D> res;
109 
110  // solve each dimension independently
111  for (std::size_t dim = 0; dim < D; dim++) {
112  double output;
113  hermite::splpos(m_ts.data(), m_ys[dim].data(), m_accs[dim].data(),
114  static_cast<int>(n), t, &output);
115  res[dim] = output;
116  }
117 
118  return res;
119  }
120 
128  Vector<D> splvel(const double t) const {
129  const std::size_t n = m_ts.size();
130  Vector<D> res;
131 
132  // solve each dimension independently
133  for (std::size_t dim = 0; dim < D; dim++) {
134  double output;
135  hermite::splvel(m_ts.data(), m_ys[dim].data(), m_accs[dim].data(),
136  static_cast<int>(n), t, &output);
137  res[dim] = output;
138  }
139 
140  return res;
141  }
142 
150  Vector<D> splacc(const double t) const {
151  const std::size_t n = m_ts.size();
152  Vector<D> res;
153 
154  // solve each dimension independently
155  for (std::size_t dim = 0; dim < D; dim++) {
156  double output;
157  hermite::splacc(m_ts.data(), m_ys[dim].data(), m_accs[dim].data(),
158  static_cast<int>(n), t, &output);
159  res[dim] = output;
160  }
161 
162  return res;
163  }
164 
165 private:
166  std::vector<double> m_ts;
167  std::array<std::vector<double>, D> m_ys;
168  std::array<std::vector<double>, D> m_accs;
169 };
170 } // namespace hermite
bool splacc(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates acceleration of spline.
Definition: cubic_impl.hpp:167
bool splvel(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates velocity of spline.
Definition: cubic_impl.hpp:122
void spline(const double t[], const double y[], const int n, const double yp1, const double ypn, double y2[])
Calculates second derivatives at tabulated points.
Definition: cubic_impl.hpp:25
bool splpos(const double xa[], const double ya[], const double y2a[], const int n, const double x, double *y)
Calculates position of spline.
Definition: cubic_impl.hpp:78
Spline vector calculator.
Definition: cubic_vec.hpp:25
CubicVec< D > & operator=(const CubicVec< D > &other)
Assignment operator.
Definition: cubic_vec.hpp:81
Vector< D > splacc(const double t) const
Definition: cubic_vec.hpp:150
CubicVec(const std::vector< Pose< D >> &waypoints)
Constructor.
Definition: cubic_vec.hpp:50
~CubicVec()=default
Destructor.
Vector< D > splvel(const double t) const
Definition: cubic_vec.hpp:128
CubicVec()=default
Default constructor.
CubicVec(const CubicVec< D > &other)
Copy constructor.
Definition: cubic_vec.hpp:75
Vector< D > splpos(const double t) const
Definition: cubic_vec.hpp:106
A pose object.
Definition: pose.hpp:23
A base vector representation.
Definition: simplevectors.hpp:63