hpp-core 9.3.0
Implement basic classes for canonical path planning for kinematic chains.
Loading...
Searching...
No Matches
piecewise-polynomial.hh
Go to the documentation of this file.
1// Copyright (c) 2017, Joseph Mirabel
2// Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3// Olivier Roussel (olivier.roussel@laas.fr)
4//
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// 1. Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
28// DAMAGE.
29
30#ifndef HPP_CORE_TIME_PARAMETERIZATION_PIECEWISE_POLYNOMIAL_HH
31#define HPP_CORE_TIME_PARAMETERIZATION_PIECEWISE_POLYNOMIAL_HH
32
33#include <algorithm>
34#include <cmath>
36#include <hpp/core/config.hh>
37#include <hpp/core/fwd.hh>
40
41namespace hpp {
42namespace core {
44
45template <int _Order>
46class HPP_CORE_DLLAPI PiecewisePolynomial : public TimeParameterization {
47 public:
48 enum {
49 Order = _Order,
51 };
52
53 typedef Eigen::Matrix<value_type, NbCoeffs, Eigen::Dynamic, Eigen::ColMajor>
55 typedef Eigen::Matrix<value_type, Eigen::Dynamic, 1> Vector_t;
56
64 : parameters_(parameters),
65 breakpoints_(breakpoints.data(),
66 breakpoints.data() + breakpoints.size()) {
67 assert(size_type(breakpoints_.size()) == parameters_.cols() + 1);
68 assert(parameters_.rows() == NbCoeffs);
69
70 for (size_type j = 0; j < parameters_.cols(); ++j) {
71 if (j > 0) {
72 assert(breakpoints_[j] > breakpoints_[j - 1]);
73 }
74 for (size_type i = 0; i < parameters_.rows(); ++i) {
75 assert(parameters_(i, j) < std::numeric_limits<value_type>::infinity());
76 assert(parameters_(i, j) >
77 -std::numeric_limits<value_type>::infinity());
78 }
79 }
80 }
81
82 const ParameterMatrix_t& parameters() const { return parameters_; }
83
84 const std::vector<value_type>& breakpoints() const { return breakpoints_; }
85
89
91 value_type value(const value_type& t) const { return val(t); }
92
94 value_type derivative(const value_type& t, const size_type& order) const {
95 return Jac(t, order);
96 }
97
101 const value_type& up) const override {
102 if (low > up)
103 throw std::invalid_argument(
104 "Derivative bound interval has reversed bounds.");
105
106 const value_type intervalLow = std::max(low, breakpoints_.front());
107 const value_type intervalUp = std::min(up, breakpoints_.back());
108 if (intervalLow > intervalUp)
109 throw std::invalid_argument(
110 "Derivative bound interval does not intersect the polynomial range.");
111
112 value_type result = 0;
113 for (size_type j = 0; j < parameters_.cols(); ++j) {
114 value_type segmentLow = std::max(intervalLow, breakpoints_[j]);
115 value_type segmentUp = std::min(intervalUp, breakpoints_[j + 1]);
116 if (segmentLow > segmentUp) continue;
117
118 if (startAtZero_) {
119 segmentLow -= breakpoints_[j];
120 segmentUp -= breakpoints_[j];
121 }
122 const auto coefficients = parameters_.col(j);
123 const auto derivativeAt = [&coefficients](const value_type& t) {
125 value_type power = 1;
126 for (size_type i = 1; i < coefficients.size(); ++i) {
127 derivative += value_type(i) * coefficients[i] * power;
128 power *= t;
129 }
130 return derivative;
131 };
132
133 result = std::max(result, std::abs(derivativeAt(segmentLow)));
134 result = std::max(result, std::abs(derivativeAt(segmentUp)));
135
136 if (Order == 3 && coefficients[3] != 0) {
137 const value_type extremum = -coefficients[2] / (3 * coefficients[3]);
138 if (segmentLow < extremum && extremum < segmentUp)
139 result = std::max(result, std::abs(derivativeAt(extremum)));
140 } else if (Order > 3) {
141 const value_type radius =
142 std::max(std::abs(segmentLow), std::abs(segmentUp));
143 value_type conservativeBound = 0;
144 value_type power = 1;
145 for (size_type i = 1; i < coefficients.size(); ++i) {
146 conservativeBound +=
147 value_type(i) * std::abs(coefficients[i]) * power;
148 power *= radius;
149 }
150 result = std::max(result, conservativeBound);
151 }
152 }
153 return result;
154 }
155
164 bool polynomialsStartAtZero() const { return startAtZero_; }
165
167 void polynomialsStartAtZero(bool startAtZero) { startAtZero_ = startAtZero; }
168
169 private:
170 value_type val(value_type t) const {
171 const size_t seg_index = findPolynomialIndex(t);
172 const auto& poly_coeffs = parameters_.col(seg_index);
173 value_type tn = 1;
174 value_type res = poly_coeffs[0];
175 for (size_type i = 1; i < poly_coeffs.size(); ++i) {
176 tn *= t;
177 res += poly_coeffs[i] * tn;
178 }
179 assert(res == res);
180 return res;
181 }
182
183 value_type Jac(const value_type& t) const { return Jac(t, 1); }
184
185 value_type Jac(value_type t, const size_type& order) const {
186 if (order >= parameters_.rows()) return 0;
187 const size_type MaxOrder = 10;
188 if (parameters_.rows() > MaxOrder)
189 throw std::invalid_argument(
190 "Cannot compute the derivative of order greater than 10.");
191 typedef path::binomials<MaxOrder> Binomials_t;
192 const Binomials_t::Factorials_t& factors = Binomials_t::factorials();
193
194 const size_t seg_index = findPolynomialIndex(t);
195 const auto& poly_coeffs = parameters_.col(seg_index);
196
197 value_type res = 0;
198 value_type tn = 1;
199 for (size_type i = order; i < poly_coeffs.size(); ++i) {
200 res += value_type(factors[i] / factors[i - order]) * poly_coeffs[i] * tn;
201 tn *= t;
202 }
203 return res;
204 }
205
206 size_t findPolynomialIndex(value_type& t) const {
207 size_t index;
208
209 // Points to the smallest element of breakpoints_ that is strictly greater
210 // than t
211 auto breakpointIter = std::lower_bound(
212 breakpoints_.begin(), breakpoints_.end(), t, std::less_equal<double>());
213 if (breakpointIter == breakpoints_.begin()) {
214 // t is smaller than breakpoints_[0]
215 assert(t < breakpoints_[0]);
216 // Should we handle numerical issues, i.e. t is very close to
217 // breakpoints_[0] ?
218 index = breakpoints_.size();
219 } else if (breakpointIter == breakpoints_.end()) {
220 if (t > breakpoints_.back())
221 index = breakpoints_.size();
222 else
223 index = breakpoints_.size() - 2;
224 } else {
225 index = std::distance(breakpoints_.begin(), breakpointIter) - 1;
226 }
227 if (index == breakpoints_.size()) {
228 std::ostringstream oss;
229 oss << "Position " << t << " is outside of range [ " << breakpoints_[0]
230 << ", " << breakpoints_[breakpoints_.size() - 1] << ']';
231 throw std::invalid_argument(oss.str());
232 }
233 if (startAtZero_) {
234 t -= breakpoints_[index];
235 }
236 return index;
237 }
238
242 ParameterMatrix_t parameters_;
243 std::vector<value_type> breakpoints_; // size N + 1
245 bool startAtZero_ = false;
246}; // class PiecewisePolynomial
247} // namespace timeParameterization
248} // namespace core
249} // namespace hpp
250#endif // HPP_CORE_TIME_PARAMETERIZATION_PIECEWISE_POLYNOMIAL_HH
Definition time-parameterization.hh:37
Eigen::Matrix< value_type, Eigen::Dynamic, 1 > Vector_t
Definition piecewise-polynomial.hh:55
void polynomialsStartAtZero(bool startAtZero)
See the corresponding getter.
Definition piecewise-polynomial.hh:167
@ NbCoeffs
Definition piecewise-polynomial.hh:50
@ Order
Definition piecewise-polynomial.hh:49
value_type value(const value_type &t) const
Computes .
Definition piecewise-polynomial.hh:91
const ParameterMatrix_t & parameters() const
Definition piecewise-polynomial.hh:82
Eigen::Matrix< value_type, NbCoeffs, Eigen::Dynamic, Eigen::ColMajor > ParameterMatrix_t
Definition piecewise-polynomial.hh:54
PiecewisePolynomial(const ParameterMatrix_t &parameters, const Vector_t &breakpoints)
Definition piecewise-polynomial.hh:62
const std::vector< value_type > & breakpoints() const
Definition piecewise-polynomial.hh:84
TimeParameterizationPtr_t copy() const
Definition piecewise-polynomial.hh:86
bool polynomialsStartAtZero() const
Definition piecewise-polynomial.hh:164
value_type derivative(const value_type &t, const size_type &order) const
Computes .
Definition piecewise-polynomial.hh:94
value_type derivativeBound(const value_type &low, const value_type &up) const override
Definition piecewise-polynomial.hh:100
CoalScalar size() const
assert(d.lhs()._blocks()==d.rhs()._blocks())
Definition piecewise-polynomial.hh:43
Definition bi-rrt-planner.hh:36
shared_ptr< TimeParameterization > TimeParameterizationPtr_t
Definition fwd.hh:190
pinocchio::value_type value_type
Definition fwd.hh:175
pinocchio::size_type size_type
Definition fwd.hh:174