3

在比较 scipy 的(0.9.0)和 matplotlib 的(1.0.1)Delaunay 三角测量例程时,我注意到一个无法解释的行为。我的点是存储在numpy.array([[easting, northing], [easting, northing], [easting, northing]]). 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
4

1 回答 1

6

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

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

现在,了解了舍入误差,我们可能想阅读论文 (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]

这会将计算移动到浮点数相当密集的区域。

于 2011-11-21T10:35:39.437 回答