trajectory_collection
CubicInterpolator.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace TrajColl
8 {
15 template<class T, class U = T>
16 class CubicInterpolator : public Interpolator<T, U>
17 {
18 protected:
20  using Vector1d = Eigen::Matrix<double, 1, 1>;
21 
22 public:
26  CubicInterpolator(const std::map<double, T> & points = {}) : Interpolator<T, U>(points)
27  {
28  func_ = std::make_shared<CubicHermiteSpline<Vector1d>>(1, std::map<double, std::pair<Vector1d, Vector1d>>{});
29 
30  if(this->points_.size() >= 2)
31  {
32  calcCoeff();
33  }
34  }
35 
37  CubicInterpolator(const CubicInterpolator & inst) : Interpolator<T, U>(inst)
38  {
39  func_ = std::make_shared<CubicHermiteSpline<Vector1d>>(*inst.func_);
40  }
41 
43  virtual std::shared_ptr<Interpolator<T, U>> clone() const override
44  {
45  return std::shared_ptr<Interpolator<T, U>>(new CubicInterpolator(*this));
46  }
47 
53  virtual void appendPoint(const std::pair<double, T> & point) override
54  {
55  this->points_.insert(point);
56  }
57 
59  virtual void calcCoeff() override
60  {
61  if(this->points_.size() < 2)
62  {
63  throw std::out_of_range("[CubicInterpolator] Number of points should be 2 or more: "
64  + std::to_string(this->points_.size()));
65  }
66 
67  func_->clearPoints();
68 
69  auto it = this->points_.begin();
70  for(size_t i = 0; i < this->points_.size(); i++)
71  {
72  func_->appendPoint(std::make_pair(
73  it->first, std::make_pair((Vector1d() << static_cast<double>(i)).finished(), Vector1d::Zero())));
74  it++;
75  }
76 
77  func_->setDomainLowerLimit(this->points_.begin()->first);
78  func_->calcCoeff();
79  }
80 
84  virtual T operator()(double t) const override
85  {
86  size_t idx = func_->index(t);
87  double ratio = (*func_)(t)[0] - static_cast<double>(idx);
88  return interpolate<T>(std::next(this->points_.begin(), idx)->second,
89  std::next(this->points_.begin(), idx + 1)->second, std::clamp(ratio, 0.0, 1.0));
90  }
91 
98  virtual U derivative(double t, int order = 1) const override
99  {
100  size_t idx = func_->index(t);
101  double ratio = (*func_)(t)[0] - static_cast<double>(idx);
102  return func_->derivative(t, order)[0]
103  * interpolateDerivative<T, U>(std::next(this->points_.begin(), idx)->second,
104  std::next(this->points_.begin(), idx + 1)->second, std::clamp(ratio, 0.0, 1.0),
105  1);
106  }
107 
108 protected:
110  std::shared_ptr<CubicHermiteSpline<Vector1d>> func_;
111 };
112 } // namespace TrajColl
TrajColl::CubicInterpolator::func_
std::shared_ptr< CubicHermiteSpline< Vector1d > > func_
Function to calculate the ratio of interpolation points.
Definition: CubicInterpolator.h:110
ElementInterpolation.h
TrajColl::CubicInterpolator::appendPoint
virtual void appendPoint(const std::pair< double, T > &point) override
Add point.
Definition: CubicInterpolator.h:53
Interpolator.h
TrajColl::CubicInterpolator::CubicInterpolator
CubicInterpolator(const CubicInterpolator &inst)
Copy constructor.
Definition: CubicInterpolator.h:37
TrajColl::CubicInterpolator::calcCoeff
virtual void calcCoeff() override
Calculate coefficients.
Definition: CubicInterpolator.h:59
TrajColl::Interpolator< T, T >::points_
std::map< double, T > points_
Times and values to be interpolated.
Definition: Interpolator.h:81
TrajColl
Definition: BangBangInterpolator.h:7
TrajColl::CubicInterpolator::CubicInterpolator
CubicInterpolator(const std::map< double, T > &points={})
Constructor.
Definition: CubicInterpolator.h:26
CubicHermiteSpline.h
TrajColl::CubicInterpolator::derivative
virtual U derivative(double t, int order=1) const override
Calculate the derivative of interpolated value.
Definition: CubicInterpolator.h:98
TrajColl::Interpolator
Interpolator of a sequence of waypoints.
Definition: Interpolator.h:14
TrajColl::CubicInterpolator::clone
virtual std::shared_ptr< Interpolator< T, U > > clone() const override
Clone this instance and get shared pointer.
Definition: CubicInterpolator.h:43
TrajColl::CubicInterpolator::Vector1d
Eigen::Matrix< double, 1, 1 > Vector1d
1D vector
Definition: CubicInterpolator.h:20
TrajColl::CubicInterpolator::operator()
virtual T operator()(double t) const override
Calculate interpolated value.
Definition: CubicInterpolator.h:84
TrajColl::CubicInterpolator
Cubic interpolator.
Definition: CubicInterpolator.h:16
TrajColl::Interpolator< T, T >::points
const std::map< double, T > & points() const
Get points.
Definition: Interpolator.h:74