1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | import tempfile |
---|
4 | import unittest |
---|
5 | from mesh_interface import * |
---|
6 | from load_mesh.loadASCII import * |
---|
7 | from utilities.polygon import is_inside_polygon |
---|
8 | from coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE |
---|
9 | |
---|
10 | class TestCase(unittest.TestCase): |
---|
11 | def setUp(self): |
---|
12 | pass |
---|
13 | |
---|
14 | def tearDown(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def test_create_mesh_from_regions(self): |
---|
18 | x=-500 |
---|
19 | y=-1000 |
---|
20 | mesh_geo = geo_reference=Geo_reference(56,x,y) |
---|
21 | |
---|
22 | # These are the absolute values |
---|
23 | polygon_absolute = [[0,0],[100,0],[100,100],[0,100]] |
---|
24 | |
---|
25 | x_p = -10 |
---|
26 | y_p = -40 |
---|
27 | geo_ref_poly = Geo_reference(56, x_p, y_p) |
---|
28 | polygon = geo_ref_poly.change_points_geo_ref(polygon_absolute) |
---|
29 | |
---|
30 | boundary_tags = {'walls':[0,1],'bom':[2]} |
---|
31 | |
---|
32 | inner1_polygon_absolute = [[10,10],[20,10],[20,20],[10,20]] |
---|
33 | inner1_polygon = geo_ref_poly. \ |
---|
34 | change_points_geo_ref(inner1_polygon_absolute) |
---|
35 | |
---|
36 | inner2_polygon_absolute = [[30,30],[40,30],[40,40],[30,40]] |
---|
37 | inner2_polygon = geo_ref_poly. \ |
---|
38 | change_points_geo_ref(inner2_polygon_absolute) |
---|
39 | |
---|
40 | interior_regions = [(inner1_polygon, 5),(inner2_polygon, 0.2)] |
---|
41 | m = create_mesh_from_regions(polygon, |
---|
42 | boundary_tags, |
---|
43 | 10000000, |
---|
44 | interior_regions=interior_regions, |
---|
45 | poly_geo_reference=geo_ref_poly, |
---|
46 | mesh_geo_reference=mesh_geo) |
---|
47 | |
---|
48 | # Test the mesh instance |
---|
49 | self.failUnless(len(m.regions)==3, |
---|
50 | 'FAILED!') |
---|
51 | segs = m.getUserSegments() |
---|
52 | self.failUnless(len(segs)==12, |
---|
53 | 'FAILED!') |
---|
54 | self.failUnless(len(m.userVertices)==12, |
---|
55 | 'FAILED!') |
---|
56 | self.failUnless(segs[0].tag=='walls', |
---|
57 | 'FAILED!') |
---|
58 | self.failUnless(segs[1].tag=='walls', |
---|
59 | 'FAILED!') |
---|
60 | |
---|
61 | self.failUnless(segs[2].tag=='bom', |
---|
62 | 'FAILED!') |
---|
63 | self.failUnless(segs[3].tag=='', |
---|
64 | 'FAILED!') |
---|
65 | |
---|
66 | # Assuming the order of the region points is known. |
---|
67 | # (This isn't true, if you consider create_mesh_from_regions |
---|
68 | # a black box) |
---|
69 | poly_point = m.getRegions()[0] |
---|
70 | |
---|
71 | #print "poly_point", poly_point |
---|
72 | #print "polygon_absolute",polygon_absolute |
---|
73 | |
---|
74 | # poly_point values are relative to the mesh geo-ref |
---|
75 | # make them absolute |
---|
76 | self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], |
---|
77 | polygon_absolute, closed = False), |
---|
78 | 'FAILED!') |
---|
79 | |
---|
80 | # Assuming the order of the region points is known. |
---|
81 | # (This isn't true, if you consider create_mesh_from_regions |
---|
82 | # a black box) |
---|
83 | poly_point = m.getRegions()[1] |
---|
84 | |
---|
85 | #print "poly_point", poly_point |
---|
86 | #print "polygon_absolute",polygon_absolute |
---|
87 | |
---|
88 | # poly_point values are relative to the mesh geo-ref |
---|
89 | # make them absolute |
---|
90 | self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], |
---|
91 | inner1_polygon_absolute, |
---|
92 | closed = False), |
---|
93 | 'FAILED!') |
---|
94 | |
---|
95 | # Assuming the order of the region points is known. |
---|
96 | # (This isn't true, if you consider create_mesh_from_regions |
---|
97 | # a black box) |
---|
98 | poly_point = m.getRegions()[2] |
---|
99 | |
---|
100 | #print "poly_point", poly_point |
---|
101 | #print "polygon_absolute",polygon_absolute |
---|
102 | |
---|
103 | # poly_point values are relative to the mesh geo-ref |
---|
104 | # make them absolute |
---|
105 | self.failUnless(is_inside_polygon([poly_point.x+x,poly_point.y+y], |
---|
106 | inner2_polygon_absolute, |
---|
107 | closed = False), |
---|
108 | 'FAILED!') |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | def test_create_mesh_from_regions2(self): |
---|
113 | |
---|
114 | # These are the absolute values |
---|
115 | min_x = -10 |
---|
116 | min_y = -88 |
---|
117 | polygon_absolute = [[min_x,min_y],[1000,100],[1000,1000],[100,1000]] |
---|
118 | |
---|
119 | x_p = -10 |
---|
120 | y_p = -40 |
---|
121 | zone = 808 |
---|
122 | geo_ref_poly = Geo_reference(zone, x_p, y_p) |
---|
123 | polygon = geo_ref_poly.change_points_geo_ref(polygon_absolute) |
---|
124 | |
---|
125 | boundary_tags = {'walls':[0,1],'bom':[2]} |
---|
126 | |
---|
127 | inner1_polygon_absolute = [[10,10],[20,10],[20,20],[10,20]] |
---|
128 | inner1_polygon = geo_ref_poly. \ |
---|
129 | change_points_geo_ref(inner1_polygon_absolute) |
---|
130 | |
---|
131 | inner2_polygon_absolute = [[30,30],[40,30],[40,40],[30,40]] |
---|
132 | inner2_polygon = geo_ref_poly. \ |
---|
133 | change_points_geo_ref(inner2_polygon_absolute) |
---|
134 | |
---|
135 | interior_regions = [(inner1_polygon, 5),(inner2_polygon, 0.2)] |
---|
136 | m = create_mesh_from_regions(polygon, |
---|
137 | boundary_tags, |
---|
138 | 10000000, |
---|
139 | interior_regions=interior_regions, |
---|
140 | poly_geo_reference=geo_ref_poly) |
---|
141 | |
---|
142 | |
---|
143 | # Test the mesh instance |
---|
144 | self.failUnless(len(m.regions)==3, |
---|
145 | 'FAILED!') |
---|
146 | segs = m.getUserSegments() |
---|
147 | self.failUnless(len(segs)==12, |
---|
148 | 'FAILED!') |
---|
149 | self.failUnless(len(m.userVertices)==12, |
---|
150 | 'FAILED!') |
---|
151 | self.failUnless(segs[0].tag=='walls', |
---|
152 | 'FAILED!') |
---|
153 | self.failUnless(segs[1].tag=='walls', |
---|
154 | 'FAILED!') |
---|
155 | |
---|
156 | self.failUnless(segs[2].tag=='bom', |
---|
157 | 'FAILED!') |
---|
158 | self.failUnless(segs[3].tag=='', |
---|
159 | 'FAILED!') |
---|
160 | |
---|
161 | self.failUnless(m.geo_reference.get_zone()==zone, |
---|
162 | 'FAILED!') |
---|
163 | self.failUnless(m.geo_reference.get_xllcorner()==min_x, |
---|
164 | 'FAILED!') |
---|
165 | self.failUnless(m.geo_reference.get_yllcorner()==min_y, |
---|
166 | 'FAILED!') |
---|
167 | |
---|
168 | |
---|
169 | def test_create_mesh_from_regions3(self): |
---|
170 | |
---|
171 | # These are the absolute values |
---|
172 | min_x = -10 |
---|
173 | min_y = -88 |
---|
174 | polygon = [[min_x,min_y],[1000,100],[1000,1000],[100,1000]] |
---|
175 | |
---|
176 | |
---|
177 | x_p = -10 |
---|
178 | y_p = -40 |
---|
179 | geo_ref_poly = Geo_reference(56, x_p, y_p) |
---|
180 | |
---|
181 | boundary_tags = {'walls':[0,1],'bom':[2]} |
---|
182 | |
---|
183 | inner1_polygon_absolute = [[10,10],[20,10],[20,20],[10,20]] |
---|
184 | inner1_polygon = geo_ref_poly. \ |
---|
185 | change_points_geo_ref(inner1_polygon_absolute) |
---|
186 | |
---|
187 | inner2_polygon_absolute = [[30,30],[40,30],[40,40],[30,40]] |
---|
188 | inner2_polygon = geo_ref_poly. \ |
---|
189 | change_points_geo_ref(inner2_polygon_absolute) |
---|
190 | |
---|
191 | interior_regions = [(inner1_polygon, 5),(inner2_polygon, 0.2)] |
---|
192 | m = create_mesh_from_regions(polygon, |
---|
193 | boundary_tags, |
---|
194 | 10000000, |
---|
195 | interior_regions=interior_regions) |
---|
196 | |
---|
197 | |
---|
198 | # Test the mesh instance |
---|
199 | self.failUnless(len(m.regions)==3, |
---|
200 | 'FAILED!') |
---|
201 | segs = m.getUserSegments() |
---|
202 | self.failUnless(len(segs)==12, |
---|
203 | 'FAILED!') |
---|
204 | self.failUnless(len(m.userVertices)==12, |
---|
205 | 'FAILED!') |
---|
206 | self.failUnless(segs[0].tag=='walls', |
---|
207 | 'FAILED!') |
---|
208 | self.failUnless(segs[1].tag=='walls', |
---|
209 | 'FAILED!') |
---|
210 | |
---|
211 | self.failUnless(segs[2].tag=='bom', |
---|
212 | 'FAILED!') |
---|
213 | self.failUnless(segs[3].tag=='', |
---|
214 | 'FAILED!') |
---|
215 | |
---|
216 | self.failUnless(m.geo_reference.get_zone()==DEFAULT_ZONE, |
---|
217 | 'FAILED!') |
---|
218 | self.failUnless(m.geo_reference.get_xllcorner()==min_x, |
---|
219 | 'FAILED!') |
---|
220 | self.failUnless(m.geo_reference.get_yllcorner()==min_y, |
---|
221 | 'FAILED!') |
---|
222 | |
---|
223 | def test_create_mesh_from_regions_interior_regions(self): |
---|
224 | """Test that create_mesh_from_regions fails when an interior region is |
---|
225 | outside bounding polygon. """ |
---|
226 | |
---|
227 | |
---|
228 | # These are the absolute values |
---|
229 | min_x = 10 |
---|
230 | min_y = 88 |
---|
231 | polygon = [[min_x,min_y],[1000,100],[1000,1000],[100,1000]] |
---|
232 | |
---|
233 | |
---|
234 | boundary_tags = {'walls':[0,1],'bom':[2]} |
---|
235 | |
---|
236 | # This one is inside bounding polygon - should pass |
---|
237 | inner_polygon = [[800,400],[900,500],[800,600]] |
---|
238 | |
---|
239 | interior_regions = [(inner_polygon, 5)] |
---|
240 | m = create_mesh_from_regions(polygon, |
---|
241 | boundary_tags, |
---|
242 | 10000000, |
---|
243 | interior_regions=interior_regions) |
---|
244 | |
---|
245 | |
---|
246 | # This one sticks outside bounding polygon - should fail |
---|
247 | inner_polygon = [[800,400],[1100,500],[800,600]] |
---|
248 | interior_regions = [(inner_polygon, 5)] |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | try: |
---|
253 | m = create_mesh_from_regions(polygon, |
---|
254 | boundary_tags, |
---|
255 | 10000000, |
---|
256 | interior_regions=interior_regions) |
---|
257 | except: |
---|
258 | pass |
---|
259 | else: |
---|
260 | msg = 'Interior polygon sticking outside bounding polygon should ' |
---|
261 | msg += 'cause an Exception to be raised' |
---|
262 | raise msg |
---|
263 | |
---|
264 | |
---|
265 | |
---|
266 | def FIXME_test_create_mesh_with_multiply_tagged_segments(self): |
---|
267 | """Test that create_mesh_from_regions fails when |
---|
268 | segments are listed repeatedly in boundary_tags. |
---|
269 | """ |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | |
---|
274 | # These are the absolute values |
---|
275 | min_x = 10 |
---|
276 | min_y = 88 |
---|
277 | polygon = [[min_x,min_y],[1000,100],[1000,1000],[100,1000]] |
---|
278 | |
---|
279 | |
---|
280 | boundary_tags = {'walls':[0,1],'bom':[1,2]} |
---|
281 | |
---|
282 | # This one is inside bounding polygon - should pass |
---|
283 | inner_polygon = [[800,400],[900,500],[800,600]] |
---|
284 | |
---|
285 | interior_regions = [(inner_polygon, 5)] |
---|
286 | m = create_mesh_from_regions(polygon, |
---|
287 | boundary_tags, |
---|
288 | 10000000, |
---|
289 | interior_regions=interior_regions) |
---|
290 | |
---|
291 | |
---|
292 | # This one sticks outside bounding polygon - should fail |
---|
293 | inner_polygon = [[800,400],[900,500],[800,600]] |
---|
294 | interior_regions = [(inner_polygon, 5)] |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | try: |
---|
299 | m = create_mesh_from_regions(polygon, |
---|
300 | boundary_tags, |
---|
301 | 10000000, |
---|
302 | interior_regions=interior_regions) |
---|
303 | except: |
---|
304 | pass |
---|
305 | else: |
---|
306 | msg = 'Tags are listed repeatedly, but create mesh from regions ' |
---|
307 | msg += 'does not cause an Exception to be raised' |
---|
308 | raise msg |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | |
---|
313 | def test_create_mesh_from_regions_with_duplicate_verts(self): |
---|
314 | |
---|
315 | # These are the absolute values |
---|
316 | |
---|
317 | polygon_absolute = [[0.0,0.0], |
---|
318 | [0,4.0], |
---|
319 | [4.0,4.0], |
---|
320 | [4.0,0.0], |
---|
321 | [4.0,0.0]] |
---|
322 | |
---|
323 | x_p = -10 |
---|
324 | y_p = -40 |
---|
325 | zone = 808 |
---|
326 | geo_ref_poly = Geo_reference(zone, x_p, y_p) |
---|
327 | polygon = geo_ref_poly.change_points_geo_ref(polygon_absolute) |
---|
328 | |
---|
329 | boundary_tags = {'50':[0], |
---|
330 | '40':[1], |
---|
331 | '30':[2], |
---|
332 | 'no where seg':[3], |
---|
333 | '20':[4] |
---|
334 | } |
---|
335 | |
---|
336 | m = create_mesh_from_regions(polygon, |
---|
337 | boundary_tags, |
---|
338 | 10000000, |
---|
339 | poly_geo_reference=geo_ref_poly) |
---|
340 | |
---|
341 | |
---|
342 | fileName = "badmesh.tsh" |
---|
343 | #m.export_mesh_file(fileName) |
---|
344 | |
---|
345 | #------------------------------------------------------------- |
---|
346 | if __name__ == "__main__": |
---|
347 | suite = unittest.makeSuite(TestCase,'test') |
---|
348 | #suite = unittest.makeSuite(meshTestCase,'test_asciiFile') |
---|
349 | runner = unittest.TextTestRunner() #verbosity=2) |
---|
350 | runner.run(suite) |
---|
351 | |
---|