leibniz 0.1.0

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

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

use crate::burn::tensor::column;

/// Coordinate columns with shapes `[samples]`.
pub type Coordinates<B> = (Tensor<B, 1>, Tensor<B, 1>);

/// Two-dimensional point with shape `[2]`.
pub type Point<B> = Tensor<B, 1>;

/// Distance to another point.
pub fn distance<B: Backend>(point: Point<B>, other: Point<B>) -> Tensor<B, 1> {
    length(point - other)
}

/// Unit left normal coordinate columns for row vectors.
pub fn left_normals<B: Backend>(
    vectors: Tensor<B, 2>,
    lengths: Tensor<B, 1>,
    minimum_length: f32,
) -> Coordinates<B> {
    let x = column(vectors.clone(), 0);
    let y = column(vectors, 1);
    let lengths = lengths.clamp_min(minimum_length);

    (-y / lengths.clone(), x / lengths)
}

/// Length as a vector.
pub fn length<B: Backend>(point: Point<B>) -> Tensor<B, 1> {
    point.powi_scalar(2).sum().sqrt()
}

/// Lengths of row vectors.
pub fn row_lengths<B: Backend>(vectors: Tensor<B, 2>) -> Tensor<B, 1> {
    vectors.powi_scalar(2).sum_dim(1).squeeze_dim::<1>(1).sqrt()
}