Changeset 1401
- Timestamp:
- May 16, 2005, 4:23:25 PM (19 years ago)
- Location:
- inundation/ga/storm_surge/pmesh
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pmesh/load_mesh/loadASCII.py
r1394 r1401 1350 1350 for i,vert in enumerate( points): 1351 1351 1352 attlist = " "1352 attlist = "," 1353 1353 for att in pointattributes.keys(): 1354 1354 attlist = attlist + str(pointattributes[att][i])+ delimiter … … 1356 1356 attlist.strip() 1357 1357 fd.write( str(vert[0]) + delimiter 1358 + str(vert[1]) + delimiter1358 + str(vert[1]) 1359 1359 + attlist + "\n") 1360 1360 fd.close() -
inundation/ga/storm_surge/pmesh/mesh.py
r1395 r1401 545 545 for element in dic_mesh.keys(): 546 546 dic[element] = dic_mesh[element] 547 547 for element in dic.keys(): 548 dic[element].sort() 549 548 550 # A dic for the exported/imported m 549 551 dic_other = other.Mesh2triangList() … … 551 553 for element in dic_mesh.keys(): 552 554 dic_other[element] = dic_mesh[element] 555 for element in dic.keys(): 556 dic_other[element].sort() 553 557 554 558 #print "dsg************************8" … … 569 573 PreCondition: maxArea is a double 570 574 """ 571 print "mode ",mode575 #print "mode ",mode 572 576 if mode == None: 573 577 self.mode = "" … … 736 740 Precondition: Each vertex has a dupindex, which is the list 737 741 index. 742 743 Note: this removes vertices that have the same x,y values, 744 not duplicate instances in the Vertices list. 738 745 """ 739 746 remove = [] … … 1312 1319 expand_pinch=expand_pinch) 1313 1320 boundary_segs = self.shape.get_boundary() 1314 print "boundary_segs",boundary_segs1321 #print "boundary_segs",boundary_segs 1315 1322 segs2delete = self.alphaUserSegments 1316 1323 … … 1622 1629 #Most probably, the Tkinter module is not available. 1623 1630 #""" 1624 # FIXME let's not use this function,1625 # use export _mesh_file instead1626 def export_triangulation_file(self,ofile):1627 """1628 export a file, ofile, with the format1629 1630 First line: <# of vertices> <# of attributes>1631 Following lines: <vertex #> <x> <y> [attributes]1632 One line: <vertex attribute titles>1633 One line: <# of triangles>1634 Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region]1635 One line: <# of segments>1636 Following lines: <segment #> <vertex #> <vertex #> [boundary tag]1637 """1638 gen_dict = self.Mesh2IODict()1639 if (ofile[-4:] == ".tsh"):1640 fd = open(ofile,'w')1641 load_mesh.loadASCII.write_ASCII_triangulation(fd,gen_dict)1642 self.writeASCIImesh(fd,1643 self.userVertices,1644 self.getUserSegments(),1645 self.holes,1646 self.regions)1647 fd.close()1648 elif (ofile[-4:] == ".msh"):1649 #print "mesh gen_dict",gen_dict1650 load_mesh.loadASCII.write_msh_file(ofile, gen_dict)1651 1631 1652 # self.writeASCIImesh(fd, does anything use this?1653 1654 #FIXME this function has a bug..1655 def exportASCIIsegmentoutlinefile(self,ofile):1656 """1657 export a file, ofile, with no triangulation and only vertices connected to segments.1658 """1659 fd = open(ofile,'w')1660 meshDict = {}1661 1662 meshDict['vertices'] = []1663 meshDict['vertex_attributes'] = []1664 meshDict['segments'] = []1665 meshDict['segment_tags'] = []1666 meshDict['triangles'] = []1667 meshDict['triangle_tags'] = []1668 meshDict['triangle_neighbors'] = []1669 1670 load_mesh.loadASCII.write_ASCII_triangulation(fd,meshDict)1671 self.writeASCIIsegmentoutline(fd,1672 self.userVertices,1673 self.getUserSegments(),1674 self.holes,1675 self.regions)1676 fd.close()1677 1632 1678 1633 def exportASCIIobj(self,ofile): … … 1706 1661 + str(tri.vertices[1].index1) + " " 1707 1662 + str(tri.vertices[2].index1) + "\n") 1708 1709 #FIXME I think this has a bug... 1710 def writeASCIIsegmentoutline(self, 1711 fd, 1712 userVertices, 1713 userSegments, 1714 holes, 1715 regions): 1716 """Write the user mesh info, only with vertices that are connected to segs 1717 """ 1718 verts = [] 1719 #dupindex = 0 1720 for seg in self.userSegments: 1721 verts.append(seg.vertices[0]) 1722 verts.append(seg.vertices[1]) 1723 print 'verts>',verts 1724 1725 verts, count = self.removeDuplicatedVertices(verts) 1726 print 'verts no dups>',verts 1727 self.writeASCIImesh(fd, 1728 verts, 1729 self.getUserSegments(), 1730 self.holes, 1731 self.regions) 1663 1664 def exportASCIIsegmentoutlinefile(self,ofile): 1665 """Write the boundary user mesh info, eg 1666 vertices that are connected to segments, 1667 segments 1668 """ 1669 1670 verts = {} 1671 for seg in self.getUserSegments(): 1672 verts[seg.vertices[0]] = seg.vertices[0] 1673 verts[seg.vertices[1]] = seg.vertices[1] 1674 #print "verts.values()",verts.values() 1675 meshDict = self.Mesh2IOOutlineDict(userVertices=verts.values()) 1676 load_mesh.loadASCII.export_mesh_file(ofile,meshDict) 1732 1677 1733 1678 # exportASCIImeshfile - this function is used … … 1740 1685 load_mesh.loadASCII.export_mesh_file(ofile,dict) 1741 1686 1742 # is this function obsolete? 1743 def writeASCIImesh(self, 1744 fd, 1745 userVertices, 1746 userSegments, 1747 holes, 1748 regions): 1749 1750 #print "mesh.writeASCIImesh*********" 1751 #print "dict",dict 1752 #print "mesh*********" 1753 load_mesh.loadASCII.write_ASCII_outline(fd, dict) 1754 1755 #FIXME the title is wrong, need more comments 1756 def exportxyafile(self,ofile): 1757 """ 1758 export a file, ofile, with the format 1759 1760 First line: <# of vertices> <# of attributes> 1761 Following lines: <vertex #> <x> <y> [attributes] 1762 """ 1763 #load_mesh.loadASCII 1764 #FIXME, this should be a mesh2io method 1765 if self.meshVertices == []: 1766 Vertices = self.userVertices 1687 def exportPointsFile(self,ofile): 1688 """ 1689 export a points (.xya or .pts) file, ofile. 1690 1691 """ 1692 1693 mesh_dict = self.Mesh2IODict() 1694 point_dict = {} 1695 point_dict['attributelist'] = {} #this will need to be expanded.. 1696 # if attributes are brought back in. 1697 point_dict['geo_reference'] = self.geo_reference 1698 if mesh_dict['vertices'] == []: 1699 point_dict['pointlist'] = mesh_dict['points'] 1767 1700 else: 1768 Vertices = self.meshVertices 1769 1770 numVert = str(len(Vertices)) 1771 1772 if Vertices == []: 1773 raise RuntimeError 1774 numVertAttrib = str(len(Vertices[0].attributes)) 1775 #title = numVert + " " + numVertAttrib + " # <vertex #> <x> <y> [attributes]" 1776 1777 #Convert the Vertices to pointlist and pointattributelist 1778 xya_dict = {} 1779 pointattributes = [] 1780 points = [] 1781 1782 for vert in Vertices: 1783 points.append([vert.x,vert.y]) 1784 pointattributes.append(vert.attributes) 1785 1786 xya_dict['pointlist'] = points 1787 xya_dict['pointattributelist'] = pointattributes 1788 xya_dict['geo_reference'] = self.geo_reference 1789 1790 load_mesh.loadASCII.export_points_file(ofile, xya_dict) 1701 point_dict['pointlist'] = mesh_dict['vertices'] 1702 1703 load_mesh.loadASCII.export_points_file(ofile,point_dict) 1791 1704 1792 1705 … … 1844 1757 vertices=[] 1845 1758 vertex_attributes=[] 1846 1847 1759 1848 1760 self.maxVertexIndex=0 … … 1963 1875 #print "*(*(" 1964 1876 return meshDict 1965 1966 1877 1967 1878 def IOTriangulation2Mesh(self, genDict): … … 2010 1921 self.meshTriangles[index].setAttribute("") 2011 1922 else: 2012 # Note, is the first attribute always the region att?2013 # haven't confirmed this2014 #Peter - I think so (from manuel)2015 #...the first such value is assumed to be a regional attribute...2016 1923 self.meshTriangles[index].setAttribute(att) 2017 1924 index += 1 -
inundation/ga/storm_surge/pmesh/pmesh.py
r1395 r1401 111 111 label='Export ASCII obj', 112 112 command=self.exportObj) 113 # FIXME, add this function back into pmesh114 #self.menuBar.addmenuitem('File', 'command',115 #'Export ASCII segment outline',116 #label='Export ASCII segment outline...',117 #command=self.exportASCIIsegmentoutlinefile)113 114 self.menuBar.addmenuitem('File', 'command', 115 'Export ASCII segment outline', 116 label='Export ASCII segment outline...', 117 command=self.exportASCIIsegmentoutlinefile) 118 118 119 119 self.menuBar.addmenuitem('File', 'command', 120 120 'Export ASCII xya file', 121 121 label='Export ASCII xya file...', 122 command=self.export xyafile)122 command=self.exportPointsFile) 123 123 124 124 self.menuBar.addmenuitem('File', 'command', … … 1244 1244 1245 1245 def exportASCIIsegmentoutlinefile(self): 1246 fileType = "tsh"1247 fileTypeDesc = "text mesh"1248 1246 1249 1247 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath, 1250 filetypes=[(fileTypeDesc, fileType), 1251 ("All Files", "*")]) 1248 filetypes=[("mesh", "*.tsh *.msh"), 1249 ("All Files", "*")]) 1250 1252 1251 if ofile: 1253 addOn = "." + fileType 1254 jumpback = - len(addOn) 1255 if ofile[jumpback:] != addOn: 1256 ofile = ofile + addOn 1252 # .tsh is the default file format 1253 if (ofile[-4:] == ".tsh" or ofile[-4:] == ".msh"): 1254 self.currentFilePathName = ofile 1255 else: 1256 self.currentFilePathName = ofile + ".tsh" 1257 1257 1258 try: 1258 1259 self.mesh.exportASCIIsegmentoutlinefile(ofile) 1260 except IOError: #FIXME should this function be throwing any errors? 1261 showerror('Export ASCII file', 1262 'Can not write to file.') 1263 1264 def exportPointsFile(self): 1265 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath, 1266 filetypes=[("point files", 1267 "*.xya *.pts"), 1268 ("All Files", "*")]) 1269 if ofile: 1270 # .xya is the default file format 1271 if (ofile[-4:] == ".xya" or ofile[-4:] == ".pts"): 1272 self.currentFilePathName = ofile 1273 else: 1274 self.currentFilePathName = ofile + ".xya" 1275 1276 try: 1277 self.mesh.exportPointsFile(ofile) 1259 1278 except IOError: 1260 1279 showerror('Export ASCII file', 1261 1280 'Can not write to file.') 1262 1263 def exportxyafile(self):1264 fileType = "xya"1265 fileTypeDesc = "text vertices"1266 1267 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath,1268 filetypes=[(fileTypeDesc, fileType),1269 ("All Files", "*")])1270 if ofile:1271 addOn = "." + fileType1272 jumpback = - len(addOn)1273 if ofile[jumpback:] != addOn:1274 ofile = ofile + addOn1275 try:1276 self.mesh.exportxyafile(ofile)1277 except IOError:1278 showerror('Export ASCII file',1279 'Can not write to file.')1280 1281 def exportTriangulation(self):1282 """1283 This function is not used?1284 """1285 fileType = "tsh"1286 fileTypeDesc = "text mesh"1287 1288 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath,1289 filetypes=[(fileTypeDesc, fileType),1290 ("All Files", "*")])1291 if ofile:1292 addOn = "." + fileType1293 jumpback = - len(addOn)1294 if ofile[jumpback:] != addOn:1295 ofile = ofile + addOn1296 try:1297 # FIXME - check that this is what we want1298 self.mesh.export_triangulation_file(ofile)1299 except IOError:1300 showerror('Export ASCII file',1301 'Can not write to file.')1302 except RuntimeError:1303 showerror('Export ASCII file',1304 'No triangulation to export.')1305 1306 1307 def export_tsh(self):1308 fileType = "tsh"1309 fileTypeDesc = "text Mesh"1310 1311 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath,1312 filetypes=[(fileTypeDesc, fileType),1313 ("All Files", "*")])1314 if ofile:1315 addOn = "." + fileType1316 jumpback = - len(addOn)1317 if ofile[jumpback:] != addOn:1318 ofile = ofile + addOn1319 try:1320 self.mesh.exportASCIImeshfile(ofile)1321 except IOError:1322 showerror('Export ASCII file',1323 'Can not write to file.')1324 except RuntimeError:1325 showerror('Export ASCII file',1326 'No mesh to export.')1327 1328 def exportMesh(self):1329 fileType = "tsh"1330 fileTypeDesc = "text Mesh"1331 1332 ofile = tkFileDialog.asksaveasfilename(initialdir=self.currentPath,1333 filetypes=[(fileTypeDesc, fileType),1334 ("All Files", "*")])1335 if ofile:1336 addOn = "." + fileType1337 jumpback = - len(addOn)1338 if ofile[jumpback:] != addOn:1339 ofile = ofile + addOn1340 try:1341 self.mesh.exportASCIImeshfile(ofile)1342 except IOError:1343 showerror('Export ASCII file',1344 'Can not write to file.')1345 except RuntimeError:1346 showerror('Export ASCII file',1347 'No mesh to export.')1348 1281 1349 1282 def importFile(self): -
inundation/ga/storm_surge/pmesh/test_mesh.py
r1395 r1401 237 237 'Segment should not be new. ') 238 238 239 def ztestautoSegment(self):239 def testautoSegment(self): 240 240 p0 = Vertex (0.0, 0.0) 241 241 p1 = Vertex (0.0, 4.0) … … 256 256 'userSegments is wrong!') 257 257 258 def ztestautoSegmentII(self):258 def testautoSegmentII(self): 259 259 p1 = Vertex (3.0, 4.0) 260 260 p2 = Vertex (3.0,2.0) … … 781 781 self.failUnless(xmin == -100.0 and ymin == -100.0 and xmax == 0.0 and ymax == 100.0, 782 782 'normalise failed') 783 # Fix me, this function has a bug784 def zztest_exportASCIIsegmentoutlinefile(self):783 784 def test_exportASCIIsegmentoutlinefile(self): 785 785 a = Vertex (0,0) 786 786 b = Vertex (0,3) … … 817 817 m.meshSegments = [] 818 818 m.userVertices=[a,b,c] 819 #print "mesh ***************dsg*", m 820 #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) 821 self.failUnless(0 == m.__cmp__(m), 822 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') 823 self.failUnless(0 == m.__cmp__(m_returned), 824 'test_exportASCIIsegmentoutlinefile:loading and saving of a mesh failed') 825 826 def test_exportASCIIsegmentoutlinefile2(self): 827 a = Vertex (0,0) 828 b = Vertex (0,1) 829 c = Vertex (1,0) 830 d = Vertex (1,1) 831 e = Vertex (0.5,0.5) 832 f = Vertex (0.6,0.6) 833 834 s1 = Segment(a,e, tag = "50") 835 s2 = Segment(b,e, tag = "40") 836 s3 = Segment(c,e, tag = "30") 837 s4 = Segment(d,e, tag = "30") 838 839 r1 = Region(2, 1,tag = "1.3") 840 h1 = Hole(1,4) 841 m = Mesh(userVertices=[a,b,c,d,e], 842 userSegments=[s1,s2,s3,s4], 843 regions=[r1], 844 holes = [h1]) 845 846 fileName = tempfile.mktemp(".tsh") 847 m.exportASCIIsegmentoutlinefile(fileName) 848 849 m_returned = importMeshFromFile(fileName) 850 #print "****************** fileName", fileName 851 os.remove(fileName) 852 853 #Trim mesh, so it should like like m_returned 854 m.meshVertices = [] 855 m.meshTriangles = [] 856 m.meshSegments = [] 857 m.userVertices=[a,e,d,b,c] 819 858 #print "mesh ***************dsg*", m 820 859 #print "(m.__cmp__(m_returned)", m.__cmp__(m_returned) … … 874 913 # 'loadxy, test 5 failed') 875 914 876 def test_export xyafile(self):915 def test_exportPointsFile(self): 877 916 a = Vertex (0,0) 878 917 b = Vertex (0,3) … … 895 934 896 935 fileName = tempfile.mktemp(".xya") 897 m.export xyafile(fileName)936 m.exportPointsFile(fileName) 898 937 file = open(fileName) 899 938 lFile = file.read().split('\n') 900 939 file.close() 901 940 902 #print "**********************"903 #print lFile904 #print "**********************"905 906 941 os.remove(fileName) 907 self.failUnless(lFile[0] == " 5 0 # <vertex #> <x> <y> [attributes]" and908 lFile[1] == "0 0" and909 lFile[2] == "0 3" and910 lFile[3] == "3 3" and911 lFile[4] == "1 2" and912 lFile[5] == "3 1"942 self.failUnless(lFile[0] == "" and 943 lFile[1] == "0,0" and 944 lFile[2] == "0,3" and 945 lFile[3] == "3,3" and 946 lFile[4] == "1,2" and 947 lFile[5] == "3,1" 913 948 , 914 949 'exported Ascii xya file is wrong') 915 950 916 951 m.generateMesh("QApz", maxArea = 2.1) 917 fileName = tempfile.mktemp(". txt")918 m.export xyafile(fileName)952 fileName = tempfile.mktemp(".xya") 953 m.exportPointsFile(fileName) 919 954 file = open(fileName) 920 955 lFile = file.read().split('\n') 921 956 file.close() 922 957 os.remove(fileName) 923 self.failUnless(lFile[0] == "6 0 # <vertex #> <x> <y> [attributes]" and 924 lFile[1] == "0.0 0.0 " and 925 lFile[2] == "0.0 3.0 " and 926 lFile[3] == "3.0 3.0 " and 927 lFile[4] == "1.0 2.0 " and 928 lFile[5] == "3.0 1.0 " and 929 lFile[6] == "1.5 1.5 " 958 self.failUnless(lFile[0] == "" and 959 lFile[1] == "0.0,0.0" and 960 lFile[2] == "0.0,3.0" and 961 lFile[3] == "3.0,3.0" and 962 lFile[4] == "1.0,2.0" and 963 lFile[5] == "3.0,1.0" and 964 lFile[6] == "1.5,1.5" 965 , 966 'exported Ascii xya file is wrong') 967 968 def test_exportPointsFilefile2(self): 969 m = Mesh() 970 971 fileName = tempfile.mktemp(".xya") 972 m.exportPointsFile(fileName) 973 file = open(fileName) 974 lFile = file.read().split('\n') 975 file.close() 976 977 os.remove(fileName) 978 #print "************* test_mesh exportPointsFilefile" 979 #print "lFile",lFile 980 #print "************* test_mesh exportPointsFilefile" 981 self.failUnless(lFile[0] == "" 930 982 , 931 983 'exported Ascii xya file is wrong') … … 1003 1055 # 'y duplicate verts not removed') 1004 1056 1005 1057 1006 1058 def test_ungenerateFileLoading(self): 1007 1059
Note: See TracChangeset
for help on using the changeset viewer.