*** nxutils_orig.c	Wed Sep 27 11:45:08 2006
--- nxutils.c	Thu Sep 28 11:03:52 2006
***************
*** 217,225 ****
--- 217,348 ----
    
  }
  
+ 
+ static PyObject *
+ point_inside_polys(PyObject *self, PyObject *args)
+ // added by Seth Olsen as a counterpoint to points_inside_poly
+ // 28 Sep 2006
+ {
+   int npols, nverts, i, j;
+   double *xv, *yv, x, y;
+   int b;
+   PyObject *xypointsarg, *vertsarg, *ret;
+   PyArrayObject *xypoints, *verts;
+   PyArrayObject *mask;
+   int dimensions[1];
+   
+   if (! PyArg_ParseTuple(args, "OO", &xypointsarg, &vertsarg))
+     return NULL;
+   
+   verts = (PyArrayObject *) PyArray_FromObject(vertsarg,PyArray_DOUBLE, 3, 3);
+   
+   if (verts == NULL)
+     {
+       PyErr_SetString(PyExc_ValueError,
+ 		      "Argument polys must be a (npoly,nverts,2) dimensioned array.");
+       Py_XDECREF(verts);
+       return NULL;
+     }
+   
+   npols = verts->dimensions[0];
+   nverts = verts->dimensions[1];
+   //printf ("found %d verts\n", npols);
+   if (verts->dimensions[2]!=2) 
+     {
+       PyErr_SetString(PyExc_ValueError,
+ 		      "Arguments polys must be a (npoly,nverts,2) dimensioned array.");
+       Py_XDECREF(verts);
+       return NULL;      
+     }
+     
+   xv = (double *) PyMem_Malloc(sizeof(double) * nverts);
+   if (xv == NULL)
+     {
+       Py_XDECREF(verts);
+       return NULL;
+     }
+   
+   yv = (double *) PyMem_Malloc(sizeof(double) * nverts);
+   if (yv == NULL)
+     {
+       Py_XDECREF(verts);
+       PyMem_Free(xv);
+       return NULL;
+     }
+   
+   xypoints = (PyArrayObject *) PyArray_FromObject(xypointsarg,PyArray_DOUBLE, 1, 1);
+   
+   if (xypoints == NULL)
+     {
+       PyErr_SetString(PyExc_ValueError,
+ 		      "Argument xypoint must be (x_pt,y_pt).");
+       Py_XDECREF(verts);
+       Py_XDECREF(xypoints);
+       PyMem_Free(xv);
+       PyMem_Free(yv);
+       return NULL;
+     }
+   
+   if (xypoints->dimensions[0]!=2) 
+     {
+       PyErr_SetString(PyExc_ValueError,
+ 		      "Argument xypoint must be (x_pt,y_pt).");
+       
+       Py_XDECREF(verts);
+       Py_XDECREF(xypoints);
+       PyMem_Free(xv);
+       PyMem_Free(yv);
+       return NULL;
+     }
+ 
+   x = *(double *)(xypoints->data);
+   y = *(double *)(xypoints->data + xypoints->strides[0]);
+   
+   dimensions[0] = npols;
+ 
+   mask = (PyArrayObject *)PyArray_FromDims(1,dimensions,PyArray_INT);
+   if (mask==NULL) 
+     {
+       Py_XDECREF(verts);
+       Py_XDECREF(xypoints);
+       PyMem_Free(xv);
+       PyMem_Free(yv);
+       return NULL;  
+     }
+   
+ 
+   //**** Loop over polys 
+   for (j=0; j<npols; ++j) 
+     {
+       // fill the verts arrays
+       for (i=0; i<nverts; ++i) 
+ 	{
+ 	  xv[i] = *(double *)(verts->data + j*verts->strides[0] + i*verts->strides[1]);
+ 	  yv[i] = *(double *)(verts->data + j*verts->strides[0] + i*verts->strides[1] + verts->strides[2]);
+ 	  //printf("adding vert: %1.3f, %1.3f\n", xv[i], yv[i]);
+ 	}
+       
+       b = pnpoly_api(nverts, xv, yv, x, y);
+       //printf("checking %d, %d, %1.3f, %1.3f, %d\n", npols, nverts, x, y, b);
+       *(int *)(mask->data+j*mask->strides[0]) = b;  
+     }
+   
+   
+   Py_XDECREF(verts);
+   Py_XDECREF(xypoints);
+   
+   PyMem_Free(xv);
+   PyMem_Free(yv);
+   ret =  Py_BuildValue("O", mask);
+   Py_XDECREF(mask);
+   return ret;
+ }
+ 
+ 
  static PyMethodDef module_methods[] = {
    {"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"},
    {"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"},
+   {"point_inside_polys",  point_inside_polys, METH_VARARGS, "mask = point_inside_polys(xypoint, polys)\nreturn a mask of length polys indicating whether each x,y point is inside the polygon defined by the sequence of x,y vertices in xyverts in polys, poly is a sequence of xyvert sequences"},
    {NULL}  /* Sentinel */
  };
  
