spline_python.cpp 6.67 KB
Newer Older
1
#include "spline/bezier_curve.h"
Steve Tonneau's avatar
Steve Tonneau committed
2
#include "spline/spline_curve.h"
Steve Tonneau's avatar
Steve Tonneau committed
3
#include "spline/exact_cubic.h"
4
#include "spline/spline_deriv_constraint.h"
5
6
7
8
9
10
11
12
13
14
15

#include <vector>

#include <eigenpy/memory.hpp>
#include <eigenpy/eigenpy.hpp>

#include <boost/python.hpp>

/*** TEMPLATE SPECIALIZATION FOR PYTHON ****/
typedef double real;
typedef Eigen::Vector3d point_t;
16
typedef Eigen::Matrix<double, 3, 1, 0, 3, 1> ret_point_t;
Steve Tonneau's avatar
Steve Tonneau committed
17
typedef Eigen::VectorXd time_waypoints_t;
Steve Tonneau's avatar
Steve Tonneau committed
18
typedef Eigen::Matrix<real, 3, Eigen::Dynamic> point_list_t;
19
20
21
22
23
typedef std::vector<point_t,Eigen::aligned_allocator<point_t> >  t_point_t;
typedef std::pair<real, point_t> Waypoint;
typedef std::vector<Waypoint> T_Waypoint;

typedef spline::bezier_curve  <real, real, 3, true, point_t> bezier_t;
Steve Tonneau's avatar
Steve Tonneau committed
24
typedef spline::spline_curve  <real, real, 3, true, point_t, t_point_t> spline_curve_t;
Steve Tonneau's avatar
Steve Tonneau committed
25
typedef spline::exact_cubic  <real, real, 3, true, point_t, t_point_t> exact_cubic_t;
Steve Tonneau's avatar
Steve Tonneau committed
26
typedef spline_curve_t::coeff_t coeff_t;
Steve Tonneau's avatar
Steve Tonneau committed
27
28
typedef std::pair<real, point_t> waypoint_t;
typedef std::vector<waypoint_t, Eigen::aligned_allocator<point_t> > t_waypoint_t;
29
30
31
32


typedef spline::spline_deriv_constraint  <real, real, 3, true, point_t, t_point_t> spline_deriv_constraint_t;
typedef spline_deriv_constraint_t::spline_constraints spline_constraints_t;
33
34
35
/*** TEMPLATE SPECIALIZATION FOR PYTHON ****/

EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(bezier_t)
36
37
38
39
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(spline_curve_t)
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(exact_cubic_t)
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(spline_constraints_t)
EIGENPY_DEFINE_STRUCT_ALLOCATOR_SPECIALIZATION(spline_deriv_constraint_t)
40
41
42
43
44
45
46
47

namespace spline
{
using namespace boost::python;

t_point_t vectorFromEigenArray(const point_list_t& array)
{
    t_point_t res;
Steve Tonneau's avatar
Steve Tonneau committed
48
49
    for(int i =0;i<array.cols();++i)
        res.push_back(array.col(i));
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    return res;
}

bezier_t* wrapBezierConstructor(const point_list_t& array)
{
    t_point_t asVector = vectorFromEigenArray(array);
    return new bezier_t(asVector.begin(), asVector.end());
}


bezier_t* wrapBezierConstructorBounds(const point_list_t& array, const real lb, const real ub)
{
    t_point_t asVector = vectorFromEigenArray(array);
    return new bezier_t(asVector.begin(), asVector.end(), lb, ub);
}

Steve Tonneau's avatar
Steve Tonneau committed
66
67
68
69
70
71
spline_curve_t* wrapSplineConstructor(const coeff_t& array)
{
    return new spline_curve_t(array, 0., 1.);
}


Steve Tonneau's avatar
Steve Tonneau committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
t_waypoint_t getWayPoints(const coeff_t& array, const time_waypoints_t& time_wp)
{
    t_waypoint_t res;
    for(int i =0;i<array.cols();++i)
        res.push_back(std::make_pair(time_wp(i), array.col(i)));
    return res;
}

exact_cubic_t* wrapExactCubicConstructor(const coeff_t& array, const time_waypoints_t& time_wp)
{
    t_waypoint_t wps = getWayPoints(array, time_wp);
    return new exact_cubic_t(wps.begin(), wps.end());
}

Steve Tonneau's avatar
Steve Tonneau committed
86

87
88
89
90
91
92
93
94
95
96
97
98
spline_deriv_constraint_t* wrapSplineDerivConstraint(const coeff_t& array, const time_waypoints_t& time_wp, const spline_constraints_t& constraints)
{
    t_waypoint_t wps = getWayPoints(array, time_wp);
    return new spline_deriv_constraint_t(wps.begin(), wps.end(),constraints);
}

spline_deriv_constraint_t* wrapSplineDerivConstraintNoConstraints(const coeff_t& array, const time_waypoints_t& time_wp)
{
    t_waypoint_t wps = getWayPoints(array, time_wp);
    return new spline_deriv_constraint_t(wps.begin(), wps.end());
}

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
point_t get_init_vel(const spline_constraints_t& c)
{
    return c.init_vel;
}

point_t get_init_acc(const spline_constraints_t& c)
{
    return c.init_acc;
}

point_t get_end_vel(const spline_constraints_t& c)
{
    return c.end_vel;
}

point_t get_end_acc(const spline_constraints_t& c)
{
    return c.end_acc;
}

void set_init_vel(spline_constraints_t& c, const point_t& val)
{
    c.init_vel = val;
}

void set_init_acc(spline_constraints_t& c, const point_t& val)
{
    c.init_acc = val;
}

void set_end_vel(spline_constraints_t& c, const point_t& val)
{
    c.end_vel = val;
}

void set_end_acc(spline_constraints_t& c, const point_t& val)
{
    c.end_acc = val;
}



141
142
143
144
145
146
BOOST_PYTHON_MODULE(spline)
{
    /** BEGIN eigenpy init**/
    eigenpy::enableEigenPy();

    eigenpy::enableEigenPySpecific<point_t,point_t>();
147
    eigenpy::enableEigenPySpecific<ret_point_t,ret_point_t>();
148
    eigenpy::enableEigenPySpecific<point_list_t,point_list_t>();
Steve Tonneau's avatar
Steve Tonneau committed
149
    eigenpy::enableEigenPySpecific<coeff_t,coeff_t>();
150
151
    /*eigenpy::exposeAngleAxis();
    eigenpy::exposeQuaternion();*/
Steve Tonneau's avatar
Steve Tonneau committed
152
    /** END eigenpy init**/
153

Steve Tonneau's avatar
Steve Tonneau committed
154
    /** BEGIN bezier curve**/
155
156
157
158
159
160
161
    class_<bezier_t>
        ("bezier", no_init)
            .def("__init__", make_constructor(&wrapBezierConstructor))
            .def("__init__", make_constructor(&wrapBezierConstructorBounds))
            .def("min", &bezier_t::min)
            .def("max", &bezier_t::max)
            .def("__call__", &bezier_t::operator())
162
163
            .def("derivate", &bezier_t::derivate)
            .def("compute_derivate", &bezier_t::compute_derivate)
164
        ;
Steve Tonneau's avatar
Steve Tonneau committed
165
166
167
168
169
170
171
172
173
174
175
176
    /** END bezier curve**/


    /** BEGIN spline curve function**/
    class_<spline_curve_t>("spline",  init<const spline_curve_t::coeff_t, const real, const real >())
            .def("__init__", make_constructor(&wrapSplineConstructor))
            .def("min", &spline_curve_t::min)
            .def("max", &spline_curve_t::max)
            .def("__call__", &spline_curve_t::operator())
            .def("derivate", &spline_curve_t::derivate)
        ;
    /** END cubic function**/
177
178


Steve Tonneau's avatar
Steve Tonneau committed
179
180
181
182
183
184
185
186
187
188
    /** BEGIN exact_cubic curve**/
    class_<exact_cubic_t>
        ("exact_cubic", no_init)
            .def("__init__", make_constructor(&wrapExactCubicConstructor))
            .def("min", &exact_cubic_t::min)
            .def("max", &exact_cubic_t::max)
            .def("__call__", &exact_cubic_t::operator())
            .def("derivate", &exact_cubic_t::derivate)
        ;
    /** END bezier curve**/
189

190
191
192
193

    /** BEGIN spline constraints**/
    class_<spline_constraints_t>
        ("spline_constraints", init<>())
194
195
196
197
            .add_property("init_vel", &get_init_vel, &set_init_vel)
            .add_property("init_acc", &get_init_acc, &set_init_acc)
            .add_property("end_vel", &get_end_vel, &set_end_vel)
            .add_property("end_acc", &get_end_acc, &set_end_acc)
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        ;
    /** END spline constraints**/


    /** BEGIN spline_deriv_constraints**/
    class_<spline_deriv_constraint_t>
        ("spline_deriv_constraint", no_init)
            .def("__init__", make_constructor(&wrapSplineDerivConstraint))
            .def("__init__", make_constructor(&wrapSplineDerivConstraintNoConstraints))
            .def("min", &exact_cubic_t::min)
            .def("max", &exact_cubic_t::max)
            .def("__call__", &exact_cubic_t::operator())
            .def("derivate", &exact_cubic_t::derivate)
        ;
    /** END spline_deriv_constraints**/


215
216
217
}

} // namespace spline