def process_model_file(filename): try: if is_bad_mesh(filename): return mesh = trimesh.load(filename) voxel_filenames = [get_voxel_filename(filename, resolution) for resolution in VOXEL_RESOLUTIONS] if not all(os.path.exists(f) for f in voxel_filenames): mesh_unit_cube = scale_to_unit_cube(mesh) surface_point_cloud = get_surface_point_cloud(mesh_unit_cube, bounding_radius=3**0.5, scan_count=SCAN_COUNT, scan_resolution=SCAN_RESOLUTION) try: for resolution in VOXEL_RESOLUTIONS: voxels = surface_point_cloud.get_voxels(resolution, use_depth_buffer=USE_DEPTH_BUFFER, check_result=True) np.save(get_voxel_filename(filename, resolution), voxels) del voxels except BadMeshException: tqdm.write("Skipping bad mesh. ({:s})".format(get_hash(filename))) mark_bad_mesh(filename) return del mesh_unit_cube, surface_point_cloud create_uniform_and_surface = CREATE_UNIFORM_AND_SURFACE and (not os.path.exists(get_uniform_filename(filename)) or not os.path.exists(get_surface_filename(filename))) create_sdf_clouds = CREATE_SDF_CLOUDS and not os.path.exists(get_sdf_cloud_filename(filename)) if create_uniform_and_surface or create_sdf_clouds: mesh_unit_sphere = scale_to_unit_sphere(mesh) surface_point_cloud = get_surface_point_cloud(mesh_unit_sphere, bounding_radius=1, scan_count=SCAN_COUNT, scan_resolution=SCAN_RESOLUTION) try: if create_uniform_and_surface: uniform_points, uniform_sdf, near_surface_points, near_surface_sdf = get_uniform_and_surface_points(surface_point_cloud, number_of_points=POINT_CLOUD_SAMPLE_SIZE) combined_uniform = np.concatenate((uniform_points, uniform_sdf[:, np.newaxis]), axis=1) np.save(get_uniform_filename(filename), combined_uniform) combined_surface = np.concatenate((near_surface_points, near_surface_sdf[:, np.newaxis]), axis=1) np.save(get_surface_filename(filename), combined_surface) if create_sdf_clouds: sdf_points, sdf_values = surface_point_cloud.sample_sdf_near_surface(use_scans=True, sign_method='depth' if USE_DEPTH_BUFFER else 'normal', number_of_points=SDF_POINT_CLOUD_SIZE, min_size=0.015) combined = np.concatenate((sdf_points, sdf_values[:, np.newaxis]), axis=1) np.save(get_sdf_cloud_filename(filename), combined) except BadMeshException: tqdm.write("Skipping bad mesh. ({:s})".format(get_hash(filename))) mark_bad_mesh(filename) return del mesh_unit_sphere, surface_point_cloud except: traceback.print_exc()
def process_model_file(filename): voxels_filename = get_voxel_filename(filename) sdf_cloud_filename = get_sdf_cloud_filename(filename) if is_bad_mesh(filename): return if not (CREATE_VOXELS and not os.path.isfile(voxels_filename) or CREATE_SDF_CLOUDS and not os.path.isfile(sdf_cloud_filename)): return mesh = trimesh.load(filename) if ROTATION is not None: mesh.apply_transform(ROTATION) mesh = scale_to_unit_sphere(mesh) surface_point_cloud = get_surface_point_cloud(mesh) if CREATE_SDF_CLOUDS: try: points, sdf = surface_point_cloud.sample_sdf_near_surface( number_of_points=SDF_CLOUD_SAMPLE_SIZE, sign_method='depth', min_size=0.015) combined = np.concatenate((points, sdf[:, np.newaxis]), axis=1) ensure_directory(os.path.dirname(sdf_cloud_filename)) np.save(sdf_cloud_filename, combined) except BadMeshException: tqdm.write("Skipping bad mesh. ({:s})".format(filename)) mark_bad_mesh(filename) return if CREATE_VOXELS: try: voxels = surface_point_cloud.get_voxels( voxel_resolution=VOXEL_RESOLUTION, use_depth_buffer=True) ensure_directory(os.path.dirname(voxels_filename)) np.save(voxels_filename, voxels) except BadMeshException: tqdm.write("Skipping bad mesh. ({:s})".format(filename)) mark_bad_mesh(filename) return
import numpy as np from mesh_to_sdf import get_surface_point_cloud, scale_to_unit_sphere import trimesh import skimage, skimage.measure import os mesh = trimesh.load('example/chair.obj') mesh = scale_to_unit_sphere(mesh) print("Scanning...") cloud = get_surface_point_cloud(mesh, surface_point_method='scan', scan_count=20, scan_resolution=400) cloud.show() os.makedirs("test", exist_ok=True) for i, scan in enumerate(cloud.scans): scan.save("test/scan_{:d}.png".format(i)) print("Voxelizing...") voxels = cloud.get_voxels(128, use_depth_buffer=True) print("Creating a mesh using Marching Cubes...") vertices, faces, normals, _ = skimage.measure.marching_cubes(voxels, level=0) mesh = trimesh.Trimesh(vertices=vertices, faces=faces, vertex_normals=normals) mesh.show()