pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
number.hpp
1//
2// Copyright (c) 2021-2022 INRIA
3//
4
5#pragma once
6
7#include "pinocchio/math/multiprecision.hpp"
8
9#include <boost/python.hpp>
10#include <boost/python/return_value_policy.hpp>
11#include <eigenpy/user-type.hpp>
12#include <eigenpy/ufunc.hpp>
13#include <sstream>
14
15namespace
16{
17
18 template<class Backend>
19 struct get_backend_precision
20 {
21 };
22
23 template<unsigned Digits10, ::boost::multiprecision::mpfr_allocation_type AllocateType>
24 struct get_backend_precision<::boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>>
25 {
26 enum
27 {
28 value = Digits10
29 };
30 };
31
32} // namespace
33
34namespace eigenpy
35{
36 namespace internal
37 {
38 template<class Backend, ::boost::multiprecision::expression_template_option ExpressionTemplates>
39 struct getitem<::boost::multiprecision::number<Backend, ExpressionTemplates>>
40 {
41
42 typedef ::boost::multiprecision::number<Backend, ExpressionTemplates> Scalar;
43
44 static PyObject * run(void * data, void * /* arr */)
45 {
46 Scalar & mpfr_scalar = *static_cast<Scalar *>(data);
47 Backend & backend = mpfr_scalar.backend();
48
49 if (backend.data()[0]._mpfr_d == 0) // If the mpfr_scalar is not initialized, we have to
50 // init it.
51 {
52 mpfr_scalar = Scalar(0);
53 // unsigned int precision = get_backend_precision<Backend>::value ?
54 // get_backend_precision<Backend>::value : backend.default_precision();
55 // mpfr_init2(backend.data(),
56 // ::boost::multiprecision::detail::digits10_2_2(precision));
57 }
58 bp::object m(boost::ref(mpfr_scalar));
59 Py_INCREF(m.ptr());
60 return m.ptr();
61 }
62 };
63 } // namespace internal
64
65} // namespace eigenpy
66
67namespace pinocchio
68{
69 namespace python
70 {
71 namespace bp = boost::python;
72
73 template<typename BoostNumber>
75 : public boost::python::def_visitor<BoostNumberPythonVisitor<BoostNumber>>
76 {
77
78 public:
79 template<class PyClass>
80 void visit(PyClass & cl) const
81 {
82 cl.def(bp::init<>("Default constructor.", bp::arg("self")))
83 .def(bp::init<BoostNumber>("Copy constructor.", bp::args("self", "value")))
84 // .def(bp::init<bool>("Copy constructor.",bp::args("self","value")))
85 // .def(bp::init<float>("Copy constructor.",bp::args("self","value")))
86 // .def(bp::init<double>("Copy constructor.",bp::args("self","value")))
87 // .def(bp::init<int>("Copy constructor.",bp::args("self","value")))
88 // .def(bp::init<long int>("Copy constructor.",bp::args("self","value")))
89 // .def(bp::init<unsigned int>("Copy constructor.",bp::args("self","value")))
90 // .def(bp::init<unsigned long int>("Copy constructor.",bp::args("self","value")))
91 .def(bp::init<std::string>("Constructor from a string.", bp::args("self", "str_value")));
92
93 PINOCCHIO_COMPILER_DIAGNOSTIC_PUSH
94 PINOCCHIO_COMPILER_DIAGNOSTIC_IGNORED_SELF_ASSIGN_OVERLOADED
95 cl.def(bp::self + bp::self)
96 .def(bp::self += bp::self)
97 .def(bp::self - bp::self)
98 .def(bp::self -= bp::self)
99 .def(bp::self * bp::self)
100 .def(bp::self *= bp::self)
101 .def(bp::self / bp::self)
102 .def(bp::self /= bp::self)
103
104 .def(bp::self < bp::self)
105 .def(bp::self <= bp::self)
106 .def(bp::self > bp::self)
107 .def(bp::self >= bp::self)
108 .def(bp::self == bp::self)
109 .def(bp::self != bp::self)
110 .def(bp::self_ns::pow(bp::self_ns::self, long()));
111 PINOCCHIO_COMPILER_DIAGNOSTIC_POP
112
113 cl.def("str", &BoostNumber::str, bp::args("self", "precision", "scientific"))
114
115 .def(
116 "default_precision", static_cast<unsigned (*)()>(BoostNumber::default_precision),
117 "Get the default precision of the class.")
118 .def(
119 "default_precision", static_cast<void (*)(unsigned)>(BoostNumber::default_precision),
120 bp::arg("digits10"), "Set the default precision of the class.")
121 .staticmethod("default_precision")
122
123 .def(
124 "precision", static_cast<unsigned (BoostNumber::*)() const>(&BoostNumber::precision),
125 bp::arg("self"), "Get the precision of this.")
126 .def(
127 "precision", static_cast<void (BoostNumber::*)(unsigned)>(&BoostNumber::precision),
128 bp::args("self", "digits10"), "Set the precision of this.")
129
130 .def("__float__", &cast<double>, bp::arg("self"), "Cast to float.")
131 .def("__int__", &cast<int64_t>, bp::arg("self"), "Cast to int.")
132
133 .def("__str__", &print, bp::arg("self"))
134 .def("__repr__", &print, bp::arg("self"))
135
136 .def(
137 "set_display_precision", &set_display_precision, bp::arg("digit"),
138 "Set the precision when printing values.")
139 .staticmethod("set_display_precision")
140
141 .def(
142 "get_display_precision", &get_display_precision,
143 "Get the precision when printing values.",
144 bp::return_value_policy<bp::copy_non_const_reference>())
145 .staticmethod("get_display_precision")
146
147 // #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
148 // .def_pickle(Pickle())
149 // #endif
150 ;
151 }
152
153 static void expose(const std::string & type_name)
154 {
155 bp::class_<BoostNumber>(type_name.c_str(), "", bp::no_init)
157
158 eigenpy::registerNewType<BoostNumber>();
159 eigenpy::registerCommonUfunc<BoostNumber>();
160
161#define IMPLICITLY_CONVERTIBLE(T1, T2) bp::implicitly_convertible<T1, T2>();
162 // bp::implicitly_convertible<T2,T1>();
163
164 IMPLICITLY_CONVERTIBLE(double, BoostNumber);
165 IMPLICITLY_CONVERTIBLE(float, BoostNumber);
166 IMPLICITLY_CONVERTIBLE(long int, BoostNumber);
167 IMPLICITLY_CONVERTIBLE(int, BoostNumber);
168 IMPLICITLY_CONVERTIBLE(long, BoostNumber);
169 IMPLICITLY_CONVERTIBLE(unsigned int, BoostNumber);
170 IMPLICITLY_CONVERTIBLE(unsigned long int, BoostNumber);
171 IMPLICITLY_CONVERTIBLE(bool, BoostNumber);
172
173#undef IMPLICITLY_CONVERTIBLE
174
175 eigenpy::registerCast<BoostNumber, double>(false);
176 eigenpy::registerCast<double, BoostNumber>(true);
177 eigenpy::registerCast<BoostNumber, float>(false);
178 eigenpy::registerCast<float, BoostNumber>(true);
179 eigenpy::registerCast<BoostNumber, long>(false);
180 eigenpy::registerCast<long, BoostNumber>(true);
181 eigenpy::registerCast<BoostNumber, int>(false);
182 eigenpy::registerCast<int, BoostNumber>(true);
183 ;
184 eigenpy::registerCast<BoostNumber, int64_t>(false);
185 eigenpy::registerCast<int64_t, BoostNumber>(true);
186 }
187
188 private:
189 template<typename T>
190 static T cast(const BoostNumber & self)
191 {
192 return static_cast<T>(self);
193 }
194
195 static std::string print(const BoostNumber & self)
196 {
197 return self.str(get_display_precision(), std::ios_base::dec);
198 }
199
200 static void set_display_precision(const int digit)
201 {
202 get_display_precision() = digit;
203 }
204
205 static int & get_display_precision()
206 {
207 static int precision = BoostNumber::default_precision();
208 return precision;
209 }
210
211 // struct Pickle : bp::pickle_suite
212 // {
213 // static
214 // boost::python::tuple
215 // getinitargs(const SE3 & M)
216 // { return bp::make_tuple((Matrix3)M.rotation(),(Vector3)M.translation()); }
217 // };
218 };
219
220 } // namespace python
221} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11