当前位置:   article > 正文

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

numpy-stl

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

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

安装

conda install -c conda-forge numpy-stl
  • 1

引入资源

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from stl import mesh
  • 1
  • 2
  • 3
  • 4

读取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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

out 输出如下

stl_mesh info: assets/fan.stl
vectors= 1792
points= 1792
normals= 1792
  • 1
  • 2
  • 3
  • 4

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])
  • 1
  • 2
  • 3

输出如下:

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 ]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

stl_mesh.xstl_mesh.ystl_mesh.z

print('stl_mesh.x: lenth =',len(stl_mesh.x))
print(stl_mesh.x)
  • 1
  • 2
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]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

获取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 )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

手动创建一个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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

旋转移动mesh对象

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

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

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>
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/795201
推荐阅读
相关标签
  

闽ICP备14008679号