Skip to content

Commit 167fbff

Browse files
committed
added inside poly profile script
svn path=/trunk/matplotlib/; revision=2752
1 parent d2500ca commit 167fbff

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

examples/lasso_demo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
inside polygon detection routine.
99
"""
1010
from matplotlib.widgets import Lasso
11-
from matplotlib.mlab import inside_poly
11+
import matplotlib.mlab
12+
from matplotlib.nxutils import points_inside_poly
1213
from matplotlib.colors import colorConverter
1314
from matplotlib.collections import RegularPolyCollection
1415

@@ -47,8 +48,8 @@ def __init__(self, ax, data):
4748

4849
def callback(self, verts):
4950
#print 'all done', verts
50-
ind = inside_poly(self.xys, verts)
51-
51+
#ind = matplotlib.mlab._inside_poly_deprecated(self.xys, verts)
52+
ind = nx.nonzero(points_inside_poly(self.xys, verts))
5253
for i in range(self.Nxy):
5354
if i in ind:
5455
self.facecolors[i] = Datum.colorin

lib/matplotlib/mlab.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import numerix.mlab
6262
from numerix import linear_algebra
6363
import numerix as nx
64+
import nxutils
6465

6566
from numerix import array, asarray, arange, divide, exp, arctan2, \
6667
multiply, transpose, ravel, repeat, resize, reshape, floor, ceil,\
@@ -1428,8 +1429,9 @@ def stineman_interp(xi,x,y,yp=None):
14281429

14291430
return yi
14301431

1431-
def inside_poly(points, verts):
1432+
def _inside_poly_deprecated(points, verts):
14321433
"""
1434+
# use nxutils.points_inside_poly instead
14331435
points is a sequence of x,y points
14341436
verts is a sequence of x,y vertices of a poygon
14351437
@@ -1439,7 +1441,6 @@ def inside_poly(points, verts):
14391441
xys = nx.asarray(points)
14401442
Nxy = xys.shape[0]
14411443
Nv = len(verts)
1442-
14431444

14441445
def angle(x1, y1, x2, y2):
14451446
twopi = 2*nx.pi
@@ -1469,6 +1470,16 @@ def angle(x1, y1, x2, y2):
14691470
angles += a
14701471
return nx.nonzero(nx.greater_equal(nx.absolute(angles), nx.pi))
14711472

1473+
def inside_poly(points, verts):
1474+
""""
1475+
points is a sequence of x,y points
1476+
verts is a sequence of x,y vertices of a poygon
1477+
1478+
return value is a sequence on indices into points for the points
1479+
that are inside the polygon
1480+
"""
1481+
return nx.nonzero(nxutils.points_inside_poly(points, verts))
1482+
14721483
### the following code was written and submitted by Fernando Perez
14731484
### from the ipython numutils package under a BSD license
14741485
# begin fperez functions

src/nxutils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ points_inside_poly(PyObject *self, PyObject *args)
221221

222222

223223
static PyMethodDef module_methods[] = {
224-
{"pnpoly", pnpoly, METH_VARARGS, "inside = pnpoly(x, y, xyverts)\nreturn True if x,y is inside the polygon defined by the sequence of x,y vertices in xyverts"},
224+
{"pnpoly", pnpoly, METH_VARARGS, "inside = pnpoly(x, y, xyverts)\nreturn 1 if x,y is inside the polygon defined by the sequence of x,y vertices in xyverts"},
225225
{"points_inside_poly", points_inside_poly, METH_VARARGS, "mask = points_inside_poly(xypoints, xyverts)\nreturn a mask of length xypoints indicating whether each x,y point is inside the polygon defined by the sequence of x,y vertices in xyverts"},
226226
{NULL} /* Sentinel */
227-
};
227+
}
228228

229229

230230
#ifdef NUMARRAY

unit/inside_poly_profile.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os, sys, time
2+
3+
import matplotlib.nxutils as nxutils
4+
import matplotlib.numerix as nx
5+
import matplotlib.mlab
6+
import matplotlib.patches as patches
7+
if 1:
8+
numtrials, numverts, numpoints = 50, 1000, 1000
9+
verts = patches.CirclePolygon((0.5, 0.5), radius=0.5, resolution=numverts).get_verts()
10+
11+
t0 = time.time()
12+
for i in range(numtrials):
13+
points = nx.mlab.rand(numpoints,2)
14+
mask = matplotlib.mlab._inside_poly_deprecated(points, verts)
15+
told = time.time() - t0
16+
17+
t0 = time.time()
18+
for i in range(numtrials):
19+
points = nx.mlab.rand(numpoints,2)
20+
mask = nxutils.points_inside_poly(points, verts)
21+
tnew = time.time() - t0
22+
print numverts, numpoints, told, tnew, told/tnew
23+
24+
25+

0 commit comments

Comments
 (0)