Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Abseil dependency to allow cleaner Python bindings #73

Open
rulyone opened this issue Apr 27, 2023 · 1 comment
Open

Remove Abseil dependency to allow cleaner Python bindings #73

rulyone opened this issue Apr 27, 2023 · 1 comment

Comments

@rulyone
Copy link

rulyone commented Apr 27, 2023

Hello.

I'm working on Python bindings for using this library, and so far I have accomplished this with a Not so clean solution using C++ lambda expressions to avoid issues with the absl::Status returned by the Reset and Update functions of the StrokeModeler class like this:

py::class_<ink::stroke_model::StrokeModeler>(handle, "StrokeModeler")
        .def(py::init<>())
        .def("Reset", [](ink::stroke_model::StrokeModeler &self) -> py::str {
            auto status = self.Reset();
            if(!status.ok()) {
                return py::str(status.message());
            }
            return py::str("OK");
        })
        .def("Reset", [](ink::stroke_model::StrokeModeler &self, ink::stroke_model::StrokeModelParams params) -> py::str {
            auto status = self.Reset(params);
            if(!status.ok()) {
                return py::str(status.message());
            }
            return py::str("OK");
        })
        .def("Update", [](ink::stroke_model::StrokeModeler &self, const ink::stroke_model::Input& input, std::vector<ink::stroke_model::Result>& result) -> py::str {
            auto status = self.Update(input, result);
            if(!status.ok()) {
                return py::str(status.message());
            }
            return py::str("OK");
        })
        .def("Save", &ink::stroke_model::StrokeModeler::Save)
        .def("Restore", &ink::stroke_model::StrokeModeler::Restore);

Here you can see that the Save and Restore bindings are much clear to translate to python.

Another issue is that for this abseil implementation, I need to add into the pybind11 CMakeLists.txt file all dependencies on this project, eg:

pybind11_add_module(
    smooth_lines 
    wrapper.cpp 
    #The following dependencies are not needed if there weren't an abseil dependency.
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/types.cc
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/params.cc
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/stroke_modeler.cc

    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/wobble_smoother.cc
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/stylus_state_modeler.cc

    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/prediction/kalman_predictor.cc
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/prediction/stroke_end_predictor.cc

    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/prediction/kalman_filter/axis_predictor.cc
    ${CMAKE_SOURCE_DIR}/ink-stroke-modeler/ink_stroke_modeler/internal/prediction/kalman_filter/kalman_filter.cc
)

If abseil is not a dependency, the CMakeLists.txt file above would be much cleaner.

My suggestion is to use a custom exception class instead of using Abseil library. Not an expert in C++ so I'm not sure if a custom class is the best option, but removing the Abseil dependency would be great for this use case (and might be the same for other bindings).

@sfreilich
Copy link
Collaborator

Do the pybind Abseil bindings make this easier?
https://github.com/pybind/pybind11_abseil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants