简体   繁体   English

从点云的3D凸包

[英]3D convex hull from point cloud

I need to plot a 3D point cloud (number of points: N), then a convex hull (actually a polyhedron with N vertices) from the points. 我需要从点绘制一个3D点云(点数:N),然后绘制一个凸包(实际上是一个带有N个顶点的多面体)。 I made a script in python with scipy.spatial ConvexHull for plot 8 points and plot a cube, the plot of the point cloud is ok, but the cube is not ok, because the code puts two lines going across the diagonal face of the cube in addition to the edge lines. 我在python中使用scipy.spatial ConvexHull创建了一个脚本,用于绘制8个点并绘制一个立方体,点云的图是可以的,但是立方体不好,因为代码将两条线穿过立方体的对角线面除了边缘线。 I don't understand why plot lines across faces. 我不明白为什么要在脸上画线。

The script: 剧本:

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

Result of the script: 脚本的结果:

在此输入图像描述

I know this is old, but I came here from Google so I think others might too. 我知道这已经过时了,但我是从Google来到这里的,所以我认为其他人也可能。

The problem is only in the plotting method you use. 问题仅出在您使用的绘图方法中。 One simplex is a nD triangle defined by 3 points. 一个单纯形是由3个点定义的nD三角形。 But the plotting function must cycle back to the last point, otherwise only 2 of 3 simplex edges are drawn. 但是绘图功能必须循环回到最后一个点,否则只绘制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()

图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM