Changeset 6158 for anuga_core/source/anuga/utilities/polygon.py
- Timestamp:
- Jan 14, 2009, 9:48:37 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/polygon.py
r6119 r6158 1 1 #!/usr/bin/env python 2 2 """Polygon manipulations 3 4 3 """ 5 4 6 7 #try: 8 # from scipy import Float, Int, zeros, ones, array, concatenate, reshape, dot 9 #except: 10 # #print 'Could not find scipy - using Numeric' 11 12 from Numeric import Float, Int, zeros, ones, array, concatenate, reshape, dot, allclose 13 5 import Numeric as num 14 6 15 7 from math import sqrt … … 49 41 # result functions for possible states 50 42 def lines_dont_coincide(p0,p1,p2,p3): return (3, None) 51 def lines_0_fully_included_in_1(p0,p1,p2,p3): return (2, array([p0,p1]))52 def lines_1_fully_included_in_0(p0,p1,p2,p3): return (2, array([p2,p3]))53 def lines_overlap_same_direction(p0,p1,p2,p3): return (2, array([p0,p3]))54 def lines_overlap_same_direction2(p0,p1,p2,p3): return (2, array([p2,p1]))55 def lines_overlap_opposite_direction(p0,p1,p2,p3): return (2, array([p0,p2]))56 def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, array([p3,p1]))43 def lines_0_fully_included_in_1(p0,p1,p2,p3): return (2, num.array([p0,p1])) 44 def lines_1_fully_included_in_0(p0,p1,p2,p3): return (2, num.array([p2,p3])) 45 def lines_overlap_same_direction(p0,p1,p2,p3): return (2, num.array([p0,p3])) 46 def lines_overlap_same_direction2(p0,p1,p2,p3): return (2, num.array([p2,p1])) 47 def lines_overlap_opposite_direction(p0,p1,p2,p3): return (2, num.array([p0,p2])) 48 def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, num.array([p3,p1])) 57 49 58 50 # this function called when an impossible state is found … … 108 100 # FIXME (Ole): Write this in C 109 101 110 line0 = ensure_numeric(line0, Float)111 line1 = ensure_numeric(line1, Float)102 line0 = ensure_numeric(line0, num.Float) 103 line1 = ensure_numeric(line1, num.Float) 112 104 113 105 x0 = line0[0,0]; y0 = line0[0,1] … … 121 113 u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) 122 114 123 if allclose(denom, 0.0, rtol=rtol, atol=atol):115 if num.allclose(denom, 0.0, rtol=rtol, atol=atol): 124 116 # Lines are parallel - check if they are collinear 125 if allclose([u0, u1], 0.0, rtol=rtol, atol=atol):117 if num.allclose([u0, u1], 0.0, rtol=rtol, atol=atol): 126 118 # We now know that the lines are collinear 127 119 state_tuple = (point_on_line([x0, y0], line1, rtol=rtol, atol=atol), … … 143 135 144 136 # Sanity check - can be removed to speed up if needed 145 assert allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol)146 assert allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol)137 assert num.allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol) 138 assert num.allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol) 147 139 148 140 # Check if point found lies within given line segments 149 141 if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0: 150 142 # We have intersection 151 return 1, array([x, y])143 return 1, num.array([x, y]) 152 144 else: 153 145 # No intersection … … 184 176 185 177 186 line0 = ensure_numeric(line0, Float)187 line1 = ensure_numeric(line1, Float)178 line0 = ensure_numeric(line0, num.Float) 179 line1 = ensure_numeric(line1, num.Float) 188 180 189 181 status, value = _intersection(line0[0,0], line0[0,1], … … 251 243 if len(points.shape) == 1: 252 244 # Only one point was passed in. Convert to array of points 253 points = reshape(points, (1,2))245 points = num.reshape(points, (1,2)) 254 246 255 247 indices, count = separate_points_by_polygon(points, polygon, … … 293 285 #if verbose: print 'Checking input to outside_polygon' 294 286 try: 295 points = ensure_numeric(points, Float)287 points = ensure_numeric(points, num.Float) 296 288 except NameError, e: 297 289 raise NameError, e … … 301 293 302 294 try: 303 polygon = ensure_numeric(polygon, Float)295 polygon = ensure_numeric(polygon, num.Float) 304 296 except NameError, e: 305 297 raise NameError, e … … 311 303 if len(points.shape) == 1: 312 304 # Only one point was passed in. Convert to array of points 313 points = reshape(points, (1,2))305 points = num.reshape(points, (1,2)) 314 306 315 307 indices, count = separate_points_by_polygon(points, polygon, … … 320 312 if count == len(indices): 321 313 # No points are outside 322 return array([])314 return num.array([]) 323 315 else: 324 316 return indices[count:][::-1] #return reversed … … 335 327 #if verbose: print 'Checking input to outside_polygon' 336 328 try: 337 points = ensure_numeric(points, Float)329 points = ensure_numeric(points, num.Float) 338 330 except NameError, e: 339 331 raise NameError, e … … 343 335 344 336 try: 345 polygon = ensure_numeric(polygon, Float)337 polygon = ensure_numeric(polygon, num.Float) 346 338 except NameError, e: 347 339 raise NameError, e … … 352 344 if len(points.shape) == 1: 353 345 # Only one point was passed in. Convert to array of points 354 points = reshape(points, (1,2))346 points = num.reshape(points, (1,2)) 355 347 356 348 … … 422 414 423 415 try: 424 points = ensure_numeric(points, Float)416 points = ensure_numeric(points, num.Float) 425 417 except NameError, e: 426 418 raise NameError, e … … 431 423 #if verbose: print 'Checking input to separate_points_by_polygon 2' 432 424 try: 433 polygon = ensure_numeric(polygon, Float)425 polygon = ensure_numeric(polygon, num.Float) 434 426 except NameError, e: 435 427 raise NameError, e … … 453 445 # Only one point was passed in. 454 446 # Convert to array of points 455 points = reshape(points, (1,2))447 points = num.reshape(points, (1,2)) 456 448 457 449 … … 472 464 473 465 474 indices = zeros( M,Int )466 indices = num.zeros( M, num.Int ) 475 467 476 468 count = _separate_points_by_polygon(points, polygon, indices, … … 607 599 608 600 try: 609 polygon = ensure_numeric(polygon, Float)601 polygon = ensure_numeric(polygon, num.Float) 610 602 except NameError, e: 611 603 raise NameError, e … … 616 608 x = polygon[:,0] 617 609 y = polygon[:,1] 618 x = concatenate((x, [polygon[0,0]]), axis = 0)619 y = concatenate((y, [polygon[0,1]]), axis = 0)610 x = num.concatenate((x, [polygon[0,0]]), axis = 0) 611 y = num.concatenate((y, [polygon[0,1]]), axis = 0) 620 612 621 613 return x, y … … 716 708 717 709 def __call__(self, x, y): 718 x = array(x).astype(Float)719 y = array(y).astype(Float)710 x = num.array(x).astype(num.Float) 711 y = num.array(y).astype(num.Float) 720 712 721 713 N = len(x) 722 714 assert len(y) == N 723 715 724 points = concatenate( (reshape(x, (N, 1)),725 reshape(y, (N, 1))), axis=1 )716 points = num.concatenate( (num.reshape(x, (N, 1)), 717 num.reshape(y, (N, 1))), axis=1 ) 726 718 727 719 if callable(self.default): 728 720 z = self.default(x,y) 729 721 else: 730 z = ones(N,Float) * self.default722 z = num.ones(N, num.Float) * self.default 731 723 732 724 for polygon, value in self.regions: … … 736 728 if callable(value): 737 729 for i in indices: 738 xx = array([x[i]])739 yy = array([y[i]])730 xx = num.array([x[i]]) 731 yy = num.array([y[i]]) 740 732 z[i] = value(xx, yy)[0] 741 733 else:
Note: See TracChangeset
for help on using the changeset viewer.