Changeset 1911 for inundation/pyvolution/test_util.py
- Timestamp:
- Oct 14, 2005, 6:44:45 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/pyvolution/test_util.py
r1903 r1911 172 172 assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) 173 173 174 def test_ensure_numeric(self):175 from util import ensure_numeric176 from Numeric import ArrayType, Float, array177 178 A = [1,2,3,4]179 B = ensure_numeric(A)180 assert type(B) == ArrayType181 assert B.typecode() == 'l'182 assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4183 184 185 A = [1,2,3.14,4]186 B = ensure_numeric(A)187 assert type(B) == ArrayType188 assert B.typecode() == 'd'189 assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4190 191 192 A = [1,2,3,4]193 B = ensure_numeric(A, Float)194 assert type(B) == ArrayType195 assert B.typecode() == 'd'196 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0197 198 199 A = [1,2,3,4]200 B = ensure_numeric(A, Float)201 assert type(B) == ArrayType202 assert B.typecode() == 'd'203 assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0204 205 206 A = array([1,2,3,4])207 B = ensure_numeric(A)208 assert type(B) == ArrayType209 assert B.typecode() == 'l'210 assert A == B211 assert A is B #Same object212 213 214 A = array([1,2,3,4])215 B = ensure_numeric(A, Float)216 assert type(B) == ArrayType217 assert B.typecode() == 'd'218 assert A == B219 assert A is not B #Not the same object220 221 174 222 175 … … 1020 973 1021 974 1022 #Polygon stuff1023 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 p11029 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 p21034 assert allclose(z, [2,0,0,2])1035 1036 1037 #Combined1038 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 callable1045 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 p11052 assert allclose(z, [10,14,0,0])1053 1054 #Combined1055 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 default1061 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 func1067 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 first1076 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 line1080 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 line1086 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 failed1090 assert not point_on_line( 40,50, 40,20, 40,40 )1091 1092 1093 #From real example that failed1094 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 square1103 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 borders1112 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) == 01129 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) == 21134 assert allclose(res, [0,1])1135 1136 1137 1138 #More convoluted and non convex polygon1139 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 polygon1149 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 polygons1174 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.51181 assert not inside_polygon( (0, 5.5), polygon )1182 1183 #Polygon with a hole1184 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 indices1192 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 square1200 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 square1203 1204 assert outside_polygon( [1.5, 0.5], U )1205 #evaluate to True as the point 1.5, 0.5 is outside the unit square1206 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 indices1211 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 square1227 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 square1230 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 square1233 1234 def test_separate_points_by_polygon(self):1235 U = [[0,0], [1,0], [1,1], [0,1]] #Unit square1236 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 == 21240 1241 #One more test of vector formulation returning indices1242 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 == 31248 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 == 31256 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) == 51264 for point in points:1265 assert inside_polygon(point, polygon)1266 1267 1268 #Very convoluted polygon1269 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) == 51274 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 quarter1283 points = populate_polygon(polygon, 5, exclude = [ex_poly])1284 1285 assert len(points) == 51286 for point in points:1287 assert inside_polygon(point, polygon)1288 assert not inside_polygon(point, ex_poly)1289 1290 1291 #overlap1292 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) == 51297 for point in points:1298 assert inside_polygon(point, polygon)1299 assert not inside_polygon(point, ex_poly)1300 1301 #Multiple1302 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 quarter1304 ex_poly2 = [[0.5,0.5], [0.5,1], [1, 1], [1,0.5]] #NE quarter1305 1306 points = populate_polygon(polygon, 20, exclude = [ex_poly1, ex_poly2])1307 1308 assert len(points) == 201309 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 polygon1316 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) == 201321 for point in points:1322 assert inside_polygon(point, polygon)1323 assert not inside_polygon(point, ex_poly), '%s' %str(point)1324 975 1325 976
Note: See TracChangeset
for help on using the changeset viewer.