2014-12-03 30 views
6

我需要繪製一個3D點雲(點數:N),然後從這些點開始繪製一個凸包(實際上是一個帶有N個頂點的多面體)。我在python中用scipy.spatial ConvexHull創建了一個腳本,繪製了8個點並繪製了一個立方體,點雲的圖是可以的,但是立方體並不正確,因爲代碼會將兩條線穿過立方體的對角面除了邊緣線。我不明白爲什麼劇情會橫跨臉部。來自點雲的3D凸包

腳本:

import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
from scipy.spatial import ConvexHull 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

points= np.array([[0,0,0], 
      [4,0,0], 
      [4,4,0], 
      [0,4,0], 
      [0,0,4], 
      [4,0,4], 
      [4,4,4], 
      [0,4,4]]) 

hull=ConvexHull(points) 

edges= zip(*points) 

for i in hull.simplices: 
    plt.plot(points[i,0], points[i,1], points[i,2], 'r-') 

ax.plot(edges[0],edges[1],edges[2],'bo') 

ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_zlabel('z') 

ax.set_xlim3d(-5,5) 
ax.set_ylim3d(-5,5) 
ax.set_zlim3d(-5,5) 

plt.show() 

腳本的結果:

enter image description here

+0

如何編輯我的問題?我從第一行忘了「Hi All」,如果我點擊「編輯」並且想保存更改,則什麼都不會發生。 – Feri 2014-12-03 11:35:52

回答

3

我知道這是老了,但我來到這裏,從谷歌,所以我認爲其他人可能也。

該問題僅在您使用的繪圖方法中出現。 一個單純形是由3個點定義的nD三角形。 但繪圖功能必須循環回到最後一點,否則只繪製3個單面邊中的2個。

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
from scipy.spatial import ConvexHull 


# 8 points defining the cube corners 
pts = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], 
       [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1], ]) 

hull = ConvexHull(pts) 

fig = plt.figure() 
ax = fig.add_subplot(111, projection="3d") 

# Plot defining corner points 
ax.plot(pts.T[0], pts.T[1], pts.T[2], "ko") 

# 12 = 2 * 6 faces are the simplices (2 simplices per square face) 
for s in hull.simplices: 
    s = np.append(s, s[0]) # Here we cycle back to the first coordinate 
    ax.plot(pts[s, 0], pts[s, 1], pts[s, 2], "r-") 

# Make axis label 
for i in ["x", "y", "z"]: 
    [enter image description here][1]eval("ax.set_{:s}label('{:s}')".format(i, i)) 

plt.show()