diff --git a/TODO.txt b/TODO.txt index 67429880..b483cc31 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,9 +1,10 @@ Remember to list any API changes below in `doc/source/api_changes.txt`. - Version 0.14 ------------ +* In `skimage._shared._geometry`, remove `clip_to_bbox` and replace with + `poly.clip_to_bbox`, if our dependency on Matplotlib is now >= 1.2. * Remove deprecated ``ntiles_*` kwargs in ``equalize_adapthist``. * Remove deprecated ``skimage.restoration.nl_means_denoising``. * Remove deprecated ``skimage.filters.gaussian_filter``. diff --git a/skimage/_shared/_geometry.py b/skimage/_shared/_geometry.py index b31f86ed..08d0e567 100644 --- a/skimage/_shared/_geometry.py +++ b/skimage/_shared/_geometry.py @@ -3,19 +3,19 @@ __all__ = ['polygon_clip', 'polygon_area'] import numpy as np -def polygon_clip(yp, xp, r0, c0, r1, c1): +def polygon_clip(rp, cp, r0, c0, r1, c1): """Clip a polygon to the given bounding box. Parameters ---------- - yp, xp : (N,) ndarray of double + rp, cp : (N,) ndarray of double Row and column coordinates of the polygon. (r0, c0), (r1, c1) : double Top-left and bottom-right coordinates of the bounding box. Returns ------- - y_clipped, x_clipped : (M,) ndarray of double + r_clipped, c_clipped : (M,) ndarray of double Coordinates of clipped polygon. Notes @@ -24,29 +24,36 @@ def polygon_clip(yp, xp, r0, c0, r1, c1): AGG 2.4 and exposed in Matplotlib. """ - from matplotlib import path, transforms + from matplotlib import _path, path, transforms - poly = path.Path(np.vstack((yp, xp)).T, closed=True) + # `clip_to_bbox` is included directly from Matplotlib + # since it was only included after v1.1 + def clip_to_bbox(poly_path, bbox, inside=True): + verts = _path.clip_path_to_rect(poly_path, bbox, inside) + paths = [path.Path(poly) for poly in verts] + return poly_path.make_compound_path(*paths) + + poly = path.Path(np.vstack((rp, cp)).T, closed=True) clip_rect = transforms.Bbox([[r0, c0], [r1, c1]]) - poly_clipped = poly.clip_to_bbox(clip_rect).to_polygons()[0] + poly_clipped = clip_to_bbox(poly, clip_rect).to_polygons()[0] return poly_clipped[:, 0], poly_clipped[:, 1] -def polygon_area(py, px): +def polygon_area(pr, pc): """Compute the area of a polygon. Parameters ---------- - py, px : (N,) array of float - Polygon coordinates. + pr, pc : (N,) array of float + Polygon row and column coordinates. Returns ------- a : float Area of the polygon. """ - py = np.asarray(py) - px = np.asarray(px) - return 0.5 * np.abs(np.sum((px[:-1] * py[1:]) - (px[1:] * py[:-1]))) + pr = np.asarray(pr) + pc = np.asarray(pc) + return 0.5 * np.abs(np.sum((pc[:-1] * pr[1:]) - (pc[1:] * pr[:-1])))