我正在尝试做一个 DataScience 项目,因此需要将 .stl 文件提供给深度模型。 我想三角形网格的体素化版本效果最好。这种体素表示的条件是必须填充 .stl 文件中的对象,以便深度模型可以理解 3D 空间中对象的体积。 到目前为止,我能够将文件转换为二进制 numpy 数组,其中包含对象表面的体素,甚至表面也有许多空点。总的来说,我对结果非常不满意。下面是我能够想出的代码。
import open3d as o3d
import numpy as np
mesh = o3d.io.read_triangle_mesh("../Sample_Model/backpack_hook.stl")
mesh.scale(1 / np.max(mesh.get_max_bound() - mesh.get_min_bound()),
center=mesh.get_center())
voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(mesh, voxel_size=0.05)
o3d.visualization.draw_geometries([voxel_grid])
voxels = voxel_grid.get_voxels()
indices = np.stack(list(vx.grid_index for vx in voxels))
a = np.zeros(shape=(np.amax(indices[:, 0]+1), np.amax(indices[:, 1]+1), np.amax(indices[:, 2]+1)))
for i in range(indices.shape[0]):
coords = indices[i]
a[coords[0], coords[1], coords[2]] = 1
import matplotlib.pyplot as plt
# change the last parameter to move through the z axis
plt.imshow(a[:, :, 3],interpolation='nearest')
plt.show()
我在过去的帖子中读到这是一个相当困难的问题,所以我不打算自己实现这个,但我希望那里已经有一个脚本可以满足我的要求。
我会使用奇偶规则来填充形状。您将光线从一个位置投射到一个方向并计算交叉点的数量。如果数字是奇数,则必须填充此位置的体素。使用 3 个 for 循环重复此操作(x、y、z,增量是体素系统的分辨率),如果你有顶点集合的 3d 体素表示。