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

Skip to content

Commit 362b102

Browse files
committed
Add a pybind11 type caster for agg::rect_d
1 parent 1c2c629 commit 362b102

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

src/_path_wrapper.cpp

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

8383
static py::tuple
84-
Py_update_path_extents(py::object path_obj, py::object trans_obj, py::object rect_obj,
84+
Py_update_path_extents(py::object path_obj, py::object trans_obj, agg::rect_d rect,
8585
py::array_t<double> minpos, bool ignore)
8686
{
8787
mpl::PathIterator path;
8888
agg::trans_affine trans;
89-
agg::rect_d rect;
9089
bool changed;
9190

9291
if (!convert_path(path_obj.ptr(), &path)) {
9392
throw py::error_already_set();
9493
}
9594
convert_trans_affine(trans_obj, trans);
96-
if (!convert_rect(rect_obj.ptr(), &rect)) {
97-
throw py::error_already_set();
98-
}
9995

10096
if (minpos.ndim() != 1) {
10197
throw py::value_error(
@@ -249,19 +245,14 @@ Py_path_in_path(py::object a_obj, py::object atrans_obj,
249245
}
250246

251247
static py::list
252-
Py_clip_path_to_rect(py::object path_obj, py::object rect_obj,
253-
bool inside)
248+
Py_clip_path_to_rect(py::object path_obj, agg::rect_d rect, bool inside)
254249
{
255250
mpl::PathIterator path;
256-
agg::rect_d rect;
257251
std::vector<Polygon> result;
258252

259253
if (!convert_path(path_obj.ptr(), &path)) {
260254
throw py::error_already_set();
261255
}
262-
if (!convert_rect(rect_obj.ptr(), &rect)) {
263-
throw py::error_already_set();
264-
}
265256

266257
clip_path_to_rect(path, rect, inside, result);
267258

@@ -305,14 +296,10 @@ Py_affine_transform(py::array_t<double, py::array::c_style | py::array::forcecas
305296
}
306297

307298
static int
308-
Py_count_bboxes_overlapping_bbox(py::object bbox_obj, py::object bboxes_obj)
299+
Py_count_bboxes_overlapping_bbox(agg::rect_d bbox, py::object bboxes_obj)
309300
{
310-
agg::rect_d bbox;
311301
numpy::array_view<const double, 3> bboxes;
312302

313-
if (!convert_rect(bbox_obj.ptr(), &bbox)) {
314-
throw py::error_already_set();
315-
}
316303
if (!convert_bboxes(bboxes_obj.ptr(), &bboxes)) {
317304
throw py::error_already_set();
318305
}
@@ -382,22 +369,18 @@ Py_convert_path_to_polygons(py::object path_obj, py::object trans_obj,
382369

383370
static py::tuple
384371
Py_cleanup_path(py::object path_obj, py::object trans_obj, bool remove_nans,
385-
py::object clip_rect_obj, py::object snap_mode_obj, double stroke_width,
372+
agg::rect_d clip_rect, py::object snap_mode_obj, double stroke_width,
386373
std::optional<bool> simplify, bool return_curves, py::object sketch_obj)
387374
{
388375
mpl::PathIterator path;
389376
agg::trans_affine trans;
390-
agg::rect_d clip_rect;
391377
e_snap_mode snap_mode;
392378
SketchParams sketch;
393379

394380
if (!convert_path(path_obj.ptr(), &path)) {
395381
throw py::error_already_set();
396382
}
397383
convert_trans_affine(trans_obj, trans);
398-
if (!convert_rect(clip_rect_obj.ptr(), &clip_rect)) {
399-
throw py::error_already_set();
400-
}
401384
if (!convert_snap(snap_mode_obj.ptr(), &snap_mode)) {
402385
throw py::error_already_set();
403386
}
@@ -460,13 +443,12 @@ postfix : bool
460443
)""";
461444

462445
static py::object
463-
Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object cliprect_obj,
446+
Py_convert_to_string(py::object path_obj, py::object trans_obj, agg::rect_d cliprect,
464447
std::optional<bool> simplify, py::object sketch_obj, int precision,
465448
std::array<std::string, 5> codes_obj, bool postfix)
466449
{
467450
mpl::PathIterator path;
468451
agg::trans_affine trans;
469-
agg::rect_d cliprect;
470452
SketchParams sketch;
471453
char *codes[5];
472454
std::string buffer;
@@ -476,9 +458,6 @@ Py_convert_to_string(py::object path_obj, py::object trans_obj, py::object clipr
476458
throw py::error_already_set();
477459
}
478460
convert_trans_affine(trans_obj, trans);
479-
if (!convert_rect(cliprect_obj.ptr(), &cliprect)) {
480-
throw py::error_already_set();
481-
}
482461
if (!convert_sketch_params(sketch_obj.ptr(), &sketch)) {
483462
throw py::error_already_set();
484463
}

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)