Return surface triangle of 3D scipy.spatial.Delaunay

后端 未结 3 867
不知归路
不知归路 2020-12-29 11:44

I have this problem. I try to triangulate points cloud by scipy.spatial.Delaunay. I used:

tri = Delaunay(points) # points: np.array() of 3d points 
indices =         


        
3条回答
  •  清酒与你
    2020-12-29 12:26

    Following Jaime's answer, but elaborating a bit more with an example:

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d as a3
    import numpy as np
    import scipy as sp
    from scipy import spatial as sp_spatial
    
    
    def icosahedron():
        h = 0.5*(1+np.sqrt(5))
        p1 = np.array([[0, 1, h], [0, 1, -h], [0, -1, h], [0, -1, -h]])
        p2 = p1[:, [1, 2, 0]]
        p3 = p1[:, [2, 0, 1]]
        return np.vstack((p1, p2, p3))
    
    
    def cube():
        points = np.array([
            [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1],
            [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1],
        ])
        return points
    
    
    points = icosahedron()
    # points = cube()
    
    hull = sp_spatial.ConvexHull(points)
    indices = hull.simplices
    faces = points[indices]
    
    print('area: ', hull.area)
    print('volume: ', hull.volume)
    
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.dist = 30
    ax.azim = -140
    ax.set_xlim([0, 2])
    ax.set_ylim([0, 2])
    ax.set_zlim([0, 2])
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
    
    for f in faces:
        face = a3.art3d.Poly3DCollection([f])
        face.set_color(mpl.colors.rgb2hex(sp.rand(3)))
        face.set_edgecolor('k')
        face.set_alpha(0.5)
        ax.add_collection3d(face)
    
    plt.show()
    

    Which should depict the following figure:

提交回复
热议问题