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