"""Tetrahedral sphere volume mesh in 3D space.

Dimensional: 3D manifold in 3D space.
"""

import pyvista as pv

from torchmesh.io import from_pyvista
from torchmesh.mesh import Mesh


def load(radius: float = 1.0, resolution: int = 20, device: str = "cpu") -> Mesh:
    """Create a tetrahedral volume mesh of a sphere.

    Args:
        radius: Radius of the sphere
        resolution: Resolution of the initial surface mesh
        device: Compute device ('cpu' or 'cuda')

    Returns:
        Mesh with n_manifold_dims=3, n_spatial_dims=3
    """
    # Create surface sphere
    surface = pv.Sphere(
        radius=radius, theta_resolution=resolution, phi_resolution=resolution
    )

    # Fill with tetrahedra using Delaunay 3D
    volume = surface.delaunay_3d()

    mesh = from_pyvista(volume, manifold_dim=3)

    # Move to specified device
    if device != str(mesh.points.device):
        mesh = mesh.to(device)

    return mesh
