2011-06-30 32 views
7

我有一個pointlist = [P1,P2,P3 ...] 其中P1 = [X1,Y1],P2 = [X2,Y2] ...蟒蛇SciPy的德勞內繪製的點雲

我想使用scipy.spatial.Delaunay在這些點雲上做三角測量,然後繪製它

我該怎麼做?

爲德勞內的文檔非常稀缺

到目前爲止,我有這個代碼

from subprocess import Popen, PIPE 
import os 


os.environ['point_num'] = "2000" 

cmd = 'rbox $point_num D2 | tail -n $point_num' 
sub_process = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE) 
output = sub_process.communicate() 
points = [line.split() for line in output[0].split('\n') if line] 
x = [p[0] for p in points if p] 
y = [p[1] for p in points if p] 

import matplotlib.pyplot as plt 
plt.plot(x,y,'bo') 

from scipy.spatial import Delaunay 

dl = Delaunay(points) 
convex = dl.convex_hull 

from numpy.core.numeric import reshape,shape 
convex = reshape(convex,(shape(convex)[0]*shape(convex)[1],1)) 
convex_x = [x[i] for i in convex] 
convex_y = [y[i] for i in convex] 

plt.plot(convex_x,convex_y,'r') 
plt.show() 

感謝

+0

樣式提示:用'numpy import'替換''numpy.core.numeric import ...'' - 在Python中通常沒有嚴格的私有區域,但是從最上面的名稱空間導入是個好習慣可能。此外,你試圖繪製什麼 - 凸包,或delaunay三角測量(我的第一個答案是,在你的示例代碼之前......) –

+0

謝謝你的提示男人!我不知道這件事! – osager

回答

14

編輯:劇情也凸包

import numpy as np 
from scipy.spatial import Delaunay 

points = np.random.rand(30, 2) # 30 points in 2-d 
tri = Delaunay(points) 

# Make a list of line segments: 
# edge_points = [ ((x1_1, y1_1), (x2_1, y2_1)), 
#     ((x1_2, y1_2), (x2_2, y2_2)), 
#     ... ] 
edge_points = [] 
edges = set() 

def add_edge(i, j): 
    """Add a line between the i-th and j-th points, if not in the list already""" 
    if (i, j) in edges or (j, i) in edges: 
     # already added 
     return 
    edges.add((i, j)) 
    edge_points.append(points[ [i, j] ]) 

# loop over triangles: 
# ia, ib, ic = indices of corner points of the triangle 
for ia, ib, ic in tri.vertices: 
    add_edge(ia, ib) 
    add_edge(ib, ic) 
    add_edge(ic, ia) 

# plot it: the LineCollection is just a (maybe) faster way to plot lots of 
# lines at once 
import matplotlib.pyplot as plt 
from matplotlib.collections import LineCollection 

lines = LineCollection(edge_points) 
plt.figure() 
plt.title('Delaunay triangulation') 
plt.gca().add_collection(lines) 
plt.plot(points[:,0], points[:,1], 'o', hold=1) 
plt.xlim(-1, 2) 
plt.ylim(-1, 2) 

# -- the same stuff for the convex hull 

edges = set() 
edge_points = [] 

for ia, ib in tri.convex_hull: 
    add_edge(ia, ib) 

lines = LineCollection(edge_points) 
plt.figure() 
plt.title('Convex hull') 
plt.gca().add_collection(lines) 
plt.plot(points[:,0], points[:,1], 'o', hold=1) 
plt.xlim(-1, 2) 
plt.ylim(-1, 2) 
plt.show() 

請注意,使用scipy.spatial.Delaunay僅僅用於計算複雜的船體可能是矯枉過正的,因爲計算只是船體原則上可以做得比計算三角形更快。不幸的是,在Scipy中沒有界面直接用Qhull來計算外殼。

+0

美麗,謝謝 – osager

+1

謝謝!你的答案對於一個偉大的[關於alpha形狀的博客文章]有貢獻(http://sgillies.net/blog/1155/the-fading-shape-of-alpha/),這反過來又爲[優秀教程在Python中爲凹形外殼](http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/),這對我來說都是非常有幫助的! –

+0

該教程非常糟糕。這不是一個真正的凹形船體。真正的凹面是更困難的! – Bytemain