python - 返回 3D scipy.spatial.Delaunay 的表面三角形

标签 python scipy cloud points delaunay

我有这个问题。我尝试通过 scipy.spatial.Delaunay 对点云进行三角测量。我用过:

tri = Delaunay(points) # points: np.array() of 3d points 
indices = tri.simplices
vertices = points[indices]

但是,这段代码返回四面体。怎么可能只返回表面的三角形?

谢谢

最佳答案

要使其以代码形式工作,您必须将表面参数化为二维。例如,在球 (r,theta, psi) 的情况下,半径是常数(将其删除)并且点由二维的 (theta,psi) 给出。

Scipy Delaunay 是 N 维三角剖分,因此如果您提供 3D 点,它会返回 3D 对象。给它 2D 点,它返回 2D 对象。

下面是我用来为 openSCAD 创建多面体的脚本。 U 和 V 是我的参数化(x 和 y),这些是我给 Delaunay 的坐标。请注意,现在“Delaunay 三角剖分属性”仅适用于 u、v 坐标(角度在 uv 空间而非 xyz 空间等中最大化)。

示例是来自 http://matplotlib.org/1.3.1/mpl_toolkits/mplot3d/tutorial.html 的修改副本最初使用Triangulation 函数(最终映射到 Delaunay?)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.tri as mtri
from scipy.spatial import Delaunay

# u, v are parameterisation variables
u = np.array([0,0,0.5,1,1]) 
v = np.array([0,1,0.5,0,1]) 

x = u
y = v
z = np.array([0,0,1,0,0])

# Triangulate parameter space to determine the triangles
#tri = mtri.Triangulation(u, v)
tri = Delaunay(np.array([u,v]).T)

print 'polyhedron(faces = ['
#for vert in tri.triangles:
for vert in tri.simplices:
    print '[%d,%d,%d],' % (vert[0],vert[1],vert[2]),
print '], points = ['
for i in range(x.shape[0]):
    print '[%f,%f,%f],' % (x[i], y[i], z[i]),
print ']);'


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

# The triangles in parameter space determine which x, y, z points are
# connected by an edge
#ax.plot_trisurf(x, y, z, triangles=tri.triangles, cmap=plt.cm.Spectral)
ax.plot_trisurf(x, y, z, triangles=tri.simplices, cmap=plt.cm.Spectral)


plt.show()

Pyramid what the above example plots

下面是(稍微更结构化的)文本输出:

polyhedron(
    faces = [[2,1,0], [3,2,0], [4,2,3], [2,4,1], ], 

    points = [[0.000000,0.000000,0.000000], 
              [0.000000,1.000000,0.000000], 
              [0.500000,0.500000,1.000000], 
              [1.000000,0.000000,0.000000], 
              [1.000000,1.000000,0.000000], ]);

关于python - 返回 3D scipy.spatial.Delaunay 的表面三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434726/

相关文章:

macos - 在 Mac OS Lion 上为 Python3 构建 numpy/scipy

SQL Azure 联合和原子单元标识

python - 如何将 Python 参数化 MySQL 语句与动态 SELECT 语句结合起来?

python - 用单个句号替换多个句号

python - Python Numpy 数组声明何时没有参数?

Python MySQLdb 获取连接详细信息

python - 根据分组列值对表元素进行分组

python - 结果稀疏性已知时的稀疏矩阵乘法(在 python|scipy|cython 中)

android - 如何将 Android 应用程序数据持久保存到 Heroku 兼容的数据库中?

azure - terraform 无法识别计数值