- Timestamp:
- Nov 6, 2008, 3:32:23 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source_numpy_conversion/anuga/utilities/polygon.py
r5889 r5902 10 10 # #print 'Could not find scipy - using Numeric' 11 11 12 from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray 12 ##from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray 13 import numpy 13 14 14 15 … … 76 77 # FIXME (Ole): Write this in C 77 78 78 line0 = ensure_numeric(line0, float)79 line1 = ensure_numeric(line1, float)79 line0 = ensure_numeric(line0, numpy.float) 80 line1 = ensure_numeric(line1, numpy.float) 80 81 81 82 x0 = line0[0,0]; y0 = line0[0,1] … … 89 90 u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0) 90 91 91 if allclose(denom, 0.0):92 if numpy.allclose(denom, 0.0): 92 93 # Lines are parallel - check if they coincide on a shared a segment 93 94 94 if allclose( [u0, u1], 0.0 ):95 if numpy.allclose( [u0, u1], 0.0 ): 95 96 # We now know that the lines if continued coincide 96 97 # The remaining check will establish if the finite lines share a segment … … 120 121 if line0_starts_on_line1 and line0_ends_on_line1: 121 122 # Shared segment is line0 fully included in line1 122 segment = array([[x0, y0], [x1, y1]])123 segment = numpy.array([[x0, y0], [x1, y1]]) 123 124 124 125 if line1_starts_on_line0 and line1_ends_on_line0: 125 126 # Shared segment is line1 fully included in line0 126 segment = array([[x2, y2], [x3, y3]])127 segment = numpy.array([[x2, y2], [x3, y3]]) 127 128 128 129 … … 130 131 if line0_starts_on_line1 and line1_ends_on_line0: 131 132 # Shared segment from line0 start to line 1 end 132 segment = array([[x0, y0], [x3, y3]])133 segment = numpy.array([[x0, y0], [x3, y3]]) 133 134 134 135 if line1_starts_on_line0 and line0_ends_on_line1: 135 136 # Shared segment from line1 start to line 0 end 136 segment = array([[x2, y2], [x1, y1]])137 segment = numpy.array([[x2, y2], [x1, y1]]) 137 138 138 139 … … 140 141 if line0_starts_on_line1 and line1_starts_on_line0: 141 142 # Shared segment from line0 start to line 1 end 142 segment = array([[x0, y0], [x2, y2]])143 segment = numpy.array([[x0, y0], [x2, y2]]) 143 144 144 145 if line0_ends_on_line1 and line1_ends_on_line0: 145 146 # Shared segment from line0 start to line 1 end 146 segment = array([[x3, y3], [x1, y1]])147 segment = numpy.array([[x3, y3], [x1, y1]]) 147 148 148 149 … … 161 162 162 163 # Sanity check - can be removed to speed up if needed 163 assert allclose(x, x2 + u1*(x3-x2))164 assert allclose(y, y2 + u1*(y3-y2))164 assert numpy.allclose(x, x2 + u1*(x3-x2)) 165 assert numpy.allclose(y, y2 + u1*(y3-y2)) 165 166 166 167 # Check if point found lies within given line segments … … 168 169 # We have intersection 169 170 170 return 1, array([x, y])171 return 1, numpy.array([x, y]) 171 172 else: 172 173 # No intersection … … 203 204 204 205 205 line0 = ensure_numeric(line0, float)206 line1 = ensure_numeric(line1, float)206 line0 = ensure_numeric(line0, numpy.float) 207 line1 = ensure_numeric(line1, numpy.float) 207 208 208 209 status, value = _intersection(line0[0,0], line0[0,1], … … 270 271 if len(points.shape) == 1: 271 272 # Only one point was passed in. Convert to array of points 272 points = reshape(points, (1,2))273 points = numpy.reshape(points, (1,2)) 273 274 274 275 indices, count = separate_points_by_polygon(points, polygon, … … 312 313 #if verbose: print 'Checking input to outside_polygon' 313 314 try: 314 points = ensure_numeric(points, float)315 points = ensure_numeric(points, numpy.float) 315 316 except NameError, e: 316 317 raise NameError, e … … 320 321 321 322 try: 322 polygon = ensure_numeric(polygon, float)323 polygon = ensure_numeric(polygon, numpy.float) 323 324 except NameError, e: 324 325 raise NameError, e … … 330 331 if len(points.shape) == 1: 331 332 # Only one point was passed in. Convert to array of points 332 points = reshape(points, (1,2))333 points = numpy.reshape(points, (1,2)) 333 334 334 335 indices, count = separate_points_by_polygon(points, polygon, … … 339 340 if count == len(indices): 340 341 # No points are outside 341 return array([])342 return numpy.array([]) 342 343 else: 343 344 return indices[count:][::-1] #return reversed … … 354 355 #if verbose: print 'Checking input to outside_polygon' 355 356 try: 356 points = ensure_numeric(points, float)357 points = ensure_numeric(points, numpy.float) 357 358 except NameError, e: 358 359 raise NameError, e … … 362 363 363 364 try: 364 polygon = ensure_numeric(polygon, float)365 polygon = ensure_numeric(polygon, numpy.float) 365 366 except NameError, e: 366 367 raise NameError, e … … 371 372 if len(points.shape) == 1: 372 373 # Only one point was passed in. Convert to array of points 373 points = reshape(points, (1,2))374 points = numpy.reshape(points, (1,2)) 374 375 375 376 … … 441 442 ## print 'Before: points=%s, flags=%s' % (type(points), str(points.flags)) 442 443 try: 443 ## points = ascontiguousarray(ensure_numeric(points,float))444 points = ensure_numeric(points, float)444 ## points = numpy.ascontiguousarray(ensure_numeric(points, numpy.float)) 445 points = ensure_numeric(points, numpy.float) 445 446 except NameError, e: 446 447 raise NameError, e … … 452 453 #if verbose: print 'Checking input to separate_points_by_polygon 2' 453 454 try: 454 ## polygon = ascontiguousarray(ensure_numeric(polygon,float))455 polygon = ensure_numeric(polygon, float)455 ## polygon = numpy.ascontiguousarray(ensure_numeric(polygon, numpy.float)) 456 polygon = ensure_numeric(polygon, numpy.float) 456 457 except NameError, e: 457 458 raise NameError, e … … 475 476 # Only one point was passed in. 476 477 # Convert to array of points 477 points = reshape(points, (1,2))478 points = numpy.reshape(points, (1,2)) 478 479 479 480 … … 494 495 495 496 496 indices = zeros( M,int )497 indices = numpy.zeros( M, numpy.int ) 497 498 498 499 count = _separate_points_by_polygon(points, polygon, indices, … … 621 622 622 623 try: 623 polygon = ensure_numeric(polygon, float)624 polygon = ensure_numeric(polygon, numpy.float) 624 625 except NameError, e: 625 626 raise NameError, e … … 630 631 x = polygon[:,0] 631 632 y = polygon[:,1] 632 x = concatenate((x, [polygon[0,0]]), axis = 0)633 y = concatenate((y, [polygon[0,1]]), axis = 0)633 x = numpy.concatenate((x, [polygon[0,0]]), axis = 0) 634 y = numpy.concatenate((y, [polygon[0,1]]), axis = 0) 634 635 635 636 return x, y … … 732 733 733 734 def __call__(self, x, y): 734 x = array(x).astype(float)735 y = array(y).astype(float)735 x = numpy.array(x).astype(numpy.float) 736 y = numpy.array(y).astype(numpy.float) 736 737 737 738 assert len(x.shape) == 1 and len(y.shape) == 1 … … 740 741 assert y.shape[0] == N 741 742 742 points = ascontiguousarray(concatenate( (x[:,newaxis], y[:,newaxis]), axis=1 ))743 points = numpy.ascontiguousarray(numpy.concatenate( (x[:,numpy.newaxis], y[:,numpy.newaxis]), axis=1 )) 743 744 744 745 if callable(self.default): 745 746 z = self.default(x, y) 746 747 else: 747 z = ones(N,float) * self.default748 z = numpy.ones(N, numpy.float) * self.default 748 749 749 750 for polygon, value in self.regions: … … 753 754 if callable(value): 754 755 for i in indices: 755 xx = array([x[i]])756 yy = array([y[i]])756 xx = numpy.array([x[i]]) 757 yy = numpy.array([y[i]]) 757 758 z[i] = value(xx, yy)[0] 758 759 else:
Note: See TracChangeset
for help on using the changeset viewer.