"""Helpers to bridge Volume (numpy+affine) to VTK image data."""
from __future__ import annotations
import numpy as np


def volume_to_vtk(vol):
    """Convert a sulcora Volume to a vtkImageData.

    Note: uses axis-aligned spacing + origin from the affine. Oblique
    (rotated) affines are approximated; full directionmatrix support is a
    documented enhancement.
    """
    import vtk
    from vtk.util import numpy_support

    data = np.ascontiguousarray(vol.data.astype(np.float32))
    img = vtk.vtkImageData()
    img.SetDimensions(*data.shape)
    spacing = vol.spacing
    img.SetSpacing(float(spacing[0]), float(spacing[1]), float(spacing[2]))
    origin = vol.affine[:3, 3]
    img.SetOrigin(float(origin[0]), float(origin[1]), float(origin[2]))
    # VTK expects Fortran-order flattening for (i,j,k)
    flat = data.flatten(order="F")
    arr = numpy_support.numpy_to_vtk(flat, deep=True, array_type=vtk.VTK_FLOAT)
    img.GetPointData().SetScalars(arr)
    return img
