我想使用Delaunay算法对3D点云进行三角剖分。为了测试我的代码,我从一个STL文件中提取点云,然后尝试重新生成它。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Delaunay
#--------------def funtion extract point cloud-------------------
def point_cloud(inp):
node = []
for line in inp:
temp1 = line.strip()
x = temp1.split()
if x[0] == "vertex":
del x[0]
node.append(x)
node = set(map(tuple,node))
return node
#--------------------end function---------------------------------
with open("D:\\cilinder.stl","r") as fo:
pc = point_cloud(fo)
u = []
v = []
w = []
for l in pc:
u.append(float(l[0]))
v.append(float(l[1]))
w.append(float(l[2]))
ua = np.array(u)
va = np.array(v)
#tri = mtri.Triangulation(u, v)
tri = Delaunay(np.array([u,v]).T)
points = []
vertex = []
for i in range(ua.shape[0]):
points.append([ua[i],va[i],w[i]])
for vert in tri.simplices:
#for vert in tri.triangles:
vertex.append(vert)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.plot_trisurf(ua, va, w, triangles=tri.simplices, cmap=plt.cm.Spectral)
#ax.plot_trisurf(ua, va, w, triangles=tri.triangles, cmap=plt.cm.Spectral)
plt.show()
运行此代码后,我得到了以下结果:
结果:
![enter image description here](https://istack.dev59.com/4b9FA.webp)
![enter image description here](https://istack.dev59.com/AdXYd.webp)
编辑:我刚刚发现函数point_cloud提供了重复的点,因为它们直接从STL文件中提取出来。
Delaunay
无法知道哪些连接应该是结果形状的“外部”。它只是连接一些点,导致你得到了那个看起来有趣的东西。 - Nico Schlömer