【问题标题】:How to save a triangular mesh in python如何在python中保存三角形网格
【发布时间】:2020-05-08 11:57:45
【问题描述】:

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

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 模块来创建体积网格。它应该接受一个表面网格并输出一个体积网格,如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 中提出了这个问题,但还没有收到好的反馈。

【问题讨论】:

标签: python mesh mplot3d


【解决方案1】:

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

import meshio 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多