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.0007
   5.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>
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值