working 2D gmsh parser

This commit is contained in:
Stephen McQuay 2010-10-22 09:20:59 -06:00
parent e4988fd150
commit 91c8a661f4
2 changed files with 52 additions and 33 deletions

View File

@ -1,52 +1,69 @@
#!/usr/bin/env python
import atexit
import os
import sys
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
import sys
import pickle
from pudb import set_trace
from itertools import combinations
import numpy as np
THREE_NODE_TRIANGLE = 2
class point(object):
def __init__(self, index, x, y, z):
def __init__(self, xyz):
self.index = index
self.x = x
self.y = y
self.z = z
self.points = []
self.xyz = xyz
self.faces = []
def __getitem__(self, i):
if isinstance(i, int):
if i == 0:
return self.x
return self.xyz[0]
elif i == 1:
return self.y
return self.xyz[1]
elif i == 2:
return self.z
return self.xyz[2]
else:
raise IndexError("there are only (x,y,z)")
elif isinstance(i, str):
if i == 'x':
return self.x
elif i == 'y':
return self.y
elif i == 'z':
return self.z
else:
raise IndexError
else:
raise TypeError("only p.x, p[0], and p['x'] access allowed")
raise TypeError("only point.x and point[0] access allowed")
def _getx(self):
return self.xyz[0]
x = property(_getx)
def _gety(self):
return self.xyz[1]
y = property(_gety)
def _getz(self):
return self.xyz[2]
z = property(_getz)
def __str__(self):
return '(%f, %f, %f)' % (self.x, self.y, self.z)
return '%s' % self.xyz
__repr__ = __str__
class face(object):
def __init__(self, index):
self.index = index
def __init__(self):
self.neighbors = []
self.points = []
if __name__ == '__main__':
if len(sys.argv) != 2:
@ -65,6 +82,7 @@ if __name__ == '__main__':
node_count = int(gmsh_file.readline())
points = []
nppoints = np.zeros((node_count, 3))
for i in xrange(node_count):
cur_line = gmsh_file.readline()
(index, x,y,z) = cur_line.split()
@ -73,34 +91,35 @@ if __name__ == '__main__':
y = float(y)
z = float(z)
points.append(point(index, x,y,z))
nppoints[i][0] = x
nppoints[i][1] = y
nppoints[i][2] = z
points.append(point(nppoints[i]))
gmsh_file.readline() # $EndNodes
gmsh_file.readline() # $Elements
element_count = int(gmsh_file.readline())
faces = {}
faceobjs = {}
faces_for_point = {}
neighbors = {}
for i in xrange(element_count):
cur_line = gmsh_file.readline()
cur_line = cur_line.split()
cur_face_index, type, rest = (int(cur_line[0]),
int(cur_line[1]),
[int(j) for j in cur_line[2:]])
cur_face_index, node_type, rest = (int(cur_line[0]),
int(cur_line[1]),
[int(j) for j in cur_line[2:]])
if(type == THREE_NODE_TRIANGLE):
if(node_type == THREE_NODE_TRIANGLE):
points_for_cur_face = [i-1 for i in rest[rest[0]+1:]]
cur_face = face(cur_face_index)
cur_face = face()
for cur_point in points_for_cur_face:
points[cur_point].faces.append(cur_face_index)
faces[cur_face_index] = points_for_cur_face
cur_face.points = points_for_cur_face
faceobjs[cur_face_index] = cur_face
@ -119,4 +138,4 @@ if __name__ == '__main__':
faceobjs[v[1]].neighbors.append(v[0])
pickle.dump([(p[0], p[1], p[2]) for p in points], open('/tmp/points.p', 'w'))
pickle.dump(faces , open('/tmp/faces.p', 'w'))
pickle.dump([f.points for f in faceobjs.itervalues()], open('/tmp/faces.p', 'w'))

View File

@ -4,7 +4,7 @@ import bpy
import pickle
points = pickle.load(open('/tmp/points.p', 'r'))
faces = pickle.load(open('/tmp/faces.p', 'r'))
faces = [faces[i] for i in faces]
# faces = [faces[i] for i in faces]
me = bpy.data.meshes.new('points')
me.verts.extend(points)
me.faces.extend(faces)