如何在 python 中保存三角网格

How to save a triangular mesh in python

我有一组顶点和三角形面,它们一起形成一个三角形网格:

import numpy as np

verts = [[0.1, 1.,  1. ]  [1.,  1.,  0.1]  [1.,  0.1, 1. ]  [1.,  1.,  1.9]  [1.,  1.9, 1. ]
 [1.9, 1.,  1. ] ]
faces = [[ 2,  1,  0]  [ 0,  3,  2]  [ 1,  4,  0]  [ 0,  4,  3]  [ 5,  1,  2]  [ 3,  5,  2]
 [ 5,  4,  1]  [ 4,  5,  3]]

我知道从这些三角形网格创建的唯一方法是使用 Poly3Dcollection

from mpl_toolkits.mplot3d.art3d import Poly3DCollection

myMesh = Poly3DCollection(verts[faces])

接下来,我要使用pygalmesh module to create a volume mesh. It should take in a surface mesh and output a volume mesh, like is shown here

根据教程,我应该可以使用以下方法创建体网格:

import pygalmesh

mesh = pygalmesh.generate_volume_mesh_from_surface_mesh(
    "elephant.vtu",
    facet_angle=25.0,
    facet_size=0.15,
    facet_distance=0.008,
    cell_radius_edge_ratio=3.0,
    verbose=False
)

然而,当我 运行:

mesh = pygalmesh.generate_volume_mesh_from_surface_mesh(myMesh)

我回来了:

TypeError: expected str, bytes or os.PathLike object, not Poly3DCollection

我认为这是因为我创建的网格不正确,或者我打算在使用 pygalmesh.generate_volume_mesh_from_surface_mesh 之前以某种形式保存它。我不确定。我已经在模块 github 中提出了这个问题,但还没有收到很好的反馈。

按照@Paddy Harrison 的建议,我使用了meshiowrite 函数。具体来说,对于我的数据集,它是这样完成的:

import meshio 

points = np.array(verts)
cells = [("triangle", np.array(faces)]
meshio.write_points_cells('out.vtu',points,cells)