Changeset 7841 for trunk/anuga_core/source/anuga/geometry
- Timestamp:
- Jun 15, 2010, 12:06:46 PM (15 years ago)
- Location:
- trunk/anuga_core/source/anuga/geometry
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_core/source/anuga/geometry/__init__.py
r7711 r7841 2 2 """ 3 3 4 pass5 4 6 5 #Add path of package to PYTHONPATH to allow C-extensions to be loaded -
trunk/anuga_core/source/anuga/geometry/polygon.py
r7778 r7841 5 5 import numpy as num 6 6 7 from math import sqrt8 7 from anuga.utilities.numerical_tools import ensure_numeric 9 8 from anuga.geospatial_data.geospatial_data import ensure_absolute, \ 10 9 Geospatial_data 11 from anuga.config import netcdf_float12 10 import anuga.utilities.log as log 13 11 … … 129 127 line1 = ensure_numeric(line1, num.float) 130 128 131 x0 = line0[0, 0]; y0 = line0[0,1]132 x1 = line0[1, 0]; y1 = line0[1,1]133 134 x2 = line1[0, 0]; y2 = line1[0,1]135 x3 = line1[1, 0]; y3 = line1[1,1]129 x0 = line0[0, 0]; y0 = line0[0, 1] 130 x1 = line0[1, 0]; y1 = line0[1, 1] 131 132 x2 = line1[0, 0]; y2 = line1[0, 1] 133 x3 = line1[1, 0]; y3 = line1[1, 1] 136 134 137 135 denom = (y3-y2)*(x1-x0) - (x3-x2)*(y1-y0) … … 208 206 line1 = ensure_numeric(line1, num.float) 209 207 210 status, value = _intersection(line0[0, 0], line0[0,1],211 line0[1, 0], line0[1,1],212 line1[0, 0], line1[0,1],213 line1[1, 0], line1[1,1])208 status, value = _intersection(line0[0, 0], line0[0, 1], 209 line0[1, 0], line0[1, 1], 210 line1[0, 0], line1[0, 1], 211 line1[1, 0], line1[1, 1]) 214 212 215 213 return status, value … … 219 217 rtol=1.0e-12, 220 218 atol=1.0e-12, 221 check_inputs=True, 222 verbose=False): 219 check_inputs=True): 223 220 """Determine if one point is inside a triangle 224 221 … … 278 275 279 276 # Quickly reject points that are clearly outside 280 if point[0] < min(triangle[:, 0]): return False 281 if point[0] > max(triangle[:, 0]): return False 282 if point[1] < min(triangle[:, 1]): return False 283 if point[1] > max(triangle[:, 1]): return False 277 if point[0] < min(triangle[:, 0]): 278 return False 279 if point[0] > max(triangle[:, 0]): 280 return False 281 if point[1] < min(triangle[:, 1]): 282 return False 283 if point[1] > max(triangle[:, 1]): 284 return False 284 285 285 286 … … 314 315 # Check if point lies on one of the edges 315 316 316 for X, Y in [[A, B], [B,C], [C,A]]:317 for X, Y in [[A, B], [B, C], [C, A]]: 317 318 res = _point_on_line(point[0], point[1], 318 319 X[0], X[1], … … 341 342 342 343 def segments_joined(seg0, seg1): 344 """ See if there are identical segments in the 2 lists. """ 343 345 for i in seg0: 344 346 for j in seg1: … … 381 383 return False 382 384 383 384 def is_inside_polygon_quick(point, polygon, closed=True, verbose=False):385 """Determine if one point is inside a polygon386 Both point and polygon are assumed to be numeric arrays or lists and387 no georeferencing etc or other checks will take place.388 389 As such it is faster than is_inside_polygon390 """391 392 # FIXME(Ole): This function isn't being used393 polygon = ensure_numeric(polygon, num.float)394 points = ensure_numeric(point, num.float) # Convert point to array of points395 points = num.ascontiguousarray(points[num.newaxis, :])396 msg = ('is_inside_polygon() must be invoked with one point only.\n'397 'I got %s and converted to %s' % (str(point), str(points.shape)))398 assert points.shape[0] == 1 and points.shape[1] == 2, msg399 400 indices = num.zeros(1, num.int)401 402 count = _separate_points_by_polygon(points, polygon, indices,403 int(closed), int(verbose))404 405 return count > 0406 407 385 408 386 def is_inside_polygon(point, polygon, closed=True, verbose=False): … … 420 398 else: 421 399 msg = 'is_inside_polygon must be invoked with one point only' 422 raise msg400 raise Exception(msg) 423 401 424 402 ## … … 743 721 figname=None, 744 722 label=None, 745 alpha=None, 746 verbose=False): 723 alpha=None): 747 724 """ Take list of polygons and plot. 748 725 … … 769 746 """ 770 747 771 from pylab import ion, hold, plot, axis, figure, legend,savefig, xlabel, \748 from pylab import ion, hold, plot, savefig, xlabel, \ 772 749 ylabel, title, close, title, fill 773 750 … … 793 770 alpha = None 794 771 else: 795 if alpha < 0.0: 796 alpha = 0.0 797 if alpha > 1.0: 798 alpha = 1.0 772 alpha = max(0.0, min(1.0, alpha)) 799 773 800 774 n = len(polygons_points) … … 818 792 for i, item in enumerate(polygons_points): 819 793 x, y = poly_xy(item) 820 if min(x) < minx: minx = min(x) 821 if max(x) > maxx: maxx = max(x) 822 if min(y) < miny: miny = min(y) 823 if max(y) > maxy: maxy = max(y) 824 plot(x,y,colour[i]) 794 if min(x) < minx: 795 minx = min(x) 796 if max(x) > maxx: 797 maxx = max(x) 798 if min(y) < miny: 799 miny = min(y) 800 if max(y) > maxy: 801 maxy = max(y) 802 plot(x, y, colour[i]) 825 803 if alpha: 826 804 fill(x, y, colour[i], alpha=alpha) … … 840 818 841 819 842 def poly_xy(polygon , verbose=False):820 def poly_xy(polygon): 843 821 """ this is used within plot_polygons so need to duplicate 844 822 the first point so can have closed polygon in plot … … 858 836 raise Exception, msg 859 837 860 x = polygon[:, 0]861 y = polygon[:, 1]862 x = num.concatenate((x, [polygon[0, 0]]), axis = 0)863 y = num.concatenate((y, [polygon[0, 1]]), axis = 0)838 x = polygon[:, 0] 839 y = polygon[:, 1] 840 x = num.concatenate((x, [polygon[0, 0]]), axis = 0) 841 y = num.concatenate((y, [polygon[0, 1]]), axis = 0) 864 842 865 843 return x, y … … 960 938 assert y.shape[0] == N 961 939 962 points = num.ascontiguousarray(num.concatenate((x[:, num.newaxis],963 y[:, num.newaxis]),964 axis =1 ))940 points = num.ascontiguousarray(num.concatenate((x[:, num.newaxis], 941 y[:, num.newaxis]), 942 axis = 1 )) 965 943 966 944 if callable(self.default): … … 1042 1020 fid.write('%f, %f\n' % point) 1043 1021 fid.close() 1044 1045 ##1046 # @brief Unimplemented.1047 def read_tagged_polygons(filename):1048 """1049 """1050 pass1051 1022 1052 1023 … … 1097 1068 if exclude is not None: 1098 1069 for ex_poly in exclude: 1099 if is_inside_polygon([x, y], ex_poly):1070 if is_inside_polygon([x, y], ex_poly): 1100 1071 append = False 1101 1072 1102 1073 if append is True: 1103 points.append([x, y])1074 points.append([x, y]) 1104 1075 1105 1076 return points … … 1122 1093 import exceptions 1123 1094 1124 class Found(exceptions.Exception): pass 1095 class Found(exceptions.Exception): 1096 pass 1125 1097 1126 1098 polygon = ensure_numeric(polygon) … … 1232 1204 # Find outer extent of polygon 1233 1205 num_polygon = ensure_numeric(polygon) 1234 max_x = max(num_polygon[:, 0])1235 max_y = max(num_polygon[:, 1])1236 min_x = min(num_polygon[:, 0])1237 min_y = min(num_polygon[:, 1])1206 max_x = max(num_polygon[:, 0]) 1207 max_y = max(num_polygon[:, 1]) 1208 min_x = min(num_polygon[:, 0]) 1209 min_y = min(num_polygon[:, 1]) 1238 1210 1239 1211 # Keep only some points making sure extrema are kept … … 1257 1229 interpolation_points=None, 1258 1230 rtol=1.0e-6, 1259 atol=1.0e-8, 1260 verbose=False): 1231 atol=1.0e-8): 1261 1232 """Interpolate linearly between values data on polyline nodes 1262 1233 of a polyline to list of interpolation points. … … 1289 1260 gauge_neighbour_id = ensure_numeric(gauge_neighbour_id, num.int) 1290 1261 1291 n = polyline_nodes.shape[0] # Number of nodes in polyline1262 num_nodes = polyline_nodes.shape[0] # Number of nodes in polyline 1292 1263 1293 1264 # Input sanity check 1294 msg = 'interpolation_points are not given (interpolate.py)' 1295 assert interpolation_points is not None, msg 1296 1297 msg = 'function value must be specified at every interpolation node' 1298 assert data.shape[0] == polyline_nodes.shape[0], msg 1299 1300 msg = 'Must define function value at one or more nodes' 1301 assert data.shape[0] > 0, msg 1302 1303 if n == 1: 1304 msg = 'Polyline contained only one point. I need more. ' + str(data) 1305 raise Exception, msg 1306 elif n > 1: 1265 assert_msg = 'interpolation_points are not given (interpolate.py)' 1266 assert interpolation_points is not None, assert_msg 1267 1268 assert_msg = 'function value must be specified at every interpolation node' 1269 assert data.shape[0] == polyline_nodes.shape[0], assert_msg 1270 1271 assert_msg = 'Must define function value at one or more nodes' 1272 assert data.shape[0] > 0, assert_msg 1273 1274 if num_nodes == 1: 1275 assert_msg = 'Polyline contained only one point. I need more. ' 1276 assert_msg += str(data) 1277 raise Exception, assert_msg 1278 elif num_nodes > 1: 1307 1279 _interpolate_polyline(data, 1308 1280 polyline_nodes, … … 1346 1318 1347 1319 else: 1348 msg = 'C implementations could not be accessed by %s.\n ' %__file__1349 msg += 'Make sure compile_all.py has been run as described in '1350 msg += 'the ANUGA installation guide.'1351 raise Exception , msg1320 error_msg = 'C implementations could not be accessed by %s.\n ' %__file__ 1321 error_msg += 'Make sure compile_all.py has been run as described in ' 1322 error_msg += 'the ANUGA installation guide.' 1323 raise Exception(error_msg) 1352 1324 1353 1325 -
trunk/anuga_core/source/anuga/geometry/test_polygon.py
r7711 r7841 178 178 179 179 def test_inside_polygon_main(self): 180 """test_is_inside_polygon _quick180 """test_is_inside_polygon 181 181 182 182 Test fast version of of is_inside_polygon … … 186 186 polygon = [[0,0], [1,0], [1,1], [0,1]] 187 187 188 assert is_inside_polygon _quick( (0.5, 0.5), polygon )189 assert not is_inside_polygon _quick( (0.5, 1.5), polygon )190 assert not is_inside_polygon _quick( (0.5, -0.5), polygon )191 assert not is_inside_polygon _quick( (-0.5, 0.5), polygon )192 assert not is_inside_polygon _quick( (1.5, 0.5), polygon )188 assert is_inside_polygon( (0.5, 0.5), polygon ) 189 assert not is_inside_polygon( (0.5, 1.5), polygon ) 190 assert not is_inside_polygon( (0.5, -0.5), polygon ) 191 assert not is_inside_polygon( (-0.5, 0.5), polygon ) 192 assert not is_inside_polygon( (1.5, 0.5), polygon ) 193 193 194 194 # Try point on borders 195 assert is_inside_polygon _quick( (1., 0.5), polygon, closed=True)196 assert is_inside_polygon _quick( (0.5, 1), polygon, closed=True)197 assert is_inside_polygon _quick( (0., 0.5), polygon, closed=True)198 assert is_inside_polygon _quick( (0.5, 0.), polygon, closed=True)199 200 assert not is_inside_polygon _quick( (0.5, 1), polygon, closed=False)201 assert not is_inside_polygon _quick( (0., 0.5), polygon, closed=False)202 assert not is_inside_polygon _quick( (0.5, 0.), polygon, closed=False)203 assert not is_inside_polygon _quick( (1., 0.5), polygon, closed=False)195 assert is_inside_polygon( (1., 0.5), polygon, closed=True) 196 assert is_inside_polygon( (0.5, 1), polygon, closed=True) 197 assert is_inside_polygon( (0., 0.5), polygon, closed=True) 198 assert is_inside_polygon( (0.5, 0.), polygon, closed=True) 199 200 assert not is_inside_polygon( (0.5, 1), polygon, closed=False) 201 assert not is_inside_polygon( (0., 0.5), polygon, closed=False) 202 assert not is_inside_polygon( (0.5, 0.), polygon, closed=False) 203 assert not is_inside_polygon( (1., 0.5), polygon, closed=False) 204 204 205 205 … … 229 229 assert not is_inside_polygon( (0.5, -0.5), polygon ) 230 230 231 assert is_inside_polygon _quick( (0.5, 0.5), polygon )232 assert is_inside_polygon _quick( (1, -0.5), polygon )233 assert is_inside_polygon _quick( (1.5, 0), polygon )234 235 assert not is_inside_polygon _quick( (0.5, 1.5), polygon )236 assert not is_inside_polygon _quick( (0.5, -0.5), polygon )231 assert is_inside_polygon( (0.5, 0.5), polygon ) 232 assert is_inside_polygon( (1, -0.5), polygon ) 233 assert is_inside_polygon( (1.5, 0), polygon ) 234 235 assert not is_inside_polygon( (0.5, 1.5), polygon ) 236 assert not is_inside_polygon( (0.5, -0.5), polygon ) 237 237 238 238 # Very convoluted polygon … … 449 449 assert is_inside_polygon(point, polygon) 450 450 451 assert is_inside_polygon _quick(point, polygon)451 assert is_inside_polygon(point, polygon) 452 452 453 453 … … 458 458 for point in points: 459 459 assert is_inside_polygon(point, polygon) 460 assert is_inside_polygon _quick(point, polygon)460 assert is_inside_polygon(point, polygon) 461 461 462 462 … … 1598 1598 assert y[4] == 6 1599 1599 1600 # Disabled 1601 def xtest_plot_polygons(self):1600 1601 def test_plot_polygons(self): 1602 1602 import os 1603 1603 … … 1605 1605 polygon1 = [[0,0], [1,0], [1,1], [0,1]] 1606 1606 polygon2 = [[1,1], [2,1], [3,2], [2,2]] 1607 v = plot_polygons([polygon1, polygon2], 'test1')1607 v = plot_polygons([polygon1, polygon2], figname='test1') 1608 1608 assert len(v) == 4 1609 1609 assert v[0] == 0 … … 1614 1614 # Another case 1615 1615 polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]] 1616 v = plot_polygons([polygon2,polygon3], 'test2')1616 v = plot_polygons([polygon2,polygon3], figname='test2') 1617 1617 assert len(v) == 4 1618 1618 assert v[0] == 1
Note: See TracChangeset
for help on using the changeset viewer.