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