I built a sphere in SolidWorks located just in center having diameter = 6m. I exported this surface into STL. Now I want to create a uniform mesh overlapping the sphere and mask points in mesh which are inside the sphere.
import numpy as np
from stl import mesh
import pyvista as pv
sphere_stl = r'sphere.STL'
grid_size = 100
stl_mesh = mesh.Mesh.from_file(sphere_stl)
min_bounds = np.min(stl_mesh.vectors, axis=(0, 1))
max_bounds = np.max(stl_mesh.vectors, axis=(0, 1))
grid = pv.UniformGrid()
grid.dimensions = (grid_size, grid_size, grid_size)
grid.origin = min_bounds
grid.spacing = (max_bounds - min_bounds) / (grid_size - 1)
# grid.plot(show_axes=True, show_bounds=True)
sphere_poly = pv.PolyData(stl_mesh.vectors.reshape(-1, 3))
voxels = grid.select_enclosed_points(sphere_poly, check_surface=False)
mask = voxels["SelectedPoints"].reshape(grid.dimensions)
print(np.sum(mask))
The grid has 1 000 000 elements, but my mask shows (last line) that there are only 3956 points inside the sphere. I have a mistake somewhere.
Also, is there any way I can visualise my mask?