1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | import tempfile |
---|
4 | import unittest |
---|
5 | from mesh import * |
---|
6 | from load_mesh.loadASCII import * |
---|
7 | |
---|
8 | class meshTestCase(unittest.TestCase): |
---|
9 | def setUp(self): |
---|
10 | pass |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | def tearDown(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def testPointDistance(self): |
---|
18 | a = Point(0.0, 0.0) |
---|
19 | b = Point(0.0, 10.0) |
---|
20 | |
---|
21 | self.failUnless( a.DistanceToPoint(b) == 10.0, |
---|
22 | 'Point DistanceToPoint is wrong!') |
---|
23 | |
---|
24 | def testVertexDistance(self): |
---|
25 | a = Vertex (0.0, 0.0) |
---|
26 | b = Vertex (0.0, 10.0) |
---|
27 | |
---|
28 | self.failUnless( a.DistanceToPoint(b) == 10.0, |
---|
29 | 'Point DistanceToPoint is wrong!') |
---|
30 | |
---|
31 | def testTriangle(self): |
---|
32 | a = Vertex (0.0, 0.0) |
---|
33 | b = Vertex (0.0, 2.0) |
---|
34 | c = Vertex (2.0,0.0) |
---|
35 | d = Vertex (0.0, 4.0) |
---|
36 | e = Vertex (2.0, 2.0) |
---|
37 | f = Vertex (4.0,0.0) |
---|
38 | |
---|
39 | t1 = Triangle(b,a,c) |
---|
40 | t2 = Triangle(b,c,e) |
---|
41 | t3 = Triangle(e,c,f) |
---|
42 | t4 = Triangle(d,b,e) |
---|
43 | t2.setNeighbors(t3,t4,t1) |
---|
44 | |
---|
45 | self.failUnless( t2.neighbors[2].vertices[0] == b, 'Triangle initialisation is wrong!') |
---|
46 | |
---|
47 | |
---|
48 | def testSegment(self): |
---|
49 | a = Vertex (0.0, 0.0) |
---|
50 | b = Vertex (0.0, 10.0) |
---|
51 | s = Segment(a,b, marker = 20) |
---|
52 | |
---|
53 | self.failUnless( s.vertices[0].DistanceToPoint(s.vertices[1]) == 10.0, |
---|
54 | 'vertices in a segment are wrong') |
---|
55 | |
---|
56 | self.failUnless( s.marker == 20.0, |
---|
57 | 'marker in a segment are wrong') |
---|
58 | |
---|
59 | def testdeleteUserVertex(self): |
---|
60 | |
---|
61 | |
---|
62 | mesh = Mesh() |
---|
63 | a = mesh.addUserVertex(0.0, 0.0) |
---|
64 | b = mesh.addUserVertex (0.0, 2.0) |
---|
65 | c = mesh.addUserVertex (2.0,0.0) |
---|
66 | |
---|
67 | s1 = mesh.addUserSegment(a,b) |
---|
68 | s2 = mesh.addUserSegment(a,c) |
---|
69 | s3 = mesh.addUserSegment(c,b) |
---|
70 | |
---|
71 | mesh.deleteMeshObject (a) |
---|
72 | self.failUnless(mesh.userSegments[0] == s3, |
---|
73 | 'Bad segment. ') |
---|
74 | self.failUnless(len(mesh.userSegments) ==1, |
---|
75 | 'Segments not deleted.') |
---|
76 | self.failUnless(len(mesh.userVertices) == 2, |
---|
77 | 'Vertex not deleted.') |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | def testgenerateMesh(self): |
---|
82 | a = Vertex (0.0, 0.0) |
---|
83 | d = Vertex (0.0, 4.0) |
---|
84 | f = Vertex (4.0,0.0) |
---|
85 | |
---|
86 | s1 = Segment(a,d) |
---|
87 | s2 = Segment(d,f) |
---|
88 | s3 = Segment(a,f) |
---|
89 | |
---|
90 | r1 = Region(0.3, 0.3,tag = 1.3,maxArea = .6) |
---|
91 | #print r1 |
---|
92 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1] ) |
---|
93 | |
---|
94 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
95 | |
---|
96 | #print m |
---|
97 | |
---|
98 | #m.plotMeshTriangle() |
---|
99 | |
---|
100 | result = 1.414214 |
---|
101 | delta = 0.00001 |
---|
102 | |
---|
103 | self.failUnless((m.meshTriangles[1].vertices[0].x < result + delta) or |
---|
104 | (m.meshTriangles[1].vertices[0].x > result - delta), |
---|
105 | 'generated mesh is wrong!') |
---|
106 | |
---|
107 | def test_regionalMaxArea(self): |
---|
108 | v0 = Vertex (0.0, 0.0) |
---|
109 | v1 = Vertex (6.0, 0.0) |
---|
110 | v2 = Vertex (6.0,6.0) |
---|
111 | v3 = Vertex (0.0,6.0) |
---|
112 | |
---|
113 | s1 = Segment(v0,v1) |
---|
114 | s2 = Segment(v1,v2) |
---|
115 | s3 = Segment(v3,v2) |
---|
116 | s4 = Segment(v3,v0) |
---|
117 | s5 = Segment(v2,v0) |
---|
118 | |
---|
119 | r1 = Region(3, 1,tag = 1.3) |
---|
120 | #print r1 |
---|
121 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], regions=[r1] ) |
---|
122 | |
---|
123 | m.generateMesh("Apz", maxArea = 36 ) |
---|
124 | |
---|
125 | #m.plotMeshTriangle() |
---|
126 | #print "len(m.meshTriangles)",len(m.meshTriangles) |
---|
127 | |
---|
128 | self.failUnless(len(m.meshTriangles) == 2, |
---|
129 | 'test_regionalMaxArea 1:generated mesh is wrong!') |
---|
130 | |
---|
131 | ## Another test case |
---|
132 | r1 = Region(3, 1,tag = 1.3) |
---|
133 | r2 = Region(1, 3,tag = 1.3, maxArea = 8) |
---|
134 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
135 | regions=[r1,r2] ) |
---|
136 | m.generateMesh("Apz", maxArea = 36 ) |
---|
137 | self.failUnless(len(m.meshTriangles) == 6, |
---|
138 | 'test_regionalMaxArea 2:generated mesh is wrong!') |
---|
139 | |
---|
140 | ## Another test case |
---|
141 | r1 = Region(3, 1, tag = 1.3, maxArea = 8) |
---|
142 | r2 = Region(1, 3, tag = 1.3, maxArea = 8) |
---|
143 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
144 | regions=[r1,r2] ) |
---|
145 | m.generateMesh("Apz", maxArea = 36 ) |
---|
146 | print "len(m.meshTriangles)",len(m.meshTriangles) |
---|
147 | self.failUnless(len(m.meshTriangles) == 8, |
---|
148 | 'test_regionalMaxArea 3:generated mesh is wrong!') |
---|
149 | |
---|
150 | ## Another test case |
---|
151 | r1 = Region(3, 1, tag = 1.3 ) |
---|
152 | r2 = Region(1, 3, tag = 1.3, maxArea = 8) |
---|
153 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
154 | regions=[r1,r2] ) |
---|
155 | m.generateMesh("Apz", maxArea = 8 ) |
---|
156 | self.failUnless(len(m.meshTriangles) == 8, |
---|
157 | 'test_regionalMaxArea 4:generated mesh is wrong!') |
---|
158 | |
---|
159 | ## Another test case |
---|
160 | r1 = Region(3, 1,tag = 1.3, maxArea = 8) |
---|
161 | r2 = Region(1, 3,tag = 1.3, maxArea = 8) |
---|
162 | m = Mesh(userVertices=[v0,v1,v2,v3], userSegments=[s1,s2,s3,s4,s5], |
---|
163 | regions=[r1,r2] ) |
---|
164 | m.generateMesh("Apz", maxArea = 36,isRegionalMaxAreas = False ) |
---|
165 | self.failUnless(len(m.meshTriangles) == 2, |
---|
166 | 'test_regionalMaxArea 5:generated mesh is wrong!') |
---|
167 | |
---|
168 | def testdeleteUserVertex(self): |
---|
169 | mesh = Mesh() |
---|
170 | a = mesh.addUserVertex(0.0, 0.0) |
---|
171 | b = mesh.addUserVertex (0.0, 2.0) |
---|
172 | c = mesh.addUserVertex (2.0,0.0) |
---|
173 | |
---|
174 | s1 = mesh.addUserSegment(a,b) |
---|
175 | s2 = mesh.addUserSegment(a,c) |
---|
176 | s3 = mesh.addUserSegment(c,b) |
---|
177 | |
---|
178 | mesh.deleteMeshObject (s2) |
---|
179 | |
---|
180 | #print ",s2 in mesh.userSegments" ,s2 in mesh.userSegments |
---|
181 | self.failUnless(not(s2 in mesh.userSegments), |
---|
182 | 'Bad segment. ') |
---|
183 | self.failUnless(len(mesh.userSegments) ==2, |
---|
184 | 'Segments not deleted.') |
---|
185 | self.failUnless(len(mesh.userVertices) == 3, |
---|
186 | 'Vertex deleted, instead of segment.') |
---|
187 | |
---|
188 | def testTriangleArea(self): |
---|
189 | a = Vertex (10.0, 10.0) |
---|
190 | b = Vertex (10.0, 20.0) |
---|
191 | c = Vertex (20.0,10.0) |
---|
192 | |
---|
193 | d = Vertex (-20.0, 0.0) |
---|
194 | e = Vertex (-20.0, -20.0) |
---|
195 | f = Vertex (20.0,-20.0) |
---|
196 | |
---|
197 | t1 = Triangle(b,a,c) |
---|
198 | t2 = Triangle(e,d,f) |
---|
199 | |
---|
200 | # print "t1", t1 |
---|
201 | # print "t1 area ", t1.calcArea() |
---|
202 | # print "t2", t2 |
---|
203 | # print "t2 area ", t2.calcArea() |
---|
204 | self.failUnless( t1.calcArea() == 50 and t2.calcArea() == 400, 'Triangle area is wrong!') |
---|
205 | def testisUserSegmentNew (self): |
---|
206 | mesh = Mesh() |
---|
207 | a = mesh.addUserVertex(0.0, 0.0) |
---|
208 | b = mesh.addUserVertex (0.0, 2.0) |
---|
209 | c = mesh.addUserVertex (2.0,0.0) |
---|
210 | d = mesh.addUserVertex (2.0,3.0) |
---|
211 | |
---|
212 | s1 = mesh.addUserSegment(a,b) |
---|
213 | s2 = mesh.addUserSegment(a,c) |
---|
214 | s3 = mesh.addUserSegment(c,b) |
---|
215 | |
---|
216 | self.failUnless(mesh.isUserSegmentNew(a,d) , |
---|
217 | 'Segment should be new. ') |
---|
218 | self.failUnless(not(mesh.isUserSegmentNew(a,b)) , |
---|
219 | 'Segment should not be new. ') |
---|
220 | |
---|
221 | |
---|
222 | def testautoSegment(self): |
---|
223 | a = Vertex (0.0, 0.0) |
---|
224 | d = Vertex (0.0, 4.0) |
---|
225 | f = Vertex (4.0,4.0) |
---|
226 | g = Vertex (4.0,0.0) |
---|
227 | |
---|
228 | s1 = Segment(a,d) |
---|
229 | #s2 = Segment(a,f) |
---|
230 | #s3 = Segment(f,d) |
---|
231 | |
---|
232 | m = Mesh(userVertices=[a,d,f,g], userSegments=[s1] ) |
---|
233 | |
---|
234 | #print m |
---|
235 | |
---|
236 | m.autoSegment() |
---|
237 | |
---|
238 | #print m |
---|
239 | |
---|
240 | #print 'Len', len(m.userSegments) |
---|
241 | |
---|
242 | self.failUnless(len(m.userSegments) == 4 , |
---|
243 | 'userSegments is wrong!') |
---|
244 | |
---|
245 | def testRegions(self): |
---|
246 | a = Vertex (0.0, 0.0) |
---|
247 | b = Vertex (0.0, 2.0) |
---|
248 | c = Vertex (2.0,0.0) |
---|
249 | d = Vertex (0.0, 4.0) |
---|
250 | e = Vertex (2.0, 2.0) |
---|
251 | f = Vertex (4.0,0.0) |
---|
252 | g = Vertex (0.0,-2.0) |
---|
253 | |
---|
254 | Segment.set_default_tag("") |
---|
255 | s1 = Segment(a,b) |
---|
256 | s2 = Segment(b,e) |
---|
257 | s3 = Segment(e,c) |
---|
258 | s4 = Segment(c,a) |
---|
259 | s5 = Segment(b,d) |
---|
260 | s6 = Segment(e,d) |
---|
261 | s7 = Segment(e,f) |
---|
262 | s8 = Segment(c,f) |
---|
263 | s9 = Segment(g,c) |
---|
264 | s10 = Segment(g,a) |
---|
265 | |
---|
266 | r1 = Region(0.1,0.1,tag="22") |
---|
267 | r2 = Region(0.1,2.1,tag="11") |
---|
268 | r3 = Region(2.1,0.1) |
---|
269 | |
---|
270 | m = Mesh(userVertices=[a,b,c,d,e,f,g], userSegments=[s1,s2,s3,s4,s5,s6,s7,s8,s9,s10], regions=[r1,r2,r3] ) |
---|
271 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
272 | #print m |
---|
273 | Triangulation = m.getTriangulation() |
---|
274 | #print Triangulation[0].attribute |
---|
275 | #print Triangulation[1].attribute |
---|
276 | #print Triangulation[2].attribute |
---|
277 | #print Triangulation[3].attribute |
---|
278 | #print Triangulation[4].attribute |
---|
279 | |
---|
280 | self.failUnless(Triangulation[0].attribute == "" and |
---|
281 | Triangulation[1].attribute == "22" and |
---|
282 | Triangulation[2].attribute == "" and |
---|
283 | Triangulation[3].attribute == "11" and |
---|
284 | Triangulation[4].attribute == "22" , |
---|
285 | 'region attributes are wrong!') |
---|
286 | |
---|
287 | def test_vertexAttribs(self): |
---|
288 | a = Vertex (0.0, 0.0, attributes = [12.0,2.0]) |
---|
289 | d = Vertex (0.0, 4.0, attributes = [9.0,7.0]) |
---|
290 | f = Vertex (4.0,0.0, attributes = [14.0,3.0]) |
---|
291 | |
---|
292 | Segment.set_default_tag("") |
---|
293 | s1 = Segment(a,d) |
---|
294 | s2 = Segment(d,f) |
---|
295 | s3 = Segment(a,f) |
---|
296 | |
---|
297 | r1 = Region(0.3, 0.3, tag = 88.9) |
---|
298 | |
---|
299 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) |
---|
300 | |
---|
301 | m.generateMesh("QApz", maxArea = 2.1) |
---|
302 | |
---|
303 | vert = m.getMeshVertices() |
---|
304 | |
---|
305 | self.failUnless(vert[0].attributes == [12.0, 2.0] and |
---|
306 | vert[1].attributes == [9.0, 7.0] and |
---|
307 | vert[2].attributes == [14.0,3.0] and |
---|
308 | vert[3].attributes == [12.232233047033631, 4.4142135623730949] and |
---|
309 | vert[4].attributes == [13.0, 2.5] , |
---|
310 | 'vertex attributes are wrong!') |
---|
311 | |
---|
312 | |
---|
313 | def test_vertexAttribs2(self): |
---|
314 | |
---|
315 | a = Vertex (0.0, 0.0) |
---|
316 | d = Vertex (0.0, 4.0) |
---|
317 | f = Vertex (4.0,0.0) |
---|
318 | |
---|
319 | Segment.set_default_tag("") |
---|
320 | s1 = Segment(a,d) |
---|
321 | s2 = Segment(d,f) |
---|
322 | s3 = Segment(a,f) |
---|
323 | |
---|
324 | r1 = Region(0.3, 0.3, tag = 88.9) |
---|
325 | |
---|
326 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3], regions=[r1]) |
---|
327 | |
---|
328 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
329 | |
---|
330 | vert = m.getMeshVertices() |
---|
331 | self.failUnless(vert[0].attributes == [] and |
---|
332 | vert[1].attributes == [] and |
---|
333 | vert[2].attributes == [] and |
---|
334 | vert[3].attributes == [] and |
---|
335 | vert[4].attributes == [], |
---|
336 | 'vertex attributes are wrong!') |
---|
337 | |
---|
338 | def test_segmarker(self): |
---|
339 | |
---|
340 | a = Vertex (0.0, 0.0) |
---|
341 | d = Vertex (0.0, 4.0) |
---|
342 | f = Vertex (4.0,0.0) |
---|
343 | |
---|
344 | s1 = Segment(a,d,marker = 5) |
---|
345 | s2 = Segment(d,f,marker = 7) |
---|
346 | s3 = Segment(a,f,marker = 9) |
---|
347 | |
---|
348 | m = Mesh(userVertices=[a,d,f], userSegments=[s1,s2,s3]) |
---|
349 | |
---|
350 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
351 | |
---|
352 | seg = m.getMeshSegments() |
---|
353 | |
---|
354 | #print "seg[0].marker" |
---|
355 | #print seg[0].marker |
---|
356 | #print "seg[0].marker" |
---|
357 | self.failUnless(seg[0].marker == 5 and |
---|
358 | seg[1].marker == 7 and |
---|
359 | seg[2].marker == 9 and |
---|
360 | seg[3].marker == 7 and |
---|
361 | seg[4].marker == 9 and |
---|
362 | seg[5].marker == 7 and |
---|
363 | seg[6].marker == 5, |
---|
364 | 'seg markers are wrong') |
---|
365 | |
---|
366 | def test_segmarker2(self): |
---|
367 | |
---|
368 | a = Vertex (0.0, 0.0) |
---|
369 | d = Vertex (0.0, 4.0) |
---|
370 | f = Vertex (4.0,0.0) |
---|
371 | e = Vertex (1.0,1.0) |
---|
372 | |
---|
373 | s1 = Segment(a,d) |
---|
374 | s2 = Segment(d,f) |
---|
375 | s3 = Segment(a,f) |
---|
376 | s4 = Segment(a,e) |
---|
377 | |
---|
378 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
379 | |
---|
380 | m.generateMesh("QApz", maxArea = 2.1) |
---|
381 | |
---|
382 | seg = m.getMeshSegments() |
---|
383 | self.failUnless(seg[0].marker == "external" and |
---|
384 | seg[1].marker == "external" and |
---|
385 | seg[2].marker == "external" and |
---|
386 | seg[3].marker == "internal" and |
---|
387 | seg[4].marker == "external", |
---|
388 | '2nd seg markers are wrong') |
---|
389 | |
---|
390 | def test_asciiFile(self): |
---|
391 | |
---|
392 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
393 | d = Vertex (0.0, 4.0) #, attributes = [1.2]) |
---|
394 | f = Vertex (4.0,0.0) #, attributes = [1.3]) |
---|
395 | e = Vertex (1.0,1.0) #, attributes = [1.4]) |
---|
396 | |
---|
397 | s1 = Segment(a,d) |
---|
398 | s2 = Segment(d,f) |
---|
399 | s3 = Segment(a,f) |
---|
400 | s4 = Segment(a,e) |
---|
401 | |
---|
402 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
403 | |
---|
404 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
405 | |
---|
406 | seg = m.getMeshSegments() |
---|
407 | |
---|
408 | fileName = tempfile.mktemp(".txt") |
---|
409 | m.exportASCIItrianglulationfile(fileName) |
---|
410 | file = open(fileName) |
---|
411 | lFile = file.read().split('\n') |
---|
412 | file.close() |
---|
413 | os.remove(fileName) |
---|
414 | |
---|
415 | #print "@^@^" |
---|
416 | #for l in lFile: |
---|
417 | # print l,"<" |
---|
418 | #print "@^@^" |
---|
419 | |
---|
420 | self.failUnless(lFile[0] == "5 0 # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." |
---|
421 | , |
---|
422 | 'Ascii file is wrong, vertex title') |
---|
423 | self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and |
---|
424 | lFile[2] == "1 0.0 4.0 " and #1.2 " and |
---|
425 | lFile[3] == "2 4.0 0.0 " and #1.3 " and |
---|
426 | lFile[4] == "3 1.0 1.0 " and #1.4 " and |
---|
427 | lFile[5] == "4 2.0 2.0 " #1.25 " |
---|
428 | , |
---|
429 | 'Ascii file is wrong, vertex') |
---|
430 | |
---|
431 | self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." |
---|
432 | , |
---|
433 | 'Ascii file is wrong, attribute column title') |
---|
434 | self.failUnless(lFile[7] == "4 # <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." and |
---|
435 | lFile[8] == "0 3 2 4 -1 2 3 " and |
---|
436 | lFile[9] == "1 1 0 3 3 2 -1 " and |
---|
437 | lFile[10] == "2 3 4 1 -1 1 0 " and |
---|
438 | lFile[11] == "3 2 3 0 1 -1 0 " |
---|
439 | , |
---|
440 | 'Ascii file is wrong, triangle') |
---|
441 | |
---|
442 | self.failUnless(lFile[12] == "5 # <segment #> <vertex #> <vertex #> [boundary marker] ...Triangulation Segments..." and |
---|
443 | lFile[13] == "0 0 1 external" and |
---|
444 | lFile[14] == "1 1 4 external" and |
---|
445 | lFile[15] == "2 2 0 external" and |
---|
446 | lFile[16] == "3 0 3 internal" and |
---|
447 | lFile[17] == "4 4 2 external" , |
---|
448 | 'Ascii file is wrong, segment') |
---|
449 | |
---|
450 | self.failUnless(lFile[18] == '4 0 # <vertex #> <x> <y> [attributes] ...Mesh Vertices...', |
---|
451 | 'Ascii file is wrong, Mesh Vertices Title') |
---|
452 | |
---|
453 | self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and |
---|
454 | lFile[20] == '1 0.0 4.0 ' and #1.2 ' and |
---|
455 | lFile[21] == '2 4.0 0.0 ' and #1.3 ' and |
---|
456 | lFile[22] == '3 1.0 1.0 ' #1.4 ' |
---|
457 | , |
---|
458 | 'Ascii file is wrong, Mesh Vertices II') |
---|
459 | |
---|
460 | self.failUnless(lFile[23] == '4 # <segment #> <vertex #> <vertex #> [boundary marker] ...Mesh Segments...' and |
---|
461 | lFile[24] == '0 0 1 ' and |
---|
462 | lFile[25] == '1 1 2 ' and |
---|
463 | lFile[26] == '2 0 2 ' and |
---|
464 | lFile[27] == '3 0 3 ' and |
---|
465 | lFile[28] == '0 # <Hole #> <x> <y> ...Mesh Holes...' and |
---|
466 | lFile[29] == '0 # <Region #> <x> <y> <tag>...Mesh Regions...' |
---|
467 | , |
---|
468 | 'Ascii file is wrong, Mesh Segments') |
---|
469 | |
---|
470 | |
---|
471 | def test_ascii_file(self): |
---|
472 | |
---|
473 | a = Vertex (0.0, 0.0) #, attributes = [1.1]) |
---|
474 | d = Vertex (0.0, 4.0) #, attributes = [1.2]) |
---|
475 | f = Vertex (4.0,0.0) #, attributes = [1.3]) |
---|
476 | e = Vertex (1.0,1.0) #, attributes = [1.4]) |
---|
477 | |
---|
478 | s1 = Segment(a,d) |
---|
479 | s2 = Segment(d,f) |
---|
480 | s3 = Segment(a,f) |
---|
481 | s4 = Segment(a,e) |
---|
482 | |
---|
483 | m = Mesh(userVertices=[a,d,f,e], userSegments=[s1,s2,s3,s4]) |
---|
484 | |
---|
485 | m.generateMesh("QApz", maxArea = 2.1 ) |
---|
486 | |
---|
487 | seg = m.getMeshSegments() |
---|
488 | |
---|
489 | fileName = tempfile.mktemp(".txt") |
---|
490 | m.exportASCIItrianglulationfile(fileName) |
---|
491 | file = open(fileName) |
---|
492 | lFile = file.read().split('\n') |
---|
493 | file.close() |
---|
494 | os.remove(fileName) |
---|
495 | |
---|
496 | #print "@^@^" |
---|
497 | #for l in lFile: |
---|
498 | # print l,"<" |
---|
499 | #print "@^@^" |
---|
500 | self.failUnless(lFile[0] == "5 0 # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." |
---|
501 | , |
---|
502 | 'Ascii file is wrong, vertex title') |
---|
503 | self.failUnless(lFile[1] == "0 0.0 0.0 " and #1.1 " and |
---|
504 | lFile[2] == "1 0.0 4.0 " and #1.2 " and |
---|
505 | lFile[3] == "2 4.0 0.0 " and #1.3 " and |
---|
506 | lFile[4] == "3 1.0 1.0 " and #1.4 " and |
---|
507 | lFile[5] == "4 2.0 2.0 " #1.25 " |
---|
508 | , |
---|
509 | 'Ascii file is wrong, vertex') |
---|
510 | |
---|
511 | self.failUnless(lFile[6] == "# attribute column titles ...Triangulation Vertex Titles..." |
---|
512 | , |
---|
513 | 'Ascii file is wrong, attribute column title') |
---|
514 | self.failUnless(lFile[7] == "4 # <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." and |
---|
515 | lFile[8] == "0 3 2 4 -1 2 3 " and |
---|
516 | lFile[9] == "1 1 0 3 3 2 -1 " and |
---|
517 | lFile[10] == "2 3 4 1 -1 1 0 " and |
---|
518 | lFile[11] == "3 2 3 0 1 -1 0 " |
---|
519 | , |
---|
520 | 'Ascii file is wrong, triangle') |
---|
521 | |
---|
522 | self.failUnless(lFile[12] == "5 # <segment #> <vertex #> <vertex #> [boundary marker] ...Triangulation Segments..." and |
---|
523 | lFile[13] == "0 0 1 external" and |
---|
524 | lFile[14] == "1 1 4 external" and |
---|
525 | lFile[15] == "2 2 0 external" and |
---|
526 | lFile[16] == "3 0 3 internal" and |
---|
527 | lFile[17] == "4 4 2 external" , |
---|
528 | 'Ascii file is wrong, segment') |
---|
529 | |
---|
530 | self.failUnless(lFile[18] == '4 0 # <vertex #> <x> <y> [attributes] ...Mesh Vertices...', |
---|
531 | 'Ascii file is wrong, Mesh Vertices Title') |
---|
532 | |
---|
533 | self.failUnless(lFile[19] == '0 0.0 0.0 ' and #1.1 ' and |
---|
534 | lFile[20] == '1 0.0 4.0 ' and #1.2 ' and |
---|
535 | lFile[21] == '2 4.0 0.0 ' and #1.3 ' and |
---|
536 | lFile[22] == '3 1.0 1.0 ' #1.4 ' |
---|
537 | , |
---|
538 | 'Ascii file is wrong, Mesh Vertices II') |
---|
539 | |
---|
540 | self.failUnless(lFile[23] == '4 # <segment #> <vertex #> <vertex #> [boundary marker] ...Mesh Segments...' and |
---|
541 | lFile[24] == '0 0 1 ' and |
---|
542 | lFile[25] == '1 1 2 ' and |
---|
543 | lFile[26] == '2 0 2 ' and |
---|
544 | lFile[27] == '3 0 3 ' and |
---|
545 | lFile[28] == '0 # <Hole #> <x> <y> ...Mesh Holes...' and |
---|
546 | lFile[29] == '0 # <Region #> <x> <y> <tag>...Mesh Regions...' |
---|
547 | , |
---|
548 | 'Ascii file is wrong, Mesh Segments') |
---|
549 | |
---|
550 | |
---|
551 | def test_thinoutVertices(self): |
---|
552 | |
---|
553 | v1 = Vertex(-20,-20) |
---|
554 | v2 = Vertex(-11,-11) |
---|
555 | v3 = Vertex(-10,-10) |
---|
556 | v4 = Vertex(-9,-1) |
---|
557 | v5 = Vertex(-8,2) |
---|
558 | v6 = Vertex(6,3) |
---|
559 | v7 = Vertex(12,9) |
---|
560 | v8 = Vertex(15,3) |
---|
561 | v9 = Vertex(24,3) |
---|
562 | m = Mesh(userVertices = [v1,v2,v3,v4,v5,v6,v7,v8,v9]) |
---|
563 | m.thinoutVertices(10) |
---|
564 | |
---|
565 | self.failUnless(v1 in m.userVertices, |
---|
566 | 'test_thinoutVertices, test 1 failed') |
---|
567 | self.failUnless(v3 in m.userVertices, |
---|
568 | 'test_thinoutVertices, test 2 failed') |
---|
569 | self.failUnless(v4 in m.userVertices, |
---|
570 | 'test_thinoutVertices, test 3 failed') |
---|
571 | self.failUnless(v6 in m.userVertices, |
---|
572 | 'test_thinoutVertices, test 4 failed') |
---|
573 | self.failUnless(v7 in m.userVertices, |
---|
574 | 'test_thinoutVertices, test 5 failed') |
---|
575 | self.failUnless(v9 in m.userVertices, |
---|
576 | 'test_thinoutVertices, test 6 failed') |
---|
577 | self.failUnless(v5 not in m.userVertices, |
---|
578 | 'test_thinoutVertices, test 7 failed') |
---|
579 | self.failUnless(v2 not in m.userVertices, |
---|
580 | 'test_thinoutVertices, test 8 failed') |
---|
581 | self.failUnless(v8 not in m.userVertices, |
---|
582 | 'test_thinoutVertices, test 9 failed') |
---|
583 | |
---|
584 | def test_same_x_y(self): |
---|
585 | v = Point(7,8) |
---|
586 | f = Point(7,8) |
---|
587 | f.same_x_y(v) |
---|
588 | |
---|
589 | self.failUnless(f.same_x_y(v), |
---|
590 | 'same_x_y True failed') |
---|
591 | e = Point(7,9) |
---|
592 | self.failUnless(not f.same_x_y(e), |
---|
593 | 'same_x_y False failed') |
---|
594 | |
---|
595 | def test_import_mesh(self): |
---|
596 | |
---|
597 | a_att = [5.0,2.0] |
---|
598 | d_att =[4.0,2.0] |
---|
599 | f_att = [3.0,2.0] |
---|
600 | e_att = [2.0,2.0] |
---|
601 | a_xy = [0.0, 0.0] |
---|
602 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
603 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
604 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
605 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
606 | |
---|
607 | s1 = Segment(a,d, marker = "50") |
---|
608 | s2 = Segment(d,f, marker = "40") |
---|
609 | s3 = Segment(a,f, marker = "30") |
---|
610 | s4 = Segment(a,e, marker = "20") |
---|
611 | |
---|
612 | r1 = Region(0.3, 0.3,tag = "1.3") |
---|
613 | m = Mesh(userVertices=[a,d,f,e], |
---|
614 | userSegments=[s1,s2,s3,s4], |
---|
615 | regions=[r1]) |
---|
616 | |
---|
617 | m.generateMesh("QApz", maxArea = 2.1) |
---|
618 | fileName = tempfile.mktemp(".tsh") |
---|
619 | #print "dgs!!!" |
---|
620 | #print "****************** fileName", fileName |
---|
621 | m.exportASCIItrianglulationfile(fileName) |
---|
622 | print "******************" |
---|
623 | print "m", m |
---|
624 | print "******************" |
---|
625 | m_returned = importMeshFromFile(fileName) |
---|
626 | print "m_returned",m_returned |
---|
627 | print "******************" |
---|
628 | #print "****************** fileName", fileName |
---|
629 | os.remove(fileName) |
---|
630 | self.failUnless(0 == m.__cmp__(m_returned), |
---|
631 | 'loading and saving of a mesh failed') |
---|
632 | |
---|
633 | |
---|
634 | def test_normaliseMesh(self): |
---|
635 | |
---|
636 | a_att = [5.0,2.0] |
---|
637 | d_att =[4.0,2.0] |
---|
638 | f_att = [3.0,2.0] |
---|
639 | e_att = [2.0,2.0] |
---|
640 | a_xy = [10.0, 10.0] |
---|
641 | a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) |
---|
642 | d = Vertex (15.0, 10.0, attributes =d_att) |
---|
643 | f = Vertex (10.0,20.0, attributes =f_att) |
---|
644 | e = Vertex (15.0,20.0, attributes =e_att) |
---|
645 | |
---|
646 | s1 = Segment(a,d, marker = 50) |
---|
647 | s2 = Segment(d,e, marker = 40) |
---|
648 | s3 = Segment(e,f, marker = 30) |
---|
649 | s4 = Segment(f,a, marker = 20) |
---|
650 | |
---|
651 | r1 = Region(0.3, 0.3,tag = 1.3) |
---|
652 | m = Mesh(userVertices=[a,d,f,e], |
---|
653 | userSegments=[s1,s2,s3,s4], |
---|
654 | regions=[r1]) |
---|
655 | m.normaliseMesh(1,0,1) |
---|
656 | [xmin, ymin, xmax, ymax] = m.boxsize() |
---|
657 | [attmin, attmax] = m.maxMinVertAtt(0) |
---|
658 | self.failUnless(attmin == 0.0 and attmax == 1.0, |
---|
659 | 'normalise failed') |
---|
660 | self.failUnless(xmin == 0.0 and ymin == 0.0 and xmax == 0.5 and ymax == 1.0, |
---|
661 | 'normalise failed') |
---|
662 | m.normaliseMesh(200,-100,5) |
---|
663 | [xmin, ymin, xmax, ymax] = m.boxsize() |
---|
664 | [attmin, attmax] = m.maxMinVertAtt(0) |
---|
665 | self.failUnless(attmin == 0.0 and attmax == 5.0, |
---|
666 | 'normalise failed') |
---|
667 | self.failUnless(xmin == -100.0 and ymin == -100.0 and xmax == 0.0 and ymax == 100.0, |
---|
668 | 'normalise failed') |
---|
669 | |
---|
670 | def test_exportASCIIsegmentoutlinefile(self): |
---|
671 | a = Vertex (0,0) |
---|
672 | b = Vertex (0,3) |
---|
673 | c = Vertex (3,3) |
---|
674 | d = Vertex (1,2) |
---|
675 | e = Vertex (3,1) |
---|
676 | |
---|
677 | s1 = Segment(a,b, marker = "50") |
---|
678 | s2 = Segment(b,c, marker = "40") |
---|
679 | s3 = Segment(c,a, marker = "30") |
---|
680 | |
---|
681 | r1 = Region(2, 1,tag = "1.3") |
---|
682 | h1 = Hole(1,4) |
---|
683 | m = Mesh(userVertices=[a,b,c,d,e], |
---|
684 | userSegments=[s1,s2,s3], |
---|
685 | regions=[r1], |
---|
686 | holes = [h1]) |
---|
687 | |
---|
688 | m.generateMesh("QApz", maxArea = 2.1) |
---|
689 | #print "mesh ***************dsg*", m |
---|
690 | |
---|
691 | fileName = tempfile.mktemp(".tsh") |
---|
692 | m.exportASCIIsegmentoutlinefile(fileName) |
---|
693 | |
---|
694 | m_returned = importMeshFromFile(fileName) |
---|
695 | |
---|
696 | #print "m_returned ****",m_returned |
---|
697 | #print "****************** fileName", fileName |
---|
698 | os.remove(fileName) |
---|
699 | |
---|
700 | #Trim mesh, so it should like like m_returned |
---|
701 | m.meshVertices = [] |
---|
702 | m.meshTriangles = [] |
---|
703 | m.meshSegments = [] |
---|
704 | m.userVertices=[a,b,c] |
---|
705 | #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) |
---|
706 | self.failUnless(0 == m.__cmp__(m), |
---|
707 | 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') |
---|
708 | self.failUnless(0 == m.__cmp__(m_returned), |
---|
709 | 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') |
---|
710 | |
---|
711 | |
---|
712 | def test_exportxyafile(self): |
---|
713 | a = Vertex (0,0) |
---|
714 | b = Vertex (0,3) |
---|
715 | c = Vertex (3,3) |
---|
716 | d = Vertex (1,2) |
---|
717 | e = Vertex (3,1) |
---|
718 | f = Vertex (3,1) |
---|
719 | |
---|
720 | s1 = Segment(a,b, marker = 50) |
---|
721 | s2 = Segment(b,c, marker = 40) |
---|
722 | s3 = Segment(c,a, marker = 30) |
---|
723 | |
---|
724 | r1 = Region(2, 1,tag = 1.3) |
---|
725 | h1 = Hole(1,4) |
---|
726 | # Warning mesh can't produce this type of data structure its self |
---|
727 | m = Mesh(userVertices=[a,b,c,d,e], |
---|
728 | userSegments=[s1,s2,s3], |
---|
729 | regions=[r1], |
---|
730 | holes = [h1]) |
---|
731 | |
---|
732 | fileName = tempfile.mktemp(".txt") |
---|
733 | m.exportxyafile(fileName) |
---|
734 | file = open(fileName) |
---|
735 | lFile = file.read().split('\n') |
---|
736 | file.close() |
---|
737 | |
---|
738 | #print "**********************" |
---|
739 | #print lFile |
---|
740 | #print "**********************" |
---|
741 | |
---|
742 | os.remove(fileName) |
---|
743 | self.failUnless(lFile[0] == "5 0 # <vertex #> <x> <y> [attributes]" and |
---|
744 | lFile[1] == "0 0 " and |
---|
745 | lFile[2] == "0 3 " and |
---|
746 | lFile[3] == "3 3 " and |
---|
747 | lFile[4] == "1 2 " and |
---|
748 | lFile[5] == "3 1 " |
---|
749 | , |
---|
750 | 'exported Ascii xya file is wrong') |
---|
751 | |
---|
752 | m.generateMesh("QApz", maxArea = 2.1) |
---|
753 | fileName = tempfile.mktemp(".txt") |
---|
754 | m.exportxyafile(fileName) |
---|
755 | file = open(fileName) |
---|
756 | lFile = file.read().split('\n') |
---|
757 | file.close() |
---|
758 | os.remove(fileName) |
---|
759 | self.failUnless(lFile[0] == "6 0 # <vertex #> <x> <y> [attributes]" and |
---|
760 | lFile[1] == "0.0 0.0 " and |
---|
761 | lFile[2] == "0.0 3.0 " and |
---|
762 | lFile[3] == "3.0 3.0 " and |
---|
763 | lFile[4] == "1.0 2.0 " and |
---|
764 | lFile[5] == "3.0 1.0 " and |
---|
765 | lFile[6] == "1.5 1.5 " |
---|
766 | , |
---|
767 | 'exported Ascii xya file is wrong') |
---|
768 | |
---|
769 | def test_strings2ints(self): |
---|
770 | list = ["sea","river inlet","","sea","","moat"] |
---|
771 | preset = ["moat", "internal boundary"] |
---|
772 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
773 | self.failUnless(intlist == [2,3 ,0 ,2 ,0 ,0 ] |
---|
774 | , |
---|
775 | 'test_strings2ints produces bad intlist') |
---|
776 | self.failUnless(converter == ['moat', 'internal boundary', |
---|
777 | 'sea', 'river inlet'] |
---|
778 | , |
---|
779 | 'test_strings2ints produces bad converter') |
---|
780 | |
---|
781 | def test_ints2strings(self): |
---|
782 | list = ["internal boundary","sea","river inlet", |
---|
783 | "","sea","","moat","internal boundary"] |
---|
784 | outlist = ['internal boundary', 'sea', 'river inlet', 'moat', |
---|
785 | 'sea', 'moat', 'moat', 'internal boundary'] |
---|
786 | preset = ["moat", "internal boundary"] |
---|
787 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
788 | newlist = segment_ints2strings(intlist, converter) |
---|
789 | self.failUnless(outlist == newlist |
---|
790 | , |
---|
791 | 'test_strings2ints produces bad intlist') |
---|
792 | self.failUnless(converter == ['moat', 'internal boundary', |
---|
793 | 'sea', 'river inlet'] |
---|
794 | , |
---|
795 | 'test_strings2ints produces bad converter') |
---|
796 | |
---|
797 | def test_ints2strings2(self): |
---|
798 | list = ["","",""] |
---|
799 | preset = ["moat", "internal boundary"] |
---|
800 | [intlist, converter] = segment_strings2ints(list,preset ) |
---|
801 | newlist = segment_ints2strings(intlist, converter) |
---|
802 | outlist = ['moat', 'moat', 'moat'] |
---|
803 | self.failUnless(outlist == newlist |
---|
804 | , |
---|
805 | 'test_strings2ints produces bad intlist') |
---|
806 | self.failUnless(converter == ['moat', 'internal boundary'] |
---|
807 | , |
---|
808 | 'test_strings2ints produces bad converter') |
---|
809 | |
---|
810 | |
---|
811 | def test_removeDuplicatedVertices(self): |
---|
812 | a = Vertex (0,0) |
---|
813 | a.index = 0 |
---|
814 | b = Vertex (0,3) |
---|
815 | b.index = 1 |
---|
816 | c = Vertex (3,3) |
---|
817 | c.index = 2 |
---|
818 | d = Vertex (1,1) |
---|
819 | d.index = 3 |
---|
820 | e = Vertex (3,1) |
---|
821 | e.index = 4 |
---|
822 | f = Vertex (1,1) |
---|
823 | f.index = 5 |
---|
824 | g = Vertex (1,1) |
---|
825 | g.index = 6 |
---|
826 | inputVerts_noDups = [a,b,c,d,e] |
---|
827 | |
---|
828 | m = Mesh(userVertices=[a,b,c,d,e,f,g]) |
---|
829 | counter = m.removeDuplicatedUserVertices() |
---|
830 | UserVerts = m.getUserVertices() |
---|
831 | |
---|
832 | |
---|
833 | self.failUnless(UserVerts == inputVerts_noDups, |
---|
834 | 'duplicate verts not removed') |
---|
835 | #for userVert, inputVert in map(None, UserVerts, inputVerts_noDups): |
---|
836 | # self.failUnless(userVert.x == inputVert.x, |
---|
837 | # 'x duplicate verts not removed') |
---|
838 | # self.failUnless(userVert.y == inputVert.y, |
---|
839 | # 'y duplicate verts not removed') |
---|
840 | |
---|
841 | #------------------------------------------------------------- |
---|
842 | if __name__ == "__main__": |
---|
843 | suite = unittest.makeSuite(meshTestCase,'test') |
---|
844 | runner = unittest.TextTestRunner() |
---|
845 | runner.run(suite) |
---|
846 | |
---|