gpt4 book ai didi

python - 在 scipy.spatial.Delaunay 附近时遗漏的点

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:21 24 4
gpt4 key购买 nike

在比较 scipy (0.9.0) 和 matplotlib (1.0.1) 的 Delaunay 三角剖分例程时,我注意到一个无法解释的行为。我的点是存储在 numpy.array([[easting, northing], [easting, northing], [easting, northing]]) 中的 UTM 坐标。 Scipy 的边缘缺少我的一些观点,而 matplotlib 的都在那里。有没有解决办法,还是我做错了什么?

import scipy
import numpy
from scipy.spatial import Delaunay
import matplotlib.delaunay


def delaunay_edges(points):
d = scipy.spatial.Delaunay(points)
s = d.vertices
return numpy.vstack((s[:,:2], s[:,1:], s[:,::-2]))


def delaunay_edges_matplotlib(points):
cens, edges, tri, neig = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
return edges


points = numpy.array([[500000.25, 6220000.25],[500000.5, 6220000.5],[500001.0, 6220001.0],[500002.0, 6220003.0],[500003.0, 6220005.0]])

edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)

numpy.unique(edges1).shape # Some points missing, presumably nearby ones
numpy.unique(edges2).shape # Includes all points

最佳答案

scipy.spatial.Delaunay 的这种行为可能与浮点运算的含义有关。

如您所知,scipy.spatial.Delaunay 使用 C qhull 库来计算 Delaunay 三角剖分。 QhullQuickhull algorithm 的实现,作者在 this 中对此进行了详细描述论文 (1)。您也可能知道计算机中使用的浮点运算是使用 IEEE 754 标准执行的(例如,您可以在 Wikipedia 中阅读相关内容)。根据标准,每个有限数最简单地由三个整数描述:s= 符号(零或一),c= 有效数(或“系数”) , q= 一个指数。用于表示这些整数的位数因数据类型而异。因此,很明显, float 在数值轴上的分布密度不是恒定的——数字越大,分布越松散。即使用谷歌计算器也能看出来——你可以从 3333333333333334 和 get 0 中减去 3333333333333333。 .发生这种情况是因为 3333333333333333 和 3333333333333334 都四舍五入为相同的 float 。

现在,了解舍入误差后,我们可能想阅读论文 (1) 的第 4 章,标题为Copying with impresicion。本章介绍一种处理舍入误差的算法:

Quickhull partitions a point and determines its horizon facets by computing 
whether the point is above or below a hyperplane. We have assumed that
computations return consistent results ... With floating-point arithmetic, we
cannot prevent errors from occurring, but we can repair the damage after
processing a point. We use brute force: if adjacent facets are nonconvex, one of
the facets is merged into a neighbor. Quickhull merges the facet that minimizes
the maximum distance of a vertex to the neighbor.

这就是可能发生的情况 - Quickhull 无法区分 2 个附近的点,因此它合并了两个面,从而成功地消除了其中一个。要消除这些错误,您可以尝试移动坐标原点:

co = points[0]
points = points - co

edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)

print numpy.unique(edges1)
>>> [0 1 2 3 4]
print numpy.unique(edges2)
>>> [0 1 2 3 4]

这会将计算转移到 float 相当密集的区域。

关于python - 在 scipy.spatial.Delaunay 附近时遗漏的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com