<p>I don't quite get what you mean by voxel, but here you go. Please let me know if you need something special.</p>
<pre><code>def voxelize(point_cloud, voxel_size):
        &quot;&quot;&quot;
        Voxelize an Open3D point cloud.
    
        Parameters:
        - point_cloud (open3d.geometry.PointCloud): Input point cloud.
        - voxel_size (float): Voxel size for downsampling.
    
        &quot;&quot;&quot;
    
        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
</code></pre>
<p>This function simply puts points to voxels, hash maps <code>voxel_dict</code> and <code>voxel_to_point_index_dict</code> should provide you two way access.</p>

CC BY-SA 4.0
2023-09-06T21:41:09.917Z
2023-09-06T21:41:09.917Z
Lady Be Good
<p>I have points that are obtained by sampling a mesh(which is not watertight). Now, I would like to convert that mesh into voxel?</p>
<p>Does anyone have any suggestions for solving this problem?</p>

CC BY-SA 4.0
2021-06-13T13:33:36.023Z
This question gets asked a lot, I suggest looking up those answers, and if they are insufficient, clarify your question with specifics of why.
pmw1234
CC BY-SA 4.0
2021-06-16T05:47:06.250Z
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?
Pseudonym
<3d><geometry>
Convert point clouds into voxels
akes
# Convert point clouds into voxels

(Asked by: username_0 on 2021-06-09)

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?

**Tags:** 3d, geometry

> **Comments:**
> **username_1**: This question gets asked a lot, I suggest looking up those answers, and if they are insufficient, clarify your question with specifics of why.  
> **username_2**: On top of what @username_1 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?  

---

## 1 answers

### Answer by username_3 on 2023-09-06. Score: 0

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.


---

