Skip to content

Check dimensions of arrays passed to C++, handle 0 dimensions #5274

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 4 commits into from
Oct 19, 2015
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
36 changes: 0 additions & 36 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,22 +922,6 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
typedef agg::conv_curve<snapped_t> snapped_curve_t;
typedef agg::conv_curve<clipped_t> curve_t;

if (offsets.dim(0) != 0 && offsets.dim(1) != 2) {
throw "Offsets array must be Nx2 or empty";
}

if (facecolors.dim(0) != 0 && facecolors.dim(1) != 4) {
throw "Facecolors array must be a Nx4 array or empty";
}

if (edgecolors.dim(0) != 0 && edgecolors.dim(1) != 4) {
throw "Edgecolors array must by Nx4 or empty";
}

if (transforms.dim(0) != 0 && (transforms.dim(1) != 3 || transforms.dim(2) != 3)) {
throw "Transforms array must by Nx3x3 or empty";
}

size_t Npaths = path_generator.num_paths();
size_t Noffsets = offsets.size();
size_t N = std::max(Npaths, Noffsets);
Expand Down Expand Up @@ -1266,14 +1250,6 @@ inline void RendererAgg::draw_gouraud_triangle(GCAgg &gc,
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);

if (points.dim(0) != 3 || points.dim(1) != 2) {
throw "points must be a 3x2 array";
}

if (colors.dim(0) != 3 || colors.dim(1) != 4) {
throw "colors must be a 3x4 array";
}

_draw_gouraud_triangle(points, colors, trans, has_clippath);
}

Expand All @@ -1288,18 +1264,6 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);

if (points.dim(1) != 3 || points.dim(2) != 2) {
throw "points must be a Nx3x2 array";
}

if (colors.dim(1) != 3 || colors.dim(2) != 4) {
throw "colors must be a Nx3x4 array";
}

if (points.dim(0) != colors.dim(0)) {
throw "points and colors arrays must be the same length";
}

for (int i = 0; i < points.dim(0); ++i) {
typename PointArray::sub_t point = points[i];
typename ColorArray::sub_t color = colors[i];
Expand Down
50 changes: 43 additions & 7 deletions src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ PyRendererAgg_draw_path_collection(PyRendererAgg *self, PyObject *args, PyObject
&convert_trans_affine,
&master_transform,
&pathobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&facecolors.converter,
&convert_colors,
&facecolors,
&edgecolors.converter,
&convert_colors,
&edgecolors,
&linewidths.converter,
&linewidths,
Expand Down Expand Up @@ -411,14 +411,14 @@ static PyObject *PyRendererAgg_draw_quad_mesh(PyRendererAgg *self, PyObject *arg
&mesh_height,
&coordinates.converter,
&coordinates,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
&facecolors.converter,
&convert_colors,
&facecolors,
&antialiased,
&edgecolors.converter,
&convert_colors,
&edgecolors)) {
return NULL;
}
Expand Down Expand Up @@ -459,6 +459,21 @@ PyRendererAgg_draw_gouraud_triangle(PyRendererAgg *self, PyObject *args, PyObjec
return NULL;
}

if (points.dim(0) != 3 || points.dim(1) != 2) {
PyErr_Format(PyExc_ValueError,
"points must be a 3x2 array, got %dx%d",
points.dim(0), points.dim(1));
return NULL;
}

if (colors.dim(0) != 3 || colors.dim(1) != 4) {
PyErr_Format(PyExc_ValueError,
"colors must be a 3x4 array, got %dx%d",
colors.dim(0), colors.dim(1));
return NULL;
}


CALL_CPP("draw_gouraud_triangle", (self->x->draw_gouraud_triangle(gc, points, colors, trans)));

Py_RETURN_NONE;
Expand All @@ -485,6 +500,27 @@ PyRendererAgg_draw_gouraud_triangles(PyRendererAgg *self, PyObject *args, PyObje
return NULL;
}

if (points.size() != 0 && (points.dim(1) != 3 || points.dim(2) != 2)) {
PyErr_Format(PyExc_ValueError,
"points must be a Nx3x2 array, got %dx%dx%d",
points.dim(0), points.dim(1), points.dim(2));
return NULL;
}

if (colors.size() != 0 && (colors.dim(1) != 3 || colors.dim(2) != 4)) {
PyErr_Format(PyExc_ValueError,
"colors must be a Nx3x4 array, got %dx%dx%d",
colors.dim(0), colors.dim(1), colors.dim(2));
return NULL;
}

if (points.size() != colors.size()) {
PyErr_Format(PyExc_ValueError,
"points and colors arrays must be the same length, got %d and %d",
points.dim(0), colors.dim(0));
return NULL;
}

CALL_CPP("draw_gouraud_triangles", self->x->draw_gouraud_triangles(gc, points, colors, trans));

Py_RETURN_NONE;
Expand Down
8 changes: 4 additions & 4 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void get_path_collection_extents(agg::trans_affine &master_transform,
agg::trans_affine &offset_trans,
extent_limits &extent)
{
if (offsets.dim(0) != 0 && offsets.dim(1) != 2) {
if (offsets.size() != 0 && offsets.dim(1) != 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this needed changing, but it doesn't hurt aside from a vague feeling of asymmetry.

throw "Offsets array must be Nx2";
}

Expand Down Expand Up @@ -416,7 +416,7 @@ void point_in_path_collection(double x,
return;
}

size_t Noffsets = offsets.dim(0);
size_t Noffsets = offsets.size();
size_t N = std::max(Npaths, Noffsets);
size_t Ntransforms = std::min(transforms.size(), N);
size_t i;
Expand Down Expand Up @@ -692,11 +692,11 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
template <class VerticesArray, class ResultArray>
void affine_transform_2d(VerticesArray &vertices, agg::trans_affine &trans, ResultArray &result)
{
if (vertices.dim(0) != 0 && vertices.dim(1) != 2) {
if (vertices.size() != 0 && vertices.dim(1) != 2) {
throw "Invalid vertices array.";
}

size_t n = vertices.dim(0);
size_t n = vertices.size();
double x;
double y;
double t0;
Expand Down
27 changes: 15 additions & 12 deletions src/_path_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_in_path",
&points.converter,
&convert_points,
&points,
&r,
&convert_path,
Expand All @@ -79,7 +79,7 @@ static PyObject *Py_points_in_path(PyObject *self, PyObject *args, PyObject *kwd
return NULL;
}

npy_intp dims[] = { points.dim(0) };
npy_intp dims[] = { points.size() };
numpy::array_view<bool, 1> results(dims);

CALL_CPP("points_in_path", (points_in_path(points, r, path, trans, results)));
Expand Down Expand Up @@ -128,7 +128,7 @@ static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwd

if (!PyArg_ParseTuple(args,
"O&dO&O&:points_on_path",
&points.converter,
&convert_points,
&points,
&r,
&convert_path,
Expand All @@ -138,7 +138,7 @@ static PyObject *Py_points_on_path(PyObject *self, PyObject *args, PyObject *kwd
return NULL;
}

npy_intp dims[] = { points.dim(0) };
npy_intp dims[] = { points.size() };
numpy::array_view<bool, 1> results(dims);

CALL_CPP("points_on_path", (points_on_path(points, r, path, trans, results)));
Expand Down Expand Up @@ -200,7 +200,10 @@ static PyObject *Py_update_path_extents(PyObject *self, PyObject *args, PyObject
}

if (minpos.dim(0) != 2) {
PyErr_SetString(PyExc_ValueError, "minpos must be of length 2");
PyErr_Format(PyExc_ValueError,
"minpos must be of length 2, got %d",
minpos.dim(0));
return NULL;
}

extent_limits e;
Expand Down Expand Up @@ -263,9 +266,9 @@ static PyObject *Py_get_path_collection_extents(PyObject *self, PyObject *args,
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans)) {
Expand Down Expand Up @@ -319,9 +322,9 @@ static PyObject *Py_point_in_path_collection(PyObject *self, PyObject *args, PyO
&convert_trans_affine,
&master_transform,
&pathsobj,
&transforms.converter,
&convert_transforms,
&transforms,
&offsets.converter,
&convert_points,
&offsets,
&convert_trans_affine,
&offset_trans,
Expand Down Expand Up @@ -434,15 +437,15 @@ static PyObject *Py_affine_transform(PyObject *self, PyObject *args, PyObject *k

try {
numpy::array_view<double, 2> vertices(vertices_obj);
npy_intp dims[] = { vertices.dim(0), 2 };
npy_intp dims[] = { vertices.size(), 2 };
numpy::array_view<double, 2> result(dims);
CALL_CPP("affine_transform", (affine_transform_2d(vertices, trans, result)));
return result.pyobj();
} catch (py::exception) {
PyErr_Clear();
try {
numpy::array_view<double, 1> vertices(vertices_obj);
npy_intp dims[] = { vertices.dim(0) };
npy_intp dims[] = { vertices.size() };
numpy::array_view<double, 1> result(dims);
CALL_CPP("affine_transform", (affine_transform_1d(vertices, trans, result)));
return result.pyobj();
Expand All @@ -464,7 +467,7 @@ static PyObject *Py_count_bboxes_overlapping_bbox(PyObject *self, PyObject *args
"O&O&:count_bboxes_overlapping_bbox",
&convert_rect,
&bbox,
&bboxes.converter,
&convert_bboxes,
&bboxes)) {
return NULL;
}
Expand Down
16 changes: 15 additions & 1 deletion src/numpy_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,23 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
return m_shape[i];
}

/*
In most cases, code should use size() instead of dim(0), since
size() == 0 when any dimension is 0.
*/
size_t size() const
{
return (size_t)dim(0);
bool empty = (ND == 0);
for (size_t i = 0; i < ND; i++) {
if (m_shape[i] == 0) {
empty = true;
}
}
if (empty) {
return 0;
} else {
return (size_t)dim(0);
}
}

bool empty() const
Expand Down
Loading