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

Skip to content

Commit 73e3b08

Browse files
committed
Add a pybind11 type caster for agg::rect_d
1 parent 24e52de commit 73e3b08

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

src/_path_wrapper.cpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,17 @@ Py_points_in_path(py::array_t<double> points_obj, double r, py::object path_obj,
8787
}
8888

8989
static py::tuple
90-
Py_update_path_extents(py::object path_obj, py::object trans_obj, py::object rect_obj,
90+
Py_update_path_extents(py::object path_obj, py::object trans_obj, agg::rect_d rect,
9191
py::array_t<double> minpos, bool ignore)
9292
{
9393
mpl::PathIterator path;
9494
agg::trans_affine trans;
95-
agg::rect_d rect;
9695
bool changed;
9796

9897
if (!convert_path(path_obj.ptr(), &path)) {
9998
throw py::error_already_set();
10099
}
101100
convert_trans_affine(trans_obj, trans);
102-
if (!convert_rect(rect_obj.ptr(), &rect)) {
103-
throw py::error_already_set();
104-
}
105101

106102
if (minpos.ndim() != 1) {
107103
throw py::value_error(
@@ -255,19 +251,14 @@ Py_path_in_path(py::object a_obj, py::object atrans_obj,
255251
}
256252

257253
static py::object
258-
Py_clip_path_to_rect(py::object path_obj, py::object rect_obj,
259-
bool inside)
254+
Py_clip_path_to_rect(py::object path_obj, agg::rect_d rect, bool inside)
260255
{
261256
mpl::PathIterator path;
262-
agg::rect_d rect;
263257
std::vector<Polygon> result;
264258

265259
if (!convert_path(path_obj.ptr(), &path)) {
266260
throw py::error_already_set();
267261
}
268-
if (!convert_rect(rect_obj.ptr(), &rect)) {
269-
throw py::error_already_set();
270-
}
271262

272263
clip_path_to_rect(path, rect, inside, result);
273264

@@ -311,14 +302,10 @@ Py_affine_transform(py::array_t<double, py::array::c_style | py::array::forcecas
311302
}
312303

313304
static int
314-
Py_count_bboxes_overlapping_bbox(py::object bbox_obj, py::object bboxes_obj)
305+
Py_count_bboxes_overlapping_bbox(agg::rect_d bbox, py::object bboxes_obj)
315306
{
316-
agg::rect_d bbox;
317307
numpy::array_view<const double, 3> bboxes;
318308

319-
if (!convert_rect(bbox_obj.ptr(), &bbox)) {
320-
throw py::error_already_set();
321-
}
322309
if (!convert_bboxes(bboxes_obj.ptr(), &bboxes)) {
323310
throw py::error_already_set();
324311
}
@@ -386,24 +373,20 @@ Py_convert_path_to_polygons(py::object path_obj, py::object trans_obj,
386373
return convert_polygon_vector(result);
387374
}
388375

389-
static py::tuple
376+
static pybind11::tuple
390377
Py_cleanup_path(py::object path_obj, py::object trans_obj, bool remove_nans,
391-
py::object clip_rect_obj, py::object snap_mode_obj, double stroke_width,
378+
agg::rect_d clip_rect, py::object snap_mode_obj, double stroke_width,
392379
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
393380
{
394381
mpl::PathIterator path;
395382
agg::trans_affine trans;
396-
agg::rect_d clip_rect;
397383
e_snap_mode snap_mode;
398384
SketchParams sketch;
399385

400386
if (!convert_path(path_obj.ptr(), &path)) {
401387
throw py::error_already_set();
402388
}
403389
convert_trans_affine(trans_obj, trans);
404-
if (!convert_rect(clip_rect_obj.ptr(), &clip_rect)) {
405-
throw py::error_already_set();
406-
}
407390
if (!convert_snap(snap_mode_obj.ptr(), &snap_mode)) {
408391
throw py::error_already_set();
409392
}
@@ -466,13 +449,12 @@ postfix : bool
466449
)""";
467450

468451
static py::object
469-
Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object cliprect_obj,
452+
Py_convert_to_string(py::object path_obj, py::object trans_obj, agg::rect_d cliprect,
470453
std::optional<bool> simplify, py::object sketch_obj, int precision,
471454
std::array<std::string, 5> codes_obj, bool postfix)
472455
{
473456
mpl::PathIterator path;
474457
agg::trans_affine trans;
475-
agg::rect_d cliprect;
476458
SketchParams sketch;
477459
char *codes[5];
478460
std::string buffer;
@@ -482,9 +464,6 @@ Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object clipr
482464
throw py::error_already_set();
483465
}
484466
convert_trans_affine(trans_obj, trans);
485-
if (!convert_rect(cliprect_obj.ptr(), &cliprect)) {
486-
throw py::error_already_set();
487-
}
488467
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
489468
throw py::error_already_set();
490469
}

src/py_converters_11.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,54 @@
88

99
namespace py = pybind11;
1010

11+
#include "agg_basics.h"
1112
#include "agg_trans_affine.h"
1213

1314
void convert_trans_affine(const py::object& transform, agg::trans_affine& affine);
1415

15-
#endif
16+
namespace PYBIND11_NAMESPACE { namespace detail {
17+
template <> struct type_caster<agg::rect_d> {
18+
public:
19+
PYBIND11_TYPE_CASTER(agg::rect_d, const_name("rect_d"));
20+
21+
bool load(handle src, bool) {
22+
if (src.is_none()) {
23+
value.x1 = 0.0;
24+
value.y1 = 0.0;
25+
value.x2 = 0.0;
26+
value.y2 = 0.0;
27+
return true;
28+
}
29+
30+
auto rect_arr = py::array_t<double>::ensure(src);
31+
32+
if (rect_arr.ndim() == 2) {
33+
if (rect_arr.shape(0) != 2 || rect_arr.shape(1) != 2) {
34+
throw py::value_error("Invalid bounding box");
35+
}
36+
37+
value.x1 = *rect_arr.data(0, 0);
38+
value.y1 = *rect_arr.data(0, 1);
39+
value.x2 = *rect_arr.data(1, 0);
40+
value.y2 = *rect_arr.data(1, 1);
41+
42+
} else if (rect_arr.ndim() == 1) {
43+
if (rect_arr.shape(0) != 4) {
44+
throw py::value_error("Invalid bounding box");
45+
}
46+
47+
value.x1 = *rect_arr.data(0);
48+
value.y1 = *rect_arr.data(1);
49+
value.x2 = *rect_arr.data(2);
50+
value.y2 = *rect_arr.data(3);
51+
52+
} else {
53+
throw py::value_error("Invalid bounding box");
54+
}
55+
56+
return true;
57+
}
58+
};
59+
}} // namespace PYBIND11_NAMESPACE::detail
60+
61+
#endif /* MPL_PY_CONVERTERS_11_H */

0 commit comments

Comments
 (0)