kaolin icon indicating copy to clipboard operation
kaolin copied to clipboard

trianglemesh_to_voxelgrid argument vertex_offset

Open JudyYe opened this issue 5 years ago • 2 comments

Hi, When I load a mesh from .obj scaled from [-0.5, 0.5], trianglemesh_to_voxelgrid default argument vertex_offset gives me weird behavior if the voxel is visualized by matplotlib. However, I got expected behavior when setting vertex_offeset to 0.5.

I guess the default argument is compatible with kaolin.visualizer. But would it be nicer if other offscreen renderers (like matplotlib) could be mentioned in doc? It would be very useful for cluster users who don't have access to display devices.

    mesh = kl.rep.TriangleMesh.from_obj(voxel_file)
    voxel1 = kl.conversions.trianglemesh_to_voxelgrid(mesh, vox_size, vertex_offset=0.)
    save_voxel(voxel1, 'offset=0.png')
    voxel2 = kl.conversions.trianglemesh_to_voxelgrid(mesh, vox_size, vertex_offset=0.5)
    save_voxel(voxel2, 'offset=0.5.png')

offset=0 (default): offset=0 (default) offset=0.5: offset=0.5

visualize code

def save_voxel(voxels, fpath):
    """
    :param voxels: numpy (D, H, W)
    :param fpath:
    :return:
    """
    plt.close()
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    ax.voxels(voxels, facecolors='red', edgecolor='k')
    plt.savefig(fpath)

JudyYe avatar May 03 '20 15:05 JudyYe

Also, if I understand correctly, this operation only voxelizes the mesh surface, not including the interior. It'd be better to put a note in doc as well...

JudyYe avatar May 04 '20 17:05 JudyYe

The reason why vertex_offset=0 generates an incorrect voxel is that trianglemesh_to_voxelgrid has normalize = True by default, and the mesh range is -0.5 ~ 0.5. https://github.com/NVIDIAGameWorks/kaolin/blob/ec193fff54bd073cf8cdafaa0fc5f03f7d23e28c/kaolin/conversions/meshconversions.py#L77-L80 It is then multiplied by (vox_size -1) to create a voxel index, resulting in a negative index. https://github.com/NVIDIAGameWorks/kaolin/blob/ec193fff54bd073cf8cdafaa0fc5f03f7d23e28c/kaolin/conversions/meshconversions.py#L136 For example -i index is equal to (vox_size -i), so such an incorrect voxel is generated. This is also not compatible with kaolin's Visualizer. https://github.com/NVIDIAGameWorks/kaolin/issues/185 I think the offset default should be 0.5 when normalize = True.

kosuke55 avatar Jul 10 '20 15:07 kosuke55