Skip to content

Commit

Permalink
I have added an import function to take pyvista meshes and convert th…
Browse files Browse the repository at this point in the history
…em to meshio ones for use within lyceanem.
  • Loading branch information
LyceanEM committed Nov 4, 2024
1 parent f2d6264 commit 201b985
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lyceanem/utility/mesh_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pyvista as pv
import meshio

def pyvista_to_meshio(polydata_object):
"""
Convert a pyvista object to a meshio object
"""
#extract only the triangles
if type(polydata_object )==pv.core.pointset.UnstructuredGrid:
cells =id_cells(polydata_object.cells)
else:
cells =id_cells(polydata_object.faces)
meshio_object = meshio.Mesh(
points=polydata_object.points,
cells=cells,
point_data=polydata_object.point_data,
)
return meshio_object

def id_cells(faces):
import numpy as np
cell_types ={1 :"vertex",
2 :"line",
3 :"triangle",
4 :"quad"
}
cells ={"vertex" :[],
"line" :[],
"triangle" :[],
"quad" :[]
}

while (faces.shape[0 ]>=1):
trim_num =faces[0]
temp_array =faces[1:trim_num +1]
cells[cell_types[trim_num]].append(temp_array.tolist())
faces =np.delete(faces ,np.arange(0 ,trim_num +1))

meshio_cells =[]
for key in cells:
if len(cells[key] ) >0:
meshio_cells.append((key ,cells[key]))

return meshio_cells

0 comments on commit 201b985

Please sign in to comment.