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