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 | #include "util_ext.h" |
---|
21 | |
---|
22 | |
---|
23 | double dist(double x, |
---|
24 | double y) { |
---|
25 | |
---|
26 | return sqrt(x*x + y*y); |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | int __point_on_line(double x, double y, |
---|
31 | double x0, double y0, |
---|
32 | double x1, double y1, |
---|
33 | double rtol, |
---|
34 | double atol) { |
---|
35 | /*Determine whether a point is on a line segment |
---|
36 | |
---|
37 | Input: x, y, x0, x0, x1, y1: where |
---|
38 | point is given by x, y |
---|
39 | line is given by (x0, y0) and (x1, y1) |
---|
40 | |
---|
41 | */ |
---|
42 | |
---|
43 | double a0, a1, a_normal0, a_normal1, b0, b1, len_a, len_b; |
---|
44 | double nominator, denominator; |
---|
45 | int is_parallel; |
---|
46 | |
---|
47 | a0 = x - x0; |
---|
48 | a1 = y - y0; |
---|
49 | |
---|
50 | a_normal0 = a1; |
---|
51 | a_normal1 = -a0; |
---|
52 | |
---|
53 | b0 = x1 - x0; |
---|
54 | b1 = y1 - y0; |
---|
55 | |
---|
56 | nominator = fabs(a_normal0*b0 + a_normal1*b1); |
---|
57 | denominator = b0*b0 + b1*b1; |
---|
58 | |
---|
59 | // Determine if line is parallel to point vector up to a tolerance |
---|
60 | is_parallel = 0; |
---|
61 | if (denominator == 0.0) { |
---|
62 | // Use absolute tolerance |
---|
63 | if (nominator <= atol) { |
---|
64 | is_parallel = 1; |
---|
65 | } |
---|
66 | } else { |
---|
67 | // Denominator is positive - use relative tolerance |
---|
68 | if (nominator/denominator <= rtol) { |
---|
69 | is_parallel = 1; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | if (is_parallel) { |
---|
74 | // Point is somewhere on the infinite extension of the line |
---|
75 | // subject to specified absolute tolerance |
---|
76 | |
---|
77 | len_a = dist(a0, a1); //sqrt(a0*a0 + a1*a1); |
---|
78 | len_b = dist(b0, b1); //sqrt(b0*b0 + b1*b1); |
---|
79 | |
---|
80 | if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) { |
---|
81 | return 1; |
---|
82 | } else { |
---|
83 | return 0; |
---|
84 | } |
---|
85 | } else { |
---|
86 | return 0; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | /* |
---|
93 | WORK IN PROGRESS TO OPTIMISE INTERSECTION |
---|
94 | int __intersection(double x0, double y0, |
---|
95 | double x1, double y1) { |
---|
96 | |
---|
97 | |
---|
98 | x0 = line0[0,0]; y0 = line0[0,1] |
---|
99 | x1 = line0[1,0]; y1 = line0[1,1] |
---|
100 | |
---|
101 | x2 = line1[0,0]; y2 = line1[0,1] |
---|
102 | x3 = line1[1,0]; y3 = line1[1,1] |
---|
103 | |
---|
104 | denom = (y3-y2)*(x1-x0) - (x3-x2)*(y1-y0) |
---|
105 | u0 = (x3-x2)*(y0-y2) - (y3-y2)*(x0-x2) |
---|
106 | u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) |
---|
107 | |
---|
108 | if allclose(denom, 0.0): |
---|
109 | # Lines are parallel - check if they coincide on a shared a segment |
---|
110 | |
---|
111 | if allclose( [u0, u1], 0.0 ): |
---|
112 | # We now know that the lines if continued coincide |
---|
113 | # The remaining check will establish if the finite lines share a segment |
---|
114 | |
---|
115 | line0_starts_on_line1 = line0_ends_on_line1 =\ |
---|
116 | line1_starts_on_line0 = line1_ends_on_line0 = False |
---|
117 | |
---|
118 | if point_on_line([x0, y0], line1): |
---|
119 | line0_starts_on_line1 = True |
---|
120 | |
---|
121 | if point_on_line([x1, y1], line1): |
---|
122 | line0_ends_on_line1 = True |
---|
123 | |
---|
124 | if point_on_line([x2, y2], line0): |
---|
125 | line1_starts_on_line0 = True |
---|
126 | |
---|
127 | if point_on_line([x3, y3], line0): |
---|
128 | line1_ends_on_line0 = True |
---|
129 | |
---|
130 | if not(line0_starts_on_line1 or line0_ends_on_line1\ |
---|
131 | or line1_starts_on_line0 or line1_ends_on_line0): |
---|
132 | # Lines are parallel and would coincide if extended, but not as they are. |
---|
133 | return 3, None |
---|
134 | |
---|
135 | |
---|
136 | # One line fully included in the other. Use direction of included line |
---|
137 | if line0_starts_on_line1 and line0_ends_on_line1: |
---|
138 | # Shared segment is line0 fully included in line1 |
---|
139 | segment = array([[x0, y0], [x1, y1]]) |
---|
140 | |
---|
141 | if line1_starts_on_line0 and line1_ends_on_line0: |
---|
142 | # Shared segment is line1 fully included in line0 |
---|
143 | segment = array([[x2, y2], [x3, y3]]) |
---|
144 | |
---|
145 | |
---|
146 | # Overlap with lines are oriented the same way |
---|
147 | if line0_starts_on_line1 and line1_ends_on_line0: |
---|
148 | # Shared segment from line0 start to line 1 end |
---|
149 | segment = array([[x0, y0], [x3, y3]]) |
---|
150 | |
---|
151 | if line1_starts_on_line0 and line0_ends_on_line1: |
---|
152 | # Shared segment from line1 start to line 0 end |
---|
153 | segment = array([[x2, y2], [x1, y1]]) |
---|
154 | |
---|
155 | |
---|
156 | # Overlap in opposite directions - use direction of line0 |
---|
157 | if line0_starts_on_line1 and line1_starts_on_line0: |
---|
158 | # Shared segment from line0 start to line 1 end |
---|
159 | segment = array([[x0, y0], [x2, y2]]) |
---|
160 | |
---|
161 | if line0_ends_on_line1 and line1_ends_on_line0: |
---|
162 | # Shared segment from line0 start to line 1 end |
---|
163 | segment = array([[x3, y3], [x1, y1]]) |
---|
164 | |
---|
165 | |
---|
166 | return 2, segment |
---|
167 | else: |
---|
168 | # Lines are parallel but they do not coincide |
---|
169 | return 4, None #FIXME (Ole): Add distance here instead of None |
---|
170 | |
---|
171 | else: |
---|
172 | # Lines are not parallel or coinciding |
---|
173 | u0 = u0/denom |
---|
174 | u1 = u1/denom |
---|
175 | |
---|
176 | x = x0 + u0*(x1-x0) |
---|
177 | y = y0 + u0*(y1-y0) |
---|
178 | |
---|
179 | # Sanity check - can be removed to speed up if needed |
---|
180 | assert allclose(x, x2 + u1*(x3-x2)) |
---|
181 | assert allclose(y, y2 + u1*(y3-y2)) |
---|
182 | |
---|
183 | # Check if point found lies within given line segments |
---|
184 | if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0: |
---|
185 | # We have intersection |
---|
186 | |
---|
187 | return 1, array([x, y]) |
---|
188 | else: |
---|
189 | # No intersection |
---|
190 | return 0, None |
---|
191 | |
---|
192 | |
---|
193 | } |
---|
194 | */ |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | int __interpolate_polyline(int number_of_nodes, |
---|
199 | int number_of_points, |
---|
200 | double* data, |
---|
201 | double* polyline_nodes, |
---|
202 | long* gauge_neighbour_id, |
---|
203 | double* interpolation_points, |
---|
204 | double* interpolated_values, |
---|
205 | double rtol, |
---|
206 | double atol) { |
---|
207 | |
---|
208 | int j, i, neighbour_id; |
---|
209 | double x0, y0, x1, y1, x, y; |
---|
210 | double segment_len, segment_delta, slope, alpha; |
---|
211 | |
---|
212 | for (j=0; j<number_of_nodes; j++) { |
---|
213 | |
---|
214 | neighbour_id = gauge_neighbour_id[j]; |
---|
215 | |
---|
216 | // FIXME(Ole): I am convinced that gauge_neighbour_id can be discarded, but need to check with John J. |
---|
217 | // Keep it for now (17 Jan 2009) |
---|
218 | // When gone, we can simply interpolate between neighbouring nodes, i.e. neighbour_id = j+1. |
---|
219 | // and the test below becomes something like: if j < number_of_nodes... |
---|
220 | |
---|
221 | if (neighbour_id >= 0) { |
---|
222 | x0 = polyline_nodes[2*j]; |
---|
223 | y0 = polyline_nodes[2*j+1]; |
---|
224 | |
---|
225 | x1 = polyline_nodes[2*neighbour_id]; |
---|
226 | y1 = polyline_nodes[2*neighbour_id+1]; |
---|
227 | |
---|
228 | |
---|
229 | segment_len = dist(x1-x0, y1-y0); |
---|
230 | segment_delta = data[neighbour_id] - data[j]; |
---|
231 | slope = segment_delta/segment_len; |
---|
232 | |
---|
233 | for (i=0; i<number_of_points; i++) { |
---|
234 | x = interpolation_points[2*i]; |
---|
235 | y = interpolation_points[2*i+1]; |
---|
236 | |
---|
237 | if (__point_on_line(x, y, x0, y0, x1, y1, rtol, atol)) { |
---|
238 | alpha = dist(x-x0, y-y0); |
---|
239 | interpolated_values[i] = slope*alpha + data[j]; |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | return 0; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | int __is_inside_triangle(double* point, |
---|
250 | double* triangle, |
---|
251 | int closed, |
---|
252 | double rtol, |
---|
253 | double atol) { |
---|
254 | |
---|
255 | double vx, vy, v0x, v0y, v1x, v1y; |
---|
256 | double a00, a10, a01, a11, b0, b1; |
---|
257 | double denom, alpha, beta; |
---|
258 | |
---|
259 | double x, y; // Point coordinates |
---|
260 | int i, j, res; |
---|
261 | |
---|
262 | x = point[0]; |
---|
263 | y = point[1]; |
---|
264 | |
---|
265 | // Quickly reject points that are clearly outside |
---|
266 | if ((x < triangle[0]) && |
---|
267 | (x < triangle[2]) && |
---|
268 | (x < triangle[4])) return 0; |
---|
269 | |
---|
270 | if ((x > triangle[0]) && |
---|
271 | (x > triangle[2]) && |
---|
272 | (x > triangle[4])) return 0; |
---|
273 | |
---|
274 | if ((y < triangle[1]) && |
---|
275 | (y < triangle[3]) && |
---|
276 | (y < triangle[5])) return 0; |
---|
277 | |
---|
278 | if ((y > triangle[1]) && |
---|
279 | (y > triangle[3]) && |
---|
280 | (y > triangle[5])) return 0; |
---|
281 | |
---|
282 | |
---|
283 | // v0 = C-A |
---|
284 | v0x = triangle[4]-triangle[0]; |
---|
285 | v0y = triangle[5]-triangle[1]; |
---|
286 | |
---|
287 | // v1 = B-A |
---|
288 | v1x = triangle[2]-triangle[0]; |
---|
289 | v1y = triangle[3]-triangle[1]; |
---|
290 | |
---|
291 | // First check if point lies wholly inside triangle |
---|
292 | a00 = v0x*v0x + v0y*v0y; // innerproduct(v0, v0) |
---|
293 | a01 = v0x*v1x + v0y*v1y; // innerproduct(v0, v1) |
---|
294 | a10 = a01; // innerproduct(v1, v0) |
---|
295 | a11 = v1x*v1x + v1y*v1y; // innerproduct(v1, v1) |
---|
296 | |
---|
297 | denom = a11*a00 - a01*a10; |
---|
298 | |
---|
299 | if (fabs(denom) > 0.0) { |
---|
300 | // v = point-A |
---|
301 | vx = x - triangle[0]; |
---|
302 | vy = y - triangle[1]; |
---|
303 | |
---|
304 | b0 = v0x*vx + v0y*vy; // innerproduct(v0, v) |
---|
305 | b1 = v1x*vx + v1y*vy; // innerproduct(v1, v) |
---|
306 | |
---|
307 | alpha = (b0*a11 - b1*a01)/denom; |
---|
308 | beta = (b1*a00 - b0*a10)/denom; |
---|
309 | |
---|
310 | if ((alpha > 0.0) && (beta > 0.0) && (alpha+beta < 1.0)) return 1; |
---|
311 | } |
---|
312 | |
---|
313 | if (closed) { |
---|
314 | // Check if point lies on one of the edges |
---|
315 | |
---|
316 | for (i=0; i<3; i++) { |
---|
317 | j = (i+1) % 3; // Circular index into triangle array |
---|
318 | res = __point_on_line(x, y, |
---|
319 | triangle[2*i], triangle[2*i+1], |
---|
320 | triangle[2*j], triangle[2*j+1], |
---|
321 | rtol, atol); |
---|
322 | if (res) return 1; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | // Default return if point is outside triangle |
---|
327 | return 0; |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | int __separate_points_by_polygon(int M, // Number of points |
---|
332 | int N, // Number of polygon vertices |
---|
333 | double* points, |
---|
334 | double* polygon, |
---|
335 | long* indices, // M-Array for storage indices |
---|
336 | int closed, |
---|
337 | int verbose) { |
---|
338 | |
---|
339 | double minpx, maxpx, minpy, maxpy, x, y, px_i, py_i, px_j, py_j, rtol=0.0, atol=0.0; |
---|
340 | int i, j, k, outside_index, inside_index, inside; |
---|
341 | |
---|
342 | //Find min and max of poly used for optimisation when points |
---|
343 | //are far away from polygon |
---|
344 | |
---|
345 | //FIXME(Ole): Pass in rtol and atol from Python |
---|
346 | |
---|
347 | minpx = polygon[0]; maxpx = minpx; |
---|
348 | minpy = polygon[1]; maxpy = minpy; |
---|
349 | |
---|
350 | for (i=0; i<N; i++) { |
---|
351 | px_i = polygon[2*i]; |
---|
352 | py_i = polygon[2*i + 1]; |
---|
353 | |
---|
354 | if (px_i < minpx) minpx = px_i; |
---|
355 | if (px_i > maxpx) maxpx = px_i; |
---|
356 | if (py_i < minpy) minpy = py_i; |
---|
357 | if (py_i > maxpy) maxpy = py_i; |
---|
358 | } |
---|
359 | |
---|
360 | //Begin main loop (for each point) |
---|
361 | inside_index = 0; //Keep track of points inside |
---|
362 | outside_index = M-1; //Keep track of points outside (starting from end) |
---|
363 | if (verbose){ |
---|
364 | printf("Separating %d points\n", M); |
---|
365 | } |
---|
366 | for (k=0; k<M; k++) { |
---|
367 | if (verbose){ |
---|
368 | if (k %((M+10)/10)==0) printf("Doing %d of %d\n", k, M); |
---|
369 | } |
---|
370 | |
---|
371 | x = points[2*k]; |
---|
372 | y = points[2*k + 1]; |
---|
373 | |
---|
374 | inside = 0; |
---|
375 | |
---|
376 | //Optimisation |
---|
377 | if ((x > maxpx) || (x < minpx) || (y > maxpy) || (y < minpy)) { |
---|
378 | //Nothing |
---|
379 | } else { |
---|
380 | //Check polygon |
---|
381 | for (i=0; i<N; i++) { |
---|
382 | //printf("k,i=%d,%d\n", k, i); |
---|
383 | j = (i+1)%N; |
---|
384 | |
---|
385 | px_i = polygon[2*i]; |
---|
386 | py_i = polygon[2*i+1]; |
---|
387 | px_j = polygon[2*j]; |
---|
388 | py_j = polygon[2*j+1]; |
---|
389 | |
---|
390 | //Check for case where point is contained in line segment |
---|
391 | if (__point_on_line(x, y, px_i, py_i, px_j, py_j, rtol, atol)) { |
---|
392 | if (closed == 1) { |
---|
393 | inside = 1; |
---|
394 | } else { |
---|
395 | inside = 0; |
---|
396 | } |
---|
397 | break; |
---|
398 | } else { |
---|
399 | //Check if truly inside polygon |
---|
400 | if ( ((py_i < y) && (py_j >= y)) || |
---|
401 | ((py_j < y) && (py_i >= y)) ) { |
---|
402 | if (px_i + (y-py_i)/(py_j-py_i)*(px_j-px_i) < x) |
---|
403 | inside = 1-inside; |
---|
404 | } |
---|
405 | } |
---|
406 | } |
---|
407 | } |
---|
408 | if (inside == 1) { |
---|
409 | indices[inside_index] = k; |
---|
410 | inside_index += 1; |
---|
411 | } else { |
---|
412 | indices[outside_index] = k; |
---|
413 | outside_index -= 1; |
---|
414 | } |
---|
415 | } // End k |
---|
416 | |
---|
417 | return inside_index; |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | // Gateways to Python |
---|
423 | PyObject *_point_on_line(PyObject *self, PyObject *args) { |
---|
424 | // |
---|
425 | // point_on_line(x, y, x0, y0, x1, y1) |
---|
426 | // |
---|
427 | |
---|
428 | double x, y, x0, y0, x1, y1, rtol, atol; |
---|
429 | int res; |
---|
430 | PyObject *result; |
---|
431 | |
---|
432 | // Convert Python arguments to C |
---|
433 | if (!PyArg_ParseTuple(args, "dddddddd", &x, &y, &x0, &y0, &x1, &y1, &rtol, &atol)) { |
---|
434 | PyErr_SetString(PyExc_RuntimeError, |
---|
435 | "point_on_line could not parse input"); |
---|
436 | return NULL; |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | // Call underlying routine |
---|
441 | res = __point_on_line(x, y, x0, y0, x1, y1, rtol, atol); |
---|
442 | |
---|
443 | // Return values a and b |
---|
444 | result = Py_BuildValue("i", res); |
---|
445 | return result; |
---|
446 | } |
---|
447 | |
---|
448 | |
---|
449 | |
---|
450 | // Gateways to Python |
---|
451 | PyObject *_interpolate_polyline(PyObject *self, PyObject *args) { |
---|
452 | // |
---|
453 | // _interpolate_polyline(data, polyline_nodes, gauge_neighbour_id, interpolation_points |
---|
454 | // interpolated_values): |
---|
455 | // |
---|
456 | |
---|
457 | |
---|
458 | PyArrayObject |
---|
459 | *data, |
---|
460 | *polyline_nodes, |
---|
461 | *gauge_neighbour_id, |
---|
462 | *interpolation_points, |
---|
463 | *interpolated_values; |
---|
464 | |
---|
465 | double rtol, atol; |
---|
466 | int number_of_nodes, number_of_points, res; |
---|
467 | |
---|
468 | // Convert Python arguments to C |
---|
469 | if (!PyArg_ParseTuple(args, "OOOOOdd", |
---|
470 | &data, |
---|
471 | &polyline_nodes, |
---|
472 | &gauge_neighbour_id, |
---|
473 | &interpolation_points, |
---|
474 | &interpolated_values, |
---|
475 | &rtol, |
---|
476 | &atol)) { |
---|
477 | |
---|
478 | PyErr_SetString(PyExc_RuntimeError, |
---|
479 | "_interpolate_polyline could not parse input"); |
---|
480 | return NULL; |
---|
481 | } |
---|
482 | |
---|
483 | // check that numpy array objects arrays are C contiguous memory |
---|
484 | CHECK_C_CONTIG(data); |
---|
485 | CHECK_C_CONTIG(polyline_nodes); |
---|
486 | CHECK_C_CONTIG(gauge_neighbour_id); |
---|
487 | CHECK_C_CONTIG(interpolation_points); |
---|
488 | CHECK_C_CONTIG(interpolated_values); |
---|
489 | |
---|
490 | number_of_nodes = polyline_nodes -> dimensions[0]; // Number of nodes in polyline |
---|
491 | number_of_points = interpolation_points -> dimensions[0]; //Number of points |
---|
492 | |
---|
493 | |
---|
494 | // Call underlying routine |
---|
495 | res = __interpolate_polyline(number_of_nodes, |
---|
496 | number_of_points, |
---|
497 | (double*) data -> data, |
---|
498 | (double*) polyline_nodes -> data, |
---|
499 | (long*) gauge_neighbour_id -> data, |
---|
500 | (double*) interpolation_points -> data, |
---|
501 | (double*) interpolated_values -> data, |
---|
502 | rtol, |
---|
503 | atol); |
---|
504 | |
---|
505 | // Return |
---|
506 | return Py_BuildValue(""); |
---|
507 | } |
---|
508 | |
---|
509 | |
---|
510 | |
---|
511 | |
---|
512 | PyObject *_is_inside_triangle(PyObject *self, PyObject *args) { |
---|
513 | // |
---|
514 | // _is_inside_triangle(point, triangle, int(closed), rtol, atol) |
---|
515 | // |
---|
516 | |
---|
517 | |
---|
518 | PyArrayObject |
---|
519 | *point, |
---|
520 | *triangle; |
---|
521 | |
---|
522 | double rtol, atol; |
---|
523 | int closed, res; |
---|
524 | |
---|
525 | PyObject *result; |
---|
526 | |
---|
527 | // Convert Python arguments to C |
---|
528 | if (!PyArg_ParseTuple(args, "OOidd", |
---|
529 | &point, |
---|
530 | &triangle, |
---|
531 | &closed, |
---|
532 | &rtol, |
---|
533 | &atol)) { |
---|
534 | |
---|
535 | PyErr_SetString(PyExc_RuntimeError, |
---|
536 | "_is_inside_triangle could not parse input"); |
---|
537 | return NULL; |
---|
538 | } |
---|
539 | |
---|
540 | // Call underlying routine |
---|
541 | res = __is_inside_triangle((double*) point -> data, |
---|
542 | (double*) triangle -> data, |
---|
543 | closed, |
---|
544 | rtol, |
---|
545 | atol); |
---|
546 | |
---|
547 | |
---|
548 | // Return result |
---|
549 | result = Py_BuildValue("i", res); |
---|
550 | return result; |
---|
551 | } |
---|
552 | |
---|
553 | |
---|
554 | |
---|
555 | /* |
---|
556 | PyObject *_intersection(PyObject *self, PyObject *args) { |
---|
557 | // |
---|
558 | // intersection(x0, y0, x1, y1) |
---|
559 | // |
---|
560 | |
---|
561 | double x, y, x0, y0, x1, y1; |
---|
562 | int res; |
---|
563 | PyObject *result; |
---|
564 | |
---|
565 | // Convert Python arguments to C |
---|
566 | if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) { |
---|
567 | PyErr_SetString(PyExc_RuntimeError, |
---|
568 | "point_on_line could not parse input"); |
---|
569 | return NULL; |
---|
570 | } |
---|
571 | |
---|
572 | |
---|
573 | // Call underlying routine |
---|
574 | res = __intersection(x, y, x0, y0, x1, y1); |
---|
575 | |
---|
576 | // Return values a and b |
---|
577 | result = Py_BuildValue("i", res); |
---|
578 | return result; |
---|
579 | } |
---|
580 | */ |
---|
581 | |
---|
582 | |
---|
583 | PyObject *_separate_points_by_polygon(PyObject *self, PyObject *args) { |
---|
584 | //def separate_points_by_polygon(points, polygon, closed, verbose, one_point): |
---|
585 | // """Determine whether points are inside or outside a polygon |
---|
586 | // |
---|
587 | // Input: |
---|
588 | // point - Tuple of (x, y) coordinates, or list of tuples |
---|
589 | // polygon - list of vertices of polygon |
---|
590 | // closed - (optional) determine whether points on boundary should be |
---|
591 | // regarded as belonging to the polygon (closed = True) |
---|
592 | // or not (closed = False) |
---|
593 | |
---|
594 | // |
---|
595 | // Output: |
---|
596 | // indices: array of same length as points with indices of points falling |
---|
597 | // inside the polygon listed from the beginning and indices of points |
---|
598 | // falling outside listed from the end. |
---|
599 | // |
---|
600 | // count: count of points falling inside the polygon |
---|
601 | // |
---|
602 | // The indices of points inside are obtained as indices[:count] |
---|
603 | // The indices of points outside are obtained as indices[count:] |
---|
604 | // |
---|
605 | // Examples: |
---|
606 | // separate_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]] ) |
---|
607 | // will return the indices [0, 2, 1] as only the first and the last point |
---|
608 | // is inside the unit square |
---|
609 | // |
---|
610 | // Remarks: |
---|
611 | // The vertices may be listed clockwise or counterclockwise and |
---|
612 | // the first point may optionally be repeated. |
---|
613 | // Polygons do not need to be convex. |
---|
614 | // Polygons can have holes in them and points inside a hole is |
---|
615 | // regarded as being outside the polygon. |
---|
616 | // |
---|
617 | // |
---|
618 | // Algorithm is based on work by Darel Finley, |
---|
619 | // http://www.alienryderflex.com/polygon/ |
---|
620 | // |
---|
621 | // |
---|
622 | |
---|
623 | PyArrayObject |
---|
624 | *points, |
---|
625 | *polygon, |
---|
626 | *indices; |
---|
627 | |
---|
628 | int closed, verbose; //Flags |
---|
629 | int count, M, N; |
---|
630 | |
---|
631 | // Convert Python arguments to C |
---|
632 | if (!PyArg_ParseTuple(args, "OOOii", |
---|
633 | &points, |
---|
634 | &polygon, |
---|
635 | &indices, |
---|
636 | &closed, |
---|
637 | &verbose)) { |
---|
638 | |
---|
639 | |
---|
640 | PyErr_SetString(PyExc_RuntimeError, |
---|
641 | "separate_points_by_polygon could not parse input"); |
---|
642 | return NULL; |
---|
643 | } |
---|
644 | |
---|
645 | // check that points, polygon and indices arrays are C contiguous |
---|
646 | CHECK_C_CONTIG(points); |
---|
647 | CHECK_C_CONTIG(polygon); |
---|
648 | CHECK_C_CONTIG(indices); |
---|
649 | |
---|
650 | M = points -> dimensions[0]; //Number of points |
---|
651 | N = polygon -> dimensions[0]; //Number of vertices in polygon |
---|
652 | |
---|
653 | //FIXME (Ole): This isn't send to Python's sys.stdout |
---|
654 | if (verbose) printf("Got %d points and %d polygon vertices\n", M, N); |
---|
655 | |
---|
656 | //Call underlying routine |
---|
657 | count = __separate_points_by_polygon(M, N, |
---|
658 | (double*) points -> data, |
---|
659 | (double*) polygon -> data, |
---|
660 | (long*) indices -> data, |
---|
661 | closed, verbose); |
---|
662 | |
---|
663 | //NOTE: return number of points inside.. |
---|
664 | return Py_BuildValue("i", count); |
---|
665 | } |
---|
666 | |
---|
667 | |
---|
668 | |
---|
669 | // Method table for python module |
---|
670 | static struct PyMethodDef MethodTable[] = { |
---|
671 | /* The cast of the function is necessary since PyCFunction values |
---|
672 | * only take two PyObject* parameters, and rotate() takes |
---|
673 | * three. |
---|
674 | */ |
---|
675 | |
---|
676 | {"_point_on_line", _point_on_line, METH_VARARGS, "Print out"}, |
---|
677 | //{"_intersection", _intersection, METH_VARARGS, "Print out"}, |
---|
678 | {"_separate_points_by_polygon", _separate_points_by_polygon, |
---|
679 | METH_VARARGS, "Print out"}, |
---|
680 | {"_interpolate_polyline", _interpolate_polyline, |
---|
681 | METH_VARARGS, "Print out"}, |
---|
682 | {"_is_inside_triangle", _is_inside_triangle, |
---|
683 | METH_VARARGS, "Print out"}, |
---|
684 | {NULL, NULL, 0, NULL} /* sentinel */ |
---|
685 | }; |
---|
686 | |
---|
687 | |
---|
688 | |
---|
689 | // Module initialisation |
---|
690 | void initpolygon_ext(void){ |
---|
691 | Py_InitModule("polygon_ext", MethodTable); |
---|
692 | |
---|
693 | import_array(); //Necessary for handling of NumPY structures |
---|
694 | } |
---|
695 | |
---|
696 | |
---|
697 | |
---|