"""Closed ellipse curve in 2D space.

Dimensional: 1D manifold in 2D space (closed, no boundary).
"""

import torch

from torchmesh.mesh import Mesh


def load(
    semi_major_axis: float = 2.0,
    semi_minor_axis: float = 1.0,
    n_points: int = 48,
    device: str = "cpu",
) -> Mesh:
    """Create a closed ellipse curve in 2D space.

    Args:
        semi_major_axis: Length of semi-major axis (x-direction)
        semi_minor_axis: Length of semi-minor axis (y-direction)
        n_points: Number of points around the ellipse
        device: Compute device ('cpu' or 'cuda')

    Returns:
        Mesh with n_manifold_dims=1, n_spatial_dims=2, n_cells=n_points
    """
    if n_points < 3:
        raise ValueError(f"n_points must be at least 3, got {n_points=}")

    theta = torch.linspace(0, 2 * torch.pi, n_points + 1, device=device)[:-1]

    points = torch.stack(
        [
            semi_major_axis * torch.cos(theta),
            semi_minor_axis * torch.sin(theta),
        ],
        dim=1,
    )

    # Create edge cells, including wrap-around edge
    cells = torch.stack(
        [
            torch.arange(n_points, device=device),
            torch.cat(
                [
                    torch.arange(1, n_points, device=device),
                    torch.tensor([0], device=device),
                ]
            ),
        ],
        dim=1,
    )

    return Mesh(points=points, cells=cells)
