【问题标题】:3D convex hull from point cloud来自点云的 3D 凸包
【发布时间】:2015-01-31 22:37:21
【问题描述】:

我需要绘制一个 3D 点云(点数:N),然后从这些点绘制一个凸包(实际上是一个具有 N 个顶点的多面体)。我用 scipy.spatial ConvexHull 在 python 中制作了一个脚本,用于绘制 8 个点并绘制一个立方体,点云的图还可以,但立方体不行,因为代码将两条线穿过立方体的对角面除了边缘线。我不明白为什么要在脸上画线。

脚本:

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

脚本结果:

【问题讨论】:

    标签: python plot 3d


    【解决方案1】:

    我知道这已经过时了,但我是从 Google 来到这里的,所以我认为其他人也可能如此。

    问题仅在于您使用的绘图方法。 一个单纯形是由 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"]:
        eval("ax.set_{:s}label('{:s}')".format(i, i))
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 2012-02-12
      • 2019-08-22
      • 2017-04-29
      • 2012-11-21
      • 1970-01-01
      • 2015-06-06
      • 2017-02-04
      相关资源
      最近更新 更多