[4] | 1 | #!/usr/bin/env python |
---|
| 2 | # |
---|
| 3 | |
---|
| 4 | import sys |
---|
| 5 | sys.path.append('..') |
---|
| 6 | |
---|
| 7 | import tempfile |
---|
| 8 | import unittest |
---|
| 9 | from mesh import * |
---|
| 10 | from loadASCII import * |
---|
| 11 | |
---|
| 12 | class loadASCIITestCase(unittest.TestCase): |
---|
| 13 | def setUp(self): |
---|
| 14 | pass |
---|
| 15 | |
---|
| 16 | def tearDown(self): |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | def test_import_trianglulation(self): |
---|
| 21 | |
---|
| 22 | a_att = [5,2] |
---|
| 23 | d_att =[4,2] |
---|
| 24 | f_att = [3,2] |
---|
| 25 | e_att = [2,2] |
---|
| 26 | a_xy = [0.0, 0.0] |
---|
| 27 | a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) |
---|
| 28 | d = Vertex (0.0, 4.0, attributes =d_att) |
---|
| 29 | f = Vertex (4.0,0.0, attributes =f_att) |
---|
| 30 | e = Vertex (1.0,1.0, attributes =e_att) |
---|
| 31 | |
---|
| 32 | s1 = Segment(a,d, marker = "50") |
---|
| 33 | s2 = Segment(d,f, marker = "40") |
---|
| 34 | s3 = Segment(a,f, marker = "30") |
---|
| 35 | s4 = Segment(a,e, marker = "20") |
---|
| 36 | |
---|
| 37 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 45) |
---|
| 38 | m = Mesh(userVertices=[a,d,f,e], |
---|
| 39 | userSegments=[s1,s2,s3,s4], |
---|
| 40 | regions=[r1]) |
---|
| 41 | |
---|
| 42 | m.generateMesh("QApza2.1") |
---|
| 43 | |
---|
| 44 | seg = m.getMeshSegments() |
---|
| 45 | points = m.getMeshVertices() |
---|
| 46 | |
---|
| 47 | fileName = tempfile.mktemp(".txt") |
---|
| 48 | #print "fileName",fileName |
---|
| 49 | m.exportASCIItrianglulationfile(fileName) |
---|
| 50 | |
---|
| 51 | dict = import_trianglulation(fileName) |
---|
| 52 | os.remove(fileName) |
---|
| 53 | |
---|
| 54 | #print dict |
---|
| 55 | #print "seg",seg |
---|
| 56 | for pimport,pactual,pimpatt in map(None,dict['generatedpointlist'],points,dict['generatedpointattributelist']): |
---|
| 57 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
| 58 | 'loadASCIITestCase failed. test 1') |
---|
| 59 | self.failUnless( pimpatt == pactual.attributes, |
---|
| 60 | 'loadASCIITestCase failed. test 1.1') |
---|
| 61 | self.failUnless( dict['segmentlist'][0] == [0,1], |
---|
| 62 | 'loadASCIITestCase failed. test 3') |
---|
| 63 | for segimp,segactual in map(None,dict['generatedsegmentmarkerlist'],seg): |
---|
| 64 | self.failUnless( segimp == segactual.marker, |
---|
| 65 | 'loadASCIITestCase failed. test 4') |
---|
| 66 | self.failUnless( dict['generatedtrianglelist'][0] == [3,2,4], |
---|
| 67 | 'loadASCIITestCase failed. test 5') |
---|
| 68 | self.failUnless( dict['generatedtriangleneighborlist'][0] == [-1,2,3], |
---|
| 69 | 'loadASCIITestCase failed. test 6') |
---|
| 70 | self.failUnless( dict['generatedtriangleattributelist'][0] == ["1.3"], |
---|
| 71 | 'loadASCIITestCase failed. test 7') |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | def test_import_mesh(self): |
---|
| 76 | |
---|
| 77 | a_att = [5,2] |
---|
| 78 | d_att =[4,2] |
---|
| 79 | f_att = [3,2] |
---|
| 80 | e_att = [2,2] |
---|
| 81 | a_xy = [0.0, 0.0] |
---|
| 82 | a = Vertex ( a_xy[0],a_xy[1], attributes =a_att) |
---|
| 83 | d = Vertex (0.0, 4.0, attributes =d_att) |
---|
| 84 | f = Vertex (4.0,0.0, attributes =f_att) |
---|
| 85 | e = Vertex (1.0,1.0, attributes =e_att) |
---|
| 86 | |
---|
| 87 | s1 = Segment(a,d, marker = "50") |
---|
| 88 | s2 = Segment(d,f, marker = "40") |
---|
| 89 | s3 = Segment(a,f, marker = "30") |
---|
| 90 | s4 = Segment(a,e, marker = "20") |
---|
| 91 | |
---|
| 92 | r1 = Region(0.3, 0.3,tag = "1.3", maxArea = 36) |
---|
| 93 | |
---|
| 94 | h1 = Hole(0.2,0.6) |
---|
| 95 | |
---|
| 96 | m = Mesh(userVertices=[a,d,f,e], |
---|
| 97 | userSegments=[s1,s2,s3,s4], |
---|
| 98 | regions=[r1], |
---|
| 99 | holes=[h1]) |
---|
| 100 | |
---|
| 101 | seg = m.getUserSegments() |
---|
| 102 | points = m.getUserVertices() |
---|
| 103 | holes = m.getHoles() |
---|
| 104 | regions = m.getRegions() |
---|
| 105 | fileName = tempfile.mktemp(".txt") |
---|
| 106 | m.exportASCIImeshfile(fileName) |
---|
| 107 | #print "***************************fileName", fileName |
---|
| 108 | dict = import_mesh(fileName) |
---|
| 109 | os.remove(fileName) |
---|
| 110 | |
---|
| 111 | #print dict |
---|
| 112 | |
---|
| 113 | for pimport,pactual,pimpatt in map(None,dict['pointlist'],points,dict['pointattributelist']): |
---|
| 114 | self.failUnless( pimport == [pactual.x,pactual.y], |
---|
| 115 | 'loadASCIITestCase failed. test 1') |
---|
| 116 | self.failUnless( pimpatt == pactual.attributes, |
---|
| 117 | 'loadASCIITestCase failed. test 1.1') |
---|
| 118 | self.failUnless( dict['segmentlist'][0] == [0,1], |
---|
| 119 | 'loadASCIITestCase failed. test 3') |
---|
| 120 | for segimp,segactual in map(None,dict['segmentmarkerlist'],seg): |
---|
| 121 | self.failUnless( segimp == segactual.marker, |
---|
| 122 | 'loadASCIITestCase failed. test 4') |
---|
| 123 | for holeimp,holeactual in map(None,dict['holelist'],holes): |
---|
| 124 | self.failUnless( holeimp == [holeactual.x,holeactual.y], |
---|
| 125 | 'loadASCIITestCase failed. test 5') |
---|
| 126 | for regimp,regactual,regattimp, regmaxarea in map(None,dict['regionlist'],regions, dict['regionattributelist'], dict['regionmaxarealist']): |
---|
| 127 | self.failUnless( regimp == [regactual.x,regactual.y], |
---|
| 128 | 'loadASCIITestCase failed. test 6') |
---|
| 129 | self.failUnless( regattimp == regactual.getTag(), |
---|
| 130 | 'loadASCIITestCase failed. test 7') |
---|
| 131 | self.failUnless( regmaxarea == regactual.getMaxArea(), |
---|
| 132 | 'loadASCIITestCase failed. test 7') |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | def test_loadxy(self): |
---|
| 137 | import os |
---|
| 138 | import tempfile |
---|
| 139 | |
---|
| 140 | fileName = tempfile.mktemp(".xya") |
---|
| 141 | file = open(fileName,"w") |
---|
| 142 | file.write("title row \n\ |
---|
| 143 | 1.0 0.0 10.0 0.0\n\ |
---|
| 144 | 0.0 1.0 0.0 10.0\n\ |
---|
| 145 | 1.0 0.0 10.4 40.0\n") |
---|
| 146 | file.close() |
---|
| 147 | #print fileName |
---|
| 148 | m = importMeshFromFile(fileName) |
---|
| 149 | os.remove(fileName) |
---|
| 150 | self.failUnless(m.userVertices[0].x == 1.0, |
---|
| 151 | 'loadxy, test 1 failed') |
---|
| 152 | self.failUnless(m.userVertices[0].y == 0.0, |
---|
| 153 | 'loadxy, test 2 failed') |
---|
| 154 | self.failUnless(m.userVertices[0].attributes == [10.0,0.0], |
---|
| 155 | 'loadxy, test 2.2 failed') |
---|
| 156 | self.failUnless(m.userVertices[1].x == 0.0, |
---|
| 157 | 'loadxy, test 3 failed') |
---|
| 158 | self.failUnless(m.userVertices[1].y == 1.0, |
---|
| 159 | 'loadxy, test 4 failed') |
---|
| 160 | self.failUnless(m.userVertices[1].attributes == [0.0,10.0], |
---|
| 161 | 'loadxy, test 5 failed') |
---|
| 162 | #------------------------------------------------------------- |
---|
| 163 | if __name__ == "__main__": |
---|
| 164 | |
---|
| 165 | suite = unittest.makeSuite(loadASCIITestCase,'test') |
---|
| 166 | runner = unittest.TextTestRunner() |
---|
| 167 | runner.run(suite) |
---|
| 168 | |
---|