curve_conversion.h
Go to the documentation of this file.
1 #ifndef _CLASS_CURVE_CONVERSION
2 #define _CLASS_CURVE_CONVERSION
3 
4 #include "curve_abc.h"
5 #include "bernstein.h"
6 #include "curve_constraint.h"
7 
8 #include "MathDefs.h"
9 
10 #include <vector>
11 #include <stdexcept>
12 
13 #include <iostream>
14 
15 namespace curves {
19 template <typename Polynomial>
20 Polynomial polynomial_from_curve(const typename Polynomial::curve_abc_t& curve) {
21  typedef typename Polynomial::t_point_t t_point_t;
22  typedef typename Polynomial::num_t num_t;
23  t_point_t coefficients;
24  coefficients.push_back(curve(curve.min()));
25  num_t fact = 1;
26  for (std::size_t i = 1; i <= curve.degree(); ++i) {
27  fact *= (num_t)i;
28  coefficients.push_back(curve.derivate(curve.min(), i) / fact);
29  }
30 
31  return Polynomial(coefficients, curve.min(), curve.max());
32 }
33 
37 template <typename Bezier>
38 Bezier bezier_from_curve(const typename Bezier::curve_abc_t& curve) {
39  if(curve.degree() > 3)
40  throw std::invalid_argument("bezier_from_curve is only implemented for curves of degree <= 3.");
41  typedef typename Bezier::point_t point_t;
42  typedef typename Bezier::t_point_t t_point_t;
43  typedef typename Bezier::num_t num_t;
44  num_t T_min = curve.min();
45  num_t T_max = curve.max();
46  num_t T = T_max - T_min;
47  // Positions and derivatives
48  point_t p0 = curve(T_min);
49  point_t p1 = curve(T_max);
50  point_t m0 = curve.derivate(T_min, 1);
51  point_t m1 = curve.derivate(T_max, 1);
52  // Convert to bezier control points
53  // for t in [Tmin,Tmax] and T=Tmax-Tmin : x'(0)=3(b_p1-b_p0)/T and x'(1)=3(b_p3-b_p2)/T
54  // so : m0=3(b_p1-b_p0)/T and m1=3(b_p3-b_p2)/T
55  // <=> b_p1=T(m0/3)+b_p0 and b_p2=-T(m1/3)+b_p3
56  point_t b_p0 = p0;
57  point_t b_p3 = p1;
58  point_t b_p1 = T * m0 / 3 + b_p0;
59  point_t b_p2 = -T * m1 / 3 + b_p3;
60  t_point_t control_points;
61  control_points.push_back(b_p0);
62  control_points.push_back(b_p1);
63  control_points.push_back(b_p2);
64  control_points.push_back(b_p3);
65  return Bezier(control_points.begin(), control_points.end(), curve.min(), curve.max());
66 }
67 
71 template <typename Hermite>
72 Hermite hermite_from_curve(const typename Hermite::curve_abc_t& curve) {
73  if(curve.degree() > 3)
74  throw std::invalid_argument("hermite_from_curve is only implemented for curves of degree <= 3.");
75  typedef typename Hermite::pair_point_tangent_t pair_point_tangent_t;
76  typedef typename Hermite::t_pair_point_tangent_t t_pair_point_tangent_t;
77  typedef typename Hermite::point_t point_t;
78  typedef typename Hermite::num_t num_t;
79  num_t T_min = curve.min();
80  num_t T_max = curve.max();
81  // Positions and derivatives
82  point_t p0 = curve(T_min);
83  point_t p1 = curve(T_max);
84  point_t m0 = curve.derivate(T_min, 1);
85  point_t m1 = curve.derivate(T_max, 1);
86  // Create pairs pos/vel
87  pair_point_tangent_t pair0(p0, m0);
88  pair_point_tangent_t pair1(p1, m1);
89  t_pair_point_tangent_t control_points;
90  control_points.push_back(pair0);
91  control_points.push_back(pair1);
92  std::vector<double> time_control_points;
93  time_control_points.push_back(T_min);
94  time_control_points.push_back(T_max);
95  return Hermite(control_points.begin(), control_points.end(), time_control_points);
96 }
97 } // namespace curves
98 #endif //_CLASS_CURVE_CONVERSION
Polynomial polynomial_from_curve(const typename Polynomial::curve_abc_t &curve)
Converts a cubic hermite spline or a bezier curve to a polynomial.
Definition: curve_conversion.h:20
Bezier bezier_from_curve(const typename Bezier::curve_abc_t &curve)
Converts a cubic hermite spline or polynomial of order 3 or less to a cubic bezier curve...
Definition: curve_conversion.h:38
Hermite hermite_from_curve(const typename Hermite::curve_abc_t &curve)
Converts a polynomial of order 3 or less/cubic bezier curve to a cubic hermite spline.
Definition: curve_conversion.h:72
interface for a Curve of arbitrary dimension.
Definition: bernstein.h:20
curve_abc< double, double, true, pointX_t, pointX_t > curve_abc_t
Definition: fwd.h:73
struct to define constraints on start / end velocities and acceleration on a curve ...