1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! This crate approximates a signed
//! distance field, given a binary image.
//! The algorithm used is called "dead reckoning",
//! as described in `The "dead reckoning" signed distance transform`
//! by George J. Grevara (2004).

pub mod binary_image;
pub mod distance_field;

pub mod prelude {
    pub use crate::{
        compute_distance_field,
        compute_f16_distance_field,
        compute_f32_distance_field
    };

    pub use crate::binary_image::{
        BinaryImage, BinaryByteImage
    };

    pub use crate::distance_field::{
        SignedDistanceField, DistanceStorage,
        F16DistanceStorage, F32DistanceStorage
    };

    #[cfg(feature = "piston_image")]
    pub use crate::binary_image::piston_image as binary_piston_image;
}


use prelude::*;

/// Compute the signed distance field with the specified distance storage of the specified binary image.
pub fn compute_distance_field<D: DistanceStorage>(image: &impl BinaryImage) -> SignedDistanceField<D> {
    SignedDistanceField::compute(image)
}

/// Compute the signed distance field with an `f16` distance storage of the specified binary image.
pub fn compute_f16_distance_field(image: &impl BinaryImage) -> SignedDistanceField<F16DistanceStorage> {
    compute_distance_field(image)
}

/// Compute the signed distance field with an `f32` distance storage of the specified binary image.
pub fn compute_f32_distance_field(image: &impl BinaryImage) -> SignedDistanceField<F32DistanceStorage> {
    compute_distance_field(image)
}


#[cfg(test)]
mod tests {
    use crate::prelude::*;

    fn is_inside_circle(center_x: usize, center_y: usize, radius: usize) -> impl Fn(usize, usize) -> bool {
        move |x,y|{
            let x = (x as isize - center_x as isize) as f32;
            let y = (y as isize - center_y as isize) as f32;
            (x * x + y * y).sqrt() < radius as f32
        }
    }

    fn is_inside_rectangle(center_x: usize, center_y: usize, width: usize, height: usize) -> impl Fn(usize, usize) -> bool {
        let w = width as f32;
        let h = height as f32;

        move |x,y|{
            let x = x as f32 - center_x as f32;
            let y = y as f32 - center_y as f32;
            x > -w && x < w && y > -h && y < h
        }
    }

    fn is_inside_checker(width: usize, height: usize) -> impl Fn(usize, usize) -> bool {
        move |x,y|{
            (x % width < width / 2) != (y % height < height / 2)
        }
    }

    #[test]
    pub fn reconstruct_circle(){
        reconstruct_binary_image::<F16DistanceStorage, _>(
            2048, 2048, 0.05, is_inside_circle(128, 128, 64)
        );
    }

    #[test]
    pub fn reconstruct_dot(){ // TODO profile this
        reconstruct_binary_image::<F16DistanceStorage, _>(
            2048, 2048, 0.05, is_inside_circle(1024, 1024, 4)
        );
    }

    #[test]
    pub fn reconstruct_rectangle(){
        reconstruct_binary_image::<F32DistanceStorage, _>(
            2048, 2048, 0.05, is_inside_rectangle(179, 179, 37, 37)
        );
    }

    #[test]
    pub fn reconstruct_stripes(){
        reconstruct_binary_image::<F32DistanceStorage, _>(
            2048, 2048, 0.07, is_inside_checker(179, 37)
        );
    }


    fn reconstruct_binary_image<D: DistanceStorage, I: Fn(usize, usize) -> bool>(
        width: usize, height: usize, tolerance: f32, image: I
    ) {
        let mut binary_image_buffer = vec![0_u8; width * height];

        for y in 0..height {
            for x in 0..width {
                binary_image_buffer[width * y + x] = if image(x, y) { 255 } else { 0 };
            }
        }

        let binary_image = BinaryByteImage::from_slice(
            width as u16, height as u16, &binary_image_buffer
        );

        let distance_field: SignedDistanceField<D> = crate::compute_distance_field(&binary_image);

        let mut wrong_pixels = 0;
        for y in 0..height as u16 {
            for x in 0..width as u16 {
                let ground_truth = binary_image.is_inside(x, y);
                let reconstructed = distance_field.get_distance(x, y) < 0.0;
                if ground_truth != reconstructed {
                    wrong_pixels += 1;
                }
            }
        }

        let quality = wrong_pixels as f32 / (width as f32 * height as f32);
        println!("wrong pixels: {} of {} ({})", wrong_pixels, width * height, quality);
        assert!(quality < tolerance, "too many incorrect pixels");
    }





    fn circle_distance(center_x: usize, center_y: usize, radius: usize) -> impl Fn(usize, usize) -> f32 {
        move |x,y|{
            let x = (x as isize - center_x as isize) as f32;
            let y = (y as isize - center_y as isize) as f32;
            (x * x + y * y).sqrt() - radius as f32
        }
    }

    fn rectangle_distance(center_x: usize, center_y: usize, width: usize, height: usize) -> impl Fn(usize, usize) -> f32 {
        move |x,y|{
            let x = x as f32 - center_x as f32;
            let y = y as f32 - center_y as f32;
            let x = x.abs() - width as f32;
            let y = y.abs() - height as f32;
            x.max(y)
        }
    }

    #[test]
    pub fn reconstruct_circle_distance_field(){
        reconstruct_distance_field::<F16DistanceStorage, _>(
            2048, 2048, 2.0, circle_distance(128, 128, 128)
        );
    }

    #[test]
    pub fn reconstruct_dot_distance_field(){
        reconstruct_distance_field::<F16DistanceStorage, _>(
            2048, 2048, 2.0, circle_distance(128, 128, 4)
        );
    }

    #[test]
    pub fn reconstruct_rectangle_distance_field(){
        reconstruct_distance_field::<F16DistanceStorage, _>(
            2048, 2048, 130.0, // FIXME an error of 130.0 pixeldistance per pixel is a bug
            rectangle_distance(179, 179, 137, 137)
        );
    }

    pub fn reconstruct_distance_field<D: DistanceStorage, I: Fn(usize, usize) -> f32>(
        width: usize, height: usize, tolerance: f32, image: I
    ) {
        let mut distance_buffer = vec![0.0; width * height];

        for y in 0..height {
            for x in 0..width {
                distance_buffer[width * y + x] = image(x, y);
            }
        }

        let binary_image_buffer: Vec<u8> = distance_buffer.iter()
            .map(|distance| if *distance < 0.0 { 255 } else { 0 })
            .collect();

        let binary_image = BinaryByteImage::from_slice(
            width as u16, height as u16, &binary_image_buffer
        );

        let distance_field: SignedDistanceField<D> = crate::compute_distance_field(&binary_image);

        let mut summed_error = 0.0;
        for y in 0..height as u16 {
            for x in 0..width as u16 {
                let ground_truth = distance_buffer[y as usize * width + x as usize];
                let reconstructed = distance_field.get_distance(x, y);

                if reconstructed.is_infinite() {
                    panic!("infinite distance at {} {}", x, y);
                }

                summed_error += (ground_truth - reconstructed).abs();
            }
        }

        let error_per_pixel = summed_error / (width as f32 * height as f32);
        println!("average error per pixel: {}", error_per_pixel);

        assert!(error_per_pixel < tolerance, "too many incorrect pixels");
    }
}