trajectory_collection
Func.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <cmath>
5 #include <functional>
6 #include <limits>
7 #include <map>
8 #include <memory>
9 #include <vector>
10 
11 namespace TrajColl
12 {
16 template<class T>
17 class Func
18 {
19 public:
21  Func() {}
22 
26  virtual T operator()(double t) const = 0;
27 
32  virtual T derivative(double t, int order = 1) const = 0;
33 
35  virtual double domainLowerLimit() const
36  {
37  return std::numeric_limits<double>::lowest();
38  }
39 
41  virtual double domainUpperLimit() const
42  {
43  return std::numeric_limits<double>::max();
44  }
45 };
46 
50 template<class T>
51 class PiecewiseFunc : public Func<T>
52 {
53 public:
56 
60  virtual T operator()(double t) const override
61  {
62  checkArg(t);
63  auto funcIt = funcs_.lower_bound(t);
64  return (*(funcIt->second))(t);
65  }
66 
71  virtual T derivative(double t, int order = 1) const override
72  {
73  checkArg(t);
74  auto funcIt = funcs_.lower_bound(t);
75  return (funcIt->second)->derivative(t, order);
76  }
77 
81  virtual size_t index(double t) const
82  {
83  checkArg(t);
84  return std::distance(funcs_.begin(), funcs_.lower_bound(t));
85  }
86 
88  virtual double domainLowerLimit() const override
89  {
90  return tLowerLimit_;
91  }
92 
94  virtual double domainUpperLimit() const override
95  {
96  if(funcs_.empty())
97  {
98  return std::numeric_limits<double>::max();
99  }
100  return funcs_.rbegin()->first;
101  }
102 
104  void clearFuncs()
105  {
106  funcs_.clear();
107  tLowerLimit_ = std::numeric_limits<double>::lowest();
108  }
109 
114  void appendFunc(double t, std::shared_ptr<Func<T>> func)
115  {
116  funcs_.insert(std::pair<double, std::shared_ptr<Func<T>>>(t, func));
117  }
118 
122  void setDomainLowerLimit(double t)
123  {
124  tLowerLimit_ = t;
125  }
126 
127 protected:
131  void checkArg(double t) const
132  {
133  if(t < tLowerLimit_ || funcs_.rbegin()->first < t)
134  {
135  throw std::runtime_error("[PiecewiseFunc] Argument is out of function range. it should be "
136  + std::to_string(tLowerLimit_) + " <= " + std::to_string(t)
137  + " <= " + std::to_string(funcs_.rbegin()->first));
138  }
139  }
140 
141 protected:
143  std::map<double, std::shared_ptr<Func<T>>> funcs_;
144 
146  double tLowerLimit_ = std::numeric_limits<double>::lowest();
147 };
148 
153 template<class T, int Order>
154 class Polynomial : public Func<T>
155 {
156 public:
161  Polynomial(const std::array<T, Order + 1> & coeff, double t0 = 0.0) : coeff_(coeff), t0_(t0) {}
162 
164  int order() const
165  {
166  return Order;
167  }
168 
172  virtual T operator()(double t) const override
173  {
174  T ret = coeff_[0];
175  for(int i = 0; i < Order; i++)
176  {
177  ret += coeff_[i + 1] * std::pow(t - t0_, i + 1);
178  }
179  return ret;
180  }
181 
186  virtual T derivative(double t, int derivativeOrder = 1) const override
187  {
188  if(derivativeOrder > Order)
189  {
190  if constexpr(std::is_scalar_v<T>)
191  {
192  return T(0);
193  }
194  else
195  {
196  T ret = coeff_[0]; // Dummy initialization for dynamic size class
197  ret.setZero();
198  return ret;
199  }
200  }
201 
202  T ret = coeff_[derivativeOrder];
203  for(int j = 0; j < derivativeOrder - 1; j++)
204  {
205  ret *= (derivativeOrder - j);
206  }
207 
208  for(int i = 0; i < Order - derivativeOrder; i++)
209  {
210  T term = coeff_[i + 1 + derivativeOrder] * std::pow(t - t0_, i + 1);
211  for(int j = 0; j < derivativeOrder; j++)
212  {
213  term *= (i + 1 + derivativeOrder - j);
214  }
215  ret += term;
216  }
217 
218  return ret;
219  }
220 
221 protected:
223  std::array<T, Order + 1> coeff_;
224 
226  double t0_;
227 };
228 
232 template<class T>
233 class Constant : public Polynomial<T, 0>
234 {
235 public:
239  Constant(const T & value) : Polynomial<T, 0>(std::array<T, 1>{value}) {}
240 
244  virtual T operator()(double t = 0.0) const override
245  {
247  }
248 };
249 
251 template<class T>
253 
255 template<class T>
257 
259 template<class T>
261 } // namespace TrajColl
TrajColl::Constant::operator()
virtual T operator()(double t=0.0) const override
Evaluate function value.
Definition: Func.h:244
TrajColl::Polynomial::derivative
virtual T derivative(double t, int derivativeOrder=1) const override
Evaluate function derivative value.
Definition: Func.h:186
TrajColl::PiecewiseFunc::appendFunc
void appendFunc(double t, std::shared_ptr< Func< T >> func)
Add function.
Definition: Func.h:114
TrajColl::Constant
Constant function.
Definition: Func.h:233
TrajColl::Func::domainUpperLimit
virtual double domainUpperLimit() const
Get upper limit of domain.
Definition: Func.h:41
TrajColl::Func::Func
Func()
Constructor.
Definition: Func.h:21
TrajColl::PiecewiseFunc::domainUpperLimit
virtual double domainUpperLimit() const override
Get upper limit of domain.
Definition: Func.h:94
TrajColl::PiecewiseFunc::derivative
virtual T derivative(double t, int order=1) const override
Evaluate function derivative value.
Definition: Func.h:71
TrajColl::Polynomial::order
int order() const
Get polynomial order.
Definition: Func.h:164
TrajColl::PiecewiseFunc
Piecewise function.
Definition: Func.h:51
TrajColl::Func::operator()
virtual T operator()(double t) const =0
Evaluate function value.
TrajColl::PiecewiseFunc::PiecewiseFunc
PiecewiseFunc()
Constructor.
Definition: Func.h:55
TrajColl
Definition: BangBangInterpolator.h:7
TrajColl::Polynomial::coeff_
std::array< T, Order+1 > coeff_
Coefficients from low order (i.e., constant term) to high order.
Definition: Func.h:223
TrajColl::PiecewiseFunc::operator()
virtual T operator()(double t) const override
Evaluate function value.
Definition: Func.h:60
TrajColl::Polynomial::operator()
virtual T operator()(double t) const override
Evaluate function value.
Definition: Func.h:172
TrajColl::PiecewiseFunc::tLowerLimit_
double tLowerLimit_
Lower limit of domain of this function.
Definition: Func.h:146
TrajColl::Func::domainLowerLimit
virtual double domainLowerLimit() const
Get lower limit of domain.
Definition: Func.h:35
TrajColl::PiecewiseFunc::domainLowerLimit
virtual double domainLowerLimit() const override
Get lower limit of domain.
Definition: Func.h:88
TrajColl::Polynomial
Polynomial function.
Definition: Func.h:154
TrajColl::Polynomial::Polynomial
Polynomial(const std::array< T, Order+1 > &coeff, double t0=0.0)
Constructor.
Definition: Func.h:161
TrajColl::PiecewiseFunc::checkArg
void checkArg(double t) const
Check argument of function.
Definition: Func.h:131
TrajColl::PiecewiseFunc::clearFuncs
void clearFuncs()
Clear function.
Definition: Func.h:104
TrajColl::Constant::Constant
Constant(const T &value)
Constructor.
Definition: Func.h:239
TrajColl::Func
Mathematical function.
Definition: Func.h:17
TrajColl::Func::derivative
virtual T derivative(double t, int order=1) const =0
Evaluate function derivative value.
TrajColl::Polynomial::t0_
double t0_
Offset of function arugment.
Definition: Func.h:226
TrajColl::PiecewiseFunc::setDomainLowerLimit
void setDomainLowerLimit(double t)
Set lower limit of domain.
Definition: Func.h:122
TrajColl::PiecewiseFunc::funcs_
std::map< double, std::shared_ptr< Func< T > > > funcs_
Map of upper bound of domain and function.
Definition: Func.h:143
TrajColl::PiecewiseFunc::index
virtual size_t index(double t) const
Get the index of piecewise functions.
Definition: Func.h:81