我正在創建一個3D打印機切片算法在Python中。該算法使用numpy-STL將.stl加載到numpy數組中,然後計算某個水平面與網格三角形的交點。我已成功設法計算交點並將交點存儲在另一個numpy數組(格式:[x1 y1 z1 x2 y2 z2 x3 y3 z3])中。然後,我爲每個切片提取x個矢量和y個矢量,並存儲它們進行繪圖。Matplotlib填充切片
嘗試對這些切片進行可視化時會出現問題。如果我用繪製散點圖:
import matplotlib as plt
plt.scatter(x_vec,y_vec)
我得到:這是我所期待什麼。
但是,當我嘗試使用連接段:
plt.plot(x_vec,y_vec)
我得到的不是本地彼此點之間的線路連接怪異:。我試圖在這裏切片的形狀是一個簡單的環。當直接檢查散點圖時以及手工梳理點數據時,我看不到任何無關的點,它們都顯示爲正確排列。
這是matplotlib如何連接最近的線問題?如果我使用fill_between
,會很困惑哪些部分是內部的,哪些不是。如果我查看堆疊在3D中的多個切片,mplot3D是否有解決方案?我試過這個:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x_vec, y_vec, z_vec, color = 'b')
但它只是在3D中的同樣的事情。任何關於可視化這些切片的建議?其他軟件包等?
我認爲連接的線是切片機如何返回numpy數據的結果。此函數採用的(M,9)的三角形陣列,並返回任一(1,3)(1,6)或根據條件(1,9)陣列:
def get_intersects(tri_entry, layer_h):
# Calculate intersections for current triangles
# 4 Cases
# Case 1: 3 vertices on plane
if check_points(tri_entry[2::3], layer_h) == 3:
return tri_entry
# Case 2: 2 vertices on plane
if check_points(tri_entry[2::3], layer_h) == 2:
return coords_on_plane(tri_entry, layer_h)
# Case 3: 1 vertex on plane
if check_points(tri_entry[2::3], layer_h) == 1:
# 2 Sub cases
# (1) Other 2 points on opposited sides of slice
if check_z(tri_entry, layer_h) == 0:
intersect = vec_intersect(tri_entry, layer_h)
return np.hstack((intersect, coords_on_plane(tri_entry, layer_h)))
# (2) Other 2 points on same side of slice
if check_z(tri_entry, layer_h) == 1:
return coords_on_plane(tri_entry, layer_h)
# Case 4: 0 vertices on plane
if check_points(tri_entry[2::3], layer_h) == 0:
# Check which lines interesct
a = vec_intersect(tri_entry[0:6], layer_h)
b = vec_intersect(tri_entry[3:9], layer_h)
c = vec_intersect(tri_entry[[0,1,2,6,7,8]], layer_h)
intersect = np.hstack((a, b, c))
intersect = list(filter(None.__ne__, intersect))
return np.asarray(intersect)
試圖通過向量化表的所有x和y的沒有考慮點的依賴是我出錯的地方。我試圖單獨繪製每個案例,並發現:Zoomed-colored
單點呈紅色,兩點呈綠色,3點呈藍色。
感謝您的答覆!我已經爲我的問題添加了一些更多描述,並將嘗試使用plt.Polygon來處理三點情況。 – Jetpack