exact_cubic.h
Go to the documentation of this file.
1 
19 #ifndef _CLASS_EXACTCUBIC
20 #define _CLASS_EXACTCUBIC
21 
22 #include "curve_abc.h"
23 #include "curve_constraint.h"
24 #include "piecewise_curve.h"
25 #include "polynomial.h"
26 
27 #include "MathDefs.h"
28 
29 #include <functional>
30 #include <vector>
31 
32 namespace ndcurves {
37 template <typename Time = double, typename Numeric = Time, bool Safe = false,
38  typename Point = Eigen::Matrix<Numeric, Eigen::Dynamic, 1>,
39  typename T_Point = std::vector<Point, Eigen::aligned_allocator<Point> >,
40  typename SplineBase = polynomial<Time, Numeric, Safe, Point, T_Point> >
41 struct exact_cubic : public piecewise_curve<Time, Numeric, Safe, Point> {
42  typedef Point point_t;
43  typedef const Eigen::Ref<const point_t> point_ref_t;
44  typedef T_Point t_point_t;
45  typedef Eigen::Matrix<Numeric, Eigen::Dynamic, Eigen::Dynamic> MatrixX;
46  typedef Eigen::Matrix<Numeric, 3, 3> Matrix3;
47  typedef Time time_t;
48  typedef Numeric num_t;
49  typedef SplineBase spline_t;
50  typedef typename std::vector<spline_t> t_spline_t;
51  typedef typename t_spline_t::iterator it_spline_t;
52  typedef typename t_spline_t::const_iterator cit_spline_t;
54 
60 
61  /* Constructors - destructors */
62  public:
65  exact_cubic() : piecewise_curve_t() {}
66 
71  template <typename In>
72  exact_cubic(In wayPointsBegin, In wayPointsEnd) : piecewise_curve_t() {
73  t_spline_t subSplines = computeWayPoints<In>(wayPointsBegin, wayPointsEnd);
74  for (cit_spline_t it = subSplines.begin(); it != subSplines.end(); ++it) {
75  this->add_curve(*it);
76  }
77  }
78 
84  template <typename In>
85  exact_cubic(In wayPointsBegin, In wayPointsEnd, const spline_constraints& constraints) : piecewise_curve_t() {
86  t_spline_t subSplines = computeWayPoints<In>(wayPointsBegin, wayPointsEnd, constraints);
87  for (cit_spline_t it = subSplines.begin(); it != subSplines.end(); ++it) {
88  this->add_curve(*it);
89  }
90  }
91 
94  exact_cubic(const t_spline_t& subSplines) : piecewise_curve_t() {
95  for (cit_spline_t it = subSplines.begin(); it != subSplines.end(); ++it) {
96  this->add_curve(*it);
97  }
98  }
99 
100  exact_cubic(const t_curve_ptr_t& subSplines) : piecewise_curve_t(subSplines) {}
101 
103  exact_cubic(const exact_cubic& other) : piecewise_curve_t(other) {}
104 
106  virtual ~exact_cubic() {}
107 
108  std::size_t getNumberSplines() { return this->getNumberCurves(); }
109 
110  spline_t getSplineAt(std::size_t index) {
111  boost::shared_ptr<spline_t> s_ptr = boost::dynamic_pointer_cast<spline_t>(this->curves_.at(index));
112  if (s_ptr)
113  return *s_ptr;
114  else
115  throw std::runtime_error(
116  "Parent piecewise curve do not contain only curves created from exact_cubic class methods");
117  }
118 
119  private:
120 
121  static polynomial_t create_cubic(point_ref_t a,point_ref_t b, point_ref_t c, point_ref_t d,
122  const time_t t_min, const time_t t_max){
123  typename polynomial_t::t_point_t coeffs;
124  coeffs.push_back(a);
125  coeffs.push_back(b);
126  coeffs.push_back(c);
127  coeffs.push_back(d);
128  return polynomial_t(coeffs.begin(), coeffs.end(), t_min, t_max);
129  }
130  static polynomial_t create_quintic(point_ref_t a,point_ref_t b, point_ref_t c, point_ref_t d,
131  point_ref_t e, point_ref_t f,
132  const time_t t_min, const time_t t_max){
133  typename polynomial_t::t_point_t coeffs;
134  coeffs.push_back(a);
135  coeffs.push_back(b);
136  coeffs.push_back(c);
137  coeffs.push_back(d);
138  coeffs.push_back(e);
139  coeffs.push_back(f);
140  return polynomial_t(coeffs.begin(), coeffs.end(), t_min, t_max);
141  }
142 
149  template <typename In>
150  t_spline_t computeWayPoints(In wayPointsBegin, In wayPointsEnd) const {
151  const std::size_t dim = wayPointsBegin->second.size();
152  const std::size_t size = std::distance(wayPointsBegin, wayPointsEnd);
153  if (Safe && size < 1) {
154  throw std::length_error("size of waypoints must be superior to 0"); // TODO
155  }
156  t_spline_t subSplines;
157  subSplines.reserve(size);
158  // refer to the paper to understand all this.
159  MatrixX h1 = MatrixX::Zero(size, size);
160  MatrixX h2 = MatrixX::Zero(size, size);
161  MatrixX h3 = MatrixX::Zero(size, size);
162  MatrixX h4 = MatrixX::Zero(size, size);
163  MatrixX h5 = MatrixX::Zero(size, size);
164  MatrixX h6 = MatrixX::Zero(size, size);
165  MatrixX a = MatrixX::Zero(size, dim);
166  MatrixX b = MatrixX::Zero(size, dim);
167  MatrixX c = MatrixX::Zero(size, dim);
168  MatrixX d = MatrixX::Zero(size, dim);
169  MatrixX x = MatrixX::Zero(size, dim);
170  In it(wayPointsBegin), next(wayPointsBegin);
171  ++next;
172  // Fill the matrices H as specified in the paper.
173  for (std::size_t i(0); next != wayPointsEnd; ++next, ++it, ++i) {
174  num_t const dTi((*next).first - (*it).first);
175  num_t const dTi_sqr(dTi * dTi);
176  num_t const dTi_cube(dTi_sqr * dTi);
177  // filling matrices values
178  h3(i, i) = -3 / dTi_sqr;
179  h3(i, i + 1) = 3 / dTi_sqr;
180  h4(i, i) = -2 / dTi;
181  h4(i, i + 1) = -1 / dTi;
182  h5(i, i) = 2 / dTi_cube;
183  h5(i, i + 1) = -2 / dTi_cube;
184  h6(i, i) = 1 / dTi_sqr;
185  h6(i, i + 1) = 1 / dTi_sqr;
186  if (i + 2 < size) {
187  In it2(next);
188  ++it2;
189  num_t const dTi_1((*it2).first - (*next).first);
190  num_t const dTi_1sqr(dTi_1 * dTi_1);
191  // this can be optimized but let's focus on clarity as long as not needed
192  h1(i + 1, i) = 2 / dTi;
193  h1(i + 1, i + 1) = 4 / dTi + 4 / dTi_1;
194  h1(i + 1, i + 2) = 2 / dTi_1;
195  h2(i + 1, i) = -6 / dTi_sqr;
196  h2(i + 1, i + 1) = (6 / dTi_1sqr) - (6 / dTi_sqr);
197  h2(i + 1, i + 2) = 6 / dTi_1sqr;
198  }
199  x.row(i) = (*it).second.transpose();
200  }
201  // adding last x
202  x.row(size - 1) = (*it).second.transpose();
203  // Compute coefficients of polynom.
204  a = x;
205  PseudoInverse(h1);
206  b = h1 * h2 * x; // h1 * b = h2 * x => b = (h1)^-1 * h2 * x
207  c = h3 * x + h4 * b;
208  d = h5 * x + h6 * b;
209  // create splines along waypoints.
210  it = wayPointsBegin, next = wayPointsBegin;
211  ++next;
212  for (int i = 0; next != wayPointsEnd; ++i, ++it, ++next) {
213  subSplines.push_back(create_cubic(a.row(i), b.row(i), c.row(i), d.row(i),
214  (*it).first, (*next).first));
215  }
216  return subSplines;
217  }
218 
219  template <typename In>
220  t_spline_t computeWayPoints(In wayPointsBegin, In wayPointsEnd, const spline_constraints& constraints) const {
221  std::size_t const size(std::distance(wayPointsBegin, wayPointsEnd));
222  if (Safe && size < 1) {
223  throw std::length_error("number of waypoints should be superior to one"); // TODO
224  }
225  t_spline_t subSplines;
226  subSplines.reserve(size - 1);
227  spline_constraints cons = constraints;
228  In it(wayPointsBegin), next(wayPointsBegin), end(wayPointsEnd - 1);
229  ++next;
230  for (std::size_t i(0); next != end; ++next, ++it, ++i) {
231  compute_one_spline<In>(it, next, cons, subSplines);
232  }
233  compute_end_spline<In>(it, next, cons, subSplines);
234  return subSplines;
235  }
236 
237  template <typename In>
238  void compute_one_spline(In wayPointsBegin, In wayPointsNext, spline_constraints& constraints,
239  t_spline_t& subSplines) const {
240  const point_t &a0 = wayPointsBegin->second, a1 = wayPointsNext->second;
241  const point_t &b0 = constraints.init_vel, c0 = constraints.init_acc / 2.;
242  const num_t &init_t = wayPointsBegin->first, end_t = wayPointsNext->first;
243  const num_t dt = end_t - init_t, dt_2 = dt * dt, dt_3 = dt_2 * dt;
244  const point_t d0 = (a1 - a0 - b0 * dt - c0 * dt_2) / dt_3;
245  subSplines.push_back(create_cubic(a0, b0, c0, d0, init_t, end_t));
246  constraints.init_vel = subSplines.back().derivate(end_t, 1);
247  constraints.init_acc = subSplines.back().derivate(end_t, 2);
248  }
249 
250  template <typename In>
251  void compute_end_spline(In wayPointsBegin, In wayPointsNext, spline_constraints& constraints,
252  t_spline_t& subSplines) const {
253  const std::size_t dim = wayPointsBegin->second.size();
254  const point_t &a0 = wayPointsBegin->second, a1 = wayPointsNext->second;
255  const point_t &b0 = constraints.init_vel, b1 = constraints.end_vel, c0 = constraints.init_acc / 2.,
256  c1 = constraints.end_acc;
257  const num_t &init_t = wayPointsBegin->first, end_t = wayPointsNext->first;
258  const num_t dt = end_t - init_t, dt_2 = dt * dt, dt_3 = dt_2 * dt, dt_4 = dt_3 * dt, dt_5 = dt_4 * dt;
259  // solving a system of four linear eq with 4 unknows: d0, e0
260  const point_t alpha_0 = a1 - a0 - b0 * dt - c0 * dt_2;
261  const point_t alpha_1 = b1 - b0 - 2 * c0 * dt;
262  const point_t alpha_2 = c1 - 2 * c0;
263  const num_t x_d_0 = dt_3, x_d_1 = 3 * dt_2, x_d_2 = 6 * dt;
264  const num_t x_e_0 = dt_4, x_e_1 = 4 * dt_3, x_e_2 = 12 * dt_2;
265  const num_t x_f_0 = dt_5, x_f_1 = 5 * dt_4, x_f_2 = 20 * dt_3;
266  point_t d, e, f;
267  MatrixX rhs = MatrixX::Zero(3, dim);
268  rhs.row(0) = alpha_0;
269  rhs.row(1) = alpha_1;
270  rhs.row(2) = alpha_2;
271  Matrix3 eq = Matrix3::Zero(3, 3);
272  eq(0, 0) = x_d_0;
273  eq(0, 1) = x_e_0;
274  eq(0, 2) = x_f_0;
275  eq(1, 0) = x_d_1;
276  eq(1, 1) = x_e_1;
277  eq(1, 2) = x_f_1;
278  eq(2, 0) = x_d_2;
279  eq(2, 1) = x_e_2;
280  eq(2, 2) = x_f_2;
281  rhs = eq.inverse().eval() * rhs;
282  d = rhs.row(0);
283  e = rhs.row(1);
284  f = rhs.row(2);
285  subSplines.push_back(create_quintic(a0, b0, c0, d, e, f, init_t, end_t));
286  }
287 
288  public:
289  // Serialization of the class
290  friend class boost::serialization::access;
291  template <class Archive>
292  void serialize(Archive& ar, const unsigned int version) {
293  if (version) {
294  // Do something depending on version ?
295  }
296  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(piecewise_curve_t);
297  }
298 };
299 } // namespace ndcurves
300 
301 DEFINE_CLASS_TEMPLATE_VERSION(SINGLE_ARG(typename Time, typename Numeric, bool Safe, typename Point,
302  typename T_Point, typename SplineBase),
304 #endif //_CLASS_EXACTCUBIC
virtual ~exact_cubic()
Destructor.
Definition: exact_cubic.h:106
T_Point t_point_t
Definition: exact_cubic.h:44
Definition: bernstein.h:20
exact_cubic(const exact_cubic &other)
Copy Constructor.
Definition: exact_cubic.h:103
t_spline_t::const_iterator cit_spline_t
Definition: exact_cubic.h:52
Definition: exact_cubic.h:41
void PseudoInverse(_Matrix_Type_ &pinvmat)
An inverse kinematics architecture enforcing an arbitrary number of strict priority levels (Reference...
Definition: MathDefs.h:26
std::vector< spline_t > t_spline_t
Definition: exact_cubic.h:50
virtual std::size_t dim() const
Get dimension of curve.
Definition: piecewise_curve.h:564
std::size_t getNumberCurves()
Definition: piecewise_curve.h:576
exact_cubic(In wayPointsBegin, In wayPointsEnd, const spline_constraints &constraints)
Constructor.
Definition: exact_cubic.h:85
Eigen::Matrix< Numeric, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Definition: exact_cubic.h:45
const Eigen::Ref< const point_t > point_ref_t
Definition: exact_cubic.h:43
Definition of a cubic spline.
SplineBase spline_t
Definition: exact_cubic.h:49
exact_cubic(const t_spline_t &subSplines)
Constructor.
Definition: exact_cubic.h:94
interface for a Curve of arbitrary dimension.
exact_cubic(const t_curve_ptr_t &subSplines)
Definition: exact_cubic.h:100
Eigen::Matrix< Numeric, 3, 3 > Matrix3
Definition: exact_cubic.h:46
t_curve_ptr_t curves_
Definition: piecewise_curve.h:581
T_Point t_point_t
Definition: polynomial.h:37
curve_abc< Time, Numeric, Safe, point_t > curve_abc_t
Definition: exact_cubic.h:56
Numeric num_t
Definition: exact_cubic.h:48
std::vector< Point, Eigen::aligned_allocator< Point > > T_Point
Definition: effector_spline.h:29
piecewise_curve< Time, Numeric, Safe, point_t > piecewise_curve_t
Definition: exact_cubic.h:57
curve_constraints< Point > spline_constraints
Definition: exact_cubic.h:53
Represents a polynomial of an arbitrary order defined on the interval . It follows the equation : ...
Definition: fwd.h:37
piecewise_curve_t::t_curve_ptr_t t_curve_ptr_t
Definition: exact_cubic.h:59
double Time
Definition: effector_spline.h:27
void serialize(Archive &ar, const unsigned int version)
Definition: exact_cubic.h:292
point_t init_acc
Definition: curve_constraint.h:63
exact_cubic(In wayPointsBegin, In wayPointsEnd)
Constructor.
Definition: exact_cubic.h:72
point_t end_acc
Definition: curve_constraint.h:66
Eigen::Matrix< Numeric, Eigen::Dynamic, 1 > Point
Definition: effector_spline.h:28
spline_t getSplineAt(std::size_t index)
Definition: exact_cubic.h:110
std::size_t getNumberSplines()
Definition: exact_cubic.h:108
struct to define constraints on start / end velocities and acceleration on a curve ...
Point point_t
Definition: exact_cubic.h:42
t_spline_t::iterator it_spline_t
Definition: exact_cubic.h:51
double Numeric
Definition: effector_spline.h:26
point_t init_vel
Definition: curve_constraint.h:62
class allowing to create a piecewise curve.
polynomial< Time, Numeric, Safe, point_t > polynomial_t
Definition: exact_cubic.h:58
Definition: fwd.h:34
void add_curve(const Curve &curve)
Definition: piecewise_curve.h:142
point_t end_vel
Definition: curve_constraint.h:65
Definition: curve_constraint.h:22
std::vector< curve_ptr_t > t_curve_ptr_t
Definition: piecewise_curve.h:41
exact_cubic()
Empty constructor. Add at least one curve to call other class functions.
Definition: exact_cubic.h:65
Time time_t
Definition: exact_cubic.h:47
Represents a curve of dimension Dim. If value of parameter Safe is false, no verification is made on ...
Definition: curve_abc.h:34
exact_cubic< Time, Numeric, Safe, point_t, T_Point, SplineBase > exact_cubic_t
Definition: exact_cubic.h:55