leibniz 0.2.0

The package provides a differentiable vector graphics rasterization loss.
Documentation
//! Sample positions.

use ::burn::tensor::{Int, Tensor, backend::Backend};

/// Sub-sample position columns with shape
/// `[height * y_sample_count * width * x_sample_count]`.
///
/// Mirrors DiffVG's render_kernel sub-sample positions: each pixel is split
/// into an `x_sample_count` by `y_sample_count` grid of centered sub-samples, with
/// sub-sample centers at `(index + 0.5) / samples`. Positions are laid out in
/// row-major order over the `[height * y_sample_count, width *
/// x_sample_count]` fine grid.
pub fn fine_positions<B: Backend>(
    height: usize,
    width: usize,
    x_sample_count: usize,
    y_sample_count: usize,
    device: &B::Device,
) -> (Tensor<B, 1>, Tensor<B, 1>) {
    let fine_width = width * x_sample_count;
    let fine_height = height * y_sample_count;
    let x = (Tensor::<B, 1, Int>::arange(0..fine_width as i64, device).float() + 0.5)
        / x_sample_count as f32;
    let y = (Tensor::<B, 1, Int>::arange(0..fine_height as i64, device).float() + 0.5)
        / y_sample_count as f32;
    let x = x.unsqueeze_dim::<2>(0).repeat_dim(0, fine_height);
    let y = y.unsqueeze_dim::<2>(1).repeat_dim(1, fine_width);

    // Winding consumes x/y columns directly, so avoid stacking positions into
    // point rows only to slice them apart again.
    (
        x.reshape([fine_height * fine_width]),
        y.reshape([fine_height * fine_width]),
    )
}