Changeset 3052
- Timestamp:
- Jun 2, 2006, 1:12:20 PM (19 years ago)
- Location:
- inundation
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pmesh/test_mesh_interface.py
r3048 r3052 5 5 from mesh_interface import * 6 6 from load_mesh.loadASCII import * 7 from utilities.polygon import i nside_polygon7 from utilities.polygon import is_inside_polygon 8 8 from coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE 9 9 … … 74 74 # poly_point values are relative to the mesh geo-ref 75 75 # make them absolute 76 self.failUnless(i nside_polygon([poly_point.x+x,poly_point.y+y],77 polygon_absolute, closed = False),76 self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], 77 polygon_absolute, closed = False), 78 78 'FAILED!') 79 79 … … 88 88 # poly_point values are relative to the mesh geo-ref 89 89 # make them absolute 90 self.failUnless(i nside_polygon([poly_point.x+x,poly_point.y+y],91 inner1_polygon_absolute,92 closed = False),90 self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], 91 inner1_polygon_absolute, 92 closed = False), 93 93 'FAILED!') 94 94 … … 103 103 # poly_point values are relative to the mesh geo-ref 104 104 # make them absolute 105 self.failUnless(i nside_polygon([poly_point.x+x,poly_point.y+y],106 inner2_polygon_absolute,107 closed = False),105 self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], 106 inner2_polygon_absolute, 107 closed = False), 108 108 'FAILED!') 109 109 -
inundation/pyvolution/test_mesh.py
r3012 r3052 15 15 from Numeric import allclose, array 16 16 17 from utilities.polygon import i nside_polygon17 from utilities.polygon import is_inside_polygon 18 18 19 19 def distance(x, y): … … 725 725 for p in points: 726 726 #print p, P 727 assert i nside_polygon(p, P)727 assert is_inside_polygon(p, P) 728 728 729 729 … … 762 762 for p in points: 763 763 #print p, P 764 assert i nside_polygon(p, P)764 assert is_inside_polygon(p, P) 765 765 766 766 … … 802 802 803 803 for p in points: 804 assert i nside_polygon(p, P)804 assert is_inside_polygon(p, P) 805 805 806 806 … … 828 828 assert len(P) == 16 829 829 for p in points: 830 assert i nside_polygon(p, P)830 assert is_inside_polygon(p, P) 831 831 832 832 … … 843 843 844 844 for p in points: 845 assert i nside_polygon(p, P)845 assert is_inside_polygon(p, P) 846 846 847 847 #print mesh.statistics() … … 909 909 for p in points: 910 910 #print p, P 911 assert i nside_polygon(p, P)911 assert is_inside_polygon(p, P) 912 912 913 913 -
inundation/test_all.py
r3028 r3052 17 17 #E.g. if they are known to fail and under development 18 18 exclude_files = ['test_metis.py', 'test_version.py', 'test_parallel_sw.py', 19 #'test_triangmodule.py', # removing this test for a bit19 'test_advection.py', # removing this test for a bit 20 20 'test_interpolate_sww.py' # this test is obsolete 21 21 ] -
inundation/utilities/polygon.py
r3051 r3052 43 43 44 44 45 def inside_polygon(points, polygon, closed = True, verbose = False, 45 def is_inside_polygon(point, polygon, closed=True, verbose=False, 46 points_geo_ref=None, polygon_geo_ref=None): 47 """Determine if one point is inside a polygon 48 49 See inside_polygon for more details 50 """ 51 52 indices = inside_polygon(point, polygon, closed, verbose, 53 points_geo_ref, polygon_geo_ref) 54 55 if indices.shape[0] == 1: 56 return True 57 elif indices.shape[0] == 0: 58 return False 59 else: 60 msg = 'is_inside_polygon must be invoked with one point only' 61 raise msg 62 63 64 def inside_polygon(points, polygon, closed=True, verbose=False, 46 65 points_geo_ref=None, polygon_geo_ref=None): 47 66 """Determine points inside a polygon … … 73 92 74 93 if len(points.shape) == 1: 75 one_point = True 76 points = reshape(points, (1,2)) 77 else: 78 one_point = False 94 # Only one point was passed in. Convert to array of points 95 points = reshape(points, (1,2)) 79 96 80 97 indices, count = separate_points_by_polygon(points, polygon, … … 82 99 verbose=verbose) 83 100 84 if one_point: 85 return count == 1 101 # Return indices of points inside polygon 102 return indices[:count] 103 104 105 106 def is_outside_polygon(point, polygon, closed=True, verbose=False, 107 points_geo_ref=None, polygon_geo_ref=None): 108 """Determine if one point is outside a polygon 109 110 See outside_polygon for more details 111 """ 112 113 indices = outside_polygon(point, polygon, closed, verbose) 114 #points_geo_ref, polygon_geo_ref) 115 116 if indices.shape[0] == 1: 117 return True 118 elif indices.shape[0] == 0: 119 return False 86 120 else: 87 return indices[:count] 121 msg = 'is_outside_polygon must be invoked with one point only' 122 raise msg 123 88 124 89 125 def outside_polygon(points, polygon, closed = True, verbose = False): … … 115 151 116 152 117 118 153 if len(points.shape) == 1: 119 one_point = True 120 points = reshape(points, (1,2)) 121 else: 122 one_point = False 154 # Only one point was passed in. Convert to array of points 155 points = reshape(points, (1,2)) 123 156 124 157 indices, count = separate_points_by_polygon(points, polygon, … … 126 159 verbose=verbose) 127 160 128 129 if one_point: 130 return count != 1 161 # Return indices of points outside polygon 162 if count == len(indices): 163 # No points are outside 164 return array([]) 131 165 else: 132 if count == len(indices): 133 # No points are outside 134 return [] 135 else: 136 return indices[count:][::-1] #return reversed 166 return indices[count:][::-1] #return reversed 137 167 138 168 … … 162 192 raise msg 163 193 164 165 166 194 if len(points.shape) == 1: 167 one_point = True 168 points = reshape(points, (1,2)) 169 else: 170 one_point = False 195 # Only one point was passed in. Convert to array of points 196 points = reshape(points, (1,2)) 197 171 198 172 199 indices, count = separate_points_by_polygon(points, polygon, 173 200 closed=closed, 174 201 verbose=verbose) 175 # Returns an array of points inside and an array of points outside 202 203 # Returns indices of points inside and indices of points outside 176 204 # the polygon 177 205 178 206 if count == len(indices): 179 207 # No points are outside … … 637 665 638 666 append = False 639 if i nside_polygon( [x,y], polygon):667 if is_inside_polygon([x,y], polygon): 640 668 641 669 append = True … … 644 672 if exclude is not None: 645 673 for ex_poly in exclude: 646 if i nside_polygon( [x,y], ex_poly):674 if is_inside_polygon([x,y], ex_poly): 647 675 append = False 648 676 … … 689 717 point = [x_delta, y_delta] 690 718 #print "point",point 691 if i nside_polygon( point, polygon, closed = False):719 if is_inside_polygon(point, polygon, closed=False): 692 720 raise Found 693 721 except Found: -
inundation/utilities/test_polygon.py
r3051 r3052 150 150 151 151 152 def test_is_inside_polygon_main(self): 153 154 155 #Simplest case: Polygon is the unit square 156 polygon = [[0,0], [1,0], [1,1], [0,1]] 157 158 assert is_inside_polygon( (0.5, 0.5), polygon ) 159 assert not is_inside_polygon( (0.5, 1.5), polygon ) 160 assert not is_inside_polygon( (0.5, -0.5), polygon ) 161 assert not is_inside_polygon( (-0.5, 0.5), polygon ) 162 assert not is_inside_polygon( (1.5, 0.5), polygon ) 163 164 #Try point on borders 165 assert is_inside_polygon( (1., 0.5), polygon, closed=True) 166 assert is_inside_polygon( (0.5, 1), polygon, closed=True) 167 assert is_inside_polygon( (0., 0.5), polygon, closed=True) 168 assert is_inside_polygon( (0.5, 0.), polygon, closed=True) 169 170 assert not is_inside_polygon( (0.5, 1), polygon, closed=False) 171 assert not is_inside_polygon( (0., 0.5), polygon, closed=False) 172 assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) 173 assert not is_inside_polygon( (1., 0.5), polygon, closed=False) 174 175 152 176 def test_inside_polygon_main(self): 153 177 154 155 #Simplest case: Polygon is the unit square 156 polygon = [[0,0], [1,0], [1,1], [0,1]] 157 158 assert inside_polygon( (0.5, 0.5), polygon ) 159 assert not inside_polygon( (0.5, 1.5), polygon ) 160 assert not inside_polygon( (0.5, -0.5), polygon ) 161 assert not inside_polygon( (-0.5, 0.5), polygon ) 162 assert not inside_polygon( (1.5, 0.5), polygon ) 163 164 #Try point on borders 165 assert inside_polygon( (1., 0.5), polygon, closed=True) 166 assert inside_polygon( (0.5, 1), polygon, closed=True) 167 assert inside_polygon( (0., 0.5), polygon, closed=True) 168 assert inside_polygon( (0.5, 0.), polygon, closed=True) 169 170 assert not inside_polygon( (0.5, 1), polygon, closed=False) 171 assert not inside_polygon( (0., 0.5), polygon, closed=False) 172 assert not inside_polygon( (0.5, 0.), polygon, closed=False) 173 assert not inside_polygon( (1., 0.5), polygon, closed=False) 174 175 178 #Simplest case: Polygon is the unit square 179 polygon = [[0,0], [1,0], [1,1], [0,1]] 176 180 177 181 #From real example (that failed) … … 191 195 #More convoluted and non convex polygon 192 196 polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]] 193 assert i nside_polygon( (0.5, 0.5), polygon )194 assert i nside_polygon( (1, -0.5), polygon )195 assert i nside_polygon( (1.5, 0), polygon )196 197 assert not i nside_polygon( (0.5, 1.5), polygon )198 assert not i nside_polygon( (0.5, -0.5), polygon )197 assert is_inside_polygon( (0.5, 0.5), polygon ) 198 assert is_inside_polygon( (1, -0.5), polygon ) 199 assert is_inside_polygon( (1.5, 0), polygon ) 200 201 assert not is_inside_polygon( (0.5, 1.5), polygon ) 202 assert not is_inside_polygon( (0.5, -0.5), polygon ) 199 203 200 204 201 205 #Very convoluted polygon 202 206 polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]] 203 assert i nside_polygon( (5, 5), polygon )204 assert i nside_polygon( (17, 7), polygon )205 assert i nside_polygon( (27, 2), polygon )206 assert i nside_polygon( (35, -5), polygon )207 assert not i nside_polygon( (15, 7), polygon )208 assert not i nside_polygon( (24, 3), polygon )209 assert not i nside_polygon( (25, -10), polygon )207 assert is_inside_polygon( (5, 5), polygon ) 208 assert is_inside_polygon( (17, 7), polygon ) 209 assert is_inside_polygon( (27, 2), polygon ) 210 assert is_inside_polygon( (35, -5), polygon ) 211 assert not is_inside_polygon( (15, 7), polygon ) 212 assert not is_inside_polygon( (24, 3), polygon ) 213 assert not is_inside_polygon( (25, -10), polygon ) 210 214 211 215 … … 213 217 #Another combination (that failed) 214 218 polygon = [[0,0], [10,0], [10,10], [0,10]] 215 assert i nside_polygon( (5, 5), polygon )216 assert i nside_polygon( (7, 7), polygon )217 assert not i nside_polygon( (-17, 7), polygon )218 assert not i nside_polygon( (7, 17), polygon )219 assert not i nside_polygon( (17, 7), polygon )220 assert not i nside_polygon( (27, 8), polygon )221 assert not i nside_polygon( (35, -5), polygon )219 assert is_inside_polygon( (5, 5), polygon ) 220 assert is_inside_polygon( (7, 7), polygon ) 221 assert not is_inside_polygon( (-17, 7), polygon ) 222 assert not is_inside_polygon( (7, 17), polygon ) 223 assert not is_inside_polygon( (17, 7), polygon ) 224 assert not is_inside_polygon( (27, 8), polygon ) 225 assert not is_inside_polygon( (35, -5), polygon ) 222 226 223 227 … … 228 232 polygon = [[0,0], [1,0], [1,1], [0,1], [0,0], 229 233 [10,10], [11,10], [11,11], [10,11], [10,10]] 230 assert i nside_polygon( (0.5, 0.5), polygon )231 assert i nside_polygon( (10.5, 10.5), polygon )234 assert is_inside_polygon( (0.5, 0.5), polygon ) 235 assert is_inside_polygon( (10.5, 10.5), polygon ) 232 236 233 237 #FIXME: Fails if point is 5.5, 5.5 234 assert not i nside_polygon( (0, 5.5), polygon )238 assert not is_inside_polygon( (0, 5.5), polygon ) 235 239 236 240 #Polygon with a hole … … 238 242 [0,0], [1,0], [1,1], [0,1], [0,0]] 239 243 240 assert i nside_polygon( (0, -0.5), polygon )241 assert not i nside_polygon( (0.5, 0.5), polygon )244 assert is_inside_polygon( (0, -0.5), polygon ) 245 assert not is_inside_polygon( (0.5, 0.5), polygon ) 242 246 243 247 … … 249 253 polygon = [[0,0], [1,0], [1,0], [1,0], [1,1], [0,1], [0,0]] 250 254 251 assert i nside_polygon( (0.5, 0.5), polygon )252 assert not i nside_polygon( (0.5, 1.5), polygon )253 assert not i nside_polygon( (0.5, -0.5), polygon )254 assert not i nside_polygon( (-0.5, 0.5), polygon )255 assert not i nside_polygon( (1.5, 0.5), polygon )255 assert is_inside_polygon( (0.5, 0.5), polygon ) 256 assert not is_inside_polygon( (0.5, 1.5), polygon ) 257 assert not is_inside_polygon( (0.5, -0.5), polygon ) 258 assert not is_inside_polygon( (-0.5, 0.5), polygon ) 259 assert not is_inside_polygon( (1.5, 0.5), polygon ) 256 260 257 261 #Try point on borders 258 assert i nside_polygon( (1., 0.5), polygon, closed=True)259 assert i nside_polygon( (0.5, 1), polygon, closed=True)260 assert i nside_polygon( (0., 0.5), polygon, closed=True)261 assert i nside_polygon( (0.5, 0.), polygon, closed=True)262 263 assert not i nside_polygon( (0.5, 1), polygon, closed=False)264 assert not i nside_polygon( (0., 0.5), polygon, closed=False)265 assert not i nside_polygon( (0.5, 0.), polygon, closed=False)266 assert not i nside_polygon( (1., 0.5), polygon, closed=False)262 assert is_inside_polygon( (1., 0.5), polygon, closed=True) 263 assert is_inside_polygon( (0.5, 1), polygon, closed=True) 264 assert is_inside_polygon( (0., 0.5), polygon, closed=True) 265 assert is_inside_polygon( (0.5, 0.), polygon, closed=True) 266 267 assert not is_inside_polygon( (0.5, 1), polygon, closed=False) 268 assert not is_inside_polygon( (0., 0.5), polygon, closed=False) 269 assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) 270 assert not is_inside_polygon( (1., 0.5), polygon, closed=False) 267 271 268 272 #From real example … … 285 289 U = [[0,0], [1,0], [1,1], [0,1]] #Unit square 286 290 287 assert not outside_polygon( [0.5, 0.5], U )291 assert not is_outside_polygon( [0.5, 0.5], U ) 288 292 #evaluate to False as the point 0.5, 0.5 is inside the unit square 289 293 290 assert outside_polygon( [1.5, 0.5], U )294 assert is_outside_polygon( [1.5, 0.5], U ) 291 295 #evaluate to True as the point 1.5, 0.5 is outside the unit square 292 296 … … 315 319 #evaluate to False as the point 0.5, 1.0 is inside the unit square 316 320 317 assert outside_polygon( [0.5, 1.0], U, closed = False )321 assert is_outside_polygon( [0.5, 1.0], U, closed = False ) 318 322 #evaluate to True as the point 0.5, 1.0 is outside the unit square 319 323 … … 390 394 assert len(points) == 5 391 395 for point in points: 392 assert i nside_polygon(point, polygon)396 assert is_inside_polygon(point, polygon) 393 397 394 398 … … 400 404 assert len(points) == 5 401 405 for point in points: 402 assert i nside_polygon(point, polygon)406 assert is_inside_polygon(point, polygon) 403 407 404 408 … … 412 416 assert len(points) == 5 413 417 for point in points: 414 assert i nside_polygon(point, polygon)415 assert not i nside_polygon(point, ex_poly)418 assert is_inside_polygon(point, polygon) 419 assert not is_inside_polygon(point, ex_poly) 416 420 417 421 … … 423 427 assert len(points) == 5 424 428 for point in points: 425 assert i nside_polygon(point, polygon)426 assert not i nside_polygon(point, ex_poly)429 assert is_inside_polygon(point, polygon) 430 assert not is_inside_polygon(point, ex_poly) 427 431 428 432 #Multiple … … 435 439 assert len(points) == 20 436 440 for point in points: 437 assert i nside_polygon(point, polygon)438 assert not i nside_polygon(point, ex_poly1)439 assert not i nside_polygon(point, ex_poly2)441 assert is_inside_polygon(point, polygon) 442 assert not is_inside_polygon(point, ex_poly1) 443 assert not is_inside_polygon(point, ex_poly2) 440 444 441 445 … … 447 451 assert len(points) == 20 448 452 for point in points: 449 assert i nside_polygon(point, polygon)450 assert not i nside_polygon(point, ex_poly), '%s' %str(point)453 assert is_inside_polygon(point, polygon) 454 assert not is_inside_polygon(point, ex_poly), '%s' %str(point) 451 455 452 456 def test_point_in_polygon(self): 453 457 polygon = [[0,0], [1,0], [1,1], [0,1]] 454 458 point = point_in_polygon(polygon) 455 assert i nside_polygon(point, polygon)459 assert is_inside_polygon(point, polygon) 456 460 457 461 #this may get into a vicious loop … … 460 464 polygon = [[1e15,1e7], [1,0], [1,1], [0,1]] 461 465 point = point_in_polygon(polygon) 462 assert i nside_polygon(point, polygon)466 assert is_inside_polygon(point, polygon) 463 467 464 468 465 469 polygon = [[0,0], [1,0], [1,1], [1e8,1e8]] 466 470 point = point_in_polygon(polygon) 467 assert i nside_polygon(point, polygon)471 assert is_inside_polygon(point, polygon) 468 472 469 473 470 474 polygon = [[1e32,1e54], [-1e32,1e54], [1e32,-1e54]] 471 475 point = point_in_polygon(polygon) 472 assert i nside_polygon(point, polygon)476 assert is_inside_polygon(point, polygon) 473 477 474 478 475 479 polygon = [[1e18,1e15], [1,0], [0,1]] 476 480 point = point_in_polygon(polygon) 477 assert i nside_polygon(point, polygon)481 assert is_inside_polygon(point, polygon) 478 482 479 483 def test_in_and_outside_polygon_main(self): … … 599 603 assert y[4] == 6 600 604 601 def test_plot_polygons(self): 602 605 # Disabled 606 def xtest_plot_polygons(self): 607 603 608 import os 604 609 … … 638 643 points = points_geo_ref.change_points_geo_ref(points_absolute) 639 644 640 assert i nside_polygon(points_absolute, polygon_absolute)641 642 points_inside = inside_polygon( 645 assert is_inside_polygon(points_absolute, polygon_absolute) 646 647 points_inside = inside_polygon(points, polygon, 643 648 polygon_geo_ref=poly_geo_ref, 644 649 points_geo_ref=points_geo_ref) … … 647 652 # polygon_geo_ref=poly_geo_ref, 648 653 # points_geo_ref=points_geo_ref)) 649 assert not i nside_polygon((0.5, 1.5), polygon,650 polygon_geo_ref=poly_geo_ref )651 assert not i nside_polygon((0.5, -0.5), polygon,652 polygon_geo_ref=poly_geo_ref )653 assert not i nside_polygon((-0.5, 0.5), polygon,654 polygon_geo_ref=poly_geo_ref )655 assert not i nside_polygon((1.5, 0.5), polygon,656 polygon_geo_ref=poly_geo_ref )654 assert not is_inside_polygon((0.5, 1.5), polygon, 655 polygon_geo_ref=poly_geo_ref ) 656 assert not is_inside_polygon((0.5, -0.5), polygon, 657 polygon_geo_ref=poly_geo_ref ) 658 assert not is_inside_polygon((-0.5, 0.5), polygon, 659 polygon_geo_ref=poly_geo_ref ) 660 assert not is_inside_polygon((1.5, 0.5), polygon, 661 polygon_geo_ref=poly_geo_ref ) 657 662 #------------------------------------------------------------- 658 663 if __name__ == "__main__":
Note: See TracChangeset
for help on using the changeset viewer.