[4] | 1 | |
---|
| 2 | from string import find, rfind |
---|
| 3 | |
---|
| 4 | def mesh_file_to_mesh_dictionary(fileName): |
---|
| 5 | """Load a pmesh file. Returning the mesh dictionary. |
---|
| 6 | """ |
---|
| 7 | try: |
---|
| 8 | meshdic = import_trianglulation(fileName) |
---|
| 9 | except IOError, e: |
---|
| 10 | msg = 'Could not open file %s ' %fileName |
---|
| 11 | raise IOError, msg |
---|
| 12 | return meshdic |
---|
| 13 | |
---|
| 14 | def import_trianglulation(ofile): |
---|
| 15 | """ |
---|
| 16 | import a file, ofile, with the format |
---|
| 17 | |
---|
| 18 | First line: <# of vertices> <# of attributes> |
---|
| 19 | Following lines: <vertex #> <x> <y> [attributes] |
---|
| 20 | One line: <# of triangles> |
---|
| 21 | Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] |
---|
| 22 | One line: <# of segments> |
---|
| 23 | Following lines: <segment #> <vertex #> <vertex #> [boundary marker] |
---|
| 24 | Note: This might throw a can't load file error |
---|
| 25 | """ |
---|
| 26 | fd = open(ofile,'r') |
---|
| 27 | dict = read_trianglulation(fd) |
---|
| 28 | dict_mesh = read_mesh(fd) |
---|
| 29 | for element in dict_mesh.keys(): |
---|
| 30 | dict[element] = dict_mesh[element] |
---|
| 31 | |
---|
| 32 | fd.close() |
---|
| 33 | return dict |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | def read_trianglulation(fd): |
---|
| 37 | delimiter = " " |
---|
| 38 | ######### loading the point info |
---|
| 39 | line = fd.readline() |
---|
| 40 | #print line |
---|
| 41 | fragments = line.split() |
---|
| 42 | #for fragment in fragments: |
---|
| 43 | # print fragment |
---|
| 44 | if fragments ==[]: |
---|
| 45 | NumOfVertices = 0 |
---|
| 46 | NumOfVertAttributes = 0 |
---|
| 47 | else: |
---|
| 48 | NumOfVertices = fragments[0] |
---|
| 49 | NumOfVertAttributes = fragments[1] |
---|
| 50 | points = [] |
---|
| 51 | pointattributes = [] |
---|
| 52 | for index in range(int(NumOfVertices)): |
---|
| 53 | #print index |
---|
| 54 | fragments = fd.readline().split() |
---|
| 55 | #print fragments |
---|
| 56 | fragments.pop(0) #pop off the index |
---|
| 57 | |
---|
| 58 | # pop the x y off so we're left with a list of attributes |
---|
| 59 | vert = [float(fragments.pop(0)),float(fragments.pop(0))] |
---|
| 60 | points.append(vert) |
---|
| 61 | apointattributes = [] |
---|
| 62 | #print fragments |
---|
| 63 | for fragment in fragments: |
---|
| 64 | apointattributes.append(float(fragment)) |
---|
| 65 | pointattributes.append(apointattributes) |
---|
| 66 | |
---|
| 67 | ######### loading the triangle info |
---|
| 68 | line = fd.readline() |
---|
| 69 | #print line |
---|
| 70 | fragments = line.split() |
---|
| 71 | #for fragment in fragments: |
---|
| 72 | # print fragment |
---|
| 73 | NumOfTriangles = fragments[0] |
---|
| 74 | triangles = [] |
---|
| 75 | triangleattributes = [] |
---|
| 76 | triangleneighbors = [] |
---|
| 77 | for index in range(int(NumOfTriangles)): |
---|
| 78 | #print index |
---|
| 79 | line = fd.readline() |
---|
| 80 | line.strip() # so we can get the region string |
---|
| 81 | fragments = line.split() |
---|
| 82 | #print fragments |
---|
| 83 | fragments.pop(0) #pop off the index |
---|
| 84 | |
---|
| 85 | tri = [int(fragments[0]),int(fragments[1]),int(fragments[2])] |
---|
| 86 | triangles.append(tri) |
---|
| 87 | neighbors = [int(fragments[3]),int(fragments[4]),int(fragments[5])] |
---|
| 88 | triangleneighbors.append(neighbors) |
---|
| 89 | for x in range(7): # remove index [<vertex #>] [<neigbouring tri #>] |
---|
| 90 | line = line[find(line,delimiter):] # remove index |
---|
| 91 | line = line.lstrip() |
---|
| 92 | stringmarker = line.strip() |
---|
| 93 | triangleattributes.append([stringmarker]) |
---|
| 94 | |
---|
| 95 | ######### loading the segment info |
---|
| 96 | line = fd.readline() |
---|
| 97 | #print line |
---|
| 98 | fragments = line.split() |
---|
| 99 | #for fragment in fragments: |
---|
| 100 | # print fragment |
---|
| 101 | NumOfSegments = fragments[0] |
---|
| 102 | segments = [] |
---|
| 103 | segmentmarkers = [] |
---|
| 104 | for index in range(int(NumOfSegments)): |
---|
| 105 | #print index |
---|
| 106 | line = fd.readline() |
---|
| 107 | line.strip() # to get the segment string |
---|
| 108 | fragments = line.split() |
---|
| 109 | #print fragments |
---|
| 110 | fragments.pop(0) #pop off the index |
---|
| 111 | seg = [int(fragments[0]),int(fragments[1])] |
---|
| 112 | segments.append(seg) |
---|
| 113 | line = line[find(line,delimiter):] # remove index |
---|
| 114 | line = line.lstrip() |
---|
| 115 | line = line[find(line,delimiter):] # remove x |
---|
| 116 | line = line.lstrip() |
---|
| 117 | line = line[find(line,delimiter):] # remove y |
---|
| 118 | stringmarker = line.strip() |
---|
| 119 | segmentmarkers.append(stringmarker) |
---|
| 120 | meshDict = {} |
---|
| 121 | meshDict['generatedpointlist'] = points |
---|
| 122 | meshDict['generatedpointattributelist'] = pointattributes |
---|
| 123 | meshDict['generatedtrianglelist'] = triangles |
---|
| 124 | meshDict['generatedtriangleattributelist'] = triangleattributes |
---|
| 125 | meshDict['generatedtriangleneighborlist'] = triangleneighbors |
---|
| 126 | meshDict['generatedsegmentlist'] = segments |
---|
| 127 | meshDict['generatedsegmentmarkerlist'] = segmentmarkers |
---|
| 128 | return meshDict |
---|
| 129 | |
---|
| 130 | def import_mesh(ofile): |
---|
| 131 | """ |
---|
| 132 | import a file, ofile, with the format |
---|
| 133 | |
---|
| 134 | First line: <# of vertices> <# of attributes> |
---|
| 135 | Following lines: <x> <y> [attributes] |
---|
| 136 | One line: <# of segments> |
---|
| 137 | Following lines: <vertex #> <vertex #> [boundary marker] |
---|
| 138 | Note: This might throw a can't load file error |
---|
| 139 | """ |
---|
| 140 | fd = open(ofile,'r') |
---|
| 141 | meshDict = read_mesh(fd) |
---|
| 142 | fd.close() |
---|
| 143 | return meshDict |
---|
| 144 | |
---|
| 145 | def read_mesh(fd): |
---|
| 146 | """ |
---|
| 147 | Note, if a file has no mesh info, it can still be read - the meshdic |
---|
| 148 | returned will be 'empty'. |
---|
| 149 | """ |
---|
| 150 | delimiter = " " # warning: split() calls are using default whitespace |
---|
| 151 | |
---|
| 152 | ######### loading the point info |
---|
| 153 | line = fd.readline() |
---|
| 154 | #print line |
---|
| 155 | fragments = line.split() |
---|
| 156 | #for fragment in fragments: |
---|
| 157 | if fragments ==[]: |
---|
| 158 | NumOfVertices = 0 |
---|
| 159 | NumOfVertAttributes = 0 |
---|
| 160 | else: |
---|
| 161 | NumOfVertices = fragments[0] |
---|
| 162 | NumOfVertAttributes = fragments[1] |
---|
| 163 | points = [] |
---|
| 164 | pointattributes = [] |
---|
| 165 | for index in range(int(NumOfVertices)): |
---|
| 166 | #print index |
---|
| 167 | fragments = fd.readline().split() |
---|
| 168 | #print fragments |
---|
| 169 | fragments.pop(0) #pop off the index |
---|
| 170 | # pop the x y off so we're left with a list of attributes |
---|
| 171 | vert = [float(fragments.pop(0)),float(fragments.pop(0))] |
---|
| 172 | points.append(vert) |
---|
| 173 | apointattributes = [] |
---|
| 174 | #print fragments |
---|
| 175 | for fragment in fragments: |
---|
| 176 | apointattributes.append(float(fragment)) |
---|
| 177 | pointattributes.append(apointattributes) |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | ######### loading the segment info |
---|
| 181 | line = fd.readline() |
---|
| 182 | #print line |
---|
| 183 | fragments = line.split() |
---|
| 184 | #for fragment in fragments: |
---|
| 185 | # print fragment |
---|
| 186 | if fragments ==[]: |
---|
| 187 | NumOfSegments = 0 |
---|
| 188 | else: |
---|
| 189 | NumOfSegments = fragments[0] |
---|
| 190 | segments = [] |
---|
| 191 | segmentmarkers = [] |
---|
| 192 | for index in range(int(NumOfSegments)): |
---|
| 193 | #print index |
---|
| 194 | line = fd.readline() |
---|
| 195 | fragments = line.split() |
---|
| 196 | #print fragments |
---|
| 197 | fragments.pop(0) #pop off the index |
---|
| 198 | |
---|
| 199 | seg = [int(fragments[0]),int(fragments[1])] |
---|
| 200 | segments.append(seg) |
---|
| 201 | line = line[find(line,delimiter):] # remove index |
---|
| 202 | line = line.lstrip() |
---|
| 203 | line = line[find(line,delimiter):] # remove x |
---|
| 204 | line = line.lstrip() |
---|
| 205 | line = line[find(line,delimiter):] # remove y |
---|
| 206 | stringmarker = line.strip() |
---|
| 207 | segmentmarkers.append(stringmarker) |
---|
| 208 | |
---|
| 209 | ######### loading the hole info |
---|
| 210 | line = fd.readline() |
---|
| 211 | #print line |
---|
| 212 | fragments = line.split() |
---|
| 213 | #for fragment in fragments: |
---|
| 214 | # print fragment |
---|
| 215 | if fragments ==[]: |
---|
| 216 | numOfHoles = 0 |
---|
| 217 | else: |
---|
| 218 | numOfHoles = fragments[0] |
---|
| 219 | holes = [] |
---|
| 220 | for index in range(int(numOfHoles)): |
---|
| 221 | #print index |
---|
| 222 | fragments = fd.readline().split() |
---|
| 223 | #print fragments |
---|
| 224 | fragments.pop(0) #pop off the index |
---|
| 225 | hole = [float(fragments[0]),float(fragments[1])] |
---|
| 226 | holes.append(hole) |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | ######### loading the region info |
---|
| 230 | line = fd.readline() |
---|
| 231 | #print line |
---|
| 232 | fragments = line.split() |
---|
| 233 | #for fragment in fragments: |
---|
| 234 | # print fragment |
---|
| 235 | if fragments ==[]: |
---|
| 236 | numOfRegions = 0 |
---|
| 237 | else: |
---|
| 238 | numOfRegions = fragments[0] |
---|
| 239 | regions = [] |
---|
| 240 | regionattributes = [] |
---|
| 241 | for index in range(int(numOfRegions)): |
---|
| 242 | line = fd.readline() |
---|
| 243 | fragments = line.split() |
---|
| 244 | #print fragments |
---|
| 245 | fragments.pop(0) #pop off the index |
---|
| 246 | region = [float(fragments[0]),float(fragments[1])] |
---|
| 247 | regions.append(region) |
---|
| 248 | |
---|
| 249 | line = line[find(line,delimiter):] # remove index |
---|
| 250 | line = line.lstrip() |
---|
| 251 | line = line[find(line,delimiter):] # remove x |
---|
| 252 | line = line.lstrip() |
---|
| 253 | line = line[find(line,delimiter):] # remove y |
---|
| 254 | stringmarker = line.strip() |
---|
| 255 | regionattributes.append(stringmarker) |
---|
| 256 | regionmaxareas = [] |
---|
| 257 | for index in range(int(numOfRegions)): # Read in the Max area info |
---|
| 258 | line = fd.readline() |
---|
| 259 | fragments = line.split() |
---|
| 260 | #print fragments |
---|
| 261 | fragments.pop(0) #pop off the index |
---|
| 262 | if len(fragments) == 0: #no max area |
---|
| 263 | regionmaxareas.append(None) |
---|
| 264 | else: |
---|
| 265 | regionmaxareas.append(float(fragments[0])) |
---|
| 266 | |
---|
| 267 | |
---|
| 268 | meshDict = {} |
---|
| 269 | meshDict['pointlist'] = points |
---|
| 270 | meshDict['pointattributelist'] = pointattributes |
---|
| 271 | meshDict['segmentlist'] = segments |
---|
| 272 | meshDict['segmentmarkerlist'] = segmentmarkers |
---|
| 273 | meshDict['holelist'] = holes |
---|
| 274 | meshDict['regionlist'] = regions |
---|
| 275 | meshDict['regionattributelist'] = regionattributes |
---|
| 276 | meshDict['regionmaxarealist'] = regionmaxareas |
---|
| 277 | |
---|
| 278 | return meshDict |
---|
| 279 | |
---|
| 280 | def clean_line(line,delimiter): |
---|
| 281 | """Remove whitespace |
---|
| 282 | """ |
---|
| 283 | #print ">%s" %line |
---|
| 284 | line = line.strip() |
---|
| 285 | #print "stripped>%s" %line |
---|
| 286 | numbers = line.split(delimiter) |
---|
| 287 | i = len(numbers) - 1 |
---|
| 288 | while i >= 0: |
---|
| 289 | if numbers[i] == '': |
---|
| 290 | numbers.pop(i) |
---|
| 291 | i += -1 |
---|
| 292 | #for num in numbers: |
---|
| 293 | # print "num>%s<" %num |
---|
| 294 | return numbers |
---|
| 295 | |
---|
| 296 | def write_ASCII_trianglulation(fd, |
---|
| 297 | gen_dict): |
---|
| 298 | vertices = gen_dict['generatedpointlist'] |
---|
| 299 | vertices_attributes = gen_dict['generatedpointattributelist'] |
---|
| 300 | triangles = gen_dict['generatedtrianglelist'] |
---|
| 301 | triangles_attributes = gen_dict['generatedtriangleattributelist'] |
---|
| 302 | triangle_neighbors = gen_dict['generatedtriangleneighborlist'] |
---|
| 303 | |
---|
| 304 | segments = gen_dict['generatedsegmentlist'] |
---|
| 305 | segment_markers = gen_dict['generatedsegmentmarkerlist'] |
---|
| 306 | |
---|
| 307 | numVert = str(len(vertices)) |
---|
| 308 | if (numVert == "0"): |
---|
| 309 | numVertAttrib = "0" |
---|
| 310 | else: |
---|
| 311 | numVertAttrib = str(len(vertices_attributes[0])) |
---|
| 312 | fd.write(numVert + " " + numVertAttrib + " # <vertex #> <x> <y> [attributes] ...Triangulation Vertices..." + "\n") |
---|
| 313 | |
---|
| 314 | #<vertex #> <x> <y> [attributes] |
---|
| 315 | index = 0 |
---|
| 316 | for vert in vertices: |
---|
| 317 | attlist = "" |
---|
| 318 | for att in vertices_attributes[index]: |
---|
| 319 | attlist = attlist + str(att)+" " |
---|
| 320 | attlist.strip() |
---|
| 321 | fd.write(str(index) + " " |
---|
| 322 | + str(vert[0]) + " " |
---|
| 323 | + str(vert[1]) + " " |
---|
| 324 | + attlist + "\n") |
---|
| 325 | index += 1 |
---|
| 326 | |
---|
| 327 | #<# of triangles> |
---|
| 328 | n = len(triangles) |
---|
| 329 | fd.write(str(n) + " # <triangle #> [<vertex #>] [<neigbouring triangle #>] [attribute of region] ...Triangulation Triangles..." + "\n") |
---|
| 330 | |
---|
| 331 | # <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] |
---|
| 332 | for index in range(n): |
---|
| 333 | neighbors = "" |
---|
| 334 | tri = triangles[index] |
---|
| 335 | for neighbor in triangle_neighbors[index]: |
---|
| 336 | if neighbor: |
---|
| 337 | neighbors += str(neighbor) + " " |
---|
| 338 | else: |
---|
| 339 | if neighbor == 0: |
---|
| 340 | neighbors += "0 " |
---|
| 341 | else: |
---|
| 342 | neighbors += "-1 " |
---|
| 343 | if triangles_attributes[index] == ['']: |
---|
| 344 | att = "" |
---|
| 345 | else: |
---|
| 346 | att = str(triangles_attributes[index]) |
---|
| 347 | fd.write(str(index) + " " |
---|
| 348 | + str(tri[0]) + " " |
---|
| 349 | + str(tri[1]) + " " |
---|
| 350 | + str(tri[2]) + " " |
---|
| 351 | + neighbors + " " |
---|
| 352 | + att + "\n") |
---|
| 353 | |
---|
| 354 | #One line: <# of segments> |
---|
| 355 | fd.write(str(len(segments)) + |
---|
| 356 | " # <segment #> <vertex #> <vertex #> [boundary marker] ...Triangulation Segments..." + "\n") |
---|
| 357 | |
---|
| 358 | #Following lines: <segment #> <vertex #> <vertex #> [boundary marker] |
---|
| 359 | for i in range(len(segments)): |
---|
| 360 | seg = segments[i] |
---|
| 361 | fd.write(str(i) + " " |
---|
| 362 | + str(seg[0]) + " " |
---|
| 363 | + str(seg[1]) + " " |
---|
| 364 | + str(segment_markers[i]) + "\n") |
---|
| 365 | |
---|
| 366 | |
---|
| 367 | def export_trianglulation_file(ofile,gen_dict): |
---|
| 368 | """ |
---|
| 369 | write a file, ofile, with the format |
---|
| 370 | |
---|
| 371 | First line: <# of vertices> <# of attributes> |
---|
| 372 | Following lines: <vertex #> <x> <y> [attributes] |
---|
| 373 | One line: <# of triangles> |
---|
| 374 | Following lines: <triangle #> <vertex #> <vertex #> <vertex #> <neigbouring triangle #> <neigbouring triangle #> <neigbouring triangle #> [attribute of region] |
---|
| 375 | One line: <# of segments> |
---|
| 376 | Following lines: <segment #> <vertex #> <vertex #> [boundary marker] |
---|
| 377 | """ |
---|
| 378 | try: |
---|
| 379 | fd = open(ofile,'w') |
---|
| 380 | write_ASCII_trianglulation(fd,gen_dict) |
---|
| 381 | fd.close() |
---|
| 382 | except IOError, e: |
---|
| 383 | msg = 'Could not write file %s ' %fileName |
---|
| 384 | raise IOError, msg |
---|
| 385 | |
---|
| 386 | ### |
---|
| 387 | # LOADING XYA FILES |
---|
| 388 | ### |
---|
| 389 | |
---|
| 390 | |
---|
| 391 | def load_xya_file(ofile,delimiter = ','): |
---|
| 392 | """ |
---|
| 393 | load a file, ofile, with the format |
---|
| 394 | x,y, [attributes] |
---|
| 395 | """ |
---|
| 396 | try: |
---|
| 397 | fd = open(ofile) |
---|
| 398 | xya_dic = read_xya_file(fd, delimiter) |
---|
| 399 | fd.close() |
---|
| 400 | except IOError, e: |
---|
| 401 | msg = 'Could not open file %s ' %fileName |
---|
| 402 | raise IOError, msg |
---|
| 403 | return xya_dic |
---|
| 404 | |
---|
| 405 | def read_xya_file(fd, delimiter): |
---|
| 406 | lines = fd.readlines() |
---|
| 407 | points = [] |
---|
| 408 | pointattributes = [] |
---|
| 409 | if len(lines) <= 1: |
---|
| 410 | raise SyntaxError |
---|
| 411 | lines.pop(0) #remove the first (title) line |
---|
| 412 | attLength = len(clean_line(lines[0],delimiter))-2 |
---|
| 413 | |
---|
| 414 | #print "initlegth" |
---|
| 415 | #print attLength |
---|
| 416 | for line in lines: |
---|
| 417 | #print "line >%s" %line |
---|
| 418 | numbers = clean_line(line,delimiter) |
---|
| 419 | #print "numbers >%s<" %numbers |
---|
| 420 | if len(numbers) < 2 and numbers != []: |
---|
| 421 | raise SyntaxError |
---|
| 422 | if numbers != []: |
---|
| 423 | try: |
---|
| 424 | x = float(numbers[0]) |
---|
| 425 | y = float(numbers[1]) |
---|
| 426 | points.append([x,y]) |
---|
| 427 | numbers.pop(0) |
---|
| 428 | numbers.pop(0) |
---|
| 429 | attributes = [] |
---|
| 430 | if attLength != len(numbers): |
---|
| 431 | raise SyntaxError |
---|
| 432 | |
---|
| 433 | attLength = len(numbers) |
---|
| 434 | |
---|
| 435 | for num in numbers: |
---|
| 436 | num.strip() |
---|
| 437 | if num != '\n' and num != '': |
---|
| 438 | attributes.append(float(num)) |
---|
| 439 | except ValueError: |
---|
| 440 | raise SyntaxError |
---|
| 441 | pointattributes.append(attributes) |
---|
| 442 | xya_dict = {} |
---|
| 443 | xya_dict['pointlist'] = points |
---|
| 444 | xya_dict['pointattributelist'] = pointattributes |
---|
| 445 | xya_dict['segmentlist'] = [] |
---|
| 446 | xya_dict['segmentmarkerlist'] = [] |
---|
| 447 | xya_dict['regionlist'] = [] |
---|
| 448 | xya_dict['regionattributelist'] = [] |
---|
| 449 | xya_dict['regionmaxarealist'] = [] |
---|
| 450 | xya_dict['holelist'] = [] |
---|
| 451 | |
---|
| 452 | return xya_dict |
---|
| 453 | |
---|
| 454 | if __name__ == "__main__": |
---|
| 455 | m = import_mesh("tee.txt") |
---|
| 456 | print m |
---|