欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > numpy-stl库的基本使用及notebook下的使用

numpy-stl库的基本使用及notebook下的使用

2024/10/23 23:30:40 来源:https://blog.csdn.net/youlinhuanyan/article/details/139846503  浏览:    关键词:numpy-stl库的基本使用及notebook下的使用

numpy-stl库的基本使用及notebook下的可视化

https://pypi.org/project/numpy-stl/

安装

conda install -c conda-forge numpy-stl

引入资源

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from stl import mesh

读取stl文件

stl_file = 'assets/fan.stl'
stl_mesh = mesh.Mesh.from_file(stl_file)print('stl_mesh info:', stl_file)
print('vectors=', len(stl_mesh.vectors))
print('points=', len(stl_mesh.points))
print('normals=', len(stl_mesh.normals))

out 输出如下

stl_mesh info: assets/fan.stl
vectors= 1792
points= 1792
normals= 1792

points、vectors、normals对比

points基本等同于vectors,只是数据结构不同。每个points对应stl文件中的一个三角面3个点的数据,每个点有3个数值

normals则为一个法向量

数据如下所示:

print('points:', stl_mesh.points[0])
print('vectors:', stl_mesh.vectors[0])
print('normals:', stl_mesh.normals[0])

输出如下:

points: [  5.044177 -23.97724   97.42546    5.656812 -25.27308  106.00075.831299 -23.8088    97.4243  ]
vectors: [[  5.044177 -23.97724   97.42546 ][  5.656812 -25.27308  106.0007  ][  5.831299 -23.8088    97.4243  ]]
normals: [-1.4429097  6.7504697  1.123177 ]

stl_mesh.xstl_mesh.ystl_mesh.z

print('stl_mesh.x: lenth =',len(stl_mesh.x))
print(stl_mesh.x)
stl_mesh.x: lenth = 1792
[[ 5.044177e+00  5.656812e+00  5.831299e+00][ 6.767709e+00  6.065555e+00  6.507149e+00][ 5.839584e+00  6.021440e+00  5.280423e+00]...[-9.914779e-05 -9.914779e-05 -9.914779e-05][-9.914779e-05 -9.914779e-05  6.999990e+01][ 6.999990e+01 -9.914779e-05  6.999990e+01]]

获取stl信息 (-获取体积,-重心, -惯性矩)

#  (Volume-获取体积, Center of gravity-重心, Inertia-惯性矩)
volume, cog, inertia = stl_mesh.get_mass_properties()
print("Volume                                  = {0}".format(volume))
print("Position of the center of gravity (COG) = {0}".format(cog))
print("Inertia matrix at expressed at the COG  = {0}".format(inertia[0,:]))
print("                                          {0}".format(inertia[1,:]))
print("                                          {0}".format(inertia[2,:]))# 获取包围盒子
def get_min_max(mesh):minx = mesh.x.min()miny = mesh.y.min()minz = mesh.z.min()maxx = mesh.x.max()maxy = mesh.y.max()maxz = mesh.z.max()return minx, miny, minz, maxx, maxy,maxz# 获取最大包围盒
minx, miny, minz, maxx, maxy,maxz = get_min_max(stl_mesh)
print('minx, miny, minz, maxx, maxy, maxz  =>', minx, miny, minz, maxx, maxy,maxz )
Volume                                  = 72816.68152088734
Position of the center of gravity (COG) = [ 33.07755097 -17.88736306  27.97393759]
Inertia matrix at expressed at the COG  = [60897330.17635337 -1572272.4035636   3817171.80348613][-1572272.4035636  80751169.91015446  3975033.54231323][ 3817171.80348613  3975033.54231323 29649477.37738535]
minx, miny, minz, maxx, maxy, maxz  => -9.914779e-05 -32.0 0.000244812 69.9999 3.552714e-15 106.0016

手动创建一个mesh网格模型,并保存

# 定义8个vector
vertices = np.array([[-1, -1, -1],[+1, -1, -1],[+1, +1, -1],[-1, +1, -1],[-1, -1, +1],[+1, -1, +1],[+1, +1, +1],[-1, +1, +1]
])
# 定义12个triangle
faces = np.array([[0, 3, 1],[1, 3, 2],[0, 4, 7],[0, 7, 3],[4, 5, 6],[4, 6, 7],[5, 1, 2],[5, 2, 6],[2, 3, 6],[3, 7, 6],[0, 1, 5],[0, 5, 4]
])# 创建mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, face in enumerate(faces):for j in range(3):cube.vectors[i][j] = vertices[face[j], :]
cube.save('cube-write.stl', mode=stl.Mode.ASCII)

旋转移动mesh对象

# 平移
stl_mesh.translate(np.array([0,30,0])) # y方向移动
# 旋转
stl_mesh.rotate([0.0, 1.0, 0.0], np.radians(180)) # 绕y轴旋转90度

stl 3D可视化


show_mesh = stl_mesh
# Create a new plot
figure = plt.figure()
axes = figure.add_subplot(projection='3d')# Load the STL files and add the vectors to the plot
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(show_mesh.vectors))# Auto scale to the mesh size
scale = show_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)# Show the plot to the screen
plt.show()

在这里插入图片描述

jupyter-notebook中使用

可直接在notebook中渲染3D
在这里插入图片描述

numpy-stl的二进制与ASCII转换

numpy-stl安装好后,还有个比较方便的格式转化命令,如下所示,可直接在命令行下执行

$ stl2bin <your_ascii_stl_file.stl> <new_binary_stl_file.stl>
$ stl2ascii <your_binary_stl_file.stl> <new_ascii_stl_file.stl>
$ stl <your_ascii_stl_file.stl> <new_binary_stl_file.stl>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com