cubic_hermite_spline.h 17.3 KB
Newer Older
1
/**
Guilhem Saurel's avatar
Guilhem Saurel committed
2
3
4
5
6
 * \file cubic_hermite_spline.h
 * \brief class allowing to create a cubic hermite spline of any dimension.
 * \author Justin Carpentier <jcarpent@laas.fr> modified by Jason Chemin <jchemin@laas.fr>
 * \date 05/2019
 */
7

8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _CLASS_CUBICHERMITESPLINE
#define _CLASS_CUBICHERMITESPLINE

#include "curve_abc.h"
#include "curve_constraint.h"

#include "MathDefs.h"

#include <vector>
#include <stdexcept>

#include <iostream>

Guilhem Saurel's avatar
Guilhem Saurel committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <boost/serialization/utility.hpp>  // To serialize std::pair

namespace curves {
/// \class CubicHermiteSpline.
/// \brief Represents a set of cubic hermite splines defining a continuous function \f$p(t)\f$.
/// A hermite cubic spline is a minimal degree polynom interpolating a function in two
/// points \f$P_i\f$ and \f$P_{i+1}\f$ with its tangent \f$m_i\f$ and \f$m_{i+1}\f$.<br>
/// A hermite cubic spline :
/// - crosses each of the waypoint given in its initialization (\f$P_0\f$, \f$P_1\f$,...,\f$P_N\f$).
/// - has its derivatives on \f$P_i\f$ and \f$P_{i+1}\f$ are \f$p'(t_{P_i}) = m_i\f$ and \f$p'(t_{P_{i+1}}) =
/// m_{i+1}\f$.
///
template <typename Time = double, typename Numeric = Time, bool Safe = false,
          typename Point = Eigen::Matrix<Numeric, Eigen::Dynamic, 1> >
struct cubic_hermite_spline : public curve_abc<Time, Numeric, Safe, Point> {
36
  typedef Point point_t;
Guilhem Saurel's avatar
Guilhem Saurel committed
37
38
39
40
  typedef std::pair<Point, Point> pair_point_tangent_t;
  typedef std::vector<pair_point_tangent_t, Eigen::aligned_allocator<Point> > t_pair_point_tangent_t;
  typedef std::vector<Time> vector_time_t;
  typedef Numeric num_t;
41
  typedef curve_abc<Time, Numeric, Safe, point_t> curve_abc_t;  // parent class
42
  typedef cubic_hermite_spline<Time, Numeric, Safe, point_t> cubic_hermite_spline_t;
43

Guilhem Saurel's avatar
Guilhem Saurel committed
44
45
46

 public:
  /// \brief Empty constructor. Curve obtained this way can not perform other class functions.
47
  ///
Guilhem Saurel's avatar
Guilhem Saurel committed
48
  cubic_hermite_spline() : dim_(0), T_min_(0), T_max_(0) {}
49

Guilhem Saurel's avatar
Guilhem Saurel committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  /// \brief Constructor.
  /// \param wayPointsBegin : an iterator pointing to the first element of a pair(position, derivative) container.
  /// \param wayPointsEns   : an iterator pointing to the last  element of a pair(position, derivative) container.
  /// \param time_control_points : vector containing time for each waypoint.
  ///
  template <typename In>
  cubic_hermite_spline(In PairsBegin, In PairsEnd, const vector_time_t& time_control_points)
      : size_(std::distance(PairsBegin, PairsEnd)), degree_(3) {
    // Check size of pairs container.
    if (Safe && size_ < 1) {
      throw std::length_error("can not create cubic_hermite_spline, number of pairs is inferior to 2.");
    }
    // Push all pairs in controlPoints
    In it(PairsBegin);
    for (; it != PairsEnd; ++it) {
      control_points_.push_back(*it);
    }
    // Set dimension according to size of points
    if (control_points_.size() != 0) {
      dim_ = control_points_[0].first.size();
    }
    // Set time
    setTime(time_control_points);
  }

  cubic_hermite_spline(const cubic_hermite_spline& other)
      : dim_(other.dim_),
        control_points_(other.control_points_),
        time_control_points_(other.time_control_points_),
        duration_splines_(other.duration_splines_),
        T_min_(other.T_min_),
        T_max_(other.T_max_),
        size_(other.size_),
        degree_(other.degree_) {}

  /// \brief Destructor.
  virtual ~cubic_hermite_spline() {}

  /*Operations*/
 public:
  ///  \brief Evaluation of the cubic hermite spline at time t.
  ///  \param t : time when to evaluate the spline.
  ///  \return \f$p(t)\f$ point corresponding on spline at time t.
  ///
  virtual Point operator()(const Time t) const {
    check_conditions();
    if (Safe & !(T_min_ <= t && t <= T_max_)) {
      throw std::invalid_argument("can't evaluate cubic hermite spline, out of range");
    }
    if (size_ == 1) {
      return control_points_.front().first;
    } else {
      return evalCubicHermiteSpline(t, 0);
    }
  }

106
  /**
107
108
   * @brief isApprox check if other and *this are approximately equals.
   * Only two curves of the same class can be approximately equals, for comparison between different type of curves see isEquivalent
109
110
111
112
   * @param other the other curve to check
   * @param prec the precision treshold, default Eigen::NumTraits<Numeric>::dummy_precision()
   * @return true is the two curves are approximately equals
   */
113
  bool isApprox(const cubic_hermite_spline_t& other, const Numeric prec = Eigen::NumTraits<Numeric>::dummy_precision()) const{
114
115
    bool equal =  curves::isApprox<num_t> (T_min_, other.min())
        && curves::isApprox<num_t> (T_max_, other.max())
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        && dim_ == other.dim()
        && degree_ == other.degree()
        && size_ == other.size()
        && time_control_points_ == other.time_control_points_
        && duration_splines_ == other.duration_splines_;
    if(!equal)
      return false;
    for (std::size_t i = 0 ; i < size_ ;++i)
    {
      if((!control_points_[i].first.isApprox(other.control_points_[i].first,prec)) ||
         (!control_points_[i].second.isApprox(other.control_points_[i].second,prec)) )
        return false;
    }
    return true;
  }

132
133
134
135
136
137
138
139
  virtual bool isApprox(const curve_abc_t* other, const Numeric prec = Eigen::NumTraits<Numeric>::dummy_precision()) const{
    const cubic_hermite_spline_t* other_cast = dynamic_cast<const cubic_hermite_spline_t*>(other);
    if(other_cast)
      return isApprox(*other_cast,prec);
    else
      return false;
  }

140
141
142
143
144
145
146
147
148
149
  virtual bool operator==(const cubic_hermite_spline_t& other) const {
    return isApprox(other);
  }

  virtual bool operator!=(const cubic_hermite_spline_t& other) const {
    return !(*this == other);
  }



Guilhem Saurel's avatar
Guilhem Saurel committed
150
151
152
153
154
155
156
157
158
159
  ///  \brief Evaluate the derivative of order N of spline at time t.
  ///  \param t : time when to evaluate the spline.
  ///  \param order : order of derivative.
  ///  \return \f$\frac{d^Np(t)}{dt^N}\f$ point corresponding on derivative spline of order N at time t.
  ///
  virtual Point derivate(const Time t, const std::size_t order) const {
    check_conditions();
    return evalCubicHermiteSpline(t, order);
  }

160
  cubic_hermite_spline_t compute_derivate(const std::size_t /*order*/) const {
161
162
163
    throw std::logic_error("Compute derivate for cubic hermite spline is not implemented yet.");
  }

164
165
166
167
168
169
170
171
  ///  \brief Compute the derived curve at order N.
  ///  \param order : order of derivative.
  ///  \return A pointer to \f$\frac{d^Nx(t)}{dt^N}\f$ derivative order N of the curve.
  cubic_hermite_spline_t* compute_derivate_ptr(const std::size_t order) const {
    return new cubic_hermite_spline_t(compute_derivate(order));
  }


172

Guilhem Saurel's avatar
Guilhem Saurel committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  /// \brief Set time of each control point of cubic hermite spline.
  /// Set duration of each spline, Exemple : \f$( 0., 0.5, 0.9, ..., 4.5 )\f$ with
  /// values corresponding to times for \f$P_0, P_1, P_2, ..., P_N\f$ respectively.<br>
  /// \param time_control_points : Vector containing time for each control point.
  ///
  void setTime(const vector_time_t& time_control_points) {
    time_control_points_ = time_control_points;
    T_min_ = time_control_points_.front();
    T_max_ = time_control_points_.back();
    if (time_control_points.size() != size()) {
      throw std::length_error("size of time control points should be equal to number of control points");
    }
    computeDurationSplines();
    if (!checkDurationSplines()) {
      throw std::invalid_argument("time_splines not monotonous, all spline duration should be superior to 0");
    }
  }

  /// \brief Get vector of pair (positition, derivative) corresponding to control points.
  /// \return vector containing control points.
  ///
  t_pair_point_tangent_t getControlPoints() { return control_points_; }
195

Guilhem Saurel's avatar
Guilhem Saurel committed
196
197
198
199
  /// \brief Get vector of Time corresponding to Time for each control point.
  /// \return vector containing time of each control point.
  ///
  vector_time_t getTime() { return time_control_points_; }
200

Guilhem Saurel's avatar
Guilhem Saurel committed
201
202
203
204
  /// \brief Get number of control points contained in the trajectory.
  /// \return number of control points.
  ///
  std::size_t size() const { return size_; }
205

Guilhem Saurel's avatar
Guilhem Saurel committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  /// \brief Get number of intervals (subsplines) contained in the trajectory.
  /// \return number of intervals (subsplines).
  ///
  std::size_t numIntervals() const { return size() - 1; }

  /// \brief Evaluate value of cubic hermite spline or its derivate at specified order at time \f$t\f$.
  /// A cubic hermite spline on unit interval \f$[0,1]\f$ and given two control points defined by
  /// their position and derivative \f$\{p_0,m_0\}\f$ and \f$\{p_1,m_1\}\f$, is defined by the polynom : <br>
  ///     \f$p(t)=h_{00}(t)P_0 + h_{10}(t)m_0 + h_{01}(t)p_1 + h_{11}(t)m_1\f$<br>
  /// To extend this formula to a cubic hermite spline on any arbitrary interval,
  /// we define \f$\alpha=(t-t_0)/(t_1-t_0) \in [0, 1]\f$ where \f$t \in [t_0, t_1]\f$.<br>
  /// Polynom \f$p(t) \in [t_0, t_1]\f$ becomes \f$p(\alpha) \in [0, 1]\f$
  /// and \f$p(\alpha) = p((t-t_0)/(t_1-t_0))\f$.
  /// \param t : time when to evaluate the curve.
  /// \param degree_derivative : Order of derivate of cubic hermite spline (set value to 0 if you do not want derivate)
  /// \return point corresponding \f$p(t)\f$ on spline at time t or its derivate order N \f$\frac{d^Np(t)}{dt^N}\f$.
  ///
  Point evalCubicHermiteSpline(const Numeric t, std::size_t degree_derivative) const {
    const std::size_t id = findInterval(t);
    // ID is on the last control point
    if (id == size_ - 1) {
      if (degree_derivative == 0) {
        return control_points_.back().first;
      } else if (degree_derivative == 1) {
        return control_points_.back().second;
      } else {
        return control_points_.back().first * 0.;  // To modify, create a new Tangent ininitialized with 0.
233
      }
Guilhem Saurel's avatar
Guilhem Saurel committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    }
    const pair_point_tangent_t Pair0 = control_points_.at(id);
    const pair_point_tangent_t Pair1 = control_points_.at(id + 1);
    const Time& t0 = time_control_points_[id];
    const Time& t1 = time_control_points_[id + 1];
    // Polynom for a cubic hermite spline defined on [0., 1.] is :
    //      p(t) = h00(t)*p0 + h10(t)*m0 + h01(t)*p1 + h11(t)*m1 with t in [0., 1.]
    //
    // For a cubic hermite spline defined on [t0, t1], we define alpha=(t-t0)/(t1-t0) in [0., 1.].
    // Polynom p(t) defined on [t0, t1] becomes p(alpha) defined on [0., 1.]
    //      p(alpha) = p((t-t0)/(t1-t0))
    //
    const Time dt = (t1 - t0);
    const Time alpha = (t - t0) / dt;
    assert(0. <= alpha && alpha <= 1. && "alpha must be in [0,1]");
    Numeric h00, h10, h01, h11;
    evalCoeffs(alpha, h00, h10, h01, h11, degree_derivative);
    // std::cout << "for val t="<<t<<" alpha="<<alpha<<" coef : h00="<<h00<<" h10="<<h10<<" h01="<<h01<<"
    // h11="<<h11<<std::endl;
    Point p_ = (h00 * Pair0.first + h10 * dt * Pair0.second + h01 * Pair1.first + h11 * dt * Pair1.second);
    // if derivative, divide by dt^degree_derivative
    for (std::size_t i = 0; i < degree_derivative; i++) {
      p_ /= dt;
    }
    return p_;
  }

  /// \brief Evaluate coefficient for polynom of cubic hermite spline.
  /// Coefficients of polynom :<br>
  ///  - \f$h00(t)=2t^3-3t^2+1\f$;
  ///  - \f$h10(t)=t^3-2t^2+t\f$;
  ///  - \f$h01(t)=-2t^3+3t^2\f$;
  ///  - \f$h11(t)=t^3-t^2\f$.<br>
  /// From it, we can calculate their derivate order N :
  /// \f$\frac{d^Nh00(t)}{dt^N}\f$, \f$\frac{d^Nh10(t)}{dt^N}\f$,\f$\frac{d^Nh01(t)}{dt^N}\f$,
  /// \f$\frac{d^Nh11(t)}{dt^N}\f$. \param t : time to calculate coefficients. \param h00 : variable to store value of
  /// coefficient. \param h10 : variable to store value of coefficient. \param h01 : variable to store value of
  /// coefficient. \param h11 : variable to store value of coefficient. \param degree_derivative : order of derivative.
  ///
  static void evalCoeffs(const Numeric t, Numeric& h00, Numeric& h10, Numeric& h01, Numeric& h11,
                         std::size_t degree_derivative) {
    Numeric t_square = t * t;
    Numeric t_cube = t_square * t;
    if (degree_derivative == 0) {
      h00 = 2 * t_cube - 3 * t_square + 1.;
      h10 = t_cube - 2 * t_square + t;
      h01 = -2 * t_cube + 3 * t_square;
      h11 = t_cube - t_square;
    } else if (degree_derivative == 1) {
      h00 = 6 * t_square - 6 * t;
      h10 = 3 * t_square - 4 * t + 1.;
      h01 = -6 * t_square + 6 * t;
      h11 = 3 * t_square - 2 * t;
    } else if (degree_derivative == 2) {
      h00 = 12 * t - 6.;
      h10 = 6 * t - 4.;
      h01 = -12 * t + 6.;
      h11 = 6 * t - 2.;
    } else if (degree_derivative == 3) {
      h00 = 12.;
      h10 = 6.;
      h01 = -12.;
      h11 = 6.;
    } else {
      h00 = 0.;
      h10 = 0.;
      h01 = 0.;
      h11 = 0.;
    }
  }

 private:
  /// \brief Get index of the interval (subspline) corresponding to time t for the interpolation.
  /// \param t : time where to look for interval.
  /// \return Index of interval for time t.
  ///
  std::size_t findInterval(const Numeric t) const {
    // time before first control point time.
    if (t < time_control_points_[0]) {
      return 0;
    }
    // time is after last control point time
    if (t > time_control_points_[size_ - 1]) {
      return size_ - 1;
    }

    std::size_t left_id = 0;
    std::size_t right_id = size_ - 1;
    while (left_id <= right_id) {
      const std::size_t middle_id = left_id + (right_id - left_id) / 2;
      if (time_control_points_.at(middle_id) < t) {
        left_id = middle_id + 1;
      } else if (time_control_points_.at(middle_id) > t) {
        right_id = middle_id - 1;
      } else {
        return middle_id;
330
      }
Guilhem Saurel's avatar
Guilhem Saurel committed
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    }
    return left_id - 1;
  }

  void check_conditions() const {
    if (control_points_.size() == 0) {
      throw std::runtime_error(
          "Error in cubic hermite : there is no control points set / did you use empty constructor ?");
    } else if (dim_ == 0) {
      throw std::runtime_error(
          "Error in cubic hermite : Dimension of points is zero / did you use empty constructor ?");
    }
  }

  /// \brief compute duration of each spline.
  /// For N control points with time \f$T_{P_0}, T_{P_1}, T_{P_2}, ..., T_{P_N}\f$ respectively,
  /// Duration of each subspline is : ( T_{P_1}-T_{P_0}, T_{P_2}-T_{P_1}, ..., T_{P_N}-T_{P_{N-1} ).
  ///
  void computeDurationSplines() {
    duration_splines_.clear();
    Time actual_time;
    Time prev_time = *(time_control_points_.begin());
    std::size_t i = 0;
    for (i = 0; i < size() - 1; i++) {
      actual_time = time_control_points_.at(i + 1);
      duration_splines_.push_back(actual_time - prev_time);
      prev_time = actual_time;
    }
  }

  /// \brief Check if duration of each subspline is strictly positive.
  /// \return true if all duration of strictly positive, false otherwise.
  ///
  bool checkDurationSplines() const {
    std::size_t i = 0;
    bool is_positive = true;
    while (is_positive && i < duration_splines_.size()) {
      is_positive = (duration_splines_.at(i) > 0.);
      i++;
    }
    return is_positive;
  }
  /*Operations*/

  /*Helpers*/
 public:
  /// \brief Get dimension of curve.
  /// \return dimension of curve.
  std::size_t virtual dim() const { return dim_; };
  /// \brief Get the minimum time for which the curve is defined
  /// \return \f$t_{min}\f$, lower bound of time range.
  Time virtual min() const { return time_control_points_.front(); }
  /// \brief Get the maximum time for which the curve is defined.
  /// \return \f$t_{max}\f$, upper bound of time range.
  Time virtual max() const { return time_control_points_.back(); }
386
387
388
  /// \brief Get the degree of the curve.
  /// \return \f$degree\f$, the degree of the curve.
  virtual std::size_t  degree() const {return degree_;}
Guilhem Saurel's avatar
Guilhem Saurel committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  /*Helpers*/

  /*Attributes*/
  /// Dim of curve
  std::size_t dim_;
  /// Vector of pair < Point, Tangent >.
  t_pair_point_tangent_t control_points_;
  /// Vector of Time corresponding to time of each N control points : time at \f$P_0, P_1, P_2, ..., P_N\f$.
  /// Exemple : \f$( 0., 0.5, 0.9, ..., 4.5 )\f$ with values corresponding to times for \f$P_0, P_1, P_2, ..., P_N\f$
  /// respectively.
  vector_time_t time_control_points_;
  /// Vector of Time corresponding to time duration of each subspline.<br>
  /// For N control points with time \f$T_{P_0}, T_{P_1}, T_{P_2}, ..., T_{P_N}\f$ respectively,
  /// duration of each subspline is : ( T_{P_1}-T_{P_0}, T_{P_2}-T_{P_1}, ..., T_{P_N}-T_{P_{N-1} )<br>
  /// It contains \f$N-1\f$ durations.
  vector_time_t duration_splines_;
  /// Starting time of cubic hermite spline : T_min_ is equal to first time of control points.
  /*const*/ Time T_min_;
  /// Ending time of cubic hermite spline : T_max_ is equal to last time of control points.
  /*const*/ Time T_max_;
  /// Number of control points (pairs).
  std::size_t size_;
  /// Degree (Cubic so degree 3)
  std::size_t degree_;
  /*Attributes*/

  // Serialization of the class
  friend class boost::serialization::access;

  template <class Archive>
  void serialize(Archive& ar, const unsigned int version) {
    if (version) {
      // Do something depending on version ?
    }
423
    ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(curve_abc_t);
Guilhem Saurel's avatar
Guilhem Saurel committed
424
425
426
427
428
429
430
431
432
433
434
    ar& boost::serialization::make_nvp("dim", dim_);
    ar& boost::serialization::make_nvp("control_points", control_points_);
    ar& boost::serialization::make_nvp("time_control_points", time_control_points_);
    ar& boost::serialization::make_nvp("duration_splines", duration_splines_);
    ar& boost::serialization::make_nvp("T_min", T_min_);
    ar& boost::serialization::make_nvp("T_max", T_max_);
    ar& boost::serialization::make_nvp("size", size_);
    ar& boost::serialization::make_nvp("degree", degree_);
  }
};  // End struct Cubic hermite spline
}  // namespace curves
435
#endif  //_CLASS_CUBICHERMITESPLINE