Ignore:
Timestamp:
Oct 14, 2005, 6:44:45 AM (18 years ago)
Author:
ole
Message:

Refactored pyvolution to use polygon functionality from new utilities package

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/test_util.py

    r1903 r1911  
    172172        assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)
    173173
    174     def test_ensure_numeric(self):
    175         from util import ensure_numeric
    176         from Numeric import ArrayType, Float, array
    177 
    178         A = [1,2,3,4]
    179         B = ensure_numeric(A)
    180         assert type(B) == ArrayType
    181         assert B.typecode() == 'l'
    182         assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
    183 
    184 
    185         A = [1,2,3.14,4]
    186         B = ensure_numeric(A)
    187         assert type(B) == ArrayType
    188         assert B.typecode() == 'd'
    189         assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
    190 
    191 
    192         A = [1,2,3,4]
    193         B = ensure_numeric(A, Float)
    194         assert type(B) == ArrayType
    195         assert B.typecode() == 'd'
    196         assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    197 
    198 
    199         A = [1,2,3,4]
    200         B = ensure_numeric(A, Float)
    201         assert type(B) == ArrayType
    202         assert B.typecode() == 'd'
    203         assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    204 
    205 
    206         A = array([1,2,3,4])
    207         B = ensure_numeric(A)
    208         assert type(B) == ArrayType
    209         assert B.typecode() == 'l'       
    210         assert A == B   
    211         assert A is B   #Same object
    212 
    213 
    214         A = array([1,2,3,4])
    215         B = ensure_numeric(A, Float)
    216         assert type(B) == ArrayType
    217         assert B.typecode() == 'd'       
    218         assert A == B   
    219         assert A is not B   #Not the same object       
    220 
    221174
    222175
     
    1020973
    1021974
    1022     #Polygon stuff
    1023     def test_polygon_function_constants(self):
    1024         p1 = [[0,0], [10,0], [10,10], [0,10]]
    1025         p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    1026 
    1027         f = Polygon_function( [(p1, 1.0)] )
    1028         z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    1029         assert allclose(z, [1,1,0,0])
    1030 
    1031 
    1032         f = Polygon_function( [(p2, 2.0)] )
    1033         z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2
    1034         assert allclose(z, [2,0,0,2])
    1035 
    1036 
    1037         #Combined
    1038         f = Polygon_function( [(p1, 1.0), (p2, 2.0)] )
    1039         z = f([5, 5, 27, 35], [5, 9, 8, -5])
    1040         assert allclose(z, [2,1,0,2])
    1041 
    1042 
    1043     def test_polygon_function_callable(self):
    1044         """Check that values passed into Polygon_function can be callable
    1045         themselves.
    1046         """
    1047         p1 = [[0,0], [10,0], [10,10], [0,10]]
    1048         p2 = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    1049 
    1050         f = Polygon_function( [(p1, test_function)] )
    1051         z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    1052         assert allclose(z, [10,14,0,0])
    1053 
    1054         #Combined
    1055         f = Polygon_function( [(p1, test_function), (p2, 2.0)] )
    1056         z = f([5, 5, 27, 35], [5, 9, 8, -5])
    1057         assert allclose(z, [2,14,0,2])
    1058 
    1059 
    1060         #Combined w default
    1061         f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14)
    1062         z = f([5, 5, 27, 35], [5, 9, 8, -5])
    1063         assert allclose(z, [2,14,3.14,2])
    1064 
    1065 
    1066         #Combined w default func
    1067         f = Polygon_function( [(p1, test_function), (p2, 2.0)],
    1068                               default = test_function)
    1069         z = f([5, 5, 27, 35], [5, 9, 8, -5])
    1070         assert allclose(z, [2,14,35,2])
    1071 
    1072 
    1073     def test_point_on_line(self):
    1074 
    1075         #Endpoints first
    1076         assert point_on_line( 0, 0, 0,0, 1,0 )
    1077         assert point_on_line( 1, 0, 0,0, 1,0 )
    1078 
    1079         #Then points on line
    1080         assert point_on_line( 0.5, 0, 0,0, 1,0 )
    1081         assert point_on_line( 0, 0.5, 0,1, 0,0 )
    1082         assert point_on_line( 1, 0.5, 1,1, 1,0 )
    1083         assert point_on_line( 0.5, 0.5, 0,0, 1,1 )
    1084 
    1085         #Then points not on line
    1086         assert not point_on_line( 0.5, 0, 0,1, 1,1 )
    1087         assert not point_on_line( 0, 0.5, 0,0, 1,1 )
    1088 
    1089         #From real example that failed
    1090         assert not point_on_line( 40,50, 40,20, 40,40 )
    1091 
    1092 
    1093         #From real example that failed
    1094         assert not point_on_line( 40,19, 40,20, 40,40 )
    1095 
    1096 
    1097 
    1098 
    1099     def test_inside_polygon_main(self):
    1100 
    1101 
    1102         #Simplest case: Polygon is the unit square
    1103         polygon = [[0,0], [1,0], [1,1], [0,1]]
    1104 
    1105         assert inside_polygon( (0.5, 0.5), polygon )
    1106         assert not inside_polygon( (0.5, 1.5), polygon )
    1107         assert not inside_polygon( (0.5, -0.5), polygon )
    1108         assert not inside_polygon( (-0.5, 0.5), polygon )
    1109         assert not inside_polygon( (1.5, 0.5), polygon )
    1110 
    1111         #Try point on borders
    1112         assert inside_polygon( (1., 0.5), polygon, closed=True)
    1113         assert inside_polygon( (0.5, 1), polygon, closed=True)
    1114         assert inside_polygon( (0., 0.5), polygon, closed=True)
    1115         assert inside_polygon( (0.5, 0.), polygon, closed=True)
    1116 
    1117         assert not inside_polygon( (0.5, 1), polygon, closed=False)
    1118         assert not inside_polygon( (0., 0.5), polygon, closed=False)
    1119         assert not inside_polygon( (0.5, 0.), polygon, closed=False)
    1120         assert not inside_polygon( (1., 0.5), polygon, closed=False)
    1121 
    1122 
    1123 
    1124         #From real example (that failed)
    1125         polygon = [[20,20], [40,20], [40,40], [20,40]]
    1126         points = [ [40, 50] ]
    1127         res = inside_polygon(points, polygon)
    1128         assert len(res) == 0
    1129 
    1130         polygon = [[20,20], [40,20], [40,40], [20,40]]
    1131         points = [ [25, 25], [30, 20], [40, 50], [90, 20], [40, 90] ]
    1132         res = inside_polygon(points, polygon)
    1133         assert len(res) == 2
    1134         assert allclose(res, [0,1])
    1135 
    1136 
    1137 
    1138         #More convoluted and non convex polygon
    1139         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1140         assert inside_polygon( (0.5, 0.5), polygon )
    1141         assert inside_polygon( (1, -0.5), polygon )
    1142         assert inside_polygon( (1.5, 0), polygon )
    1143 
    1144         assert not inside_polygon( (0.5, 1.5), polygon )
    1145         assert not inside_polygon( (0.5, -0.5), polygon )
    1146 
    1147 
    1148         #Very convoluted polygon
    1149         polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    1150         assert inside_polygon( (5, 5), polygon )
    1151         assert inside_polygon( (17, 7), polygon )
    1152         assert inside_polygon( (27, 2), polygon )
    1153         assert inside_polygon( (35, -5), polygon )
    1154         assert not inside_polygon( (15, 7), polygon )
    1155         assert not inside_polygon( (24, 3), polygon )
    1156         assert not inside_polygon( (25, -10), polygon )
    1157 
    1158 
    1159 
    1160         #Another combination (that failed)
    1161         polygon = [[0,0], [10,0], [10,10], [0,10]]
    1162         assert inside_polygon( (5, 5), polygon )
    1163         assert inside_polygon( (7, 7), polygon )
    1164         assert not inside_polygon( (-17, 7), polygon )
    1165         assert not inside_polygon( (7, 17), polygon )
    1166         assert not inside_polygon( (17, 7), polygon )
    1167         assert not inside_polygon( (27, 8), polygon )
    1168         assert not inside_polygon( (35, -5), polygon )
    1169 
    1170 
    1171 
    1172 
    1173         #Multiple polygons
    1174 
    1175         polygon = [[0,0], [1,0], [1,1], [0,1], [0,0],
    1176                    [10,10], [11,10], [11,11], [10,11], [10,10]]
    1177         assert inside_polygon( (0.5, 0.5), polygon )
    1178         assert inside_polygon( (10.5, 10.5), polygon )
    1179 
    1180         #FIXME: Fails if point is 5.5, 5.5
    1181         assert not inside_polygon( (0, 5.5), polygon )
    1182 
    1183         #Polygon with a hole
    1184         polygon = [[-1,-1], [2,-1], [2,2], [-1,2], [-1,-1],
    1185                    [0,0], [1,0], [1,1], [0,1], [0,0]]
    1186 
    1187         assert inside_polygon( (0, -0.5), polygon )
    1188         assert not inside_polygon( (0.5, 0.5), polygon )
    1189 
    1190     def test_inside_polygon_vector_version(self):
    1191         #Now try the vector formulation returning indices
    1192         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1193         points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    1194         res = inside_polygon( points, polygon, verbose=False )
    1195 
    1196         assert allclose( res, [0,1,2] )
    1197 
    1198     def test_outside_polygon(self):
    1199         U = [[0,0], [1,0], [1,1], [0,1]] #Unit square   
    1200 
    1201         assert not outside_polygon( [0.5, 0.5], U )
    1202         #evaluate to False as the point 0.5, 0.5 is inside the unit square
    1203        
    1204         assert outside_polygon( [1.5, 0.5], U )
    1205         #evaluate to True as the point 1.5, 0.5 is outside the unit square
    1206        
    1207         indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    1208         assert allclose( indices, [1] )
    1209        
    1210         #One more test of vector formulation returning indices
    1211         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1212         points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    1213         res = outside_polygon( points, polygon )
    1214 
    1215         assert allclose( res, [3, 4] )
    1216 
    1217 
    1218 
    1219         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1220         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    1221         res = outside_polygon( points, polygon )
    1222 
    1223         assert allclose( res, [0, 4, 5] )       
    1224      
    1225     def test_outside_polygon2(self):
    1226         U = [[0,0], [1,0], [1,1], [0,1]] #Unit square   
    1227    
    1228         assert not outside_polygon( [0.5, 1.0], U, closed = True )
    1229         #evaluate to False as the point 0.5, 1.0 is inside the unit square
    1230        
    1231         assert outside_polygon( [0.5, 1.0], U, closed = False )
    1232         #evaluate to True as the point 0.5, 1.0 is outside the unit square
    1233 
    1234     def test_separate_points_by_polygon(self):
    1235         U = [[0,0], [1,0], [1,1], [0,1]] #Unit square   
    1236 
    1237         indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    1238         assert allclose( indices, [0,2,1] )
    1239         assert count == 2
    1240        
    1241         #One more test of vector formulation returning indices
    1242         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1243         points = [ [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    1244         res, count = separate_points_by_polygon( points, polygon )
    1245 
    1246         assert allclose( res, [0,1,2,4,3] )
    1247         assert count == 3
    1248 
    1249 
    1250         polygon = [[0,0], [1,0], [0.5,-1], [2, -1], [2,1], [0,1]]
    1251         points = [ [0.5, 1.4], [0.5, 0.5], [1, -0.5], [1.5, 0], [0.5, 1.5], [0.5, -0.5]]
    1252         res, count = separate_points_by_polygon( points, polygon )
    1253 
    1254         assert allclose( res, [1,2,3,5,4,0] )       
    1255         assert count == 3
    1256        
    1257 
    1258     def test_populate_polygon(self):
    1259 
    1260         polygon = [[0,0], [1,0], [1,1], [0,1]]
    1261         points = populate_polygon(polygon, 5)
    1262 
    1263         assert len(points) == 5
    1264         for point in points:
    1265             assert inside_polygon(point, polygon)
    1266 
    1267 
    1268         #Very convoluted polygon
    1269         polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    1270 
    1271         points = populate_polygon(polygon, 5)
    1272 
    1273         assert len(points) == 5
    1274         for point in points:
    1275             assert inside_polygon(point, polygon)
    1276 
    1277 
    1278     def test_populate_polygon_with_exclude(self):
    1279        
    1280 
    1281         polygon = [[0,0], [1,0], [1,1], [0,1]]
    1282         ex_poly = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter
    1283         points = populate_polygon(polygon, 5, exclude = [ex_poly])
    1284 
    1285         assert len(points) == 5
    1286         for point in points:
    1287             assert inside_polygon(point, polygon)
    1288             assert not inside_polygon(point, ex_poly)           
    1289 
    1290 
    1291         #overlap
    1292         polygon = [[0,0], [1,0], [1,1], [0,1]]
    1293         ex_poly = [[-1,-1], [0.5,0], [0.5, 0.5], [-1,0.5]]
    1294         points = populate_polygon(polygon, 5, exclude = [ex_poly])
    1295 
    1296         assert len(points) == 5
    1297         for point in points:
    1298             assert inside_polygon(point, polygon)
    1299             assert not inside_polygon(point, ex_poly)                       
    1300        
    1301         #Multiple
    1302         polygon = [[0,0], [1,0], [1,1], [0,1]]
    1303         ex_poly1 = [[0,0], [0.5,0], [0.5, 0.5], [0,0.5]] #SW quarter
    1304         ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter       
    1305        
    1306         points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2])
    1307 
    1308         assert len(points) == 20
    1309         for point in points:
    1310             assert inside_polygon(point, polygon)
    1311             assert not inside_polygon(point, ex_poly1)
    1312             assert not inside_polygon(point, ex_poly2)                               
    1313        
    1314 
    1315         #Very convoluted polygon
    1316         polygon = [[0,0], [10,10], [15,5], [20, 10], [25,0], [30,10], [40,-10]]
    1317         ex_poly = [[-1,-1], [5,0], [5, 5], [-1,5]]
    1318         points = populate_polygon(polygon, 20, exclude = [ex_poly])
    1319        
    1320         assert len(points) == 20
    1321         for point in points:
    1322             assert inside_polygon(point, polygon)
    1323             assert not inside_polygon(point, ex_poly), '%s' %str(point)                       
    1324975
    1325976
Note: See TracChangeset for help on using the changeset viewer.