# Convert point clouds into voxels

I have points that are obtained by sampling a mesh(which is not watertight). Now, I would like to convert that mesh into voxel?

Does anyone have any suggestions for solving this problem?

• This question gets asked a lot, I suggest looking up those answers, and if they are insufficient, clarify your question with specifics of why. Jun 13, 2021 at 13:33
• On top of what @pmw1234 said, I think you need to be more clear on precisely what the end goal is. Are you trying to resample the point cloud to a regular grid? Are you trying to recover the mesh? Are you trying to visualise the surface, or the volume? Jun 16, 2021 at 5:47
• You say point cloud and you say mesh. Be specific, these are different problems.
– user1703
Sep 7, 2023 at 13:15

I don't quite get what you mean by voxel, but here you go. Please let me know if you need something special.

def voxelize(point_cloud, voxel_size):
"""
Voxelize an Open3D point cloud.

Parameters:
- point_cloud (open3d.geometry.PointCloud): Input point cloud.
- voxel_size (float): Voxel size for downsampling.

"""

points_np = np.asarray(point_cloud.points)

min_bound = np.min(points_np, axis=0)
max_bound = np.max(points_np, axis=0)

voxel_counts = np.ceil((max_bound - min_bound) / voxel_size).astype(int)

voxel_indices = np.floor((points_np - min_bound) / voxel_size).astype(int)

packed_indices = voxel_indices[:, 0] + voxel_indices[:, 1] * voxel_counts[0] + voxel_indices[:, 2] * voxel_counts[
0] * voxel_counts[1]

voxel_dict = {}
voxel_to_point_index_dict = {}
for i, index in enumerate(packed_indices):
if index in voxel_dict:
voxel_dict[index].append(points_np[i])
voxel_to_point_index_dict[index].append(i)
else:
voxel_dict[index] = [points_np[i]]
voxel_to_point_index_dict[index] = [i]
return voxel_dict, voxel_to_point_index_dict


This function simply puts points to voxels, hash maps voxel_dict and voxel_to_point_index_dict should provide you two way access.

If you're looking for a C++ solution, try this one:

https://github.com/sjhalayka/point_to_mesh_text

The input looks like:

-0.0757585 -0.136365 -1.16667
-0.0757585 -0.106062 -1.16667
-0.0757585 -0.0757585 -1.16667
-0.0757585 -0.0454555 -1.16667
-0.0757585 -0.0151525 -1.16667
-0.0757585 0.0151506 -1.16667
-0.0757585 0.0454536 -1.16667
-0.0757585 0.0757566 -1.16667
-0.0757585 0.10606 -1.16667
-0.0757585 0.136363 -1.16667
-0.0757585 0.166666 -1.16667
-0.0454555 -0.106062 -1.16667
-0.0454555 -0.0757585 -1.16667
-0.0454555 -0.0454555 -1.16667
...


and the resulting mesh looks like: