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


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:06