google / google/ink-stroke-modeler

Remove Abseil dependency to allow cleaner Python bindings

Open
#73 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
139
Forks
20
PR merge metrics
No merged PRs in 30d

Description

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:

```cpp
py::class_(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& 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:

```bash
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).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.