Skip to content

Commit

Permalink
Add voxel_range option for mesh to voxel conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Kosuke Takeuchi <[email protected]>
  • Loading branch information
kosuke55 committed Jul 10, 2020
1 parent ec193ff commit eb09fad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
5 changes: 4 additions & 1 deletion kaolin/conversions/meshconversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def trianglemesh_to_pointcloud(mesh: kaolin.rep.Mesh, num_points: int):


def trianglemesh_to_voxelgrid(mesh: kaolin.rep.Mesh, resolution: int,
normalize: bool = True, vertex_offset: float = 0.):
normalize: bool = True, vertex_offset: float = 0.,
voxel_range: float = 1.):
r""" Converts mesh to a voxel model of a given resolution
Args:
Expand All @@ -63,6 +64,7 @@ def trianglemesh_to_voxelgrid(mesh: kaolin.rep.Mesh, resolution: int,
unit cube centered at the origin.
vertex_offset (float): Offset applied to all vertices after
normalizing.
voxel_range (float): Range of voxelization.
Returns:
voxels (torch.Tensor): voxel array of desired resolution
Expand All @@ -80,6 +82,7 @@ def trianglemesh_to_voxelgrid(mesh: kaolin.rep.Mesh, resolution: int,
mesh.vertices = (mesh.vertices - verts_min) / (verts_max - verts_min) - 0.5

mesh.vertices = mesh.vertices + vertex_offset
mesh.vertices /= voxel_range

points = mesh.vertices
smallest_side = (1. / resolution)**2
Expand Down
9 changes: 7 additions & 2 deletions kaolin/datasets/shapenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ class ShapeNet_Voxels(data.Dataset):
split (float): amount of dataset that is training out of 1
resolutions (list): list of resolutions to be returned
no_progress (bool): if True, disables progress bar
voxel_range (float): Range of voxelization.
Returns:
.. code-block::
Expand All @@ -387,7 +388,8 @@ class ShapeNet_Voxels(data.Dataset):
"""

def __init__(self, root: str, cache_dir: str, categories: list = ['chair'], train: bool = True,
split: float = .7, resolutions=[128, 32], no_progress: bool = False):
split: float = .7, resolutions=[128, 32], no_progress: bool = False,
voxel_range: float = 1.0):
self.root = Path(root)
self.cache_dir = Path(cache_dir) / 'voxels'
self.cache_transforms = {}
Expand All @@ -406,7 +408,10 @@ def __init__(self, root: str, cache_dir: str, categories: list = ['chair'], trai

for res in self.params['resolutions']:
self.cache_transforms[res] = tfs.CacheCompose([
tfs.TriangleMeshToVoxelGrid(res, normalize=False, vertex_offset=0.5),
tfs.TriangleMeshToVoxelGrid(res,
normalize=False,
vertex_offset=0.5,
voxel_range=voxel_range),
tfs.FillVoxelGrid(thresh=0.5),
tfs.ExtractProjectOdmsFromVoxelGrid()
], self.cache_dir)
Expand Down
8 changes: 6 additions & 2 deletions kaolin/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,18 @@ class TriangleMeshToVoxelGrid(Transform):
unit cube centered at the origin.
vertex_offset (float): Offset applied to all vertices after
normalizing.
voxel_range (float): Range of voxelization.
"""

def __init__(self, resolution: int,
normalize: bool = True,
vertex_offset: float = 0.):
vertex_offset: float = 0.,
voxel_range: float = 1.):
self.resolution = resolution
self.normalize = normalize
self.vertex_offset = vertex_offset
self.voxel_range = voxel_range

def __call__(self, mesh: TriangleMesh):
"""
Expand All @@ -684,7 +687,8 @@ def __call__(self, mesh: TriangleMesh):
"""
voxels = cvt.trianglemesh_to_voxelgrid(mesh, self.resolution,
normalize=self.normalize,
vertex_offset=self.vertex_offset)
vertex_offset=self.vertex_offset,
voxel_range=self.voxel_range)
return voxels


Expand Down

0 comments on commit eb09fad

Please sign in to comment.