Directory: | ./ |
---|---|
File: | include/pinocchio/bindings/python/utils/cast.hpp |
Date: | 2025-02-12 21:03:38 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 33 | 34 | 97.1% |
Branches: | 45 | 90 | 50.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2020 INRIA | ||
3 | // | ||
4 | |||
5 | #ifndef __pinocchio_python_utils_cast_hpp__ | ||
6 | #define __pinocchio_python_utils_cast_hpp__ | ||
7 | |||
8 | #include "pinocchio/bindings/python/fwd.hpp" | ||
9 | |||
10 | #include <eigenpy/registration.hpp> | ||
11 | |||
12 | namespace pinocchio | ||
13 | { | ||
14 | namespace python | ||
15 | { | ||
16 | |||
17 | namespace bp = boost::python; | ||
18 | |||
19 | /// | ||
20 | /// \brief Add the Python method cast | ||
21 | /// | ||
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 | 1104 | void visit(PyClass & cl) const | |
27 | { | ||
28 | 1104 | cl.def("cast", &C::template cast<NewScalar>, "Returns a cast of *this."); | |
29 | 1104 | } | |
30 | }; | ||
31 | |||
32 | template<class _From, class _To> | ||
33 | struct ExposeConstructorByCastVisitor | ||
34 | : public bp::def_visitor<ExposeConstructorByCastVisitor<_From, _To>> | ||
35 | { | ||
36 | template<class PyClass> | ||
37 | 1104 | void visit(PyClass & /*cl*/) const | |
38 | { | ||
39 | 1104 | expose_constructor<_From, _To>(); | |
40 | 1104 | expose_constructor<_To, _From>(); | |
41 | 1104 | } | |
42 | |||
43 | protected: | ||
44 | template<typename From, typename To> | ||
45 | 2256 | static void expose_constructor() | |
46 | { | ||
47 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1518 times.
✗ Branch 7 not taken.
|
2256 | if (eigenpy::check_registration<From>() && eigenpy::check_registration<To>()) |
48 | { | ||
49 | 2256 | const bp::type_info to_info = bp::type_id<To>(); | |
50 |
1/2✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
|
2256 | const bp::converter::registration * to_reg = bp::converter::registry::query(to_info); |
51 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1518 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1518 times.
✗ Branch 9 not taken.
|
2256 | bp::object to_class_obj(bp::handle<>(bp::borrowed(to_reg->get_class_object()))); |
52 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | const std::string to_module_name = |
53 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1518 times.
✗ Branch 8 not taken.
|
4512 | bp::extract<std::string>(to_class_obj.attr("__module__")); |
54 |
5/10✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1518 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1518 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1518 times.
✗ Branch 14 not taken.
|
4512 | const std::string to_class_name = bp::extract<std::string>(to_class_obj.attr("__name__")); |
55 | |||
56 | 2256 | const bp::type_info from_info = bp::type_id<From>(); | |
57 |
1/2✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
|
2256 | const bp::converter::registration * from_reg = bp::converter::registry::query(from_info); |
58 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1518 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1518 times.
✗ Branch 9 not taken.
|
2256 | bp::object from_class_obj(bp::handle<>(bp::borrowed(from_reg->get_class_object()))); |
59 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | const std::string from_module_name = |
60 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1518 times.
✗ Branch 8 not taken.
|
4512 | bp::extract<std::string>(from_class_obj.attr("__module__")); |
61 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | const std::string from_class_name = |
62 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1518 times.
✗ Branch 8 not taken.
|
4512 | bp::extract<std::string>(from_class_obj.attr("__name__")); |
63 | |||
64 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | const std::string to_full_class_name = to_module_name + "." + to_class_name; |
65 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | const std::string from_full_class_name = from_module_name + "." + from_class_name; |
66 |
1/2✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
|
2256 | std::stringstream to_doc; |
67 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | to_doc << "Copy constructor from " << from_full_class_name; |
68 |
2/4✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1518 times.
✗ Branch 5 not taken.
|
2256 | to_doc << " -> " << to_full_class_name; |
69 |
3/6✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1518 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1518 times.
✗ Branch 9 not taken.
|
4512 | bp::objects::add_to_namespace( |
70 | to_class_obj, "__init__", | ||
71 | bp::make_constructor( | ||
72 | ✗ | &ExposeConstructorByCastVisitor::constructor<From, To>, bp::default_call_policies(), | |
73 |
1/2✓ Branch 1 taken 1518 times.
✗ Branch 2 not taken.
|
4512 | bp::arg("clone")), |
74 | to_doc.str().c_str()); | ||
75 | 2256 | } | |
76 | 2256 | } | |
77 | |||
78 | template<typename From, typename To> | ||
79 | 8 | static To * constructor(const From & clone) | |
80 | { | ||
81 | typedef typename To::Scalar NewScalar; | ||
82 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
8 | return new To(clone.template cast<NewScalar>()); |
83 | } | ||
84 | }; | ||
85 | } // namespace python | ||
86 | } // namespace pinocchio | ||
87 | |||
88 | #endif // ifndef __pinocchio_python_utils_cast_hpp__ | ||
89 |