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

Skip to content

Convert remaining code to pybind11 #28856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions src/_backend_agg_basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
/* Contains some simple types from the Agg backend that are also used
by other modules */

#include <pybind11/pybind11.h>

#include <unordered_map>
#include <vector>

#include "agg_color_rgba.h"
Expand All @@ -13,6 +16,8 @@

#include "py_adaptors.h"

namespace py = pybind11;

struct ClipPath
{
mpl::PathIterator path;
Expand Down Expand Up @@ -121,4 +126,132 @@
GCAgg &operator=(const GCAgg &);
};

namespace PYBIND11_NAMESPACE { namespace detail {
template <> struct type_caster<agg::line_cap_e> {
public:
PYBIND11_TYPE_CASTER(agg::line_cap_e, const_name("line_cap_e"));

bool load(handle src, bool) {
const std::unordered_map<std::string, agg::line_cap_e> enum_values = {
{"butt", agg::butt_cap},
{"round", agg::round_cap},
{"projecting", agg::square_cap},
};
value = enum_values.at(src.cast<std::string>());
return true;
}
};

template <> struct type_caster<agg::line_join_e> {
public:
PYBIND11_TYPE_CASTER(agg::line_join_e, const_name("line_join_e"));

bool load(handle src, bool) {
const std::unordered_map<std::string, agg::line_join_e> enum_values = {
{"miter", agg::miter_join_revert},
{"round", agg::round_join},
{"bevel", agg::bevel_join},
};
value = enum_values.at(src.cast<std::string>());
return true;
}
};

template <> struct type_caster<ClipPath> {
public:
PYBIND11_TYPE_CASTER(ClipPath, const_name("ClipPath"));

bool load(handle src, bool) {
if (src.is_none()) {
return true;

Check warning on line 166 in src/_backend_agg_basic_types.h

View check run for this annotation

Codecov / codecov/patch

src/_backend_agg_basic_types.h#L166

Added line #L166 was not covered by tests
}

auto [path, trans] =
src.cast<std::pair<std::optional<mpl::PathIterator>, agg::trans_affine>>();
if (path) {
value.path = *path;
}
value.trans = trans;

return true;
}
};

template <> struct type_caster<Dashes> {
public:
PYBIND11_TYPE_CASTER(Dashes, const_name("Dashes"));

bool load(handle src, bool) {
auto [dash_offset, dashes_seq_or_none] =
src.cast<std::pair<double, std::optional<py::sequence>>>();

if (!dashes_seq_or_none) {
return true;
}

auto dashes_seq = *dashes_seq_or_none;

auto nentries = dashes_seq.size();
// If the dashpattern has odd length, iterate through it twice (in
// accordance with the pdf/ps/svg specs).
auto dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;

for (py::size_t i = 0; i < dash_pattern_length; i += 2) {
auto length = dashes_seq[i % nentries].cast<double>();
auto skip = dashes_seq[(i + 1) % nentries].cast<double>();

value.add_dash_pair(length, skip);
}

value.set_dash_offset(dash_offset);

return true;
}
};

template <> struct type_caster<SketchParams> {
public:
PYBIND11_TYPE_CASTER(SketchParams, const_name("SketchParams"));

bool load(handle src, bool) {
if (src.is_none()) {
value.scale = 0.0;
value.length = 0.0;
value.randomness = 0.0;
return true;
}

auto params = src.cast<std::tuple<double, double, double>>();
std::tie(value.scale, value.length, value.randomness) = params;

return true;
}
};

template <> struct type_caster<GCAgg> {
public:
PYBIND11_TYPE_CASTER(GCAgg, const_name("GCAgg"));

bool load(handle src, bool) {
value.linewidth = src.attr("_linewidth").cast<double>();
value.alpha = src.attr("_alpha").cast<double>();
value.forced_alpha = src.attr("_forced_alpha").cast<bool>();
value.color = src.attr("_rgb").cast<agg::rgba>();
value.isaa = src.attr("_antialiased").cast<bool>();
value.cap = src.attr("_capstyle").cast<agg::line_cap_e>();
value.join = src.attr("_joinstyle").cast<agg::line_join_e>();
value.dashes = src.attr("get_dashes")().cast<Dashes>();
value.cliprect = src.attr("_cliprect").cast<agg::rect_d>();
value.clippath = src.attr("get_clip_path")().cast<ClipPath>();
value.snap_mode = src.attr("get_snap")().cast<e_snap_mode>();
value.hatchpath = src.attr("get_hatch_path")().cast<mpl::PathIterator>();
value.hatch_color = src.attr("get_hatch_color")().cast<agg::rgba>();
value.hatch_linewidth = src.attr("get_hatch_linewidth")().cast<double>();
value.sketch = src.attr("get_sketch_params")().cast<SketchParams>();

return true;
}
};
}} // namespace PYBIND11_NAMESPACE::detail

#endif
71 changes: 19 additions & 52 deletions src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,48 +111,25 @@ static void
PyRendererAgg_draw_path_collection(RendererAgg *self,
GCAgg &gc,
agg::trans_affine master_transform,
py::object paths_obj,
py::object transforms_obj,
py::object offsets_obj,
mpl::PathGenerator paths,
py::array_t<double> transforms_obj,
py::array_t<double> offsets_obj,
agg::trans_affine offset_trans,
py::object facecolors_obj,
py::object edgecolors_obj,
py::object linewidths_obj,
py::array_t<double> facecolors_obj,
py::array_t<double> edgecolors_obj,
py::array_t<double> linewidths_obj,
DashesVector dashes,
py::object antialiaseds_obj,
py::array_t<uint8_t> antialiaseds_obj,
py::object Py_UNUSED(ignored_obj),
// offset position is no longer used
py::object Py_UNUSED(offset_position_obj))
{
mpl::PathGenerator paths;
numpy::array_view<const double, 3> transforms;
numpy::array_view<const double, 2> offsets;
numpy::array_view<const double, 2> facecolors;
numpy::array_view<const double, 2> edgecolors;
numpy::array_view<const double, 1> linewidths;
numpy::array_view<const uint8_t, 1> antialiaseds;

if (!convert_pathgen(paths_obj.ptr(), &paths)) {
throw py::error_already_set();
}
if (!convert_transforms(transforms_obj.ptr(), &transforms)) {
throw py::error_already_set();
}
if (!convert_points(offsets_obj.ptr(), &offsets)) {
throw py::error_already_set();
}
if (!convert_colors(facecolors_obj.ptr(), &facecolors)) {
throw py::error_already_set();
}
if (!convert_colors(edgecolors_obj.ptr(), &edgecolors)) {
throw py::error_already_set();
}
if (!linewidths.converter(linewidths_obj.ptr(), &linewidths)) {
throw py::error_already_set();
}
if (!antialiaseds.converter(antialiaseds_obj.ptr(), &antialiaseds)) {
throw py::error_already_set();
}
auto transforms = convert_transforms(transforms_obj);
auto offsets = convert_points(offsets_obj);
auto facecolors = convert_colors(facecolors_obj);
auto edgecolors = convert_colors(edgecolors_obj);
auto linewidths = linewidths_obj.unchecked<1>();
auto antialiaseds = antialiaseds_obj.unchecked<1>();

self->draw_path_collection(gc,
master_transform,
Expand All @@ -174,26 +151,16 @@ PyRendererAgg_draw_quad_mesh(RendererAgg *self,
unsigned int mesh_width,
unsigned int mesh_height,
py::array_t<double, py::array::c_style | py::array::forcecast> coordinates_obj,
py::object offsets_obj,
py::array_t<double> offsets_obj,
agg::trans_affine offset_trans,
py::object facecolors_obj,
py::array_t<double> facecolors_obj,
bool antialiased,
py::object edgecolors_obj)
py::array_t<double> edgecolors_obj)
{
numpy::array_view<const double, 2> offsets;
numpy::array_view<const double, 2> facecolors;
numpy::array_view<const double, 2> edgecolors;

auto coordinates = coordinates_obj.mutable_unchecked<3>();
if (!convert_points(offsets_obj.ptr(), &offsets)) {
throw py::error_already_set();
}
if (!convert_colors(facecolors_obj.ptr(), &facecolors)) {
throw py::error_already_set();
}
if (!convert_colors(edgecolors_obj.ptr(), &edgecolors)) {
throw py::error_already_set();
}
auto offsets = convert_points(offsets_obj);
auto facecolors = convert_colors(facecolors_obj);
auto edgecolors = convert_colors(edgecolors_obj);

self->draw_quad_mesh(gc,
master_transform,
Expand Down
57 changes: 27 additions & 30 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ inline void points_in_path(PointArray &points,
typedef agg::conv_curve<no_nans_t> curve_t;
typedef agg::conv_contour<curve_t> contour_t;

size_t i;
for (i = 0; i < safe_first_shape(points); ++i) {
for (auto i = 0; i < safe_first_shape(points); ++i) {
result[i] = false;
}

Expand All @@ -270,10 +269,11 @@ template <class PathIterator>
inline bool point_in_path(
double x, double y, const double r, PathIterator &path, agg::trans_affine &trans)
{
npy_intp shape[] = {1, 2};
numpy::array_view<double, 2> points(shape);
points(0, 0) = x;
points(0, 1) = y;
py::ssize_t shape[] = {1, 2};
py::array_t<double> points_arr(shape);
*points_arr.mutable_data(0, 0) = x;
*points_arr.mutable_data(0, 1) = y;
auto points = points_arr.mutable_unchecked<2>();

int result[1];
result[0] = 0;
Expand All @@ -292,10 +292,11 @@ inline bool point_on_path(
typedef agg::conv_curve<no_nans_t> curve_t;
typedef agg::conv_stroke<curve_t> stroke_t;

npy_intp shape[] = {1, 2};
numpy::array_view<double, 2> points(shape);
points(0, 0) = x;
points(0, 1) = y;
py::ssize_t shape[] = {1, 2};
py::array_t<double> points_arr(shape);
*points_arr.mutable_data(0, 0) = x;
*points_arr.mutable_data(0, 1) = y;
auto points = points_arr.mutable_unchecked<2>();

int result[1];
result[0] = 0;
Expand Down Expand Up @@ -382,20 +383,19 @@ void get_path_collection_extents(agg::trans_affine &master_transform,
throw std::runtime_error("Offsets array must have shape (N, 2)");
}

size_t Npaths = paths.size();
size_t Noffsets = safe_first_shape(offsets);
size_t N = std::max(Npaths, Noffsets);
size_t Ntransforms = std::min(safe_first_shape(transforms), N);
size_t i;
auto Npaths = paths.size();
auto Noffsets = safe_first_shape(offsets);
auto N = std::max(Npaths, Noffsets);
auto Ntransforms = std::min(safe_first_shape(transforms), N);

agg::trans_affine trans;

reset_limits(extent);

for (i = 0; i < N; ++i) {
for (auto i = 0; i < N; ++i) {
typename PathGenerator::path_iterator path(paths(i % Npaths));
if (Ntransforms) {
size_t ti = i % Ntransforms;
py::ssize_t ti = i % Ntransforms;
trans = agg::trans_affine(transforms(ti, 0, 0),
transforms(ti, 1, 0),
transforms(ti, 0, 1),
Expand Down Expand Up @@ -429,24 +429,23 @@ void point_in_path_collection(double x,
bool filled,
std::vector<int> &result)
{
size_t Npaths = paths.size();
auto Npaths = paths.size();

if (Npaths == 0) {
return;
}

size_t Noffsets = safe_first_shape(offsets);
size_t N = std::max(Npaths, Noffsets);
size_t Ntransforms = std::min(safe_first_shape(transforms), N);
size_t i;
auto Noffsets = safe_first_shape(offsets);
auto N = std::max(Npaths, Noffsets);
auto Ntransforms = std::min(safe_first_shape(transforms), N);

agg::trans_affine trans;

for (i = 0; i < N; ++i) {
for (auto i = 0; i < N; ++i) {
typename PathGenerator::path_iterator path = paths(i % Npaths);

if (Ntransforms) {
size_t ti = i % Ntransforms;
auto ti = i % Ntransforms;
trans = agg::trans_affine(transforms(ti, 0, 0),
transforms(ti, 1, 0),
transforms(ti, 0, 1),
Expand Down Expand Up @@ -1224,17 +1223,15 @@ bool convert_to_string(PathIterator &path,
}

template<class T>
bool is_sorted_and_has_non_nan(PyArrayObject *array)
bool is_sorted_and_has_non_nan(py::array_t<T> array)
{
char* ptr = PyArray_BYTES(array);
npy_intp size = PyArray_DIM(array, 0),
stride = PyArray_STRIDE(array, 0);
auto size = array.shape(0);
using limits = std::numeric_limits<T>;
T last = limits::has_infinity ? -limits::infinity() : limits::min();
bool found_non_nan = false;

for (npy_intp i = 0; i < size; ++i, ptr += stride) {
T current = *(T*)ptr;
for (auto i = 0; i < size; ++i) {
T current = *array.data(i);
// The following tests !isnan(current), but also works for integral
// types. (The isnan(IntegralType) overload is absent on MSVC.)
if (current == current) {
Expand Down
Loading
Loading