From 201b9851c1bb57095583410a5f428d4e6c311674 Mon Sep 17 00:00:00 2001 From: LyceanEM <60020395+LyceanEM@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:33:01 +0000 Subject: [PATCH] I have added an import function to take pyvista meshes and convert them to meshio ones for use within lyceanem. --- lyceanem/utility/mesh_functions.py | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lyceanem/utility/mesh_functions.py diff --git a/lyceanem/utility/mesh_functions.py b/lyceanem/utility/mesh_functions.py new file mode 100644 index 0000000..4ebceb9 --- /dev/null +++ b/lyceanem/utility/mesh_functions.py @@ -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 \ No newline at end of file