Skip to content

Instantly share code, notes, and snippets.

@hzxie
Created September 22, 2018 06:34
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hzxie/20ff7816e38a5382a54f157a9d629bae to your computer and use it in GitHub Desktop.
Save hzxie/20ff7816e38a5382a54f157a9d629bae to your computer and use it in GitHub Desktop.
Convert Point Cloud to Voxels
import numpy as np
import pandas as pd
from pyntcloud import PyntCloud
import binvox_rw
cloud = PyntCloud.from_file("test/00000.txt",
sep=" ",
header=0,
names=["x","y","z"])
# cloud.plot(mesh=True, backend="threejs")
voxelgrid_id = cloud.add_structure("voxelgrid", n_x=32, n_y=32, n_z=32)
voxelgrid = cloud.structures[voxelgrid_id]
# voxelgrid.plot(d=3, mode="density", cmap="hsv")
x_cords = voxelgrid.voxel_x
y_cords = voxelgrid.voxel_y
z_cords = voxelgrid.voxel_z
voxel = np.zeros((32, 32, 32)).astype(np.bool)
for x, y, z in zip(x_cords, y_cords, z_cords):
voxel[x][y][z] = True
with open("test/00000.binvox", 'wb') as f:
v = binvox_rw.Voxels(voxel, (32, 32, 32), (0, 0, 0), 1, 'xyz')
v.write(f)
@sunnyHelen
Copy link

Hi, thanks for your code. I want to convert my points cloud to voxels. Can I ask what's the format of the file you need to read ? And what's the meaning of the voxelgrid's parameters, n_x, n_y, and n_z.

@hzxie
Copy link
Author

hzxie commented Aug 31, 2019

@sunnyHelen
x, y, z are the coordinates of the points.

@zendevil
Copy link

Running this gives me the following error:

Traceback (most recent call last):
  File "voxel.py", line 11, in <module>
    names=["x","y","z"])
  File "/usr/local/lib/python3.7/site-packages/pyntcloud/core_class.py", line 130, in from_file
    return cls(**FROM_FILE[ext](filename, **kwargs))
TypeError: read_ply() got an unexpected keyword argument 'sep'

After removing the sep keyword, I get the following error:

traceback (most recent call last):
  file "voxel.py", line 10, in <module>
    names=["x","y","z"])
  file "/usr/local/lib/python3.7/site-packages/pyntcloud/core_class.py", line 130, in from_file
    return cls(**from_file[ext](filename, **kwargs))
typeerror: read_ply() got an unexpected keyword argument 'header'

After removing the header keyword argument, I get the following error:

Traceback (most recent call last):
  File "voxel.py", line 9, in <module>
    names=["x","y","z"])
  File "/usr/local/lib/python3.7/site-packages/pyntcloud/core_class.py", line 130, in from_file
    return cls(**FROM_FILE[ext](filename, **kwargs))
TypeError: read_ply() got an unexpected keyword argument 'names'

After removing the names argument, I get this error:

Traceback (most recent call last):
  File "voxel.py", line 28, in <module>
    v.write(f)
  File "/Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/binvox_rw.py", line 94, in write
    write(self, fp)
  File "/Users/prikshetsharma/Documents/clotorch/src/clotorch/flight/binvox_rw.py", line 229, in write
    fp.write('#binvox 1\n')
TypeError: a bytes-like object is required, not 'str'

@mserouar
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment