1 | // Python - C extension for polygon module. |
---|
2 | // |
---|
3 | // To compile (Python2.3): |
---|
4 | // gcc -c polygon_ext.c -I/usr/include/python2.3 -o polygon_ext.o -Wall -O |
---|
5 | // gcc -shared polygon_ext.o -o polygon_ext.so |
---|
6 | // |
---|
7 | // See the module polygon.py |
---|
8 | // |
---|
9 | // |
---|
10 | // Ole Nielsen, GA 2004 |
---|
11 | // |
---|
12 | // NOTE: We use long* instead of int* for Numeric arrays as this will work both |
---|
13 | // for 64 as well as 32 bit systems |
---|
14 | |
---|
15 | |
---|
16 | #include "Python.h" |
---|
17 | #include "numpy/arrayobject.h" |
---|
18 | #include "math.h" |
---|
19 | |
---|
20 | |
---|
21 | int __point_on_line(double x, double y, |
---|
22 | double x0, double y0, |
---|
23 | double x1, double y1, |
---|
24 | double rtol, |
---|
25 | double atol) { |
---|
26 | /*Determine whether a point is on a line segment |
---|
27 | |
---|
28 | Input: x, y, x0, x0, x1, y1: where |
---|
29 | point is given by x, y |
---|
30 | line is given by (x0, y0) and (x1, y1) |
---|
31 | |
---|
32 | */ |
---|
33 | |
---|
34 | double a0, a1, a_normal0, a_normal1, b0, b1, len_a, len_b; |
---|
35 | double nominator, denominator; |
---|
36 | int is_parallel; |
---|
37 | |
---|
38 | a0 = x - x0; |
---|
39 | a1 = y - y0; |
---|
40 | |
---|
41 | a_normal0 = a1; |
---|
42 | a_normal1 = -a0; |
---|
43 | |
---|
44 | b0 = x1 - x0; |
---|
45 | b1 = y1 - y0; |
---|
46 | |
---|
47 | nominator = fabs(a_normal0*b0 + a_normal1*b1); |
---|
48 | denominator = b0*b0 + b1*b1; |
---|
49 | |
---|
50 | // Determine if line is parallel to point vector up to a tolerance |
---|
51 | is_parallel = 0; |
---|
52 | if (denominator == 0.0) { |
---|
53 | // Use absolute tolerance |
---|
54 | if (nominator <= atol) { |
---|
55 | is_parallel = 1; |
---|
56 | } |
---|
57 | } else { |
---|
58 | // Denominator is positive - use relative tolerance |
---|
59 | if (nominator/denominator <= rtol) { |
---|
60 | is_parallel = 1; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | if (is_parallel) { |
---|
65 | // Point is somewhere on the infinite extension of the line |
---|
66 | // subject to specified absolute tolerance |
---|
67 | |
---|
68 | len_a = sqrt(a0*a0 + a1*a1); |
---|
69 | len_b = sqrt(b0*b0 + b1*b1); |
---|
70 | |
---|
71 | if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) { |
---|
72 | return 1; |
---|
73 | } else { |
---|
74 | return 0; |
---|
75 | } |
---|
76 | } else { |
---|
77 | return 0; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | /* |
---|
84 | WORK IN PROGRESS TO OPTIMISE INTERSECTION |
---|
85 | int __intersection(double x0, double y0, |
---|
86 | double x1, double y1) { |
---|
87 | |
---|
88 | |
---|
89 | x0 = line0[0,0]; y0 = line0[0,1] |
---|
90 | x1 = line0[1,0]; y1 = line0[1,1] |
---|
91 | |
---|
92 | x2 = line1[0,0]; y2 = line1[0,1] |
---|
93 | x3 = line1[1,0]; y3 = line1[1,1] |
---|
94 | |
---|
95 | denom = (y3-y2)*(x1-x0) - (x3-x2)*(y1-y0) |
---|
96 | u0 = (x3-x2)*(y0-y2) - (y3-y2)*(x0-x2) |
---|
97 | u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) |
---|
98 | |
---|
99 | if allclose(denom, 0.0): |
---|
100 | # Lines are parallel - check if they coincide on a shared a segment |
---|
101 | |
---|
102 | if allclose( [u0, u1], 0.0 ): |
---|
103 | # We now know that the lines if continued coincide |
---|
104 | # The remaining check will establish if the finite lines share a segment |
---|
105 | |
---|
106 | line0_starts_on_line1 = line0_ends_on_line1 =\ |
---|
107 | line1_starts_on_line0 = line1_ends_on_line0 = False |
---|
108 | |
---|
109 | if point_on_line([x0, y0], line1): |
---|
110 | line0_starts_on_line1 = True |
---|
111 | |
---|
112 | if point_on_line([x1, y1], line1): |
---|
113 | line0_ends_on_line1 = True |
---|
114 | |
---|
115 | if point_on_line([x2, y2], line0): |
---|
116 | line1_starts_on_line0 = True |
---|
117 | |
---|
118 | if point_on_line([x3, y3], line0): |
---|
119 | line1_ends_on_line0 = True |
---|
120 | |
---|
121 | if not(line0_starts_on_line1 or line0_ends_on_line1\ |
---|
122 | or line1_starts_on_line0 or line1_ends_on_line0): |
---|
123 | # Lines are parallel and would coincide if extended, but not as they are. |
---|
124 | return 3, None |
---|
125 | |
---|
126 | |
---|
127 | # One line fully included in the other. Use direction of included line |
---|
128 | if line0_starts_on_line1 and line0_ends_on_line1: |
---|
129 | # Shared segment is line0 fully included in line1 |
---|
130 | segment = array([[x0, y0], [x1, y1]]) |
---|
131 | |
---|
132 | if line1_starts_on_line0 and line1_ends_on_line0: |
---|
133 | # Shared segment is line1 fully included in line0 |
---|
134 | segment = array([[x2, y2], [x3, y3]]) |
---|
135 | |
---|
136 | |
---|
137 | # Overlap with lines are oriented the same way |
---|
138 | if line0_starts_on_line1 and line1_ends_on_line0: |
---|
139 | # Shared segment from line0 start to line 1 end |
---|
140 | segment = array([[x0, y0], [x3, y3]]) |
---|
141 | |
---|
142 | if line1_starts_on_line0 and line0_ends_on_line1: |
---|
143 | # Shared segment from line1 start to line 0 end |
---|
144 | segment = array([[x2, y2], [x1, y1]]) |
---|
145 | |
---|
146 | |
---|
147 | # Overlap in opposite directions - use direction of line0 |
---|
148 | if line0_starts_on_line1 and line1_starts_on_line0: |
---|
149 | # Shared segment from line0 start to line 1 end |
---|
150 | segment = array([[x0, y0], [x2, y2]]) |
---|
151 | |
---|
152 | if line0_ends_on_line1 and line1_ends_on_line0: |
---|
153 | # Shared segment from line0 start to line 1 end |
---|
154 | segment = array([[x3, y3], [x1, y1]]) |
---|
155 | |
---|
156 | |
---|
157 | return 2, segment |
---|
158 | else: |
---|
159 | # Lines are parallel but they do not coincide |
---|
160 | return 4, None #FIXME (Ole): Add distance here instead of None |
---|
161 | |
---|
162 | else: |
---|
163 | # Lines are not parallel or coinciding |
---|
164 | u0 = u0/denom |
---|
165 | u1 = u1/denom |
---|
166 | |
---|
167 | x = x0 + u0*(x1-x0) |
---|
168 | y = y0 + u0*(y1-y0) |
---|
169 | |
---|
170 | # Sanity check - can be removed to speed up if needed |
---|
171 | assert allclose(x, x2 + u1*(x3-x2)) |
---|
172 | assert allclose(y, y2 + u1*(y3-y2)) |
---|
173 | |
---|
174 | # Check if point found lies within given line segments |
---|
175 | if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0: |
---|
176 | # We have intersection |
---|
177 | |
---|
178 | return 1, array([x, y]) |
---|
179 | else: |
---|
180 | # No intersection |
---|
181 | return 0, None |
---|
182 | |
---|
183 | |
---|
184 | } |
---|
185 | */ |
---|
186 | |
---|
187 | |
---|
188 | int __separate_points_by_polygon(int M, // Number of points |
---|
189 | int N, // Number of polygon vertices |
---|
190 | double* points, |
---|
191 | double* polygon, |
---|
192 | long* indices, // M-Array for storage indices |
---|
193 | int closed, |
---|
194 | int verbose) { |
---|
195 | |
---|
196 | double minpx, maxpx, minpy, maxpy, x, y, px_i, py_i, px_j, py_j, rtol=0.0, atol=0.0; |
---|
197 | int i, j, k, outside_index, inside_index, inside; |
---|
198 | |
---|
199 | //Find min and max of poly used for optimisation when points |
---|
200 | //are far away from polygon |
---|
201 | |
---|
202 | //FIXME(Ole): Pass in rtol and atol from Python |
---|
203 | |
---|
204 | minpx = polygon[0]; maxpx = minpx; |
---|
205 | minpy = polygon[1]; maxpy = minpy; |
---|
206 | |
---|
207 | for (i=0; i<N; i++) { |
---|
208 | px_i = polygon[2*i]; |
---|
209 | py_i = polygon[2*i + 1]; |
---|
210 | |
---|
211 | if (px_i < minpx) minpx = px_i; |
---|
212 | if (px_i > maxpx) maxpx = px_i; |
---|
213 | if (py_i < minpy) minpy = py_i; |
---|
214 | if (py_i > maxpy) maxpy = py_i; |
---|
215 | } |
---|
216 | |
---|
217 | //Begin main loop (for each point) |
---|
218 | inside_index = 0; //Keep track of points inside |
---|
219 | outside_index = M-1; //Keep track of points outside (starting from end) |
---|
220 | if (verbose){ |
---|
221 | printf("Separating %d points\n", M); |
---|
222 | } |
---|
223 | for (k=0; k<M; k++) { |
---|
224 | x = points[2*k]; |
---|
225 | y = points[2*k + 1]; |
---|
226 | |
---|
227 | if (verbose){ |
---|
228 | if (k %((M+10)/10)==0) printf("Doing %d of %d, x=%f, y=%f\n", k, M, x, y); |
---|
229 | } |
---|
230 | |
---|
231 | inside = 0; |
---|
232 | |
---|
233 | //Optimisation |
---|
234 | if ((x > maxpx) || (x < minpx) || (y > maxpy) || (y < minpy)) { |
---|
235 | //Nothing |
---|
236 | } else { |
---|
237 | //Check polygon |
---|
238 | for (i=0; i<N; i++) { |
---|
239 | //printf("k,i=%d,%d\n", k, i); |
---|
240 | j = (i+1)%N; |
---|
241 | |
---|
242 | px_i = polygon[2*i]; |
---|
243 | py_i = polygon[2*i+1]; |
---|
244 | px_j = polygon[2*j]; |
---|
245 | py_j = polygon[2*j+1]; |
---|
246 | |
---|
247 | //Check for case where point is contained in line segment |
---|
248 | if (__point_on_line(x, y, px_i, py_i, px_j, py_j, rtol, atol)) { |
---|
249 | if (closed == 1) { |
---|
250 | inside = 1; |
---|
251 | } else { |
---|
252 | inside = 0; |
---|
253 | } |
---|
254 | break; |
---|
255 | } else { |
---|
256 | //Check if truly inside polygon |
---|
257 | if ( ((py_i < y) && (py_j >= y)) || |
---|
258 | ((py_j < y) && (py_i >= y)) ) { |
---|
259 | if (px_i + (y-py_i)/(py_j-py_i)*(px_j-px_i) < x) |
---|
260 | inside = 1-inside; |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | if (inside == 1) { |
---|
266 | indices[inside_index] = k; |
---|
267 | inside_index += 1; |
---|
268 | } else { |
---|
269 | indices[outside_index] = k; |
---|
270 | outside_index -= 1; |
---|
271 | } |
---|
272 | } // End k |
---|
273 | |
---|
274 | return inside_index; |
---|
275 | } |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | // Gateways to Python |
---|
280 | PyObject *_point_on_line(PyObject *self, PyObject *args) { |
---|
281 | // |
---|
282 | // point_on_line(x, y, x0, y0, x1, y1) |
---|
283 | // |
---|
284 | |
---|
285 | double x, y, x0, y0, x1, y1, rtol, atol; |
---|
286 | int res; |
---|
287 | PyObject *result; |
---|
288 | |
---|
289 | // Convert Python arguments to C |
---|
290 | if (!PyArg_ParseTuple(args, "dddddddd", &x, &y, &x0, &y0, &x1, &y1, &rtol, &atol)) { |
---|
291 | PyErr_SetString(PyExc_RuntimeError, |
---|
292 | "point_on_line could not parse input"); |
---|
293 | return NULL; |
---|
294 | } |
---|
295 | |
---|
296 | |
---|
297 | // Call underlying routine |
---|
298 | res = __point_on_line(x, y, x0, y0, x1, y1, rtol, atol); |
---|
299 | |
---|
300 | // Return values a and b |
---|
301 | result = Py_BuildValue("i", res); |
---|
302 | return result; |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | /* |
---|
307 | PyObject *_intersection(PyObject *self, PyObject *args) { |
---|
308 | // |
---|
309 | // intersection(x0, y0, x1, y1) |
---|
310 | // |
---|
311 | |
---|
312 | double x, y, x0, y0, x1, y1; |
---|
313 | int res; |
---|
314 | PyObject *result; |
---|
315 | |
---|
316 | // Convert Python arguments to C |
---|
317 | if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) { |
---|
318 | PyErr_SetString(PyExc_RuntimeError, |
---|
319 | "point_on_line could not parse input"); |
---|
320 | return NULL; |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | // Call underlying routine |
---|
325 | res = __intersection(x, y, x0, y0, x1, y1); |
---|
326 | |
---|
327 | // Return values a and b |
---|
328 | result = Py_BuildValue("i", res); |
---|
329 | return result; |
---|
330 | } |
---|
331 | */ |
---|
332 | |
---|
333 | |
---|
334 | PyObject *_separate_points_by_polygon(PyObject *self, PyObject *args) { |
---|
335 | //def separate_points_by_polygon(points, polygon, closed, verbose, one_point): |
---|
336 | // """Determine whether points are inside or outside a polygon |
---|
337 | // |
---|
338 | // Input: |
---|
339 | // point - Tuple of (x, y) coordinates, or list of tuples |
---|
340 | // polygon - list of vertices of polygon |
---|
341 | // closed - (optional) determine whether points on boundary should be |
---|
342 | // regarded as belonging to the polygon (closed = True) |
---|
343 | // or not (closed = False) |
---|
344 | |
---|
345 | // |
---|
346 | // Output: |
---|
347 | // indices: array of same length as points with indices of points falling |
---|
348 | // inside the polygon listed from the beginning and indices of points |
---|
349 | // falling outside listed from the end. |
---|
350 | // |
---|
351 | // count: count of points falling inside the polygon |
---|
352 | // |
---|
353 | // The indices of points inside are obtained as indices[:count] |
---|
354 | // The indices of points outside are obtained as indices[count:] |
---|
355 | // |
---|
356 | // Examples: |
---|
357 | // separate_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]] ) |
---|
358 | // will return the indices [0, 2, 1] as only the first and the last point |
---|
359 | // is inside the unit square |
---|
360 | // |
---|
361 | // Remarks: |
---|
362 | // The vertices may be listed clockwise or counterclockwise and |
---|
363 | // the first point may optionally be repeated. |
---|
364 | // Polygons do not need to be convex. |
---|
365 | // Polygons can have holes in them and points inside a hole is |
---|
366 | // regarded as being outside the polygon. |
---|
367 | // |
---|
368 | // |
---|
369 | // Algorithm is based on work by Darel Finley, |
---|
370 | // http://www.alienryderflex.com/polygon/ |
---|
371 | // |
---|
372 | // |
---|
373 | |
---|
374 | PyArrayObject |
---|
375 | *points, |
---|
376 | *polygon, |
---|
377 | *indices; |
---|
378 | // PyObject *xxxx; |
---|
379 | |
---|
380 | int closed, verbose; //Flags |
---|
381 | int count, M, N; |
---|
382 | |
---|
383 | // Convert Python arguments to C |
---|
384 | if (!PyArg_ParseTuple(args, "OOOii", |
---|
385 | &points, |
---|
386 | &polygon, |
---|
387 | &indices, |
---|
388 | &closed, |
---|
389 | &verbose)) { |
---|
390 | |
---|
391 | |
---|
392 | PyErr_SetString(PyExc_RuntimeError, |
---|
393 | "separate_points_by_polygon could not parse input"); |
---|
394 | return NULL; |
---|
395 | } |
---|
396 | |
---|
397 | // points = (PyArrayObject *) PyArray_ContiguousFromObject(xxxx, PyArray_DOUBLE, 1, 1); |
---|
398 | |
---|
399 | M = points -> dimensions[0]; //Number of points |
---|
400 | N = polygon -> dimensions[0]; //Number of vertices in polygon |
---|
401 | |
---|
402 | //FIXME (Ole): This isn't send to Python's sys.stdout |
---|
403 | if (verbose) printf("Got %d points and %d polygon vertices\n", M, N); |
---|
404 | |
---|
405 | //Call underlying routine |
---|
406 | count = __separate_points_by_polygon(M, N, |
---|
407 | (double*) points -> data, |
---|
408 | (double*) polygon -> data, |
---|
409 | (long*) indices -> data, |
---|
410 | closed, verbose); |
---|
411 | |
---|
412 | //NOTE: return number of points inside.. |
---|
413 | return Py_BuildValue("i", count); |
---|
414 | } |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | // Method table for python module |
---|
419 | static struct PyMethodDef MethodTable[] = { |
---|
420 | /* The cast of the function is necessary since PyCFunction values |
---|
421 | * only take two PyObject* parameters, and rotate() takes |
---|
422 | * three. |
---|
423 | */ |
---|
424 | |
---|
425 | {"_point_on_line", _point_on_line, METH_VARARGS, "Print out"}, |
---|
426 | //{"_intersection", _intersection, METH_VARARGS, "Print out"}, |
---|
427 | {"_separate_points_by_polygon", _separate_points_by_polygon, |
---|
428 | METH_VARARGS, "Print out"}, |
---|
429 | {NULL, NULL, 0, NULL} /* sentinel */ |
---|
430 | }; |
---|
431 | |
---|
432 | |
---|
433 | |
---|
434 | // Module initialisation |
---|
435 | void initpolygon_ext(void){ |
---|
436 | Py_InitModule("polygon_ext", MethodTable); |
---|
437 | |
---|
438 | import_array(); //Necessary for handling of NumPY structures |
---|
439 | } |
---|
440 | |
---|
441 | |
---|
442 | |
---|