pub fn s_edt_1d(
    grid: &mut [f32],
    offset: usize,
    stride: usize,
    length: usize,
    v: &mut [usize], // The horizontal grid location of the i-th parabola in the lower envelope is stored in v[i].
    z: &mut [f32], // The range in which the i-th parabola of the lower envelope is below the others is given by z[i] and z[i+1].
    f: &mut [f32],
) {
    v[0] = 0;
    z[0] = f32::NEG_INFINITY;
    z[1] = f32::INFINITY;
    f[0] = grid[offset];

    let mut k = 0; // The variable k keeps track of the number of parabolas in the lower envelope.

    for q in 1..length {
        f[q] = grid[offset + q * stride];

        let mut s;
        let q2 = q * q;

        let is_k_underflow = loop {
            let r = v[k];
            s = (f[q] - f[r] + (q2 - r * r) as f32) / (q - r) as f32 * 0.5f32;

            if z[k] < s {
                break false;
            }

            if k == 0 {
                break true;
            }

            k -= 1;
        };

        if !is_k_underflow {
            k += 1;
        }

        v[k] = q;
        z[k] = s;
        z[k + 1] = f32::INFINITY;
    }

    k = 0;

    for q in 0..length {
        while z[k + 1] < q as f32 {
            k += 1;
        }

        let r = v[k];
        let qr = q as isize - r as isize;
        grid[offset + q * stride] = f[r] + (qr * qr) as f32;
    }
}
// 1-D squared Euclidean distance transform
1-D squared Euclidean distance transform
1 - D squared Euclidean distance transform
pub fn s_edt_1d(
    grid: &mut [f32],
    offset: usize,
    stride: usize,
    length: usize,
    v: &mut [usize], // The horizontal grid location of the i-th parabola in the lower envelope is stored in v[i].
    z: &mut [f32], // The range in which the i-th parabola of the lower envelope is below the others is given by z[i] and z[i+1].
    f: &mut [f32],
) {
    v[0] = 0;
    z[0] = f32::NEG_INFINITY;
    z[1] = f32::INFINITY;
    f[0] = grid[offset];
    let mut k = 0; // The variable k keeps track of the number of parabolas in the lower envelope.
    for q in 1..length {
        f[q] = grid[offset + q * stride];
        let mut s;
        let q2 = q * q;
        let is_k_underflow = loop {
            let r = v[k];
            s = (f[q] - f[r] + (q2 - r * r) as f32) / (q - r) as f32 * 0.5f32;
            if z[k] < s {
                break false;
            }
            if k == 0 {
                break true;
            }
            k -= 1;
        };
        if !is_k_underflow {
            k += 1;
        }
        v[k] = q;
        z[k] = s;
        z[k + 1] = f32::INFINITY;
    }
    k = 0;
    for q in 0..length {
        while z[k + 1] < q as f32 {
            k += 1;
        }
        let r = v[k];
        let qr = q as isize - r as isize;
        grid[offset + q * stride] = f[r] + (qr * qr) as f32;
    }
}
pub fn s_edt_1d ( grid : & mut [ f32 ] , offset : usize , stride : usize , length : usize , v : & mut [ usize ] , // The horizontal grid location of the i-th parabola in the lower envelope is stored in v[i]. z : & mut [ f32 ] , // The range in which the i-th parabola of the lower envelope is below the others is given by z[i] and z[i+1]. f : & mut [ f32 ] , ) { v [ 0 ] = 0 ; z [ 0 ] = f32 :: NEG_INFINITY ; z [ 1 ] = f32 :: INFINITY ; f [ 0 ] = grid [ offset ] ; let mut k = 0 ; // The variable k keeps track of the number of parabolas in the lower envelope. for q in 1 .. length { f [ q ] = grid [ offset + q * stride ] ; let mut s ; let q2 = q * q ; let is_k_underflow = loop { let r = v [ k ] ; s = ( f [ q ] - f [ r ] + ( q2 - r * r ) as f32 ) / ( q - r ) as f32 * 0.5f32 ; if z [ k ] < s { break false ; } if k == 0 { break true ; } k -= 1 ; } ; if ! is_k_underflow { k += 1 ; } v [ k ] = q ; z [ k ] = s ; z [ k + 1 ] = f32 :: INFINITY ; } k = 0 ; for q in 0 .. length { while z [ k + 1 ] < q as f32 { k += 1 ; } let r = v [ k ] ; let qr = q as isize - r as isize ; grid [ offset + q * stride ] = f [ r ] + ( qr * qr ) as f32 ; } }
1-D squared Euclidean distance transform
1 - D squared Euclidean distance transform
grid
&mut [f32]
offset
usize
stride
usize
length
usize
v
&mut [usize]
z
&mut [f32]
f
&mut [f32]