mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-30 17:07:47 +08:00
Refactor pnpoly function in Cython and add to shared package
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
|
||||
cdef inline unsigned char point_in_polygon(int nr_verts, double *xp, double *yp,
|
||||
double x, double y)
|
||||
|
||||
cdef void points_in_polygon(int nr_verts, double *xp, double *yp,
|
||||
int nr_points, double *x, double *y,
|
||||
unsigned char *result)
|
||||
@@ -0,0 +1,27 @@
|
||||
#cython: cdivison=True
|
||||
#cython: boundscheck=False
|
||||
#cython: nonecheck=False
|
||||
#cython: wraparound=False
|
||||
|
||||
|
||||
cdef inline unsigned char point_in_polygon(int nr_verts, double *xp, double *yp,
|
||||
double x, double y):
|
||||
cdef int i
|
||||
cdef unsigned char c = 0
|
||||
cdef int j = nr_verts - 1
|
||||
for i in range(nr_verts):
|
||||
if (
|
||||
(((yp[i] <= y) and (y < yp[j])) or
|
||||
((yp[j] <= y) and (y < yp[i])))
|
||||
and (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])
|
||||
):
|
||||
c = not c
|
||||
j = i
|
||||
return c
|
||||
|
||||
cdef void points_in_polygon(int nr_verts, double *xp, double *yp,
|
||||
int nr_points, double *x, double *y,
|
||||
unsigned char *result):
|
||||
cdef int n
|
||||
for n in range(nr_points):
|
||||
result[n] = point_in_polygon(nr_verts, xp, yp, x[n], y[n])
|
||||
@@ -13,9 +13,11 @@ def configuration(parent_package='', top_path=None):
|
||||
config = Configuration('_shared', parent_package, top_path)
|
||||
config.add_data_dir('tests')
|
||||
|
||||
cython(['geometry.pyx'], working_path=base_path)
|
||||
cython(['interpolation.pyx'], working_path=base_path)
|
||||
cython(['transform.pyx'], working_path=base_path)
|
||||
|
||||
config.add_extension('geometry', sources=['geometry.c'])
|
||||
config.add_extension('interpolation', sources=['interpolation.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
config.add_extension('transform', sources=['transform.c'],
|
||||
|
||||
@@ -3,11 +3,7 @@ import math
|
||||
from libc.math cimport sqrt
|
||||
cimport numpy as np
|
||||
cimport cython
|
||||
|
||||
|
||||
cdef extern from "../morphology/_pnpoly.h":
|
||||
int pnpoly(int nr_verts, double *xp, double *yp,
|
||||
double x, double y)
|
||||
from skimage._shared.geometry cimport point_in_polygon
|
||||
|
||||
|
||||
@cython.boundscheck(False)
|
||||
@@ -119,7 +115,7 @@ def polygon(y, x, shape=None):
|
||||
|
||||
for r in range(minr, maxr+1):
|
||||
for c in range(minc, maxc+1):
|
||||
if pnpoly(nr_verts, cptr, rptr, c, r):
|
||||
if point_in_polygon(nr_verts, cptr, rptr, c, r):
|
||||
rr.append(r)
|
||||
cc.append(c)
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ def configuration(parent_package='', top_path=None):
|
||||
cython(['_draw.pyx'], working_path=base_path)
|
||||
|
||||
config.add_extension('_draw', sources=['_draw.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
include_dirs=[get_numpy_include_dirs(), '../shared'])
|
||||
|
||||
return config
|
||||
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
/* `pnpoly` is from
|
||||
|
||||
http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
|
||||
|
||||
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:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimers.
|
||||
2. Redistributions in binary form must reproduce the above
|
||||
copyright notice in the documentation and/or other materials
|
||||
provided with the distribution.
|
||||
3. 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. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
unsigned char pnpoly(int nr_verts, double *xp, double *yp, double x, double y)
|
||||
{
|
||||
int i, j;
|
||||
unsigned char c = 0;
|
||||
for (i = 0, j = nr_verts-1; i < nr_verts; j = i++) {
|
||||
if ((((yp[i]<=y) && (y<yp[j])) ||
|
||||
((yp[j]<=y) && (y<yp[i]))) &&
|
||||
(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
|
||||
|
||||
c = !c;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
void npnpoly(int nr_verts, double *xp, double *yp,
|
||||
int nr_points, double *x, double *y,
|
||||
unsigned char *result)
|
||||
/*
|
||||
* For N provided points, calculate whether they are in
|
||||
* the polygon defined by vertices *xp, *yp.
|
||||
*
|
||||
* nr_verts : number of vertices
|
||||
* *xp, *yp : x and y coordinates of vertices
|
||||
* nr_points : number of data points provided
|
||||
* *x, *y : data points
|
||||
*/
|
||||
{
|
||||
unsigned char n = 0;
|
||||
for (n = 0; n < nr_points; n++) {
|
||||
result[n] = pnpoly(nr_verts, xp, yp, x[n], y[n]);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -2,14 +2,7 @@
|
||||
|
||||
cimport numpy as np
|
||||
import numpy as np
|
||||
|
||||
cdef extern from "_pnpoly.h":
|
||||
int pnpoly(int nr_verts, double *xp, double *yp,
|
||||
double x, double y)
|
||||
|
||||
void npnpoly(int nr_verts, double *xp, double *yp,
|
||||
int nr_points, double *x, double *y,
|
||||
unsigned char *result)
|
||||
from skimage._shared.geometry cimport point_in_polygon, points_in_polygon
|
||||
|
||||
|
||||
def grid_points_inside_poly(shape, verts):
|
||||
@@ -49,10 +42,10 @@ def grid_points_inside_poly(shape, verts):
|
||||
|
||||
for m in range(M):
|
||||
for n in range(N):
|
||||
out[m, n] = pnpoly(V, <double*>vx.data, <double*>vy.data, m, n)
|
||||
out[m, n] = point_in_polygon(V, <double*>vx.data, <double*>vy.data, m, n)
|
||||
|
||||
return out.view(bool)
|
||||
|
||||
|
||||
|
||||
def points_inside_poly(points, verts):
|
||||
"""Test whether points lie inside a polygon.
|
||||
@@ -84,8 +77,8 @@ def points_inside_poly(points, verts):
|
||||
|
||||
cdef np.ndarray[np.uint8_t, ndim=1] out = \
|
||||
np.zeros(x.shape[0], dtype=np.uint8)
|
||||
|
||||
npnpoly(vx.shape[0], <double*>vx.data, <double*>vy.data,
|
||||
|
||||
points_in_polygon(vx.shape[0], <double*>vx.data, <double*>vy.data,
|
||||
x.shape[0], <double*>x.data, <double*>y.data,
|
||||
<unsigned char*>out.data)
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ def configuration(parent_package='', top_path=None):
|
||||
config.add_extension('_skeletonize_cy', sources=['_skeletonize_cy.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
config.add_extension('_pnpoly', sources=['_pnpoly.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
include_dirs=[get_numpy_include_dirs(), '../shared'])
|
||||
config.add_extension('_convex_hull', sources=['_convex_hull.c'],
|
||||
include_dirs=[get_numpy_include_dirs()])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user