hermite  0.0.1
cubic.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <cstddef>
10 #include <vector>
11 
12 #include "hermite/base_spline.hpp"
14 #include "hermite/pose.hpp"
16 
17 namespace hermite {
44 template <std::size_t D> class Cubic : public BaseSpline<D> {
45 public:
51  Cubic() = default;
52 
65  Cubic(const std::vector<Pose<D>> &waypoints) : m_waypoints{waypoints} {
66  std::sort(m_waypoints.begin(), m_waypoints.end(),
67  [](const Pose<D> &a, const Pose<D> &b) {
68  return a.getTime() < b.getTime();
69  });
70  m_spl = CubicVec<D>{m_waypoints};
71  }
72 
76  Cubic(const Cubic<D> &other)
77  : m_waypoints{other.m_waypoints}, m_spl{other.m_spl} {}
78 
82  Cubic<D> &operator=(const Cubic<D> &other) {
83  if (this == &other) {
84  return *this;
85  }
86 
87  m_waypoints = other.m_waypoints;
88  m_spl = other.m_spl;
89 
90  return *this;
91  }
92 
96  ~Cubic() override = default;
97 
103  std::vector<Pose<D>> getAllWaypoints() const { return m_waypoints; }
104 
114  double getLowestTime() const override {
115  if (m_waypoints.size() == 0) {
116  return 0;
117  }
118 
119  return m_waypoints[0].getTime();
120  }
121 
131  double getHighestTime() const override {
132  if (m_waypoints.size() == 0) {
133  return 0;
134  }
135 
136  return m_waypoints[m_waypoints.size() - 1].getTime();
137  }
138 
153  Vector<D> getPos(const double t) const override {
154  Vector<D> res;
155  if (m_waypoints.size() < 2) {
156  return res;
157  }
158 
159  return m_spl.splpos(t);
160  }
161 
174  Vector<D> getVel(const double t) const override {
175  Vector<D> res;
176  if (m_waypoints.size() < 2) {
177  return res;
178  }
179 
180  return m_spl.splvel(t);
181  }
182 
195  Vector<D> getAcc(const double t) const override {
196  Vector<D> res;
197  if (m_waypoints.size() < 2) {
198  return res;
199  }
200 
201  return m_spl.splacc(t);
202  }
203 
216  double getLength(const double timeStep) const override {
217  double res = 0.0;
218 
219  if (m_waypoints.size() < 2) {
220  return res;
221  }
222 
223  double time = getLowestTime() + timeStep;
224  const double timeEnd = getHighestTime();
225  while (time <= timeEnd) {
226  auto vel = getVel(time);
227  auto speed = magn(vel);
228  res += speed * timeStep;
229 
230  time += timeStep;
231  }
232 
233  return res;
234  }
235 
236 private:
237  std::vector<Pose<D>> m_waypoints;
238  CubicVec<D> m_spl;
239 };
240 } // namespace hermite
Abstract base class for interpolating splines.
Definition: base_spline.hpp:20
Spline vector calculator.
Definition: cubic_vec.hpp:25
A natural cubic spline.
Definition: cubic.hpp:44
double getLowestTime() const override
Gets the lower bound of the domain of the piecewise spline function, which is the first time (lowest ...
Definition: cubic.hpp:114
Cubic(const std::vector< Pose< D >> &waypoints)
Constructor.
Definition: cubic.hpp:65
~Cubic() override=default
Destructor.
Vector< D > getAcc(const double t) const override
Gets acceleration of the function at a certain time.
Definition: cubic.hpp:195
double getHighestTime() const override
Gets the upper bound of the domain of the piecewise spline function, which is the first time (lowest ...
Definition: cubic.hpp:131
Cubic()=default
Default constructor.
double getLength(const double timeStep) const override
Gets arc length.
Definition: cubic.hpp:216
Vector< D > getPos(const double t) const override
Gets position at a certain time.
Definition: cubic.hpp:153
Cubic< D > & operator=(const Cubic< D > &other)
Assignment operator.
Definition: cubic.hpp:82
std::vector< Pose< D > > getAllWaypoints() const
Gets a list of all waypoints.
Definition: cubic.hpp:103
Cubic(const Cubic< D > &other)
Copy constructor.
Definition: cubic.hpp:76
Vector< D > getVel(const double t) const override
Gets velocity at a certain time.
Definition: cubic.hpp:174
A pose object.
Definition: pose.hpp:23
A base vector representation.
Definition: simplevectors.hpp:63