File: frames.cpp

package info (click to toggle)
orocos-kdl 1.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,112 kB
  • sloc: cpp: 17,296; python: 981; xml: 73; makefile: 70; sh: 21
file content (496 lines) | stat: -rw-r--r-- 19,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
233
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
330
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
386
387
388
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//Copyright  (C)  2007  Ruben Smits <ruben dot smits at intermodalics dot eu>
//
//Version: 1.0
//Author: Ruben Smits Ruben Smits <ruben dot smits at intermodalics dot eu>
//Author: Zihan Chen <zihan dot chen dot jhu at gmail dot com>
//Author: Matthijs van der Burgh <MatthijsBurgh at outlook dot com>
//Maintainer: Ruben Smits Ruben Smits <ruben dot smits at intermodalics dot eu>
//Maintainer: Matthijs van der Burgh <MatthijsBurgh at outlook dot com>
//URL: http://www.orocos.org/kdl
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


#include <kdl/frames.hpp>
#include <kdl/frames_io.hpp>
#include "PyKDL.h"

namespace py = pybind11;
using namespace KDL;


void init_frames(py::module &m)
{
    // --------------------
    // Vector
    // --------------------
    py::class_<Vector> vector(m, "Vector");
    vector.def(py::init<>());
    vector.def(py::init<double, double, double>(), py::arg("x"), py::arg("y"), py::arg("z"));
    vector.def(py::init<const Vector&>());
    vector.def("x", (void (Vector::*)(double)) &Vector::x);
    vector.def("y", (void (Vector::*)(double)) &Vector::y);
    vector.def("z", (void (Vector::*)(double)) &Vector::z);
    vector.def("x", (double (Vector::*)(void) const) &Vector::x);
    vector.def("y", (double (Vector::*)(void) const) &Vector::y);
    vector.def("z", (double (Vector::*)(void) const) &Vector::z);
    vector.def("__getitem__", [](const Vector &v, int i)
    {
        if (i < 0 || i > 2)
            throw py::index_error("Vector index out of range");

        return v(i);
    });
    vector.def("__setitem__", [](Vector &v, int i, double value)
    {
        if (i < 0 || i > 2)
            throw py::index_error("Vector index out of range");

        v(i) = value;
    });
    vector.def("__repr__", [](const Vector &v)
    {
        std::ostringstream oss;
        oss << v;
        return oss.str();
    });
    vector.def("ReverseSign", &Vector::ReverseSign);
    vector.def(py::self -= py::self);
    vector.def(py::self += py::self);
    vector.def(py::self + py::self);
    vector.def(py::self - py::self);
    vector.def(py::self * double());
    vector.def(double() * py::self);
    vector.def(py::self / double());
    vector.def(py::self * py::self);
    vector.def(py::self == py::self);
    vector.def(py::self != py::self);
    vector.def("__neg__", [](const Vector &a)
    {
        return operator-(a);
    }, py::is_operator());
    vector.def("__copy__", [](const Vector& self)
    {
        return Vector(self);
    });
    vector.def("__deepcopy__", [](const Vector& self, py::dict)
    {
        return Vector(self);
    }, py::arg("memo"));
    vector.def_static("Zero", &Vector::Zero);
    vector.def("Norm", &Vector::Norm, py::arg("eps")=epsilon);
    vector.def("Normalize", &Vector::Normalize, py::arg("eps")=epsilon);
    vector.def(py::pickle(
            [](const Vector &v)
            { // __getstate__
                /* Return a tuple that fully encodes the state of the object */
                return py::make_tuple(v.x(), v.y(), v.z());
            },
            [](py::tuple t)
            { // __setstate__
                if (t.size() != 3)
                    throw std::runtime_error("Invalid state!");

                /* Create a new C++ instance */
                Vector v(t[0].cast<double>(), t[1].cast<double>(), t[2].cast<double>());
                return v;
            }));

    m.def("SetToZero", (void (*)(Vector&)) &KDL::SetToZero);
    m.def("dot", (double (*)(const Vector&, const Vector&)) &KDL::dot);
    m.def("Equal", (bool (*)(const Vector&, const Vector&, double)) &KDL::Equal,
          py::arg("a"), py::arg("b"), py::arg("eps")=epsilon);


    // --------------------
    // Wrench
    // --------------------
    py::class_<Wrench> wrench(m, "Wrench");
    wrench.def(py::init<>());
    wrench.def(py::init<const Vector&, const Vector&>(), py::arg("force"), py::arg("torque"));
    wrench.def(py::init<const Wrench&>());
    wrench.def_readwrite("force", &Wrench::force);
    wrench.def_readwrite("torque", &Wrench::torque);
    wrench.def("__getitem__", [](const Wrench &t, int i)
    {
        if (i < 0 || i > 5)
            throw py::index_error("Wrench index out of range");

        return t(i);
    });
    wrench.def("__setitem__", [](Wrench &t, int i, double value)
    {
        if (i < 0 || i > 5)
            throw py::index_error("Wrench index out of range");

        t(i) = value;
    });
    wrench.def("__repr__", [](const Wrench &t)
    {
        std::ostringstream oss;
        oss << t;
        return oss.str();
    });
    wrench.def("__copy__", [](const Wrench& self)
    {
        return Wrench(self);
    });
    wrench.def("__deepcopy__", [](const Wrench& self, py::dict)
    {
        return Wrench(self);
    }, py::arg("memo"));
    wrench.def_static("Zero", &Wrench::Zero);
    wrench.def("ReverseSign", &Wrench::ReverseSign);
    wrench.def("RefPoint", &Wrench::RefPoint, py::arg("base"));
    wrench.def(py::self -= py::self);
    wrench.def(py::self += py::self);
    wrench.def(py::self * double());
    wrench.def(double() * py::self);
    wrench.def(py::self / double());
    wrench.def(py::self + py::self);
    wrench.def(py::self - py::self);
    wrench.def(py::self == py::self);
    wrench.def(py::self != py::self);
    wrench.def("__neg__", [](const Wrench &w)
    {
        return operator-(w);
    }, py::is_operator());
    wrench.def(py::pickle(
            [](const Wrench &wr)
            { // __getstate__
                /* Return a tuple that fully encodes the state of the object */
                return py::make_tuple(wr.force, wr.torque);
            },
            [](py::tuple t)
            { // __setstate__
                if (t.size() != 2)
                    throw std::runtime_error("Invalid state!");

                /* Create a new C++ instance */
                Wrench wr(t[0].cast<Vector>(), t[1].cast<Vector>());
                return wr;
            }));

    m.def("SetToZero", (void (*)(Wrench&)) &KDL::SetToZero);
    m.def("Equal", (bool (*)(const Wrench&, const Wrench&, double eps)) &KDL::Equal,
          py::arg("a"), py::arg("b"), py::arg("eps")=epsilon);


    // --------------------
    // Twist
    // --------------------
    py::class_<Twist> twist(m, "Twist");
    twist.def(py::init<>());
    twist.def(py::init<const Vector&, const Vector&>(), py::arg("vel"), py::arg("rot"));
    twist.def(py::init<const Twist&>());
    twist.def_readwrite("vel", &Twist::vel);
    twist.def_readwrite("rot", &Twist::rot);
    twist.def("__getitem__", [](const Twist &t, int i)
    {
        if (i < 0 || i > 5)
            throw py::index_error("Twist index out of range");

        return t(i);
    });
    twist.def("__setitem__", [](Twist &t, int i, double value)
    {
        if (i < 0 || i > 5)
            throw py::index_error("Twist index out of range");

        t(i) = value;
    });
    twist.def("__repr__", [](const Twist &t)
    {
        std::ostringstream oss;
        oss << t;
        return oss.str();
    });
    twist.def("__copy__", [](const Twist& self)
    {
        return Twist(self);
    });
    twist.def("__deepcopy__", [](const Twist& self, py::dict)
    {
        return Twist(self);
    }, py::arg("memo"));
    twist.def_static("Zero", &Twist::Zero);
    twist.def("ReverseSign", &Twist::ReverseSign);
    twist.def("RefPoint", &Twist::RefPoint, py::arg("base"));
    twist.def(py::self -= py::self);
    twist.def(py::self += py::self);
    twist.def(py::self * double());
    twist.def(double() * py::self);
    twist.def(py::self / double());
    twist.def(py::self + py::self);
    twist.def(py::self - py::self);
    twist.def(py::self == py::self);
    twist.def(py::self != py::self);
    twist.def("__neg__", [](const Twist &a)
    {
        return operator-(a);
    }, py::is_operator());
    twist.def(py::pickle(
            [](const Twist &tt)
            { // __getstate__
                /* Return a tuple that fully encodes the state of the object */
                return py::make_tuple(tt.vel, tt.rot);
            },
            [](py::tuple t)
            { // __setstate__
                if (t.size() != 2)
                    throw std::runtime_error("Invalid state!");

                /* Create a new C++ instance */
                Twist tt(t[0].cast<Vector>(), t[1].cast<Vector>());
                return tt;
            }));

    m.def("dot", (double (*)(const Twist&, const Wrench&)) &KDL::dot);
    m.def("dot", (double (*)(const Wrench&, const Twist&)) &KDL::dot);
    m.def("SetToZero", (void (*)(Twist&)) &KDL::SetToZero);
    m.def("Equal", (bool (*)(const Twist&, const Twist&, double eps)) &KDL::Equal,
          py::arg("a"), py::arg("b"), py::arg("eps")=epsilon);


    // --------------------
    // Rotation
    // --------------------
    py::class_<Rotation> rotation(m, "Rotation");
    rotation.def(py::init<>());
    rotation.def(py::init<double, double, double, double, double, double, double, double, double>(),
                 py::arg("xx"), py::arg("yx"), py::arg("zx"),
                 py::arg("xy"), py::arg("yy"), py::arg("zy"),
                 py::arg("xz"), py::arg("yz"), py::arg("zz"));
    rotation.def(py::init<const Vector&, const Vector&, const Vector&>(),
                 py::arg("x"), py::arg("y"), py::arg("z"));
    rotation.def(py::init<const Rotation&>());
    rotation.def("__getitem__", [](const Rotation &r, std::tuple<int, int> idx)
    {
        int i = std::get<0>(idx);
        int j = std::get<1>(idx);
        if (i < 0 || i > 2 || j < 0 || j > 2)
            throw py::index_error("Rotation index out of range");

        return r(i, j);
    });
    rotation.def("__setitem__", [](Rotation &r, std::tuple<int, int> idx, double value)
    {
        int i = std::get<0>(idx);
        int j = std::get<1>(idx);
        if (i < 0 || i > 2 || j < 0 || j > 2)
            throw py::index_error("Rotation index out of range");

        r(i, j) = value;
    });
    rotation.def("__repr__", [](const Rotation &r)
    {
            std::ostringstream oss;
            oss << r;
            return oss.str();
    });
    rotation.def("__copy__", [](const Rotation& self)
    {
        return Rotation(self);
    });
    rotation.def("__deepcopy__", [](const Rotation& self, py::dict)
    {
        return Rotation(self);
    }, py::arg("memo"));
    rotation.def("SetInverse", &Rotation::SetInverse);
    rotation.def("Inverse", (Rotation (Rotation::*)(void) const) &Rotation::Inverse);
    rotation.def("Inverse", (Vector (Rotation::*)(const Vector&) const) &Rotation::Inverse);
    rotation.def("Inverse", (Wrench (Rotation::*)(const Wrench&) const) &Rotation::Inverse);
    rotation.def("Inverse", (Twist (Rotation::*)(const Twist&) const) &Rotation::Inverse);
    rotation.def_static("Identity", &Rotation::Identity);
    rotation.def_static("RotX", &Rotation::RotX);
    rotation.def_static("RotY", &Rotation::RotY);
    rotation.def_static("RotZ", &Rotation::RotZ);
    rotation.def_static("Rot", &Rotation::Rot);
    rotation.def_static("Rot2", &Rotation::Rot2);
    rotation.def_static("EulerZYZ", &Rotation::EulerZYZ);
    rotation.def_static("RPY", &Rotation::RPY);
    rotation.def_static("EulerZYX", &Rotation::EulerZYX);
    rotation.def_static("Quaternion", &Rotation::Quaternion, py::arg("x"), py::arg("y"), py::arg("z"), py::arg("w"));
    rotation.def("DoRotX", &Rotation::DoRotX);
    rotation.def("DoRotY", &Rotation::DoRotY);
    rotation.def("DoRotZ", &Rotation::DoRotZ);
    rotation.def("GetRot", &Rotation::GetRot);
    rotation.def("GetRotAngle", [](const Rotation &r, double eps)
    {
        Vector axis;
        double ret = r.GetRotAngle(axis, eps);
        return py::make_tuple(ret, axis);
    }, py::arg("eps") = epsilon);
    rotation.def("GetEulerZYZ", [](const Rotation &r)
    {
        double Alfa, Beta, Gamma;
        r.GetEulerZYZ(Alfa, Beta, Gamma);
        return py::make_tuple(Alfa, Beta, Gamma);
    });
    rotation.def("GetRPY", [](const Rotation &r)
    {
        double roll, pitch, yaw;
        r.GetRPY(roll, pitch, yaw);
        return py::make_tuple(roll, pitch, yaw);
    });
    rotation.def("GetEulerZYX", [](const Rotation &r)
    {
        double Alfa, Beta, Gamma;
        r.GetEulerZYX(Alfa, Beta, Gamma);
        return py::make_tuple(Alfa, Beta, Gamma);
    });
    rotation.def("GetQuaternion", [](const Rotation &r)
    {
        double x, y, z, w;
        r.GetQuaternion(x, y, z, w);
        return py::make_tuple(x, y, z, w);
    });
    rotation.def("UnitX", (Vector (Rotation::*)() const) &Rotation::UnitX);
    rotation.def("UnitY", (Vector (Rotation::*)() const) &Rotation::UnitY);
    rotation.def("UnitZ", (Vector (Rotation::*)() const) &Rotation::UnitZ);
    rotation.def("UnitX", (void (Rotation::*)(const Vector&)) &Rotation::UnitX);
    rotation.def("UnitY", (void (Rotation::*)(const Vector&)) &Rotation::UnitY);
    rotation.def("UnitZ", (void (Rotation::*)(const Vector&)) &Rotation::UnitZ);
    rotation.def(py::self * Vector());
    rotation.def(py::self * Twist());
    rotation.def(py::self * Wrench());
    rotation.def(py::self == py::self);
    rotation.def(py::self != py::self);
    rotation.def(py::self * py::self);
    rotation.def(py::pickle(
            [](const Rotation &rot)
            { // __getstate__
                /* Return a tuple that fully encodes the state of the object */
                double roll{0}, pitch{0}, yaw{0};
                rot.GetRPY(roll, pitch, yaw);
                return py::make_tuple(roll, pitch, yaw);
            },
            [](py::tuple t)
            { // __setstate__
                if (t.size() != 3)
                    throw std::runtime_error("Invalid state!");

                /* Create a new C++ instance */
                return Rotation::RPY(t[0].cast<double>(), t[1].cast<double>(), t[2].cast<double>());
            }));

    m.def("Equal", (bool (*)(const Rotation&, const Rotation&, double eps)) &KDL::Equal,
          py::arg("a"), py::arg("b"), py::arg("eps")=epsilon);


    // --------------------
    // Frame
    // --------------------
    py::class_<Frame> frame(m, "Frame");
    frame.def(py::init<const Rotation&, const Vector&>(), py::arg("R"), py::arg("V"));
    frame.def(py::init<const Vector&>());
    frame.def(py::init<const Rotation&>());
    frame.def(py::init<const Frame&>());
    frame.def(py::init<>());
    frame.def_readwrite("M", &Frame::M);
    frame.def_readwrite("p", &Frame::p);
    frame.def("__getitem__", [](const Frame &frm, std::tuple<int, int> idx)
    {
        int i = std::get<0>(idx);
        int j = std::get<1>(idx);
        if (i < 0 || i > 2 || j < 0 || j > 3)
            throw py::index_error("Frame index out of range");

        return frm(i, j);
    });
    frame.def("__setitem__", [](Frame &frm, std::tuple<int, int> idx, double value)
    {
        int i = std::get<0>(idx);
        int j = std::get<1>(idx);
        if (i < 0 || i > 2 || j < 0 || j > 3)
            throw py::index_error("Frame index out of range");

        if (j == 3)
            frm.p(i) = value;
        else
            frm.M(i, j) = value;
    });
    frame.def("__repr__", [](const Frame &frm)
    {
        std::ostringstream oss;
        oss << frm;
        return oss.str();
    });
    frame.def("__copy__", [](const Frame& self)
    {
        return Frame(self);
    });
    frame.def("__deepcopy__", [](const Frame& self, py::dict)
    {
        return Frame(self);
    }, py::arg("memo"));
    frame.def("DH_Craig1989", &Frame::DH_Craig1989);
    frame.def("DH", &Frame::DH);
    frame.def("Inverse", (Frame (Frame::*)() const) &Frame::Inverse);
    frame.def("Inverse", (Vector (Frame::*)(const Vector&) const) &Frame::Inverse);
    frame.def("Inverse", (Wrench (Frame::*)(const Wrench&) const) &Frame::Inverse);
    frame.def("Inverse", (Twist (Frame::*)(const Twist&) const) &Frame::Inverse);
    frame.def_static("Identity", &Frame::Identity);
    frame.def("Integrate", &Frame::Integrate);
    frame.def(py::self * Vector());
    frame.def(py::self * Wrench());
    frame.def(py::self * Twist());
    frame.def(py::self * py::self);
    frame.def(py::self == py::self);
    frame.def(py::self != py::self);
    frame.def(py::pickle(
            [](const Frame &frm)
            { // __getstate__
                /* Return a tuple that fully encodes the state of the object */
                return py::make_tuple(frm.M, frm.p);
            },
            [](py::tuple t)
            { // __setstate__
                if (t.size() != 2)
                    throw std::runtime_error("Invalid state!");

                /* Create a new C++ instance */
                Frame frm(t[0].cast<Rotation>(), t[1].cast<Vector>());
                return frm;
            }));

    m.def("Equal", (bool (*)(const Frame&, const Frame&, double eps)) &KDL::Equal,
          py::arg("a"), py::arg("b"), py::arg("eps")=epsilon);


    // --------------------
    // Global
    // --------------------
    m.def("diff", (Vector (*)(const Vector&, const Vector&, double dt)) &KDL::diff,
          py::arg("a"), py::arg("b"), py::arg("dt") = 1);
    m.def("diff", (Vector (*)(const Rotation&, const Rotation&, double dt)) &KDL::diff,
          py::arg("a"), py::arg("b"), py::arg("dt") = 1);
    m.def("diff", (Twist (*)(const Frame&, const Frame&, double dt)) &KDL::diff,
          py::arg("a"), py::arg("b"), py::arg("dt") = 1);
    m.def("diff", (Twist (*)(const Twist&, const Twist&, double dt)) &KDL::diff,
          py::arg("a"), py::arg("b"), py::arg("dt") = 1);
    m.def("diff", (Wrench (*)(const Wrench&, const Wrench&, double dt)) &KDL::diff,
          py::arg("a"), py::arg("b"), py::arg("dt") = 1);
    m.def("addDelta", (Vector (*)(const Vector&, const Vector&, double dt)) &KDL::addDelta,
          py::arg("a"), py::arg("da"), py::arg("dt") = 1);
    m.def("addDelta", (Rotation (*)(const Rotation&, const Vector&, double dt)) &KDL::addDelta,
          py::arg("a"), py::arg("da"), py::arg("dt") = 1);
    m.def("addDelta", (Frame (*)(const Frame&, const Twist&, double dt)) &KDL::addDelta,
          py::arg("a"), py::arg("da"), py::arg("dt") = 1);
    m.def("addDelta", (Twist (*)(const Twist&, const Twist&, double dt)) &KDL::addDelta,
          py::arg("a"), py::arg("da"), py::arg("dt") = 1);
    m.def("addDelta", (Wrench (*)(const Wrench&, const Wrench&, double dt)) &KDL::addDelta,
          py::arg("a"), py::arg("da"), py::arg("dt") = 1);
}