from string import find, rfind def mesh_file_to_mesh_dictionary(fileName): """Load a pmesh file. Returning the mesh dictionary. """ try: meshdic = import_trianglulation(fileName) except IOError, e: msg = 'Could not open file %s ' %fileName raise IOError, msg return meshdic def import_trianglulation(ofile): """ import a file, ofile, with the format First line: <# of vertices> <# of attributes> Following lines: [attributes] One line: <# of triangles> Following lines: [attribute of region] One line: <# of segments> Following lines: [boundary marker] Note: This might throw a can't load file error """ fd = open(ofile,'r') dict = read_trianglulation(fd) dict_mesh = read_mesh(fd) for element in dict_mesh.keys(): dict[element] = dict_mesh[element] fd.close() return dict def read_trianglulation(fd): delimiter = " " ######### loading the point info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment if fragments ==[]: NumOfVertices = 0 NumOfVertAttributes = 0 else: NumOfVertices = fragments[0] NumOfVertAttributes = fragments[1] points = [] pointattributes = [] for index in range(int(NumOfVertices)): #print index fragments = fd.readline().split() #print fragments fragments.pop(0) #pop off the index # pop the x y off so we're left with a list of attributes vert = [float(fragments.pop(0)),float(fragments.pop(0))] points.append(vert) apointattributes = [] #print fragments for fragment in fragments: apointattributes.append(float(fragment)) pointattributes.append(apointattributes) ######### loading the triangle info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment NumOfTriangles = fragments[0] triangles = [] triangleattributes = [] triangleneighbors = [] for index in range(int(NumOfTriangles)): #print index line = fd.readline() line.strip() # so we can get the region string fragments = line.split() #print fragments fragments.pop(0) #pop off the index tri = [int(fragments[0]),int(fragments[1]),int(fragments[2])] triangles.append(tri) neighbors = [int(fragments[3]),int(fragments[4]),int(fragments[5])] triangleneighbors.append(neighbors) for x in range(7): # remove index [] [] line = line[find(line,delimiter):] # remove index line = line.lstrip() stringmarker = line.strip() triangleattributes.append([stringmarker]) ######### loading the segment info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment NumOfSegments = fragments[0] segments = [] segmentmarkers = [] for index in range(int(NumOfSegments)): #print index line = fd.readline() line.strip() # to get the segment string fragments = line.split() #print fragments fragments.pop(0) #pop off the index seg = [int(fragments[0]),int(fragments[1])] segments.append(seg) line = line[find(line,delimiter):] # remove index line = line.lstrip() line = line[find(line,delimiter):] # remove x line = line.lstrip() line = line[find(line,delimiter):] # remove y stringmarker = line.strip() segmentmarkers.append(stringmarker) meshDict = {} meshDict['generatedpointlist'] = points meshDict['generatedpointattributelist'] = pointattributes meshDict['generatedtrianglelist'] = triangles meshDict['generatedtriangleattributelist'] = triangleattributes meshDict['generatedtriangleneighborlist'] = triangleneighbors meshDict['generatedsegmentlist'] = segments meshDict['generatedsegmentmarkerlist'] = segmentmarkers return meshDict def import_mesh(ofile): """ import a file, ofile, with the format First line: <# of vertices> <# of attributes> Following lines: [attributes] One line: <# of segments> Following lines: [boundary marker] Note: This might throw a can't load file error """ fd = open(ofile,'r') meshDict = read_mesh(fd) fd.close() return meshDict def read_mesh(fd): """ Note, if a file has no mesh info, it can still be read - the meshdic returned will be 'empty'. """ delimiter = " " # warning: split() calls are using default whitespace ######### loading the point info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: if fragments ==[]: NumOfVertices = 0 NumOfVertAttributes = 0 else: NumOfVertices = fragments[0] NumOfVertAttributes = fragments[1] points = [] pointattributes = [] for index in range(int(NumOfVertices)): #print index fragments = fd.readline().split() #print fragments fragments.pop(0) #pop off the index # pop the x y off so we're left with a list of attributes vert = [float(fragments.pop(0)),float(fragments.pop(0))] points.append(vert) apointattributes = [] #print fragments for fragment in fragments: apointattributes.append(float(fragment)) pointattributes.append(apointattributes) ######### loading the segment info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment if fragments ==[]: NumOfSegments = 0 else: NumOfSegments = fragments[0] segments = [] segmentmarkers = [] for index in range(int(NumOfSegments)): #print index line = fd.readline() fragments = line.split() #print fragments fragments.pop(0) #pop off the index seg = [int(fragments[0]),int(fragments[1])] segments.append(seg) line = line[find(line,delimiter):] # remove index line = line.lstrip() line = line[find(line,delimiter):] # remove x line = line.lstrip() line = line[find(line,delimiter):] # remove y stringmarker = line.strip() segmentmarkers.append(stringmarker) ######### loading the hole info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment if fragments ==[]: numOfHoles = 0 else: numOfHoles = fragments[0] holes = [] for index in range(int(numOfHoles)): #print index fragments = fd.readline().split() #print fragments fragments.pop(0) #pop off the index hole = [float(fragments[0]),float(fragments[1])] holes.append(hole) ######### loading the region info line = fd.readline() #print line fragments = line.split() #for fragment in fragments: # print fragment if fragments ==[]: numOfRegions = 0 else: numOfRegions = fragments[0] regions = [] regionattributes = [] for index in range(int(numOfRegions)): line = fd.readline() fragments = line.split() #print fragments fragments.pop(0) #pop off the index region = [float(fragments[0]),float(fragments[1])] regions.append(region) line = line[find(line,delimiter):] # remove index line = line.lstrip() line = line[find(line,delimiter):] # remove x line = line.lstrip() line = line[find(line,delimiter):] # remove y stringmarker = line.strip() regionattributes.append(stringmarker) regionmaxareas = [] for index in range(int(numOfRegions)): # Read in the Max area info line = fd.readline() fragments = line.split() #print fragments fragments.pop(0) #pop off the index if len(fragments) == 0: #no max area regionmaxareas.append(None) else: regionmaxareas.append(float(fragments[0])) meshDict = {} meshDict['pointlist'] = points meshDict['pointattributelist'] = pointattributes meshDict['segmentlist'] = segments meshDict['segmentmarkerlist'] = segmentmarkers meshDict['holelist'] = holes meshDict['regionlist'] = regions meshDict['regionattributelist'] = regionattributes meshDict['regionmaxarealist'] = regionmaxareas return meshDict def clean_line(line,delimiter): """Remove whitespace """ #print ">%s" %line line = line.strip() #print "stripped>%s" %line numbers = line.split(delimiter) i = len(numbers) - 1 while i >= 0: if numbers[i] == '': numbers.pop(i) i += -1 #for num in numbers: # print "num>%s<" %num return numbers def write_ASCII_trianglulation(fd, gen_dict): vertices = gen_dict['generatedpointlist'] vertices_attributes = gen_dict['generatedpointattributelist'] triangles = gen_dict['generatedtrianglelist'] triangles_attributes = gen_dict['generatedtriangleattributelist'] triangle_neighbors = gen_dict['generatedtriangleneighborlist'] segments = gen_dict['generatedsegmentlist'] segment_markers = gen_dict['generatedsegmentmarkerlist'] numVert = str(len(vertices)) if (numVert == "0"): numVertAttrib = "0" else: numVertAttrib = str(len(vertices_attributes[0])) fd.write(numVert + " " + numVertAttrib + " # [attributes] ...Triangulation Vertices..." + "\n") # [attributes] index = 0 for vert in vertices: attlist = "" for att in vertices_attributes[index]: attlist = attlist + str(att)+" " attlist.strip() fd.write(str(index) + " " + str(vert[0]) + " " + str(vert[1]) + " " + attlist + "\n") index += 1 #<# of triangles> n = len(triangles) fd.write(str(n) + " # [] [] [attribute of region] ...Triangulation Triangles..." + "\n") # [attribute of region] for index in range(n): neighbors = "" tri = triangles[index] for neighbor in triangle_neighbors[index]: if neighbor: neighbors += str(neighbor) + " " else: if neighbor == 0: neighbors += "0 " else: neighbors += "-1 " if triangles_attributes[index] == ['']: att = "" else: att = str(triangles_attributes[index]) fd.write(str(index) + " " + str(tri[0]) + " " + str(tri[1]) + " " + str(tri[2]) + " " + neighbors + " " + att + "\n") #One line: <# of segments> fd.write(str(len(segments)) + " # [boundary marker] ...Triangulation Segments..." + "\n") #Following lines: [boundary marker] for i in range(len(segments)): seg = segments[i] fd.write(str(i) + " " + str(seg[0]) + " " + str(seg[1]) + " " + str(segment_markers[i]) + "\n") def export_trianglulation_file(ofile,gen_dict): """ write a file, ofile, with the format First line: <# of vertices> <# of attributes> Following lines: [attributes] One line: <# of triangles> Following lines: [attribute of region] One line: <# of segments> Following lines: [boundary marker] """ try: fd = open(ofile,'w') write_ASCII_trianglulation(fd,gen_dict) fd.close() except IOError, e: msg = 'Could not write file %s ' %fileName raise IOError, msg ### # LOADING XYA FILES ### def load_xya_file(ofile,delimiter = ','): """ load a file, ofile, with the format x,y, [attributes] """ try: fd = open(ofile) xya_dic = read_xya_file(fd, delimiter) fd.close() except IOError, e: msg = 'Could not open file %s ' %fileName raise IOError, msg return xya_dic def read_xya_file(fd, delimiter): lines = fd.readlines() points = [] pointattributes = [] if len(lines) <= 1: raise SyntaxError lines.pop(0) #remove the first (title) line attLength = len(clean_line(lines[0],delimiter))-2 #print "initlegth" #print attLength for line in lines: #print "line >%s" %line numbers = clean_line(line,delimiter) #print "numbers >%s<" %numbers if len(numbers) < 2 and numbers != []: raise SyntaxError if numbers != []: try: x = float(numbers[0]) y = float(numbers[1]) points.append([x,y]) numbers.pop(0) numbers.pop(0) attributes = [] if attLength != len(numbers): raise SyntaxError attLength = len(numbers) for num in numbers: num.strip() if num != '\n' and num != '': attributes.append(float(num)) except ValueError: raise SyntaxError pointattributes.append(attributes) xya_dict = {} xya_dict['pointlist'] = points xya_dict['pointattributelist'] = pointattributes xya_dict['segmentlist'] = [] xya_dict['segmentmarkerlist'] = [] xya_dict['regionlist'] = [] xya_dict['regionattributelist'] = [] xya_dict['regionmaxarealist'] = [] xya_dict['holelist'] = [] return xya_dict if __name__ == "__main__": m = import_mesh("tee.txt") print m