我有一个stl文件中的顶点数组,我将其转换为一个2D的numpy数组。下面是其中的一些例子:
print(vertices.shape)
(67748, 3)我需要将这些转换成三维二进制数组,其中每个元素=1,其中索引由顶点数组提供。
使用5x3顶点数组(而不是67748 x3)的最小可重现示例(预期输出):
verts = np.array([[ 77, 239, 83],
[100, 237, 88],
[100, 149, 94],
[100, 220, 128],
[100, 145, 86]])
voxels = np.zeros((256,256,256)).astype(int)
voxels[77,239,83] = 1
voxels[100,149,94] = 1
voxels[100,237,88] = 1
voxels[100,220,128] = 1
voxels[100,145, 86] = 1发布于 2022-05-02 09:19:39
您可以使用np.put和np.ravel_multi_index
np.put(voxel_array,
np.ravel_multi_index(vertices.T,
voxel_array.shape),
1)然后:
np.where(voxel_array)
Out[]:
(array([ 77, 100, 100, 100, 100], dtype=int64),
array([239, 145, 149, 220, 237], dtype=int64),
array([ 83, 86, 94, 128, 88], dtype=int64))发布于 2022-05-03 14:03:34
Michael Szczesny在评论中的回答:
对于x,y,z的顶点: voxel_arrayx,y,z=1
这和丹尼尔F的一样好,但更容易理解。在256 x 265 x 265数组上运行都需要0.0秒。
https://stackoverflow.com/questions/72084310
复制相似问题