robotology / idyntree

Multibody Dynamics Library designed for Free Floating Robots
BSD 3-Clause "New" or "Revised" License
155 stars 65 forks source link

Expose `std::unordered_map<std::string, double>` to SWIG bindings #1180

Closed diegoferigo closed 2 months ago

diegoferigo commented 2 months ago

This is necessary for the new methods introduced in #1174 to accept a dictionary from the target language and convert it to C++. I only tested that it works from Python.

traversaro commented 2 months ago

Cool! Can you add the snippet of code that you used to test if this worked?

diegoferigo commented 2 months ago

Cool! Can you add the snippet of code that you used to test if this worked?

Sure:

import idyntree.bindings as idt
import resolve_robotics_uri_py

urdf_path = resolve_robotics_uri_py.resolve_robotics_uri(
    uri="model://ergoCubSN001/model.urdf"
)

mdl_loader = idt.ModelLoader()

assert mdl_loader.loadReducedModelFromString(
    urdf_path.read_text(),
    [],
    {"my_joint": 3.14}
)
traversaro commented 2 months ago

Great, so basically the user never needs to use the StringToDoubleUnorderedMap type explicitly, great.

diegoferigo commented 2 months ago

Great, so basically the user never needs to use the StringToDoubleUnorderedMap type explicitly, great.

Yep! SWIG automatically converts a Python dictionary to a C++ unordered map. It is not even too picky on the type, from Python the dictionary could also be dict[str, int] and it still works.