[旧代码] 生成空间平面

发布于 2023-11-17  205 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

保存旧代码,以下功能已经/或者即将在PolyKriging包的Plane类中以更优雅的方式实现。

Plane类的调用方式:

from polykriging.geometry import Plane

关于Plane类的更多信息: PolyKriging documentation

以下旧代码:

import numpy as np
import trimesh as tm


def cut_plane(point, normal, path = './', plot = False):
    # a plane is a*x+b*y+c*z+d=0
    # [a,b,c] is the normal. Thus, we have to calculate d and we're set
    d = -point.dot(normal)
    xx, yy = np.meshgrid(np.arange(-0.7+ 0.15 + point[0], 0.7 + 0.15 + point[0], 1),
                         np.arange(-1.1 + 0.1 + point[1], 1.1 + 0.1 + point[1], 2.1))
    if normal[2] != 0:
        # calculate corresponding z
        z = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]

        print(z)
    elif normal[1] != 0 and normal[2] == 0:
        length = float(input('Please input the length of the plane:'))
        width = float(input('Please input the width of the plane:'))
        xx = np.array([point[0]-0.5*length, point[0] + 0.5*length, point[0]-
                       0.5*length, point[0] + 0.5*length ])
        yy = (-normal[0] * xx  - d) * 1. /normal[1]
        z = np.array([point[2]-0.5*width, point[2] - 0.5*width, point[2] +
                      0.5*width, point[2] + 0.5*width ])        

    elif normal[1] == 0 and normal[2] == 0:
        length = float(input('Please input the length of the plane:'))
        width = float(input('Please input the width of the plane:'))
        xx = np.array([point[0], point[0], point[0], point[0] ])
        yy = np.array([point[1]-0.5*length, point[1] + 0.5*length, point[1]-
                       0.5*length, point[1] + 0.5*length ])
        z = np.array([point[2]-0.5*width, point[2] - 0.5*width, point[2] +
                      0.5*width, point[2] + 0.5*width ])

    if plot == True:
        import matplotlib.pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D
        # plot the surface
        plt3d = plt.figure().gca(projection='3d')
        plt3d.plot_surface(xx, yy, z)
        plt.show()

    # Create the cut plane and save as triangulated mesh.
    vertices = np.zeros([4,3])
    vertices[:, 0], vertices[:, 1], vertices[:, 2] = xx.flatten(), yy.flatten(), z.flatten()
    # mesh objects can be created from existing faces and vertex data
    tri_mesh = tm.Trimesh(vertices, faces=[[0, 1, 3], [3, 2, 0]])
    tri_mesh.export(path+'perpendicular2.stl', file_type='stl_ascii')

if __name__ == "__main__":
    tangent_ubinder2 = np.load(
        'D:/polyKriging/Fabric/binder/tangent_ubinder2_9e-4.npy')
    centerline_ubinder2 = np.load(
        'D:/polyKriging/Fabric/binder/centerline_ubinder2_9e-4.npy')
    index = [26, 70, 129, 183]

    point  = centerline_ubinder2[274]
    normal = tangent_ubinder2[274]

    cut_plane(point, normal)
Everything not saved will be lost.
最后更新于 2023-11-17