目前介绍Delaunay的中文教程不多,基本都是零零散散的代码实例,很少有系统的函数参数介绍。晚上用了50分钟,把英文教程翻译了一遍。
函数参数:
class scipy.spatial.Delaunay(points, furthest_site=False, incremental=False, qhull_options=None)
Delaunay tesselation in N dimensions.
参数解释:
points : ndarray of floats, shape (npoints, ndim)
把点转化为三角形。
furthest_site : bool, optional
是否计算一个最远的Delaunay三角形,默认为否。这是版本 0.12.0.中的新参数。
incremental : bool, optional
增量地添加新点,需要额外的资源.
qhull_options : str, optional
Additional options to pass to Qhull. See Qhull manual for details. Option “Qt” is always enabled. Default:”Qbb Qc Qz Qx” for ndim > 4 and “Qbb Qc Qz” otherwise. Incremental mode omits “Qz”.。这是版本 0.12.0.中的新参数。
使用示范:
# 点集的Delaunay三角划分:
>>>
>>> points = np.array([[0, 0], [0, 1.1], [1, 0], [1, 1]])
>>> from scipy.spatial import Delaunay
>>> tri = Delaunay(points)
# 画出来:
>>>
>>> import matplotlib.pyplot as plt
>>> plt.triplot(points[:,0], points[:,1], tri.simplices.copy())
>>> plt.plot(points[:,0], points[:,1], 'o')
>>> plt.show()
介绍一下单纯型 (simplex) 和三角剖分 (triangulation) :
单纯形是代数拓扑中最基本的概念。单纯形是三角形和四面体的一种泛化,一个k维单纯形是指包含 k+1个节点的凸多面体。
人们希望能够把一个拓扑对象剖分成许多个小的单纯形,要求任何两个相邻的单纯形相交的公共部分仍是一个单纯形.这种剖分称为(曲)单纯剖分。在曲面情形,就是熟知的三角剖分。
# 点的索引、三角划分后的两个三角形的坐标
>>> tri.simplices # 三角形中顶点的索引
array([[3, 2, 0],
[3, 1, 0]], dtype=int32)
>>> points[tri.simplices] # 三角形坐标
array([[[ 1. , 1. ],
[ 1. , 0. ],
[ 0. , 0. ]],
[[ 1. , 1. ],
[ 0. , 1.1],
[ 0. , 0. ]]])
# 三角形0是三角形1的唯一邻居, 它和三角形1的顶点1方向相反
>>> tri.neighbors[1] # 查看三角形1的邻居,不存在为-1
array([-1, 0, -1], dtype=int32)
>>> points[tri.simplices[1,1]] # 查看第2个三角形的第2个顶点坐标
array([ 0. , 1.1])
# 找出点在哪个三角形中
>>> p = np.array([(0.1, 0.2), (1.5, 0.5)])
>>> tri.find_simplex(p)
array([ 1, -1], dtype=int32)
# 计算三角形1中这些点的重心坐标 (barycentric coordinates)
>>> b = tri.transform[1,:2].dot(p - tri.transform[1,2])
>>> np.c_[b, 1 - b.sum(axis=1)] # 矩阵列相加, 要求行相等
array([[ 0.1 , 0.2 , 0.7 ],
[ 1.27272727, 0.27272727, -0.54545455]])
# 注: 第一个点的坐标是正的,表示它确实在三角形内
一些属性:
transform | 从 x 到重心坐标 c 的仿射变换. |
vertex_to_simplex | 查找数组, 从一个点找到它所在的单纯型 |
convex_hull | 构成凸包的面状点集 Vertices of facets forming the convex hull of the point set. |
vertex_neighbor_vertices | 点的邻居节点 |
points | (ndarray of double, shape (npoints, ndim)) 点集合 |
simplices | (ndarray of ints, shape (nsimplex, ndim+1)) 构成单纯型的点的索引. 对二维来说,点是朝着逆时针方向的. |
neighbors | (ndarray of ints, shape (nsimplex, ndim+1)) 每个单纯型的邻居单纯型的索引. 第k个邻居位于第k个节点的相反方向. 对于边界单纯型, -1表示无邻居. |
equations | (ndarray of double, shape (nsimplex, ndim+2)) [normal, offset] 构成抛物面上的小面的超平面方程 |
paraboloid_scale, paraboloid_shift | (float) 额外抛物面维度的范围、移动 |
coplanar | (ndarray of int, shape (ncoplanar, 3)) 共面点的索引 and the 最近共面和最近点的索引. 共面点是输入点,由于数值精度问题不在三角划分中. If option “Qc” is not specified, this list is not computed. .. versionadded:: 0.12.0 |
vertices | 和simplices相似,不赞成使用 |
方法:
add_points(points[, restart]) | 增加新点 |
close() | 完成增量过程 |
find_simplex(self, xi[, bruteforce, tol]) | 找到包含指定点的单纯型 |
lift_points(self, x) | Lift points to the Qhull paraboloid. 指向Qhull抛物面的升力点 |
plane_distance(self, xi) | 计算点 xi 到所有单纯型的超平面距离 |