point-cloud-utils
point-cloud-utils copied to clipboard
Report a bug in signed_distance_to_mesh function.
Hi,
Thanks for your excellent implementation.
I am calling signed_distance_to_mesh() to compute the signed distances of a triangle mesh with a uniform sampled volume. However, I find something quite strange when I apply a marching cube to the signed distance: the extracted mesh suffers a significant distortion from the original. The images below show that the signed distances of the left hood may be incorrect.
Original:
Extracted:
The car.obj file and code block are attached to reproduce it.
import numpy as np
import trimesh
import point_cloud_utils as pcu
from skimage.measure import marching_cubes
# init query point
octree_depth = 7
boundary = 1.0
bbox_min = np.array([-boundary, -boundary, -boundary])
bbox_max = np.array([boundary, boundary, boundary])
bbox_size = bbox_max - bbox_min
length = bbox_max - bbox_min
num_cells = np.exp2(octree_depth)
x = np.linspace(bbox_min[0], bbox_max[0], int(num_cells) + 2, dtype=np.float32)
y = np.linspace(bbox_min[1], bbox_max[1], int(num_cells) + 2, dtype=np.float32)
z = np.linspace(bbox_min[2], bbox_max[2], int(num_cells) + 2, dtype=np.float32)
[xs, ys, zs] = np.meshgrid(x, y, z, indexing="ij")
xyz = np.stack((xs, ys, zs), axis=-1)
query_points = xyz.reshape(-1, 3)
grid_size = [int(num_cells) + 2, int(num_cells) + 2, int(num_cells) + 2]
# load mesh
mesh = trimesh.load('car.obj')
v = mesh.vertices
f = mesh.faces
# compute sdf
sdf = mesh2sdf.core.compute(v, f, size=int(np.exp2(octree_depth)))
sdf.resize(grid_size[0], grid_size[1], grid_size[2])
# extract mesh
vertices, faces, normals, _ = marching_cubes(query_sdf, level=0)
watertight_mesh = trimesh.Trimesh(vertices, faces, normals=normals)
watertight_mesh.export('watertight_mesh.obj')
Can confirm, using point-cloud-utils v0.31.0 with Python 3.12.3, I always get only positive distances from signed_distance_to_mesh.