13 #ifndef _STRUCT_POLYNOMIAL 14 #define _STRUCT_POLYNOMIAL 32 template <
typename Time = double,
typename Numeric =
Time,
bool Safe =
false,
33 typename Point = Eigen::Matrix<Numeric, Eigen::Dynamic, 1>,
34 typename T_Point = std::vector<Point, Eigen::aligned_allocator<Point> > >
35 struct polynomial :
public curve_abc<Time, Numeric, Safe, Point> {
60 dim_(coefficients.rows()),
62 degree_(coefficients.cols() - 1),
76 dim_(coefficients.begin()->size()),
77 coefficients_(init_coeffs(coefficients.begin(), coefficients.end())),
91 template <
typename In>
94 dim_(zeroOrderCoefficient->size()),
109 if (
T_min_ >=
T_max_)
throw std::invalid_argument(
"T_min must be strictly lower than T_max");
110 if (init.size() != end.size())
throw std::invalid_argument(
"init and end points must have the same dimensions.");
112 coeffs.push_back(init);
113 coeffs.push_back((end - init) / (max - min));
131 if (
T_min_ >=
T_max_)
throw std::invalid_argument(
"T_min must be strictly lower than T_max");
132 if (init.size() != end.size())
throw std::invalid_argument(
"init and end points must have the same dimensions.");
133 if (init.size() != d_init.size())
134 throw std::invalid_argument(
"init and d_init points must have the same dimensions.");
135 if (init.size() != d_end.size())
136 throw std::invalid_argument(
"init and d_end points must have the same dimensions.");
144 double T = max -
min;
145 Eigen::Matrix<double, 4, 4> m;
146 m << 1., 0, 0, 0, 1., T, T * T, T * T * T, 0, 1., 0, 0, 0, 1., 2. * T, 3. * T * T;
147 Eigen::Matrix<double, 4, 4> m_inv = m.inverse();
148 Eigen::Matrix<double, 4, 1> bc;
150 for (
size_t i = 0; i <
dim_; ++i) {
173 const Point& dd_end,
const time_t
min,
const time_t
max)
175 if (
T_min_ >=
T_max_)
throw std::invalid_argument(
"T_min must be strictly lower than T_max");
176 if (init.size() != end.size())
throw std::invalid_argument(
"init and end points must have the same dimensions.");
177 if (init.size() != d_init.size())
178 throw std::invalid_argument(
"init and d_init points must have the same dimensions.");
179 if (init.size() != d_end.size())
180 throw std::invalid_argument(
"init and d_end points must have the same dimensions.");
181 if (init.size() != dd_init.size())
182 throw std::invalid_argument(
"init and dd_init points must have the same dimensions.");
183 if (init.size() != dd_end.size())
184 throw std::invalid_argument(
"init and dd_end points must have the same dimensions.");
194 double T = max -
min;
195 Eigen::Matrix<double, 6, 6> m;
196 m << 1., 0, 0, 0, 0, 0, 1., T, T * T, pow(T, 3), pow(T, 4), pow(T, 5), 0, 1., 0, 0, 0, 0, 0, 1., 2. * T,
197 3. * T * T, 4. * pow(T, 3), 5. * pow(T, 4), 0, 0, 2, 0, 0, 0, 0, 0, 2, 6. * T, 12. * T * T, 20. * pow(T, 3);
198 Eigen::Matrix<double, 6, 6> m_inv = m.inverse();
199 Eigen::Matrix<double, 6, 1> bc;
201 for (
size_t i = 0; i <
dim_; ++i) {
236 static polynomial_t
MinimumJerk(
const point_t& p_init,
const point_t& p_final,
const time_t t_min = 0.,
237 const time_t t_max = 1.) {
238 if (t_min > t_max)
throw std::invalid_argument(
"final time should be superior or equal to initial time.");
239 const size_t dim(p_init.size());
240 if (static_cast<size_t>(p_final.size()) !=
dim)
241 throw std::invalid_argument(
"Initial and final points must have the same dimension.");
242 const double T = t_max - t_min;
243 const double T2 = T * T;
244 const double T3 = T2 * T;
245 const double T4 = T3 * T;
246 const double T5 = T4 * T;
248 coeff_t coeffs = coeff_t::Zero(
dim, 6);
249 coeffs.col(0) = p_init;
250 coeffs.col(3) = 10 * (p_final - p_init) / T3;
251 coeffs.col(4) = -15 * (p_final - p_init) / T4;
252 coeffs.col(5) = 6 * (p_final - p_init) / T5;
260 throw std::invalid_argument(
"Tmin should be inferior to Tmax");
263 throw std::runtime_error(
"Spline order and coefficients do not match");
276 check_if_not_empty();
277 if ((t < T_min_ || t >
T_max_) && Safe) {
278 throw std::invalid_argument(
279 "error in polynomial : time t to evaluate should be in range [Tmin, Tmax] of the curve");
281 time_t
const dt(t -
T_min_);
283 for (
int i = (
int)(
degree_ - 1); i >= 0; i--) {
297 bool isApprox(
const polynomial_t& other,
const Numeric prec = Eigen::NumTraits<Numeric>::dummy_precision())
const {
298 return curves::isApprox<num_t>(
T_min_, other.
min()) && curves::isApprox<num_t>(
T_max_, other.
max()) &&
303 const Numeric prec = Eigen::NumTraits<Numeric>::dummy_precision())
const {
304 const polynomial_t* other_cast =
dynamic_cast<const polynomial_t*
>(other);
313 virtual bool operator!=(
const polynomial_t& other)
const {
return !(*
this == other); }
319 virtual point_t
derivate(
const time_t t,
const std::size_t order)
const {
320 check_if_not_empty();
321 if ((t < T_min_ || t >
T_max_) && Safe) {
322 throw std::invalid_argument(
323 "error in polynomial : time t to evaluate derivative should be in range [Tmin, Tmax] of the curve");
325 time_t
const dt(t -
T_min_);
327 point_t currentPoint_ = point_t::Zero(
dim_);
328 for (
int i = (
int)(order); i < (int)(
degree_ + 1); ++i, cdt *= dt) {
329 currentPoint_ += cdt *
coefficients_.col(i) * fact(i, order);
331 return currentPoint_;
335 check_if_not_empty();
362 num_t fact(
const std::size_t n,
const std::size_t order)
const {
364 for (std::size_t i = 0; i < std::size_t(order); ++i) {
365 res *= (
num_t)(n - i);
370 coeff_t deriv_coeff(coeff_t
coeff)
const {
371 if (coeff.cols() == 1)
372 return coeff_t::Zero(coeff.rows(), 1);
373 coeff_t coeff_derivated(coeff.rows(), coeff.cols() - 1);
374 for (std::size_t i = 0; i < std::size_t(coeff_derivated.cols()); i++) {
375 coeff_derivated.col(i) = coeff.col(i + 1) * (
num_t)(i + 1);
377 return coeff_derivated;
380 void check_if_not_empty()
const {
382 throw std::runtime_error(
"Error in polynomial : there is no coefficients set / did you use empty constructor ?");
391 std::size_t
virtual dim()
const {
return dim_; };
411 template <
typename In>
412 coeff_t init_coeffs(In zeroOrderCoefficient, In highestOrderCoefficient) {
413 std::size_t size = std::distance(zeroOrderCoefficient, highestOrderCoefficient);
414 coeff_t res =
coeff_t(dim_, size);
416 for (In cit = zeroOrderCoefficient; cit != highestOrderCoefficient; ++cit, ++i) {
426 template <
class Archive>
427 void serialize(Archive& ar,
const unsigned int version) {
431 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(curve_abc_t);
432 ar& boost::serialization::make_nvp(
"dim", dim_);
433 ar& boost::serialization::make_nvp(
"coefficients", coefficients_);
434 ar& boost::serialization::make_nvp(
"dim", dim_);
435 ar& boost::serialization::make_nvp(
"degree", degree_);
436 ar& boost::serialization::make_nvp(
"T_min", T_min_);
437 ar& boost::serialization::make_nvp(
"T_max", T_max_);
444 DEFINE_CLASS_TEMPLATE_VERSION(SINGLE_ARG(
typename Time,
typename Numeric,
bool Safe,
typename Point,
typename T_Point),
446 #endif //_STRUCT_POLYNOMIAL Eigen::Ref< coeff_t > coeff_t_ref
Definition: polynomial.h:42
double Numeric
Definition: effector_spline.h:26
polynomial(const coeff_t &coefficients, const time_t min, const time_t max)
Constructor.
Definition: polynomial.h:58
polynomial(const Point &init, const Point &end, const time_t min, const time_t max)
Constructor from boundary condition with C0 : create a polynomial that connect exactly init and end (...
Definition: polynomial.h:107
virtual bool operator!=(const polynomial_t &other) const
Definition: polynomial.h:313
virtual point_t derivate(const time_t t, const std::size_t order) const
Evaluation of the derivative of order N of spline at time t.
Definition: polynomial.h:319
virtual std::size_t degree() const
Get the degree of the curve.
Definition: polynomial.h:400
polynomial_t compute_derivate(const std::size_t order) const
Definition: polynomial.h:334
static polynomial_t MinimumJerk(const point_t &p_init, const point_t &p_final, const time_t t_min=0., const time_t t_max=1.)
MinimumJerk Build a polynomial curve connecting p_init to p_final minimizing the time integral of the...
Definition: polynomial.h:236
curve_abc_t::curve_ptr_t curve_ptr_t
Definition: polynomial.h:44
T_Point t_point_t
Definition: polynomial.h:37
polynomial(const polynomial &other)
Definition: polynomial.h:218
time_t T_min_
Definition: polynomial.h:407
interface for a Curve of arbitrary dimension.
Definition: bernstein.h:20
std::vector< Point, Eigen::aligned_allocator< Point > > T_Point
Definition: effector_spline.h:29
Represents a polynomial of an arbitrary order defined on the interval . It follows the equation : ...
Definition: fwd.h:37
virtual std::size_t dim() const
Get dimension of curve.
Definition: polynomial.h:391
~polynomial()
Destructor.
Definition: polynomial.h:214
curve_abc< Time, Numeric, Safe, Point > curve_abc_t
Definition: polynomial.h:40
virtual bool isApprox(const curve_abc_t *other, const Numeric prec=Eigen::NumTraits< Numeric >::dummy_precision()) const
Definition: polynomial.h:302
friend class boost::serialization::access
Definition: polynomial.h:424
time_t T_max_
Definition: polynomial.h:407
polynomial< Time, Numeric, Safe, Point, T_Point > polynomial_t
Definition: polynomial.h:43
Numeric num_t
Definition: polynomial.h:39
Eigen::MatrixXd coeff() const
Definition: polynomial.h:351
Time time_t
Definition: polynomial.h:38
Point point_t
Definition: polynomial.h:36
coeff_t coefficients_
Definition: polynomial.h:405
polynomial_t * compute_derivate_ptr(const std::size_t order) const
Compute the derived curve at order N.
Definition: polynomial.h:347
Eigen::MatrixXd coeff_t
Definition: polynomial.h:41
virtual num_t min() const
Get the minimum time for which the curve is defined.
Definition: polynomial.h:394
void serialize(Archive &ar, const unsigned int version)
Definition: polynomial.h:427
std::size_t dim_
Definition: polynomial.h:404
bool isApprox(const polynomial_t &other, const Numeric prec=Eigen::NumTraits< Numeric >::dummy_precision()) const
isApprox check if other and *this are approximately equals. Only two curves of the same class can be ...
Definition: polynomial.h:297
polynomial(In zeroOrderCoefficient, In out, const time_t min, const time_t max)
Constructor.
Definition: polynomial.h:92
polynomial(const Point &init, const Point &d_init, const Point &dd_init, const Point &end, const Point &d_end, const Point &dd_end, const time_t min, const time_t max)
Constructor from boundary condition with C2 : create a polynomial that connect exactly init and end a...
Definition: polynomial.h:172
boost::shared_ptr< curve_t > curve_ptr_t
Definition: curve_abc.h:41
polynomial(const T_Point &coefficients, const time_t min, const time_t max)
Constructor.
Definition: polynomial.h:74
std::size_t degree_
Definition: polynomial.h:406
polynomial(const Point &init, const Point &d_init, const Point &end, const Point &d_end, const time_t min, const time_t max)
Constructor from boundary condition with C1 : create a polynomial that connect exactly init and end a...
Definition: polynomial.h:128
virtual num_t max() const
Get the maximum time for which the curve is defined.
Definition: polynomial.h:397
double Time
Definition: effector_spline.h:27
point_t coeffAtDegree(const std::size_t degree) const
Definition: polynomial.h:353
virtual point_t operator()(const time_t t) const
Evaluation of the cubic spline at time t using horner's scheme.
Definition: polynomial.h:275
polynomial()
Empty constructor. Curve obtained this way can not perform other class functions. ...
Definition: polynomial.h:50
Eigen::Matrix< Numeric, Eigen::Dynamic, 1 > Point
Definition: effector_spline.h:28
virtual bool operator==(const polynomial_t &other) const
Definition: polynomial.h:311