1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | #FIXME (Ole): I think we need a unit test of write_ASCII_trianglulation |
---|
5 | # in order to quash bug in merimbula data |
---|
6 | # - to quash the bug need a raised error in pyvolution for reading xya files |
---|
7 | # when the title attribute information is not consistent with the data in the |
---|
8 | # file - DSG |
---|
9 | |
---|
10 | import sys |
---|
11 | sys.path.append('..') |
---|
12 | |
---|
13 | import tempfile |
---|
14 | import unittest |
---|
15 | from mesh import * |
---|
16 | from loadASCII import * |
---|
17 | from Numeric import array |
---|
18 | |
---|
19 | class loadASCIITestCase(unittest.TestCase): |
---|
20 | def setUp(self): |
---|
21 | pass |
---|
22 | |
---|
23 | def tearDown(self): |
---|
24 | pass |
---|
25 | |
---|
26 | |
---|
27 | def test_import_trianglulation(self): |
---|
28 | |
---|
29 | a_att = [5,2] |
---|
30 | d_att =[4,2] |
---|
31 | f_att = [3,2] |
---|
32 | e_att = [2,2] |
---|
33 | a_xy = [0.0, 0.0] |
---|
34 | a = Vertex ( a_xy[0],a_xy[1]) #, attributes =a_att) |
---|
35 | d = Vertex (0.0, 4.0) #, attributes =d_att) |
---|
36 | f = Vertex (4.0,0.0) #, attributes =f_att) |
---|
37 | e = Vertex (1.0,1.0) #, attributes =e_att) |
---|
38 | |
---|
39 | s1 = Segment(a,d, marker = "50") |
---|
40 | s2 = Segment(d,f, marker = "40") |
---|
41 | s3 = Segment(a,f, marker = "30") |
---|
42 | s4 = Segment(a,e, marker = "20") |
---|
43 | |
---|
44 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
45 | m = Mesh(userVertices=[a,d,f,e], |
---|
46 | userSegments=[s1,s2,s3,s4], |
---|
47 | regions=[r1]) |
---|
48 | |
---|
49 | m.generateMesh("QApza2.1") |
---|
50 | |
---|
51 | seg = m.getMeshSegments() |
---|
52 | points = m.getMeshVertices() |
---|
53 | |
---|
54 | fileName = tempfile.mktemp(".txt") |
---|
55 | #print "fileName",fileName |
---|
56 | m.exportASCIItrianglulationfile(fileName) |
---|
57 | |
---|
58 | dict = import_trianglulation(fileName) |
---|
59 | os.remove(fileName) |
---|
60 | |
---|
61 | #print dict |
---|
62 | #print "seg",seg |
---|
63 | for pimport,pactual,pimpatt in map(None,dict['generatedpointlist'],points,dict['generatedpointattributelist']): |
---|
64 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
65 | 'loadASCIITestCase failed. test 1') |
---|
66 | self.failUnless( pimpatt == pactual.attributes, |
---|
67 | 'loadASCIITestCase failed. test 1.1') |
---|
68 | self.failUnless( dict['segmentlist'][0] == [0,1], |
---|
69 | 'loadASCIITestCase failed. test 3') |
---|
70 | for segimp,segactual in map(None,dict['generatedsegmentmarkerlist'],seg): |
---|
71 | self.failUnless( segimp == segactual.marker, |
---|
72 | 'loadASCIITestCase failed. test 4') |
---|
73 | self.failUnless( dict['generatedtrianglelist'][0] == [3,2,4], |
---|
74 | 'loadASCIITestCase failed. test 5') |
---|
75 | self.failUnless( dict['generatedtriangleneighborlist'][0] == [-1,2,3], |
---|
76 | 'loadASCIITestCase failed. test 6') |
---|
77 | self.failUnless( dict['generatedtriangleattributelist'][0] == ["1.3"], |
---|
78 | 'loadASCIITestCase failed. test 7') |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | def test_import_mesh(self): |
---|
83 | |
---|
84 | a_att = [5,2] |
---|
85 | d_att =[4,2] |
---|
86 | f_att = [3,2] |
---|
87 | e_att = [2,2] |
---|
88 | a_xy = [0.0, 0.0] |
---|
89 | a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) |
---|
90 | d = Vertex (0.0, 4.0, attributes =d_att) |
---|
91 | f = Vertex (4.0,0.0, attributes =f_att) |
---|
92 | e = Vertex (1.0,1.0, attributes =e_att) |
---|
93 | |
---|
94 | s1 = Segment(a,d, marker = "50") |
---|
95 | s2 = Segment(d,f, marker = "40") |
---|
96 | s3 = Segment(a,f, marker = "30") |
---|
97 | s4 = Segment(a,e, marker = "20") |
---|
98 | |
---|
99 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 36) |
---|
100 | |
---|
101 | |
---|
102 | h1 = Hole(0.2,0.6) |
---|
103 | |
---|
104 | m = Mesh(userVertices=[a,d,f,e], |
---|
105 | userSegments=[s1,s2,s3,s4], |
---|
106 | regions=[r1], |
---|
107 | holes=[h1]) |
---|
108 | |
---|
109 | seg = m.getUserSegments() |
---|
110 | points = m.getUserVertices() |
---|
111 | holes = m.getHoles() |
---|
112 | regions = m.getRegions() |
---|
113 | fileName = tempfile.mktemp(".txt") |
---|
114 | m.exportASCIImeshfile(fileName) |
---|
115 | #print "***************************fileName", fileName |
---|
116 | dict = import_mesh(fileName) |
---|
117 | os.remove(fileName) |
---|
118 | |
---|
119 | #print dict |
---|
120 | |
---|
121 | for pimport,pactual,pimpatt in map(None,dict['pointlist'],points,dict['pointattributelist']): |
---|
122 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
123 | 'loadASCIITestCase failed. test 1') |
---|
124 | self.failUnless( pimpatt == pactual.attributes, |
---|
125 | 'loadASCIITestCase failed. test 1.1') |
---|
126 | self.failUnless( dict['segmentlist'][0] == [0,1], |
---|
127 | 'loadASCIITestCase failed. test 3') |
---|
128 | for segimp,segactual in map(None,dict['segmentmarkerlist'],seg): |
---|
129 | self.failUnless( segimp == segactual.marker, |
---|
130 | 'loadASCIITestCase failed. test 4') |
---|
131 | for holeimp,holeactual in map(None,dict['holelist'],holes): |
---|
132 | self.failUnless( holeimp == [holeactual.x,holeactual.y], |
---|
133 | 'loadASCIITestCase failed. test 5') |
---|
134 | for regimp,regactual,regattimp, regmaxarea in map(None,dict['regionlist'],regions, dict['regionattributelist'], dict['regionmaxarealist']): |
---|
135 | self.failUnless( regimp == [regactual.x,regactual.y], |
---|
136 | 'loadASCIITestCase failed. test 6') |
---|
137 | self.failUnless( regattimp == regactual.getTag(), |
---|
138 | 'loadASCIITestCase failed. test 7') |
---|
139 | self.failUnless( regmaxarea == regactual.getMaxArea(), |
---|
140 | 'loadASCIITestCase failed. test 7') |
---|
141 | |
---|
142 | |
---|
143 | def test_export_trianglulation_file(self): |
---|
144 | import os |
---|
145 | import tempfile |
---|
146 | |
---|
147 | meshDict = {} |
---|
148 | meshDict['generatedpointlist'] = [(0.0, 0.0), (0.0, 4.0), |
---|
149 | (4.0, 0.0), (1.0, 1.0), (2.0, 2.0)] |
---|
150 | meshDict['generatedtrianglelist'] = [(3, 2, 4), (1, 0, 3), |
---|
151 | (3, 4,1), (2, 3, 0)] |
---|
152 | meshDict['generatedsegmentlist'] = [(0, 1), (1, 4), (2, 0), |
---|
153 | (0, 3), (4, 2)] |
---|
154 | meshDict['generatedtriangleattributelist'] = ['1.3', '1.3', |
---|
155 | '1.3', '1.3'] |
---|
156 | meshDict['generatedpointattributelist'] = [[1.2,2.], [1.2,2.], [1.2,2.], [1.2,2.], [1.2,3.]] |
---|
157 | meshDict['generatedtriangleneighborlist'] = [[-1, 2, 3], [3, 2, -1], |
---|
158 | [-1, 1, 0], [1, -1, 0]] |
---|
159 | meshDict['generatedsegmentmarkerlist'] = ['50', '40', '30', '20', '40'] |
---|
160 | meshDict['generatedpointattributetitlelist'] = ['bed elevation', 'height'] |
---|
161 | |
---|
162 | fileName = tempfile.mktemp(".tsh") |
---|
163 | export_trianglulation_file(fileName, meshDict) |
---|
164 | loadedDict = import_trianglulation(fileName) |
---|
165 | |
---|
166 | #print "*(*( meshDict" |
---|
167 | #print meshDict |
---|
168 | #print "*(*( loadedDcit" |
---|
169 | #print loadedDict |
---|
170 | #print "*(*(" |
---|
171 | |
---|
172 | self.failUnless(array(meshDict['generatedpointlist']) == |
---|
173 | array(loadedDict['generatedpointlist']), |
---|
174 | 'test_export_trianglulation_file failed. Test 1') |
---|
175 | self.failUnless(array(meshDict['generatedtrianglelist']) == |
---|
176 | array(loadedDict['generatedtrianglelist']), |
---|
177 | 'test_export_trianglulation_file failed. Test 2') |
---|
178 | self.failUnless(array(meshDict['generatedsegmentlist']) == |
---|
179 | array(loadedDict['generatedsegmentlist']), |
---|
180 | 'test_export_trianglulation_file failed. Test 3') |
---|
181 | self.failUnless(array(meshDict['generatedtriangleattributelist']) == |
---|
182 | array(loadedDict['generatedtriangleattributelist']), |
---|
183 | 'test_export_trianglulation_file failed. Test 4') |
---|
184 | |
---|
185 | self.failUnless(meshDict['generatedpointattributelist'] == |
---|
186 | loadedDict['generatedpointattributelist'], |
---|
187 | 'test_export_trianglulation_file failed. Test 5') |
---|
188 | self.failUnless(array(meshDict['generatedtriangleneighborlist']) == |
---|
189 | array(loadedDict['generatedtriangleneighborlist']), |
---|
190 | 'test_export_trianglulation_file failed. Test 6') |
---|
191 | self.failUnless(array(meshDict['generatedsegmentmarkerlist']) == |
---|
192 | array(loadedDict['generatedsegmentmarkerlist']), |
---|
193 | 'test_export_trianglulation_file failed. Test 7') |
---|
194 | self.failUnless(array(meshDict['generatedpointattributetitlelist']) == |
---|
195 | array(loadedDict['generatedpointattributetitlelist']), |
---|
196 | 'test_export_trianglulation_file failed. Test 8') |
---|
197 | |
---|
198 | #os.remove(fileName) |
---|
199 | |
---|
200 | def test_loadxy(self): |
---|
201 | import os |
---|
202 | import tempfile |
---|
203 | |
---|
204 | fileName = tempfile.mktemp(".xya") |
---|
205 | file = open(fileName,"w") |
---|
206 | file.write("title row \n\ |
---|
207 | 1.0 0.0 10.0 0.0\n\ |
---|
208 | 0.0 1.0 0.0 10.0\n\ |
---|
209 | 1.0 0.0 10.4 40.0\n") |
---|
210 | file.close() |
---|
211 | #print fileName |
---|
212 | m = importMeshFromFile(fileName) |
---|
213 | os.remove(fileName) |
---|
214 | self.failUnless(m.userVertices[0].x == 1.0, |
---|
215 | 'loadxy, test 1 failed') |
---|
216 | self.failUnless(m.userVertices[0].y == 0.0, |
---|
217 | 'loadxy, test 2 failed') |
---|
218 | #self.failUnless(m.userVertices[0].attributes == [10.0,0.0], |
---|
219 | # 'loadxy, test 2.2 failed') |
---|
220 | self.failUnless(m.userVertices[1].x == 0.0, |
---|
221 | 'loadxy, test 3 failed') |
---|
222 | self.failUnless(m.userVertices[1].y == 1.0, |
---|
223 | 'loadxy, test 4 failed') |
---|
224 | #self.failUnless(m.userVertices[1].attributes == [0.0,10.0], |
---|
225 | # 'loadxy, test 5 failed') |
---|
226 | #------------------------------------------------------------- |
---|
227 | if __name__ == "__main__": |
---|
228 | |
---|
229 | suite = unittest.makeSuite(loadASCIITestCase,'test') |
---|
230 | runner = unittest.TextTestRunner() |
---|
231 | runner.run(suite) |
---|
232 | |
---|