当比较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发布于 2011-11-21 18:35:39
scipy.spatial.Delaunay的这种行为可能与浮点算术的深刻印象有关。
如你所知,scipy.spatial.Delaunay使用C qhull库来计算Delaunay三角剖分。反过来,Qhull是Quickhull algorithm的实现,作者在this论文(1)中详细描述了这一点。您也可能知道,计算机中使用的浮点算法是使用IEEE754标准实现的(例如,您可以在Wikipedia中阅读到有关该标准的内容)。根据该标准,每个有限数最简单地由三个整数来描述:s=符号(0或1),c=有效数(或‘系数’),q=指数。用于表示这些整数的位数因数据类型而异。因此,很明显,浮点数在数值轴上的分布密度不是恒定的--数字越大,分布越松散。即使用谷歌计算器也能看到--你可以从3333333333333333和get 0中减去3333333333333334。这是因为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不能区分两个邻近的点,所以它合并了两个方面,从而成功地消除了其中一个。要消除这些错误,例如,可以尝试移动坐标原点:
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]这会将计算移动到浮点数相当密集的区域。
https://stackoverflow.com/questions/8071382
复制相似问题