I have array of vertices from an stl file which i converted to a 2D numpy array. Here's some of it as an example:
print(vertices.shape)
(67748, 3)
I need to turn these into a 3D binary array where each element = 1 where the index is given by the vertices array.
Minimal reproducible example (expected output) using a 5 x 3 vertices array instead of 67748 x 3:
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
CodePudding user response:
You can use np.put
and np.ravel_multi_index
np.put(voxel_array,
np.ravel_multi_index(vertices.T,
voxel_array.shape),
1)
And then:
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))
CodePudding user response:
Answer from Michael Szczesny in the comments:
for x, y, z in vertices: voxel_array[x, y, z] = 1
This works just as well as Daniel F's but is easier to understand. Both take 0.0 seconds to run on 256 x 265 x 265 array