conversions.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019-2021 CNRS INRIA
3 //
4 
5 #include <sstream>
6 
9 
10 namespace pinocchio
11 {
12  namespace python
13  {
14  namespace bp = boost::python;
15 
17  {
19  typedef context::SE3 SE3;
21  enum
22  {
24  };
25  typedef Eigen::Matrix<Scalar, 7, 1, Options> Vector7s;
26  typedef Eigen::Map<SE3::Quaternion> QuatMap;
27  typedef Eigen::Map<const SE3::Quaternion> QuatConstMap;
28 
29  static VectorXs fromSE3(const SE3 & M)
30  {
31  Vector7s res;
32  res.head<3>() = M.translation();
33  QuatMap(res.tail<4>().data()) = M.rotation();
34  return res;
35  }
36 
37  static bp::tuple fromSE3tuple(const SE3 & M)
38  {
39  const SE3::Quaternion q(M.rotation());
40  return bp::make_tuple(
41  M.translation()(0), M.translation()(1), M.translation()(2), q.x(), q.y(), q.z(), q.w());
42  }
43 
44  template<typename TupleOrList>
45  static SE3 toSE3fromTupleOrList(const TupleOrList & v)
46  {
47 
48  bp::ssize_t size = bp::len(v);
49  if (size != 7)
50  {
51  throw std::invalid_argument(
52  "Wrong size: v(" + std::to_string(size) + ") should have 7 elements");
53  }
54 
55  // bp::extract<SE3::Scalar> to_double;
56  const Scalar & v0 = bp::extract<Scalar>(v[0]);
57  const Scalar & v1 = bp::extract<Scalar>(v[1]);
58  const Scalar & v2 = bp::extract<Scalar>(v[2]);
59  const Scalar & v3 = bp::extract<Scalar>(v[3]);
60  const Scalar & v4 = bp::extract<Scalar>(v[4]);
61  const Scalar & v5 = bp::extract<Scalar>(v[5]);
62  const Scalar & v6 = bp::extract<Scalar>(v[6]);
63 
64  SE3::Quaternion q(v6, v3, v4, v5);
65  SE3::Vector3 t(v0, v1, v2);
66  return SE3(q.matrix(), t);
67  }
68 
69  template<typename Vector7Like>
70  SE3 XYZQUATToSE3_ei(const Vector7Like & v)
71  {
72  if (v.rows() != 7 || v.cols() != 1)
73  {
74  std::ostringstream shape;
75  shape << "(" << v.rows() << ", " << v.cols() << ")";
76  throw std::invalid_argument(
77  "Wrong size: v" + shape.str() + " but should have the following shape (7, 1)");
78  }
79  QuatConstMap q(v.template tail<4>().data());
80  return SE3(q.matrix(), v.template head<3>());
81  }
82 
83  template<typename Vector7Like>
84  static SE3 toSE3(const Vector7Like & v)
85  {
86  if (v.rows() != 7 || v.cols() != 1)
87  {
88  std::ostringstream shape;
89  shape << "(" << v.rows() << ", " << v.cols() << ")";
90  throw std::invalid_argument(
91  "Wrong size: v" + shape.str() + " but should have the following shape (7, 1)");
92  }
93 
94  PINOCCHIO_ASSERT_MATRIX_SPECIFIC_SIZE(Vector7Like, v, 7, 1);
95  QuatConstMap q(v.template tail<4>().data());
96  return SE3(q.matrix(), v.template head<3>());
97  }
98 
99  static void expose()
100  {
101 
102  const char * doc1 = "Convert the input SE3 object to a numpy array.";
103  bp::def("SE3ToXYZQUAT", fromSE3, "M", doc1);
104  const char * doc1_tuple =
105  "Convert the input SE3 object to a 7D tuple of floats [X,Y,Z,x,y,z,w].";
106  bp::def("SE3ToXYZQUATtuple", fromSE3tuple, "M", doc1_tuple);
107 
108  const char * doc2 =
109  "Reverse function of SE3ToXYZQUAT: convert [X,Y,Z,x,y,z,w] to an SE3 element.";
110  bp::def(
111  "XYZQUATToSE3", static_cast<SE3 (*)(const bp::tuple &)>(toSE3fromTupleOrList<bp::tuple>),
112  bp::arg("tuple"), doc2);
113  bp::def(
114  "XYZQUATToSE3", static_cast<SE3 (*)(const bp::list &)>(toSE3fromTupleOrList<bp::list>),
115  bp::arg("list"), doc2);
116  bp::def(
117  "XYZQUATToSE3", static_cast<SE3 (*)(const VectorXs &)>(toSE3<VectorXs>), bp::arg("array"),
118  doc2);
119  }
120  };
121 
123  {
125  }
126 
127  } // namespace python
128 } // namespace pinocchio
boost::python
pinocchio::SE3Tpl< Scalar, Options >::Vector3
traits< SE3Tpl >::Vector3 Vector3
Definition: spatial/se3-tpl.hpp:55
pinocchio::python::exposeConversions
void exposeConversions()
Definition: conversions.cpp:122
meshcat-viewer.v0
int v0
Definition: meshcat-viewer.py:88
pinocchio::SE3Tpl< Scalar, Options >
pinocchio::python::XYZQUATConverter::fromSE3
static VectorXs fromSE3(const SE3 &M)
Definition: conversions.cpp:29
pinocchio::python::XYZQUATConverter::expose
static void expose()
Definition: conversions.cpp:99
pinocchio::python::XYZQUATConverter::XYZQUATToSE3_ei
SE3 XYZQUATToSE3_ei(const Vector7Like &v)
Definition: conversions.cpp:70
pinocchio::python::XYZQUATConverter::toSE3fromTupleOrList
static SE3 toSE3fromTupleOrList(const TupleOrList &v)
Definition: conversions.cpp:45
pinocchio::context::VectorXs
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > VectorXs
Definition: context/generic.hpp:47
python
pinocchio::python::v
const Vector3Like & v
Definition: bindings/python/spatial/explog.hpp:66
pinocchio::python::XYZQUATConverter::VectorXs
context::VectorXs VectorXs
Definition: conversions.cpp:20
size
FCL_REAL size() const
pinocchio::python::XYZQUATConverter::QuatMap
Eigen::Map< SE3::Quaternion > QuatMap
Definition: conversions.cpp:26
M
M
pinocchio::python::XYZQUATConverter::Vector7s
Eigen::Matrix< Scalar, 7, 1, Options > Vector7s
Definition: conversions.cpp:25
pinocchio::q
JointCollectionTpl const Eigen::MatrixBase< ConfigVectorType > & q
Definition: joint-configuration.hpp:1083
pinocchio::python::context::Options
@ Options
Definition: bindings/python/context/generic.hpp:40
pinocchio::SE3Tpl< Scalar, Options >::Quaternion
Eigen::Quaternion< Scalar, Options > Quaternion
Definition: spatial/se3-tpl.hpp:54
pinocchio::python::XYZQUATConverter::SE3
context::SE3 SE3
Definition: conversions.cpp:19
pinocchio::python::XYZQUATConverter::fromSE3tuple
static bp::tuple fromSE3tuple(const SE3 &M)
Definition: conversions.cpp:37
fwd.hpp
pinocchio::python::XYZQUATConverter
Definition: conversions.cpp:16
t
Transform3f t
PINOCCHIO_ASSERT_MATRIX_SPECIFIC_SIZE
#define PINOCCHIO_ASSERT_MATRIX_SPECIFIC_SIZE(type, M, nrows, ncols)
Ensure that a matrix (or vector) is of correct size (compile-time and run-time assertion)
Definition: include/pinocchio/macros.hpp:75
pinocchio::python::XYZQUATConverter::Options
@ Options
Definition: conversions.cpp:23
pinocchio::python::XYZQUATConverter::QuatConstMap
Eigen::Map< const SE3::Quaternion > QuatConstMap
Definition: conversions.cpp:27
se3.hpp
pinocchio::python::res
ReturnType res
Definition: bindings/python/spatial/explog.hpp:68
pinocchio::python::XYZQUATConverter::Scalar
context::Scalar Scalar
Definition: conversions.cpp:18
pinocchio::python::context::Scalar
PINOCCHIO_PYTHON_SCALAR_TYPE Scalar
Definition: bindings/python/context/generic.hpp:37
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27
pinocchio::python::XYZQUATConverter::toSE3
static SE3 toSE3(const Vector7Like &v)
Definition: conversions.cpp:84


pinocchio
Author(s):
autogenerated on Fri Nov 1 2024 02:41:43