簡體   English   中英

Convex Hull和SciPy

[英]Convex Hull and 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]]

數字是頂點索引。 我的問題是他們沒有訂購 我需要它們處於CW或CCW順序,以便在KML中輕松地將它們可視化。

是否有任何簡單的方法讓scipy.spatial計算正確的順時針順序?

所以這段代碼似乎可以解決問題,但可能更簡單......基本上,我首先從船體中收集頂點數。 然后我計算平均值,重新定位數據集並按平均值的角度對其進行排序。

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]))]]

在當前dev的DOC(0.13.0.dev) scipy.spatial.ConvexHull ,有一個vertices屬性,其是在逆時針方向2D。

我發現了一個很好的方法,但它需要scipy 0.11.0(sparse.csgraph)

這是一個完整的例子,實際排序是“排序船體......”評論之后的2個lignes。

import numpy as np
import scipy as sp

# random point cloud and hull
X = np.random.randint(0,200,(30,2))
hull = sp.spatial.qhull.Delaunay(X).convex_hull

# sort hull indices using (sparse) adjacency matrix graph stuff
g = sp.sparse.csr_matrix((np.ones(hull.shape[0]),hull.T), shape=(hull.max()+1,)*2)
sorted_hull = sp.sparse.csgraph.depth_first_order(g,hull[0,0],directed=False)[0]

# display with matplotlib
from matplotlib import pyplot as plt
plt.plot(X[:,0],X[:,1],'.')
plt.plot(X[sorted_hull,0],X[sorted_hull,1])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM