Download envs/kitoverlay/skimage/_shared/_geometry.py from AVSim/simulation-package: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/datasets/AVSim/simulation-package/resolve/main/envs/kitoverlay/skimage/_shared/_geometry.py
- Command line
-
hf download hf://datasets/AVSim/simulation-package/envs/kitoverlay/skimage/_shared/_geometry.py
-
curl -L -o _geometry.py https://huggingface.co/datasets/AVSim/simulation-package/resolve/main/envs/kitoverlay/skimage/_shared/_geometry.py
1.34 kB
| __all__ = ['polygon_clip', 'polygon_area'] | |
| import numpy as np | |
| from .version_requirements import require | |
| def polygon_clip(rp, cp, r0, c0, r1, c1): | |
| """Clip a polygon to the given bounding box. | |
| Parameters | |
| ---------- | |
| rp, cp : (K,) 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 | |
| ------- | |
| r_clipped, c_clipped : (L,) ndarray of double | |
| Coordinates of clipped polygon. | |
| Notes | |
| ----- | |
| This makes use of Sutherland-Hodgman clipping as implemented in | |
| AGG 2.4 and exposed in Matplotlib. | |
| """ | |
| from matplotlib import path, transforms | |
| 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] | |
| return poly_clipped[:, 0], poly_clipped[:, 1] | |
| def polygon_area(pr, pc): | |
| """Compute the area of a polygon. | |
| Parameters | |
| ---------- | |
| pr, pc : (K,) array of float | |
| Polygon row and column coordinates. | |
| Returns | |
| ------- | |
| a : float | |
| Area of the polygon. | |
| """ | |
| pr = np.asarray(pr) | |
| pc = np.asarray(pc) | |
| return 0.5 * np.abs(np.sum((pc[:-1] * pr[1:]) - (pc[1:] * pr[:-1]))) | |