pinocchio 4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Loading...
Searching...
No Matches
cast.hpp
1//
2// Copyright (c) 2020 INRIA
3//
4
5#pragma once
6
7#include "pinocchio/bindings/python/fwd.hpp"
8
9#include <eigenpy/registration.hpp>
10
11namespace pinocchio
12{
13 namespace python
14 {
15
16 namespace bp = boost::python;
17
21 template<class C, typename NewScalar = PINOCCHIO_PYTHON_SCALAR_TYPE_DEFAULT>
22 struct CastVisitor : public bp::def_visitor<CastVisitor<C, NewScalar>>
23 {
24 template<class PyClass>
25 void visit(PyClass & cl) const
26 {
27 cl.def("cast", &C::template cast<NewScalar>, "Returns a cast of *this.");
28 }
29 };
30
31 template<class _From, class _To>
33 : public bp::def_visitor<ExposeConstructorByCastVisitor<_From, _To>>
34 {
35 template<class PyClass>
36 void visit(PyClass & /*cl*/) const
37 {
38 expose_constructor<_From, _To>();
39 expose_constructor<_To, _From>();
40 }
41
42 protected:
43 template<typename From, typename To>
44 static void expose_constructor()
45 {
46 if (eigenpy::check_registration<From>() && eigenpy::check_registration<To>())
47 {
48 const bp::type_info to_info = bp::type_id<To>();
49 const bp::converter::registration * to_reg = bp::converter::registry::query(to_info);
50 bp::object to_class_obj(bp::handle<>(bp::borrowed(to_reg->get_class_object())));
51 const std::string to_module_name =
52 bp::extract<std::string>(to_class_obj.attr("__module__"));
53 const std::string to_class_name = bp::extract<std::string>(to_class_obj.attr("__name__"));
54
55 const bp::type_info from_info = bp::type_id<From>();
56 const bp::converter::registration * from_reg = bp::converter::registry::query(from_info);
57 bp::object from_class_obj(bp::handle<>(bp::borrowed(from_reg->get_class_object())));
58 const std::string from_module_name =
59 bp::extract<std::string>(from_class_obj.attr("__module__"));
60 const std::string from_class_name =
61 bp::extract<std::string>(from_class_obj.attr("__name__"));
62
63 const std::string to_full_class_name = to_module_name + "." + to_class_name;
64 const std::string from_full_class_name = from_module_name + "." + from_class_name;
65 std::stringstream to_doc;
66 to_doc << "Copy constructor from " << from_full_class_name;
67 to_doc << " -> " << to_full_class_name;
68 bp::objects::add_to_namespace(
69 to_class_obj, "__init__",
70 bp::make_constructor(
71 &ExposeConstructorByCastVisitor::constructor<From, To>, bp::default_call_policies(),
72 bp::arg("clone")),
73 to_doc.str().c_str());
74 }
75 }
76
77 template<typename From, typename To>
78 static To * constructor(const From & clone)
79 {
80 typedef typename To::Scalar NewScalar;
81 return new To(clone.template cast<NewScalar>());
82 }
83 };
84 } // namespace python
85} // namespace pinocchio
Main pinocchio namespace.
Definition treeview.dox:11
Add the Python method cast.
Definition cast.hpp:23