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

Skip to content

Commit 25fbbd3

Browse files
committed
Add a pybind11 type caster for agg::rect_d
1 parent 879779b commit 25fbbd3

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

src/_path_wrapper.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,17 @@ Py_points_in_path(py::array_t<double> points_obj, double r, py::object path_obj,
7777
}
7878

7979
static py::tuple
80-
Py_update_path_extents(py::object path_obj, py::object trans_obj, py::object rect_obj,
80+
Py_update_path_extents(py::object path_obj, py::object trans_obj, agg::rect_d rect,
8181
py::array_t<double> minpos, bool ignore)
8282
{
8383
mpl::PathIterator path;
8484
agg::trans_affine trans;
85-
agg::rect_d rect;
8685
bool changed;
8786

8887
if (!convert_path(path_obj.ptr(), &path)) {
8988
throw py::error_already_set();
9089
}
9190
convert_trans_affine(trans_obj, trans);
92-
if (!convert_rect(rect_obj.ptr(), &rect)) {
93-
throw py::error_already_set();
94-
}
9591

9692
if (minpos.ndim() != 1) {
9793
throw py::value_error(
@@ -241,19 +237,14 @@ Py_path_in_path(py::object a_obj, py::object atrans_obj,
241237
}
242238

243239
static py::list
244-
Py_clip_path_to_rect(py::object path_obj, py::object rect_obj,
245-
bool inside)
240+
Py_clip_path_to_rect(py::object path_obj, agg::rect_d rect, bool inside)
246241
{
247242
mpl::PathIterator path;
248-
agg::rect_d rect;
249243
std::vector<Polygon> result;
250244

251245
if (!convert_path(path_obj.ptr(), &path)) {
252246
throw py::error_already_set();
253247
}
254-
if (!convert_rect(rect_obj.ptr(), &rect)) {
255-
throw py::error_already_set();
256-
}
257248

258249
clip_path_to_rect(path, rect, inside, result);
259250

@@ -297,14 +288,10 @@ Py_affine_transform(py::array_t<double, py::array::c_style | py::array::forcecas
297288
}
298289

299290
static int
300-
Py_count_bboxes_overlapping_bbox(py::object bbox_obj, py::object bboxes_obj)
291+
Py_count_bboxes_overlapping_bbox(agg::rect_d bbox, py::object bboxes_obj)
301292
{
302-
agg::rect_d bbox;
303293
numpy::array_view<const double, 3> bboxes;
304294

305-
if (!convert_rect(bbox_obj.ptr(), &bbox)) {
306-
throw py::error_already_set();
307-
}
308295
if (!convert_bboxes(bboxes_obj.ptr(), &bboxes)) {
309296
throw py::error_already_set();
310297
}
@@ -374,22 +361,18 @@ Py_convert_path_to_polygons(py::object path_obj, py::object trans_obj,
374361

375362
static py::tuple
376363
Py_cleanup_path(py::object path_obj, py::object trans_obj, bool remove_nans,
377-
py::object clip_rect_obj, py::object snap_mode_obj, double stroke_width,
364+
agg::rect_d clip_rect, py::object snap_mode_obj, double stroke_width,
378365
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
379366
{
380367
mpl::PathIterator path;
381368
agg::trans_affine trans;
382-
agg::rect_d clip_rect;
383369
e_snap_mode snap_mode;
384370
SketchParams sketch;
385371

386372
if (!convert_path(path_obj.ptr(), &path)) {
387373
throw py::error_already_set();
388374
}
389375
convert_trans_affine(trans_obj, trans);
390-
if (!convert_rect(clip_rect_obj.ptr(), &clip_rect)) {
391-
throw py::error_already_set();
392-
}
393376
if (!convert_snap(snap_mode_obj.ptr(), &snap_mode)) {
394377
throw py::error_already_set();
395378
}
@@ -449,13 +432,12 @@ postfix : bool
449432
)""";
450433

451434
static py::object
452-
Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object cliprect_obj,
435+
Py_convert_to_string(py::object path_obj, py::object trans_obj, agg::rect_d cliprect,
453436
std::optional<bool> simplify, py::object sketch_obj, int precision,
454437
std::array<std::string, 5> codes_obj, bool postfix)
455438
{
456439
mpl::PathIterator path;
457440
agg::trans_affine trans;
458-
agg::rect_d cliprect;
459441
SketchParams sketch;
460442
char *codes[5];
461443
std::string buffer;
@@ -465,9 +447,6 @@ Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object clipr
465447
throw py::error_already_set();
466448
}
467449
convert_trans_affine(trans_obj, trans);
468-
if (!convert_rect(cliprect_obj.ptr(), &cliprect)) {
469-
throw py::error_already_set();
470-
}
471450
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
472451
throw py::error_already_set();
473452
}

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)