Changeset 5225 for anuga_core/source/anuga/utilities/polygon.py
- Timestamp:
- Apr 21, 2008, 5:20:32 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/polygon.py
r5114 r5225 10 10 # #print 'Could not find scipy - using Numeric' 11 11 12 from Numeric import Float, Int, zeros, ones, array, concatenate, reshape, dot 12 from Numeric import Float, Int, zeros, ones, array, concatenate, reshape, dot, allclose 13 13 14 14 … … 17 17 from anuga.geospatial_data.geospatial_data import ensure_absolute 18 18 19 def point_on_line( x, y, x0, y0, x1, y1):19 def point_on_line(point, line): 20 20 """Determine whether a point is on a line segment 21 21 22 Input: x, y, x0, x0, x1, y1: where 23 point is given by x, y 24 line is given by (x0, y0) and (x1, y1) 25 26 """ 27 28 a = array([x - x0, y - y0]) 29 a_normal = array([a[1], -a[0]]) 30 31 b = array([x1 - x0, y1 - y0]) 32 33 if dot(a_normal, b) == 0: 34 #Point is somewhere on the infinite extension of the line 35 36 len_a = sqrt(sum(a**2)) 37 len_b = sqrt(sum(b**2)) 38 if dot(a, b) >= 0 and len_a <= len_b: 39 return True 40 else: 41 return False 22 Input: 23 point is given by [x, y] 24 line is given by [x0, y0], [x1, y1]] or 25 the equivalent 2x2 Numeric array with each row corresponding to a point. 26 27 Output: 28 29 """ 30 31 point = ensure_numeric(point) 32 line = ensure_numeric(line) 33 34 35 res = _point_on_line(point[0], point[1], 36 line[0,0], line[0,1], 37 line[1,0], line[1,1]) 38 39 return bool(res) 40 41 42 43 44 45 def intersection(line0, line1): 46 """Returns intersecting point between two line segments or None (if parallel or no intersection is found) 47 48 Inputs: 49 line0, line1: Each defined by two end points as in: [[x0, y0], [x1, y1]] 50 A line can also be a 2x2 numeric array with each row corresponding to a point. 51 52 53 Output: 54 Point [x,y] or None. 55 56 If line extensions intersect outside their limits, None is returned as well. 57 58 """ 59 60 # FIXME (Ole): Write this in C 61 62 line0 = ensure_numeric(line0, Float) 63 line1 = ensure_numeric(line1, Float) 64 65 x0 = line0[0,0]; y0 = line0[0,1] 66 x1 = line0[1,0]; y1 = line0[1,1] 67 68 x2 = line1[0,0]; y2 = line1[0,1] 69 x3 = line1[1,0]; y3 = line1[1,1] 70 71 denom = (y3-y2)*(x1-x0) - (x3-x2)*(y1-y0) 72 73 #print 'denom', denom, line0, line1 74 if allclose(denom, 0.0): 75 # Lines are parallel 76 return None 42 77 else: 43 return False 78 u0 = (x3-x2)*(y0-y2) - (y3-y2)*(x0-x2) 79 u0 = u0/denom 80 81 u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) 82 u1 = u1/denom 83 84 x = x0 + u0*(x1-x0) 85 y = y0 + u0*(y1-y0) 86 87 assert allclose(x, x2 + u1*(x3-x2)) 88 assert allclose(y, y2 + u1*(y3-y2)) 89 90 # Check if point found lies within given line segments 91 if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0: 92 # We have intersection 93 94 # Need tolerances if going ahead with this check 95 #msg = 'Algorithm error. Intersection was detected but point (%f, %f) does not lie ' %(x,y) 96 #msg += 'on line0: %s' %(line0) 97 #assert point_on_line([x, y], line0), msg 98 #msg = 'Algorithm error. Intersection was detected but point (%f, %f) does not lie ' %(x,y) 99 #msg += 'on line1: %s' %(line1) 100 #assert point_on_line([x, y], line1), msg 101 102 return [x, y] 103 else: 104 return None 44 105 45 106 … … 305 366 indices = zeros( M, Int ) 306 367 307 from polygon_ext import separate_points_by_polygon as sep_points 308 count = sep_points(points, polygon, indices, 309 int(closed), int(verbose)) 368 count = _separate_points_by_polygon(points, polygon, indices, 369 int(closed), int(verbose)) 310 370 311 371 if verbose: print 'Found %d points (out of %d) inside polygon'\ … … 780 840 from anuga.utilities.compile import can_use_C_extension 781 841 if can_use_C_extension('polygon_ext.c'): 782 # Underlying C implementations can be accessed 783 784 from polygon_ext import point_on_line 842 # Underlying C implementations can be accessed 843 from polygon_ext import _point_on_line 844 from polygon_ext import _separate_points_by_polygon 845 785 846 else: 786 847 msg = 'C implementations could not be accessed by %s.\n ' %__file__
Note: See TracChangeset
for help on using the changeset viewer.