python - 如何用8个点形成一个平面?

标签 python arrays 3d surface plane

我已经用长方体的中心绘制了 8 个角点,如图 8_corner_points_with center

我尝试过散点图,但现在我想要连接这 8 个点的曲面图。

当我尝试使用表面图时,我无法注意到这一点。您能否建议任何解决方案

l = 0.3 
w = 0.4
h = 0.1
center = 
 [2.10737, -0.100085, 0.716869]
F= 
   [[array([[1.]]) array([[-0.001]]) array([[-0.017]])]
 [array([[0.]]) array([[-0.999]]) array([[0.037]])]
 [array([[0.017]]) array([[0.037]]) array([[0.999]])]]


def cuboid(center, size):
    ox, oy, oz = center
    l, w, h = size

    ax = fig.gca(projection='3d') ##plot the project cuboid

    X=[ox-l/2,ox-l/2,ox-l/2,ox-l/2,ox+l/2,ox+l/2,ox+l/2,ox+l/2]
    Y=[oy+w/2,oy-w/2,oy-w/2,oy+w/2,oy+w/2,oy-w/2,oy-w/2,oy+w/2]
    Z=[oz-h/2,oz-h/2,oz+h/2,oz+h/2,oz+h/2,oz+h/2,oz-h/2,oz-h/2]

    X_new = ([])
    Y_new = ([])
    Z_new = ([])

    for i in range(0,8):

        c=np.matrix([[X[i]],
                    [Y[i]],
                    [Z[i]]])
        u=F*c

        X_new = np.append(X_new, u.item(0))
        Y_new = np.append(Y_new, u.item(1))
        Z_new = np.append(Z_new, u.item(2))

    ax.scatter(X_new,Y_new,Z_new,c='darkred',marker='o')  #the  plot of points after rotated
    ax.scatter(ox,oy,oz,c='crimson',marker='o')  #the previous plot of points before rotated



    ## Add title 
    plt.title('Plot_for_PSM', fontsize=20)
    plt.gca().invert_yaxis()

    ##labelling the axes
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')


最佳答案

enter image description here

这里有一个解决方案。

###Added import
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection

###Addded size and center in this format in order to manipulate them easily
size = [0.3, 0.4, 0.1]
center = [2.10737, -0.100085, 0.716869]

###This numpy vector will be used to store the position of the sides
side = np.zeros((8,3))

###Just re-ordered your matrix in some np.arrays
F = [[np.array([1., -0.001, -0.017])],
    [np.array([0., -0.999, 0.037])],
    [np.array([0.017, 0.037, 0.999])]]

def cuboid(center, size):
    ox, oy, oz = center
    l, w, h = size

    ###Added the fig in order to be able to plot it later
    fig = plt.figure()
    ax = fig.gca(projection='3d') ##plot the project cuboid

    X=[ox-l/2,ox-l/2,ox-l/2,ox-l/2,ox+l/2,ox+l/2,ox+l/2,ox+l/2]
    Y=[oy+w/2,oy-w/2,oy-w/2,oy+w/2,oy+w/2,oy-w/2,oy-w/2,oy+w/2]
    Z=[oz-h/2,oz-h/2,oz+h/2,oz+h/2,oz+h/2,oz+h/2,oz-h/2,oz-h/2]

    X_new = ([])
    Y_new = ([])
    Z_new = ([])

    for i in range(0,8):

        c=np.matrix([[X[i]],
                    [Y[i]],
                    [Z[i]]])
        u=F*c

        X_new = np.append(X_new, u.item(0))
        Y_new = np.append(Y_new, u.item(1))
        Z_new = np.append(Z_new, u.item(2))

        ###Doing a dot product between F and c like you did earlier but using np.dot as we're now working with Numpy format
        side[i,:] = np.dot(F, c)

    ###Storing the position of every points
    sides = [[side[0],side[1],side[2],side[3]],
            [side[4],side[5],side[6],side[7]], 
            [side[0],side[1],side[4],side[5]], 
            [side[2],side[3],side[4],side[5]], 
            [side[1],side[2],side[5],side[6]],
            [side[4],side[7],side[0],side[3]]]


    ###Scatter plot 
    ax.scatter(X_new,Y_new,Z_new,c='darkred',marker='o')  #the  plot of points after rotated
    ax.scatter(ox,oy,oz,c='crimson',marker='o')  #the previous plot of points before rotated



    ### Add title 
    plt.title('Plot_for_PSM', fontsize=20)
    plt.gca().invert_yaxis()

    ##labelling the axes
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

    ###This draw the plane sides as you wanted
    ax.add_collection3d(Poly3DCollection(sides, facecolors='blue', linewidths=1, edgecolors='r', alpha=.25))


cuboid(center, size)
###Mandatory to plot the cube
plt.show()

它使用 mpl_toolkits 中的 Poly3DCollection、Line3DCollection 绘制 6 个平面正方形,代表立方体的侧面。

第一步是找到每条边的 4 个坐标。然后你需要使用Poly3DCollection来绘制它。

关于python - 如何用8个点形成一个平面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56562178/

相关文章:

android - 确定 JSON 是 JSONObject 还是 JSONArray

php - 如何在 Laravel 中存储关联数组

c++ - 使用 Python 的 3d 游戏,从无到有

c# - C# 中的简单 3D 图形

authentication - 让用户登录,然后检查某人是否已经登录

python - 带有获取请求的 Http 客户端

python - 异常 : Range exceeds grid limits. 最大行数 : 1001, 最大列数:2 0",\n "状态”: "INVALID_ARGUMENT"

你能解释一下 *ptr++ 和 *(++ptr) 之间的区别吗?

ios - Scenekit Pan 2D Translation to Orthographic 3D only horizo​​ntal

python - peewee 模型会自动关闭连接吗?