PyVista: Getting mesh indices (triangles)

143 views Asked by At

I was originally using https://github.com/pmneila/PyMCubes which conveniently outputs the mesh points and indices but have since moved to PyVista since for whatever reason PyMCubes applied some sort of weird transformation that isn't ideal. How can I extract the same information from PyVista?

For a simple sphere:

def sphere(x, y, z):
    scalar = 8 * np.ones(len(x*y*z))
    dist = (x**2 + y**2 + z**2)**(1/2)
    scalar[(dist >= 1**2) | (dist <= 0.75**2) | (z <= 0)] = 0
    return scalar

# create a uniform grid to sample the function with
n = 100
x_min, y_min, z_min = -2, -2, -2
grid = pv.ImageData(
    dimensions=(n, n, n),
    spacing=(abs(x_min) / n * 2, abs(y_min) / n * 2, abs(z_min) / n * 2),
    origin=(x_min, y_min, z_min),
)

x, y, z = grid.points.T

values = sphere(x, y, z)

fig = go.Figure([go.Scatter3d(x=x, y=y, z=z, name='inner', mode='markers', marker=dict(size=values)),])
# fig.show(renderer='browser')

mesh = grid.contour([1], values, method='marching_cubes').smooth()
dist = np.linalg.norm(mesh.points, axis=1)
mesh.plot(scalars=dist, smooth_shading=True, specular=1, opacity=0.3, cmap="plasma", show_scalar_bar=False)

triangulated = mesh.extract_surface()
triangles = triangulated.surface_indices().reshape(-1, 3)
vertices = mesh.points

The vertices and mesh points I have grabbed definitely do not correspond to each other since plotting them independently of PyVista generates garbage.

0

There are 0 answers