fn dt(grid: &mut Vec<f64>, offset: usize, step_by: usize, size: usize) {
    // For our purposes, f is a one-dimensional slice of the grid
    let f: Vec<f64> = grid.iter().skip(offset).step_by(step_by).copied().collect();

    // It may be possible to make this more functional in style,
    // but for now this is more or less a "dumb" transcription of
    // the algorithm presented in the paper by Felzenszwalb & Huttenlocher.
    let mut k = 0;
    let mut v = vec![0; size];
    let mut z = vec![0f64; size + 1];
    let mut s: f64;

    z[0] = f64::MIN;
    z[1] = f64::MAX;

    for q in 1..size {
        loop {
            let q2 = (q * q) as f64;
            let vk2 = (v[k] * v[k]) as f64;
            let denom = (2 * q - 2 * v[k]) as f64;
            s = ((f[q] + q2) - (f[v[k]] + vk2)) / denom;

            if s <= z[k] {
                k -= 1;
            } else {
                k += 1;
                v[k] = q;
                z[k] = s;
                z[k + 1] = f64::MAX;

                break;
            }
        }
    }

    k = 0;
    for q in 0..size {
        let qf64 = q as f64;
        while z[k + 1] < qf64 {
            k += 1;
        }
        let vkf64 = v[k] as f64;
        grid[offset + q * step_by] = (qf64 - vkf64) * (qf64 - vkf64) + f[v[k]];
    }
}
/// An O(n) Euclidean Distance Transform algorithm.
/// See page 6 (420) of http://cs.brown.edu/people/pfelzens/papers/dt-final.pdf for details and
/// further discussion of the math behind this.
An O(n) Euclidean Distance Transform algorithm.
An O ( n ) Euclidean Distance Transform algorithm .
fn dt(grid: &mut Vec<f64>, offset: usize, step_by: usize, size: usize) {
    // For our purposes, f is a one-dimensional slice of the grid
    let f: Vec<f64> = grid.iter().skip(offset).step_by(step_by).copied().collect();
    // It may be possible to make this more functional in style,
    // but for now this is more or less a "dumb" transcription of
    // the algorithm presented in the paper by Felzenszwalb & Huttenlocher.
    let mut k = 0;
    let mut v = vec![0; size];
    let mut z = vec![0f64; size + 1];
    let mut s: f64;
    z[0] = f64::MIN;
    z[1] = f64::MAX;
    for q in 1..size {
        loop {
            let q2 = (q * q) as f64;
            let vk2 = (v[k] * v[k]) as f64;
            let denom = (2 * q - 2 * v[k]) as f64;
            s = ((f[q] + q2) - (f[v[k]] + vk2)) / denom;
            if s <= z[k] {
                k -= 1;
            } else {
                k += 1;
                v[k] = q;
                z[k] = s;
                z[k + 1] = f64::MAX;
                break;
            }
        }
    }
    k = 0;
    for q in 0..size {
        let qf64 = q as f64;
        while z[k + 1] < qf64 {
            k += 1;
        }
        let vkf64 = v[k] as f64;
        grid[offset + q * step_by] = (qf64 - vkf64) * (qf64 - vkf64) + f[v[k]];
    }
}
fn dt ( grid : & mut Vec < f64 > , offset : usize , step_by : usize , size : usize ) { // For our purposes, f is a one-dimensional slice of the grid let f : Vec < f64 > = grid . iter ( ) . skip ( offset ) . step_by ( step_by ) . copied ( ) . collect ( ) ; // It may be possible to make this more functional in style, // but for now this is more or less a "dumb" transcription of // the algorithm presented in the paper by Felzenszwalb & Huttenlocher. let mut k = 0 ; let mut v = vec ! [ 0 size ] ; let mut z = vec ! [ 0f64 size 1 ] ; let mut s : f64 ; z [ 0 ] = f64 :: MIN ; z [ 1 ] = f64 :: MAX ; for q in 1 .. size { loop { let q2 = ( q * q ) as f64 ; let vk2 = ( v [ k ] * v [ k ] ) as f64 ; let denom = ( 2 * q - 2 * v [ k ] ) as f64 ; s = ( ( f [ q ] + q2 ) - ( f [ v [ k ] ] + vk2 ) ) / denom ; if s <= z [ k ] { k -= 1 ; } else { k += 1 ; v [ k ] = q ; z [ k ] = s ; z [ k + 1 ] = f64 :: MAX ; break ; } } } k = 0 ; for q in 0 .. size { let qf64 = q as f64 ; while z [ k + 1 ] < qf64 { k += 1 ; } let vkf64 = v [ k ] as f64 ; grid [ offset + q * step_by ] = ( qf64 - vkf64 ) * ( qf64 - vkf64 ) + f [ v [ k ] ] ; } }
An O(n) Euclidean Distance Transform algorithm.
An O ( n ) Euclidean Distance Transform algorithm .
grid
&mut Vec<f64>
offset
usize
step_by
usize
size
usize
grid
&mut Vec<f64>
offset
usize
step_by
usize
size
usize