凸壳和SciPy [英] Convex Hull and SciPy

查看:194
本文介绍了凸壳和SciPy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以使用下面的代码得到凸包:

我试图使用scipy(0.10.1)来快速入侵以显示凸包。 :

  vecs = [[-0.094218,51.478927],[-0.09348,51.479364],[-0.094218,51.478927],
...
[-0.094218,51.478927],[-0.094321,51.479918],[-0.094218,51.478927],
[-0.094222,51.478837],[-0.094241,51.478388] -0.094108,51.478116],
[-0.09445,51.480279],[-0.094256,51.478028],[-0.094326,51.500511]]
hull = scipy.spatial.Delaunay(vecs).convex_hull

结果数组如下所示:

  [[56,9],[16,1],[56,1],[55,9],[53,55],[53,16]] 

这些数字是顶点索引。我的问题是他们没有订购。我需要他们是顺时针或逆时针顺序,以便于在KML中可视化。



是否有任何简单的方法让scipy.spatial计算顺时针为什么这个代码似乎能做到这一点,但可能会更简单...
本质上来说,我首先从船体收集顶点号码。然后我计算平均值,重新计算数据集,并按平均值的角度对其进行排序。

  ps = set()
for x,y in shell:
ps.add(x)
ps.add(y)
ps = numpy.array(list(ps))
center = vecs [ps] .mean(axis = 0)
A = vecs [ps] - center
h = vecs [ps [numpy.arctan2(A [:,1],A [: ,0]))]]


I'm trying to use scipy (0.10.1) for a quick hack to visualize the convex hull.

I can get the convex hull using the following code:

vecs = [[-0.094218, 51.478927], [-0.09348,  51.479364], [-0.094218, 51.478927],
        ...
        [-0.094218, 51.478927], [-0.094321, 51.479918], [-0.094218, 51.478927],
        [-0.094222, 51.478837], [-0.094241, 51.478388], [-0.094108, 51.478116],
        [-0.09445,  51.480279], [-0.094256, 51.478028], [-0.094326, 51.500511]]
hull = scipy.spatial.Delaunay(vecs).convex_hull

the resulting array looks like this:

[[56,  9], [16,  1], [56,  1], [55,  9], [53, 55], [53, 16]]

the numbers are the vertex indices. My problem is they are not ordered. I'd need them to be in CW or CCW order in order to easily visualize them in KML.

Is there any easy way to have scipy.spatial compute the proper clockwise order?

解决方案

So this code seems to do the trick, but could be simpler... Essentially, I first collect the vertex numbers from the hull. Then I compute the mean, recenter the dataset and sort it by the angle from the mean.

ps = set()
for x, y in hull:
    ps.add(x)
    ps.add(y)
ps = numpy.array(list(ps))
center = vecs[ps].mean(axis=0)
A = vecs[ps] - center
h = vecs[ps[numpy.argsort(numpy.arctan2(A[:,1], A[:,0]))]]

这篇关于凸壳和SciPy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆