-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Added initial implementation of Polygon to_mask and as_patch #87
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
91c56b7
Added initial implementation of polygon_overlap_grid
astrofrog 12df57b
Explicitly convert vertices to float
astrofrog 1063e9f
Implement contains for polygons
astrofrog 8b232cf
Added URLs
astrofrog bf3f349
Deal properly with scalar PixCoords
astrofrog d90426d
Add license line
astrofrog 9f542c1
Add note about why pxd files are required
astrofrog b085a2a
Tidy up Cython code
astrofrog c6cef1e
Added mask reference test for polygon region
astrofrog 4f7a97f
Remove unused import
astrofrog e02710d
Add note about algorithm
astrofrog File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
|
||
# This file is needed in order to be able to cimport functions into other Cython files | ||
|
||
cimport numpy as np | ||
|
||
ctypedef np.float64_t DTYPE_t | ||
|
||
cdef int point_in_polygon(double x, double y, np.ndarray[DTYPE_t, ndim=1] vx, np.ndarray[DTYPE_t, ndim=1] vy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
# | ||
# The code in this file was adapted from code written by Greg von Winckel: | ||
# | ||
# https://github.com/gregvw/pnpoly | ||
# | ||
# and which was released under the following license: | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# The MIT License (MIT) | ||
# | ||
# Copyright (c) 2014 Greg von Winckel | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
# this software and associated documentation files (the "Software"), to deal in | ||
# the Software without restriction, including without limitation the rights to | ||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
# the Software, and to permit persons to whom the Software is furnished to do so, | ||
# subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
# | ||
# ---------------------------------------------------------------------------- | ||
# | ||
# This code was itself adapted from code written by W. Randolph Franklin: | ||
# | ||
# http://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html | ||
# | ||
# and released under the following license: | ||
# | ||
# Copyright (c) 1970-2003, Wm. Randolph Franklin | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimers. | ||
# | ||
# Redistributions in binary form must reproduce the above copyright notice in | ||
# the documentation and/or other materials provided with the distribution. | ||
# | ||
# The name of W. Randolph Franklin may not be used to endorse or promote | ||
# products derived from this Software without specific prior written permission. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
import numpy as np | ||
cimport numpy as np | ||
|
||
DTYPE_BOOL = np.bool | ||
ctypedef np.uint8_t DTYPE_BOOL_t | ||
|
||
|
||
def points_in_polygon(np.ndarray[DTYPE_t, ndim=1] x, | ||
np.ndarray[DTYPE_t, ndim=1] y, | ||
np.ndarray[DTYPE_t, ndim=1] vx, | ||
np.ndarray[DTYPE_t, ndim=1] vy): | ||
|
||
cdef int i, n | ||
cdef np.ndarray[np.uint8_t, ndim=1, cast=True] result | ||
|
||
n = x.shape[0] | ||
|
||
result = np.zeros(n, DTYPE_BOOL) | ||
|
||
for i in range(n): | ||
result[i] = point_in_polygon(x[i], y[i], vx, vy) | ||
|
||
return result | ||
|
||
|
||
cdef int point_in_polygon(double x, double y, | ||
np.ndarray[DTYPE_t, ndim=1] vx, | ||
np.ndarray[DTYPE_t, ndim=1] vy): | ||
""" | ||
Determine whether a test point (x, y) is within a polygon defined by a set | ||
of vertices (vx, vy). | ||
|
||
This uses the even-odd rule, as described here: | ||
|
||
https://en.wikipedia.org/wiki/Even–odd_rule | ||
""" | ||
|
||
cdef int i, j, k, m, n | ||
cdef int result | ||
|
||
n = vx.shape[0] | ||
|
||
result = 0 | ||
|
||
for i in range(n): | ||
j = (i + n - 1) % n | ||
if(((vy[i] > y) != (vy[j] > y)) and | ||
(x < (vx[j] - vx[i]) * (y - vy[i]) / (vy[j] - vy[i]) + vx[i])): | ||
result += 1 | ||
|
||
return result % 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
|
||
from __future__ import (absolute_import, division, print_function, | ||
unicode_literals) | ||
|
||
import numpy as np | ||
cimport numpy as np | ||
|
||
|
||
__all__ = ['polygonal_overlap_grid'] | ||
|
||
|
||
DTYPE = np.float64 | ||
ctypedef np.float64_t DTYPE_t | ||
|
||
from .pnpoly cimport point_in_polygon | ||
|
||
__all__ = ['polygonal_overlap_grid'] | ||
|
||
|
||
def polygonal_overlap_grid(double xmin, double xmax, double ymin, double ymax, | ||
int nx, int ny, | ||
np.ndarray[DTYPE_t, ndim=1] vx, | ||
np.ndarray[DTYPE_t, ndim=1] vy, | ||
int use_exact, | ||
int subpixels): | ||
""" | ||
polygonal_overlap_grid(xmin, xmax, ymin, ymax, nx, ny, r, | ||
use_exact, subpixels) | ||
|
||
Area of overlap between a polygon and a pixel grid. | ||
|
||
Parameters | ||
---------- | ||
xmin, xmax, ymin, ymax : float | ||
Extent of the grid in the x and y direction. | ||
nx, ny : int | ||
Grid dimensions. | ||
vx, vy : `numpy.ndarray` | ||
The vertices of the polygon | ||
use_exact : 0 or 1 | ||
If ``1`` calculates exact overlap, if ``0`` uses ``subpixel`` number | ||
of subpixels to calculate the overlap. | ||
subpixels : int | ||
Each pixel resampled by this factor in each dimension, thus each | ||
pixel is divided into ``subpixels ** 2`` subpixels. | ||
|
||
Returns | ||
------- | ||
frac : `~numpy.ndarray` (float) | ||
2-d array of shape (ny, nx) giving the fraction of the overlap. | ||
""" | ||
|
||
cdef unsigned int i, j | ||
cdef double x, y, dx, dy, d, pixel_radius | ||
cdef double bxmin, bxmax, bymin, bymax | ||
cdef double pxmin, pxcen, pxmax, pymin, pycen, pymax | ||
|
||
# Define output array | ||
cdef np.ndarray[DTYPE_t, ndim=2] frac = np.zeros([ny, nx], dtype=DTYPE) | ||
|
||
if use_exact == 1: | ||
raise NotImplementedError("Exact mode has not been implemented for " | ||
"polygonal apertures") | ||
|
||
# Find the width of each element in x and y | ||
dx = (xmax - xmin) / nx | ||
dy = (ymax - ymin) / ny | ||
|
||
# Define bounding box | ||
bxmin = vx.min() | ||
bxmax = vx.max() | ||
bymin = vy.min() | ||
bymax = vy.max() | ||
|
||
for i in range(nx): | ||
pxmin = xmin + i * dx # lower end of pixel | ||
pxmax = pxmin + dx # upper end of pixel | ||
if pxmax > bxmin and pxmin < bxmax: | ||
for j in range(ny): | ||
pymin = ymin + j * dy | ||
pymax = pymin + dy | ||
if pymax > bymin and pymin < bymax: | ||
frac[j, i] = polygonal_overlap_single_subpixel(pxmin, pymin, | ||
pxmax, pymax, | ||
vx, vy, subpixels) | ||
|
||
return frac | ||
|
||
|
||
cdef double polygonal_overlap_single_subpixel(double x0, double y0, | ||
double x1, double y1, | ||
np.ndarray[DTYPE_t, ndim=1] vx, | ||
np.ndarray[DTYPE_t, ndim=1] vy, | ||
int subpixels): | ||
""" | ||
Return the fraction of overlap between a polygon and a single pixel | ||
with given extent, using a sub-pixel sampling method. | ||
""" | ||
|
||
cdef unsigned int i, j | ||
cdef double x, y, dx, dy | ||
cdef double frac = 0. # Accumulator. | ||
|
||
dx = (x1 - x0) / subpixels | ||
dy = (y1 - y0) / subpixels | ||
|
||
x = x0 - 0.5 * dx | ||
for i in range(subpixels): | ||
x += dx | ||
y = y0 - 0.5 * dy | ||
for j in range(subpixels): | ||
y += dy | ||
if point_in_polygon(x, y, vx, vy) == 1: | ||
frac += 1. | ||
|
||
return frac / (subpixels * subpixels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
regions/shapes/tests/reference/test_to_mask_poly-mode_center.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 |
7 changes: 7 additions & 0 deletions
7
regions/shapes/tests/reference/test_to_mask_poly-mode_subpixels-subpixels_1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00 |
7 changes: 7 additions & 0 deletions
7
regions/shapes/tests/reference/test_to_mask_poly-mode_subpixels-subpixels_18.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
4.19753086e-01 3.98148148e-01 2.31481481e-01 6.17283951e-02 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
6.04938272e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 8.88888889e-01 6.94444444e-01 1.85185185e-02 | ||
4.07407407e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 5.83333333e-01 0.00000000e+00 | ||
1.94444444e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 9.72222222e-01 1.11111111e-01 0.00000000e+00 | ||
2.16049383e-02 9.72222222e-01 1.00000000e+00 1.00000000e+00 6.01851852e-01 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 3.91975309e-01 8.88888889e-01 9.81481481e-01 1.29629630e-01 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 2.77777778e-02 2.56172840e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 |
7 changes: 7 additions & 0 deletions
7
regions/shapes/tests/reference/test_to_mask_poly-mode_subpixels-subpixels_5.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
4.40000000e-01 4.00000000e-01 2.00000000e-01 4.00000000e-02 0.00000000e+00 0.00000000e+00 0.00000000e+00 | ||
6.00000000e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 8.80000000e-01 6.80000000e-01 0.00000000e+00 | ||
4.00000000e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 5.60000000e-01 0.00000000e+00 | ||
2.00000000e-01 1.00000000e+00 1.00000000e+00 1.00000000e+00 9.60000000e-01 8.00000000e-02 0.00000000e+00 | ||
0.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00 6.00000000e-01 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 4.00000000e-01 9.20000000e-01 1.00000000e+00 1.60000000e-01 0.00000000e+00 0.00000000e+00 | ||
0.00000000e+00 0.00000000e+00 4.00000000e-02 2.80000000e-01 0.00000000e+00 0.00000000e+00 0.00000000e+00 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add standard license line at the top, even if it's a very small file.
Can you add a docstring with a sentence saying why we need a
pxd
file at all?(my Cython is always rusty because I use it so rarely. It's the same for most Python programmers.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My knowledge of Cython is 'because Cython complains if it's not there' ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (it's because it's needed for cimport to work)