Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29f6abc

Browse files
committed
added pnpoly for better poly hit testing
svn path=/trunk/matplotlib/; revision=2749
1 parent 41ec0cf commit 29f6abc

6 files changed

Lines changed: 249 additions & 18 deletions

File tree

examples/mathtext_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
text(1, 1.6, tex, fontsize=20)
2222

2323
#title(r'$\Delta_i^j \hspace{0.4} \rm{versus} \hspace{0.4} \Delta_{i+1}^j$', fontsize=20)
24-
savefig('mathtext_demo.png')
25-
savefig('mathtext_demo.ps')
24+
savefig('mathtext_demo')
25+
2626

2727

2828
show()

lib/matplotlib/nxutils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys, numerix
2+
3+
if numerix.which[0] == "numarray":
4+
try:
5+
from matplotlib._na_nxutils import *
6+
except ImportError:
7+
numerix._import_fail_message("nxutils", "_na")
8+
raise
9+
elif numerix.which[0] == "numeric":
10+
try:
11+
from matplotlib._nc_nxutils import *
12+
except ImportError:
13+
numerix._import_fail_message("nxutils", "_nc")
14+
raise
15+
else: # Must be numpy
16+
try:
17+
from matplotlib._ns_nxutils import *
18+
except ImportError:
19+
numerix._import_fail_message("nxutils", "_ns")
20+
raise
21+
22+
23+

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
from distutils.core import Extension, setup
6363
from setupext import build_agg, build_gtkagg, build_tkagg, build_wxagg,\
6464
build_ft2font, build_image, build_windowing, build_transforms, \
65-
build_contour, build_enthought, build_swigagg, build_gdk, \
65+
build_contour, build_nxutils, build_enthought, build_swigagg, build_gdk, \
6666
build_subprocess, build_isnan
6767
import distutils.sysconfig
6868

@@ -131,6 +131,7 @@ def run(self):
131131
# these are not optional
132132
BUILD_FT2FONT = 1
133133
BUILD_CONTOUR = 1
134+
BUILD_NXUTILS = 1
134135

135136
# jdh
136137
packages = [
@@ -264,6 +265,7 @@ def add_dateutil():
264265

265266
if 1: # I don't think we need to make these optional
266267
build_contour(ext_modules, packages, NUMERIX)
268+
build_nxutils(ext_modules, packages, NUMERIX)
267269

268270
for mod in ext_modules:
269271
if VERBOSE:

setupext.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
BUILT_WXAGG = False
7979
BUILT_WINDOWING = False
8080
BUILT_CONTOUR = False
81+
BUILT_NXUTILS = False
8182
BUILT_ENTHOUGHT = False
8283
BUILT_CONTOUR = False
8384
BUILT_GDK = False
@@ -873,6 +874,47 @@ def build_contour(ext_modules, packages, numerix):
873874
BUILT_CONTOUR = True
874875

875876

877+
def build_nxutils(ext_modules, packages, numerix):
878+
global BUILT_NXUTILS
879+
if BUILT_NXUTILS: return # only build it if you you haven't already
880+
881+
if 'numarray' in numerix: # Build for numarray
882+
temp_copy('src/nxutils.c', 'src/_na_nxutils.c')
883+
module = Extension(
884+
'matplotlib._na_nxutils',
885+
[ 'src/_na_nxutils.c',],
886+
include_dirs=numarray_inc_dirs,
887+
)
888+
module.extra_compile_args.append('-DNUMARRAY=1')
889+
add_base_flags(module)
890+
ext_modules.append(module)
891+
892+
if 'Numeric' in numerix: # Build for Numeric
893+
temp_copy('src/nxutils.c', 'src/_nc_nxutils.c')
894+
module = Extension(
895+
'matplotlib._nc_nxutils',
896+
[ 'src/_nc_nxutils.c'],
897+
include_dirs=numeric_inc_dirs,
898+
)
899+
module.extra_compile_args.append('-DNUMERIC=1')
900+
add_base_flags(module)
901+
ext_modules.append(module)
902+
if 'numpy' in numerix: # Build for numpy
903+
temp_copy('src/nxutils.c', 'src/_ns_nxutils.c')
904+
module = Extension(
905+
'matplotlib._ns_nxutils',
906+
[ 'src/_ns_nxutils.c'],
907+
include_dirs=numeric_inc_dirs,
908+
)
909+
add_numpy_flags(module)
910+
module.extra_compile_args.append('-DSCIPY=1')
911+
add_base_flags(module)
912+
ext_modules.append(module)
913+
914+
915+
BUILT_NXUTILS = True
916+
917+
876918
def build_gdk(ext_modules, packages, numerix):
877919
global BUILT_GDK
878920
if BUILT_GDK: return # only build it if you you haven't already

src/cntr.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,7 @@
1818
#include "structmember.h"
1919
#include <stdlib.h>
2020
#include <stdio.h>
21-
22-
#ifdef NUMARRAY
23-
#include "numarray/arrayobject.h"
24-
#else
25-
#ifdef NUMERIC
26-
#include "Numeric/arrayobject.h"
27-
#else
28-
#define PY_ARRAY_TYPES_PREFIX NumPy
29-
#include "numpy/arrayobject.h"
30-
#if (NDARRAY_VERSION >= 0x00090908)
31-
#include "numpy/oldnumeric.h"
32-
#endif
33-
#endif
34-
#endif
35-
21+
#include "numerix.h"
3622

3723
/* Note that all arrays in these routines are Fortran-style,
3824
in the sense that the "i" index varies fastest; the dimensions

src/nxutils.c

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#include <Python.h>
2+
#include "structmember.h"
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
#include "numerix.h"
7+
8+
9+
/*
10+
pnpoly license
11+
Copyright (c) 1970-2003, Wm. Randolph Franklin
12+
13+
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:
14+
15+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
16+
2. Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
17+
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.
18+
19+
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.
20+
*/
21+
22+
int pnpoly_api(int npol, double *xp, double *yp, double x, double y)
23+
{
24+
int i, j, c = 0;
25+
for (i = 0, j = npol-1; i < npol; j = i++) {
26+
if ((((yp[i]<=y) && (y<yp[j])) ||
27+
((yp[j]<=y) && (y<yp[i]))) &&
28+
(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
29+
30+
c = !c;
31+
}
32+
return c;
33+
}
34+
35+
36+
static PyObject *
37+
pnpoly(PyObject *self, PyObject *args)
38+
{
39+
int npol, i;
40+
double x, y;
41+
double *xv, *yv;
42+
int b;
43+
PyObject *vertsarg;
44+
PyArrayObject *verts;
45+
if (! PyArg_ParseTuple(args, "ddO", &x, &y, &vertsarg))
46+
return NULL;
47+
48+
if (!PyArray_Check(vertsarg))
49+
{
50+
PyErr_SetString(PyExc_TypeError,
51+
"Arguments must be float, float, Nx2 array");
52+
return NULL;
53+
}
54+
55+
verts = (PyArrayObject *) PyArray_ContiguousFromObject(vertsarg,PyArray_DOUBLE, 2, 2);
56+
57+
if (verts == NULL)
58+
{
59+
PyErr_SetString(PyExc_ValueError,
60+
"Arguments verts must be a Nx2 array.");
61+
Py_XDECREF(verts);
62+
return NULL;
63+
64+
}
65+
66+
npol = verts->dimensions[0];
67+
//printf ("found %d verts\n", npol);
68+
if (verts->dimensions[1]!=2)
69+
{
70+
PyErr_SetString(PyExc_ValueError,
71+
"Arguments verts must be a Nx2 array.");
72+
Py_XDECREF(verts);
73+
return NULL;
74+
75+
}
76+
77+
78+
xv = (double *) PyMem_Malloc(sizeof(double) * npol);
79+
if (xv == NULL)
80+
{
81+
Py_XDECREF(verts);
82+
return NULL;
83+
}
84+
85+
yv = (double *) PyMem_Malloc(sizeof(double) * npol);
86+
if (yv == NULL)
87+
{
88+
Py_XDECREF(verts);
89+
PyMem_Free(xv);
90+
return NULL;
91+
}
92+
93+
for (i=0; i<npol; ++i) {
94+
xv[i] = *(double *)(verts->data + i*verts->strides[0]);
95+
yv[i] = *(double *)(verts->data + i*verts->strides[0] + verts->strides[1]);
96+
//printf("adding vert: %1.3f, %1.3f\n", xv[i], yv[i]);
97+
}
98+
99+
b = pnpoly_api(npol, xv, yv, x, y);
100+
//printf("in poly %d\n", b);
101+
102+
Py_XDECREF(verts);
103+
PyMem_Free(xv);
104+
PyMem_Free(yv);
105+
return Py_BuildValue("i", b);
106+
107+
}
108+
109+
110+
111+
static PyMethodDef module_methods[] = {
112+
{"pnpoly", pnpoly, METH_VARARGS},
113+
{NULL} /* Sentinel */
114+
};
115+
116+
117+
#ifdef NUMARRAY
118+
#if PY_MINOR_VERSION > 2
119+
PyMODINIT_FUNC
120+
#else
121+
DL_EXPORT(void)
122+
#endif
123+
init_na_nxutils(void)
124+
{
125+
PyObject* m;
126+
127+
128+
m = Py_InitModule3("_na_nxutils", module_methods,
129+
"general purpose utilities (numarray).");
130+
131+
if (m == NULL)
132+
return;
133+
134+
import_array();
135+
136+
}
137+
#endif
138+
#ifdef NUMERIC
139+
#if PY_MINOR_VERSION > 2
140+
PyMODINIT_FUNC
141+
#else
142+
DL_EXPORT(void)
143+
#endif
144+
init_nc_nxutils(void)
145+
{
146+
PyObject* m;
147+
148+
m = Py_InitModule3("_nc_nxutils", module_methods,
149+
"general purpose utilities (Numeric).");
150+
151+
if (m == NULL)
152+
return;
153+
154+
import_array();
155+
}
156+
#endif
157+
158+
#ifdef SCIPY
159+
#if PY_MINOR_VERSION > 2
160+
PyMODINIT_FUNC
161+
#else
162+
DL_EXPORT(void)
163+
#endif
164+
init_ns_nxutils(void)
165+
{
166+
PyObject* m;
167+
168+
m = Py_InitModule3("_ns_nxutils", module_methods,
169+
"general purpose utilities (numpy).");
170+
171+
if (m == NULL)
172+
return;
173+
174+
import_array();
175+
}
176+
#endif
177+
178+

0 commit comments

Comments
 (0)