初始化三角形网格,例如基于具有顶点坐标的 np 数组和具有三角形坐标的 np 数组的 VTK

Initialize triangular mesh in e.g. VTK based on np array with coordinates of vertices and np array with triangles

提问人:aaxx 提问时间:6/4/2022 更新时间:6/5/2022 访问量:718

问:

你能告诉我如何基于具有顶点坐标的 np 数组和带有三角形的 np 数组初始化三角形网格(它包含来自第一个数组的顶点索引的三元组)?

可以在 VTK 或任何其他 Python 库中完成,最终目标是对这个网格进行抽取。

谢谢。

Python NumPy 网格 VTK 抽取

评论

1赞 Ali_Sh 6/4/2022
形状是什么?如果你有点和边,你可以通过一些库来做到这一点,比如 Meshio 或可能的 scipy。准备你所拥有的小数组,坐标和边来处理。输出应该是什么,你说的 * 是什么意思,在这个网格上做抽取*?
0赞 aaxx 6/5/2022
@Ali_Sh,感谢您对 meshio 和 scipy 的回复和指点。在这种情况下,我不明白你所说的形状是什么意思——如果你是这个意思,网格是三角形的。输出应该是简化的网格 - 即网格大致保持原始网格的整体形状,但三角形少 x2。理想情况下,采用与输入相同的格式 - 带有顶点的数组和带有三角形的数组。
1赞 Ali_Sh 6/5/2022
这回答了你的问题吗?在 Python 中创建 2D 非矩形形状的三角形网格划分
1赞 Ali_Sh 6/5/2022
另一个有用的链接:stackoverflow.com/questions/26434726/...

答:

2赞 mmusy 6/5/2022 #1

使用 vedo 的解决方案,它基于 vtk:

https://github.com/marcomusy/vedo/blob/master/examples/basic/buildmesh.py

from vedo import Mesh, show

verts = [(50,50,50), (70,40,50), (50,40,80), (80,70,50)]
faces = [(0,1,2), (2,1,3), (1,0,3)]
# (the first triangle face is formed by vertex 0, 1 and 2)

# Build the polygonal Mesh object:
mesh = Mesh([verts, faces])
#mesh.decimate()
show(mesh)

enter image description here

评论

0赞 aaxx 6/5/2022
谢谢!你能告诉我是否有办法从抽取的网格中获取三角形和顶点的数组吗?
1赞 mmusy 6/5/2022
是的,只需使用 和my_decimated_mesh.points().faces()
0赞 aaxx 6/6/2022
谢谢。该库是否提供了抽取网格与原始网格差异的度量值?
0赞 mmusy 6/6/2022
嗯,是的,你可以用方法计算与原始的距离。.distanceTo()
2赞 Ali_Sh 6/5/2022 #2

您可以使用多个库:

特里梅什

Trimesh 是最好的之一;它可以在 Colab 上用于显示:

import trimesh

verts = [[50,50,50], [70,40,50], [50,40,80], [80,70,50]]
faces = [[0,1,2], [2,1,3], [1,0,3]]

mesh = trimesh.Trimesh(vertices=verts, faces=faces)
mesh.show()

# for facet in mesh.facets:
#     mesh.visual.face_colors[facet] = trimesh.visual.random_color()
# mesh.split()
# mesh.show()

enter image description here

科学皮

1. 凸壳

import scipy as sp
from scipy.spatial import ConvexHull
import matplotlib as mpl
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d as a3

verts = np.array(verts)

hull = ConvexHull(verts)
indices = hull.simplices
faces = verts[indices]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlim([35, 85])
ax.set_ylim([35, 85])
ax.set_zlim([35, 85])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

for f in faces:
    face = a3.art3d.Poly3DCollection([f])
    face.set_color(mpl.colors.rgb2hex(np.random.rand(3)))
    face.set_edgecolor('k')
    face.set_alpha(0.5)
    ax.add_collection3d(face)

plt.show()

enter image description here

2. 德劳内

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay

verts = np.array(verts)
tri = Delaunay(verts)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.azim = 70
ax.plot_trisurf(verts[:, 0], verts[:, 1], verts[:, 2], triangles=tri.simplices, cmap=plt.cm.Spectral)
plt.show()

enter image description here