def loadDolfin(filename, exterior=False): """Reads a `Fenics/Dolfin` file format (.xml or .xdmf). Return an ``Actor(vtkActor)`` object.""" import sys if sys.version_info[0] < 3: return _loadDolfin_old(filename) import dolfin if filename.lower().endswith('.xdmf'): f = dolfin.XDMFFile(filename) m = dolfin.Mesh() f.read(m) else: m = dolfin.Mesh(filename) bm = dolfin.BoundaryMesh(m, "exterior") if exterior: poly = utils.buildPolyData(bm.coordinates(), bm.cells(), fast=True) else: polyb = utils.buildPolyData(bm.coordinates(), bm.cells(), fast=True) polym = utils.buildPolyData(m.coordinates(), m.cells(), fast=True) app = vtk.vtkAppendPolyData() app.AddInputData(polym) app.AddInputData(polyb) app.Update() poly = app.GetOutput() return Actor(poly).lw(0.1)
def loadGmesh(filename): """Reads a `gmesh` file format. Return an ``Actor(vtkActor)`` object.""" f = open(filename, "r") lines = f.readlines() f.close() nnodes = 0 index_nodes = 0 for i, line in enumerate(lines): if "$Nodes" in line: index_nodes = i + 1 nnodes = int(lines[index_nodes]) break node_coords = [] for i in range(index_nodes + 1, index_nodes + 1 + nnodes): cn = lines[i].split() node_coords.append([float(cn[1]), float(cn[2]), float(cn[3])]) nelements = 0 index_elements = 0 for i, line in enumerate(lines): if "$Elements" in line: index_elements = i + 1 nelements = int(lines[index_elements]) break elements = [] for i in range(index_elements + 1, index_elements + 1 + nelements): ele = lines[i].split() elements.append([int(ele[-3]), int(ele[-2]), int(ele[-1])]) poly = utils.buildPolyData(node_coords, elements, indexOffset=1) return Actor(poly)
def loadNeutral(filename): """Reads a `Neutral` tetrahedral file format. Return an ``Actor(vtkActor)`` object.""" f = open(filename, "r") lines = f.readlines() f.close() ncoords = int(lines[0]) coords = [] for i in range(1, ncoords + 1): x, y, z = lines[i].split() coords.append([float(x), float(y), float(z)]) ntets = int(lines[ncoords + 1]) idolf_tets = [] for i in range(ncoords + 2, ncoords + ntets + 2): text = lines[i].split() v0, v1, v2, v3 = int(text[1])-1, int(text[2])-1, int(text[3])-1, int(text[4])-1 # p0, p1, p2, p3 = np.array(coords[v1]), np.array(coords[v0]), coords[v3], coords[v2] # d10 = p1-p0 # d21 = p2-p1 # dc = np.cross(d10, d21) # print(np.dot(dc,p3-p0)) idolf_tets.append([v0, v1, v2, v3]) poly = utils.buildPolyData(coords, idolf_tets) return Actor(poly)
def __init__(self, *inputobj, **options): c = options.pop("c", "gold") alpha = options.pop("alpha", 1) exterior = options.pop("exterior", False) fast = options.pop("fast", False) computeNormals = options.pop("computeNormals", False) mesh, u = _inputsort(inputobj) if not mesh: return if exterior: import dolfin meshc = dolfin.BoundaryMesh(mesh, 'exterior') else: meshc = mesh poly = utils.buildPolyData(meshc.coordinates(), meshc.cells(), fast=fast) Actor.__init__( self, poly, c=c, alpha=alpha, computeNormals=computeNormals, ) self.mesh = mesh # holds a dolfin Mesh obj self.u = u # holds a dolfin function_data # holds the actual values of u on the mesh self.u_values = _compute_uvalues(u, mesh)
def _buildactor(d): #print('_buildactor', d) vertices = d['points'] if not len(vertices): return None cells = None lines = None keys = d.keys() if 'cells' in keys: cells = d['cells'] if 'lines' in keys: lines = d['lines'] poly = utils.buildPolyData(vertices, cells, lines) act = Mesh(poly) loadcommon(act, d) act.mapper().ScalarVisibilityOff() if 'celldata' in keys: for csc, cscname in d['celldata']: act.addCellScalars(csc, cscname) if 'normal' not in cscname.lower(): act.getCellArray(cscname) # activate if 'pointdata' in keys: for psc, pscname in d['pointdata']: act.addPointScalars(psc, pscname) if 'normal' not in pscname.lower(): act.getPointArray(pscname) # activate prp = act.GetProperty() if 'specular' in keys: prp.SetSpecular(d['specular']) if 'specularpower' in keys: prp.SetSpecularPower(d['specularpower']) if 'specularcolor' in keys: prp.SetSpecularColor(d['specularcolor']) if 'shading' in keys: prp.SetInterpolation(d['shading']) if 'alpha' in keys: prp.SetOpacity(d['alpha']) if 'opacity' in keys: prp.SetOpacity(d['opacity']) # synonym if 'pointsize' in keys and d['pointsize']: prp.SetPointSize(d['pointsize']) if 'texture' in keys and d['texture']: act.texture(d['texture']) if 'linewidth' in keys and d['linewidth']: act.lineWidth(d['linewidth']) if 'linecolor' in keys and d['linecolor']: act.lineColor(d['linecolor']) if 'representation' in keys: prp.SetRepresentation(d['representation']) if 'color' in keys and d['color']: act.color(d['color']) if 'backColor' in keys and d['backColor']: act.backColor(d['backColor']) if 'activedata' in keys and d['activedata'] is not None: act.mapper().ScalarVisibilityOn() if d['activedata'][0] == 'celldata': poly.GetCellData().SetActiveScalars(d['activedata'][1]) if d['activedata'][0] == 'pointdata': poly.GetPointData().SetActiveScalars(d['activedata'][1]) return act
def __init__(self, *inputobj, **options): c = options.pop("c", "gold") alpha = options.pop("alpha", 1) wire = options.pop("wire", True) bc = options.pop("bc", None) exterior = options.pop("exterior", False) fast = options.pop("fast", False) computeNormals = options.pop("computeNormals", False) mesh, u = _inputsort(inputobj) if not mesh: return if exterior: import dolfin meshc = dolfin.BoundaryMesh(mesh, 'exterior') else: meshc = mesh poly = utils.buildPolyData(meshc.coordinates(), meshc.cells(), fast=fast) Actor.__init__( self, poly, c=c, alpha=alpha, wire=wire, bc=bc, computeNormals=computeNormals, ) self.mesh = mesh # holds a dolfin Mesh obj self.u = u # holds a dolfin function_data self.u_values = None # holds the actual values of u on the mesh u_values = None if u: u_values = np.array([u(p) for p in self.mesh.coordinates()]) #print(u_values) if u_values is not None: # colorize if a dolfin function is passed if len(u_values.shape) == 2: if u_values.shape[1] in [2, 3]: # u_values is 2D or 3D self.u_values = u_values dispsizes = utils.mag(u_values) else: # u_values is 1D dispsizes = u_values self.addPointScalars(dispsizes, "u_values") #.mapPointsToCells()
def _loadDolfin_old(filename, exterior='dummy'): import xml.etree.ElementTree as et if filename.endswith(".gz"): import gzip inF = gzip.open(filename, "rb") outF = open("/tmp/filename.xml", "wb") outF.write(inF.read()) outF.close() inF.close() tree = et.parse("/tmp/filename.xml") else: tree = et.parse(filename) coords, connectivity = [], [] for mesh in tree.getroot(): for elem in mesh: for e in elem.findall("vertex"): x = float(e.get("x")) y = float(e.get("y")) ez = e.get("z") if ez is None: coords.append([x, y]) else: z = float(ez) coords.append([x, y, z]) tets = elem.findall("tetrahedron") if not len(tets): tris = elem.findall("triangle") for e in tris: v0 = int(e.get("v0")) v1 = int(e.get("v1")) v2 = int(e.get("v2")) connectivity.append([v0, v1, v2]) else: for e in tets: v0 = int(e.get("v0")) v1 = int(e.get("v1")) v2 = int(e.get("v2")) v3 = int(e.get("v3")) connectivity.append([v0, v1, v2, v3]) poly = utils.buildPolyData(coords, connectivity) return Actor(poly)
def loadNeutral(filename): """Reads a `Neutral` tetrahedral file format. Return an ``Actor(vtkActor)`` object.""" f = open(filename, "r") lines = f.readlines() f.close() ncoords = int(lines[0]) fdolf_coords = [] for i in range(1, ncoords + 1): x, y, z = lines[i].split() fdolf_coords.append([float(x), float(y), float(z)]) ntets = int(lines[ncoords + 1]) idolf_tets = [] for i in range(ncoords + 2, ncoords + ntets + 2): text = lines[i].split() v0, v1, v2, v3 = text[1], text[2], text[3], text[4] idolf_tets.append([int(v0) - 1, int(v1) - 1, int(v2) - 1, int(v3) - 1]) poly = utils.buildPolyData(fdolf_coords, idolf_tets) return Actor(poly)
def __init__(self, *inputobj, **options): c = options.pop("c", None) alpha = options.pop("alpha", 1) exterior = options.pop("exterior", False) fast = options.pop("fast", False) computeNormals = options.pop("computeNormals", False) mesh, u = _inputsort(inputobj) if not mesh: return if exterior: import dolfin meshc = dolfin.BoundaryMesh(mesh, 'exterior') else: meshc = mesh if hasattr(mesh, "coordinates"): coords = mesh.coordinates() else: coords = mesh.geometry.points poly = utils.buildPolyData(coords, meshc.cells(), fast=fast, tetras=True) Mesh.__init__( self, poly, c=c, alpha=alpha, computeNormals=computeNormals, ) self.mesh = mesh # holds a dolfin Mesh obj self.u = u # holds a dolfin function_data # holds the actual values of u on the mesh self.u_values = _compute_uvalues(u, mesh)
def loadOFF(filename): """Read OFF file format.""" f = open(filename, "r") lines = f.readlines() f.close() vertices = [] faces = [] NumberOfVertices = None i = -1 for text in lines: if len(text) == 0: continue if text == '\n': continue if "#" in text: continue if "OFF" in text: continue ts = text.split() n = len(ts) if not NumberOfVertices and n > 1: NumberOfVertices, NumberOfFaces = int(ts[0]), int(ts[1]) continue i += 1 if i < NumberOfVertices and n == 3: x, y, z = float(ts[0]), float(ts[1]), float(ts[2]) vertices.append([x, y, z]) ids = [] if NumberOfVertices <= i < (NumberOfVertices + NumberOfFaces + 1) and n > 2: ids += [int(xx) for xx in ts[1:]] faces.append(ids) return Actor(utils.buildPolyData(vertices, faces))
def loadPCD(filename): """Return ``vtkActor`` from `Point Cloud` file format. Return an ``Actor(vtkActor)`` object.""" f = open(filename, "r") lines = f.readlines() f.close() start = False pts = [] N, expN = 0, 0 for text in lines: if start: if N >= expN: break l = text.split() pts.append([float(l[0]), float(l[1]), float(l[2])]) N += 1 if not start and "POINTS" in text: expN = int(text.split()[1]) if not start and "DATA ascii" in text: start = True if expN != N: colors.printc("~!? Mismatch in pcd file", expN, len(pts), c="red") poly = utils.buildPolyData(pts) return Actor(poly).pointSize(4)