File size: 1,652 Bytes
681b3db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
from scipy.ndimage import distance_transform_edt, gaussian_filter, zoom, median_filter
import matplotlib.pyplot as plt
from skimage.measure import marching_cubes
from scipy.ndimage import binary_fill_holes, label
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from scipy.spatial import Delaunay
from scipy.interpolate import griddata
# import trimesh

def compute_sdf(voxel_matrix):
    inside_dist = distance_transform_edt(voxel_matrix)
    outside_dist = distance_transform_edt(1 - voxel_matrix)
    sdf = outside_dist - inside_dist
    # sdf = gaussian_filter(sdf, sigma=3.0)
    sdf = sdf / 32.0
    sdf = sdf.clip(-0.2, 0.2)
    return sdf

def sdf_to_voxel(sdf, level=0.02):

    sdf_smoothed = gaussian_filter(sdf, sigma=0.5)
    binary_mask = binary_fill_holes(sdf_smoothed <= level).astype(np.uint8)
    # structuring_element = ball(radius=1)
    # binary_mask_opened = binary_opening(binary_mask, footprint=structuring_element)
    # binary_mask_closed = binary_closing(binary_mask_opened, footprint=structuring_element)
    # binary_mask_eroded = binary_erosion(binary_mask_closed, structure=structuring_element)
    # binary_mask_dilated = binary_dilation(binary_mask_eroded, structure=structuring_element)
    # filled_voxel = gaussian_filter(binary_mask_dilated.astype(np.float32), sigma=1.0)
    # filled_voxel = filled_voxel > 0.5
    filled_voxel = binary_mask
    
    return filled_voxel

# def sdf_to_voxel(sdf, level=0.02):

#     sdf = gaussian_filter(sdf, sigma=3.0)
#     sdf = median_filter(sdf, size=5)
#     filled_voxel = binary_fill_holes(sdf <= level).astype(np.uint8)
#     return filled_voxel