6

凸包を視覚化するための簡単なハックに 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 で簡単に視覚化するには、CW または CCW の順序にする必要があります。

scipy.spatial に適切な時計回りの順序を計算させる簡単な方法はありますか?

4

3 に答える 3

12

したがって、このコードはうまくいくように見えますが、もっと単純になる可能性があります...基本的に、最初に船体から頂点番号を収集します。次に、平均を計算し、データセットを再センタリングして、平均からの角度で並べ替えます。

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]))]]
于 2013-02-06T09:25:55.887 に答える
9

の現在の開発ドキュメント (0.13.0.dev) にはscipy.spatial.ConvexHullvertices2D で反時計回りのプロパティがあります。

于 2013-07-27T17:20:33.530 に答える
5

素敵な方法を見つけましたが、scipy 0.11.0 (sparse.csgraph) が必要です

以下は完全な例です。実際のソートは、「sort hull ...」コメントに続く 2 つのリーニュです。

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])
于 2013-04-11T09:34:24.647 に答える