Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit beb989e

Browse files
committed
Add a pybind11 type caster for SketchParams
1 parent 78c8f04 commit beb989e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/_path_wrapper.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "_path.h"
1313

14+
#include "_backend_agg_basic_types.h"
1415
#include "py_adaptors.h"
1516
#include "py_converters.h"
1617
#include "py_converters_11.h"
@@ -288,14 +289,8 @@ Py_convert_path_to_polygons(mpl::PathIterator path, agg::trans_affine trans,
288289
static py::tuple
289290
Py_cleanup_path(mpl::PathIterator path, agg::trans_affine trans, bool remove_nans,
290291
agg::rect_d clip_rect, e_snap_mode snap_mode, double stroke_width,
291-
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
292+
std::optional<bool> simplify, bool return_curves, SketchParams sketch)
292293
{
293-
SketchParams sketch;
294-
295-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
296-
throw py::error_already_set();
297-
}
298-
299294
if (!simplify.has_value()) {
300295
simplify = path.should_simplify();
301296
}
@@ -350,18 +345,13 @@ postfix : bool
350345
static py::object
351346
Py_convert_to_string(mpl::PathIterator path, agg::trans_affine trans,
352347
agg::rect_d cliprect, std::optional<bool> simplify,
353-
py::object sketch_obj, int precision,
348+
SketchParams sketch, int precision,
354349
std::array<std::string, 5> codes_obj, bool postfix)
355350
{
356-
SketchParams sketch;
357351
char *codes[5];
358352
std::string buffer;
359353
bool status;
360354

361-
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
362-
throw py::error_already_set();
363-
}
364-
365355
for (auto i = 0; i < 5; ++i) {
366356
codes[i] = const_cast<char *>(codes_obj[i].c_str());
367357
}

src/py_converters_11.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ namespace PYBIND11_NAMESPACE { namespace detail {
127127
}
128128
};
129129
#endif
130+
131+
/* Remove all this macro magic after dropping NumPy usage and just include `_backend_agg_basic_types.h`. */
132+
#ifdef MPL_BACKEND_AGG_BASIC_TYPES_H
133+
template <> struct type_caster<SketchParams> {
134+
public:
135+
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));
136+
137+
bool load(handle src, bool) {
138+
if (src.is_none()) {
139+
value.scale = 0.0;
140+
return true;
141+
}
142+
143+
auto params = src.cast<std::tuple<double, double, double>>();
144+
std::tie(value.scale, value.length, value.randomness) = params;
145+
146+
return true;
147+
}
148+
};
149+
#endif
130150
}} // namespace PYBIND11_NAMESPACE::detail
131151

132152
#endif /* MPL_PY_CONVERTERS_11_H */

0 commit comments

Comments
 (0)