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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! This crate approximates a signed
//! distance field, given a binary image,
//! 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::binary_image::{ self, BinaryImage };

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

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

    pub use crate::{
        compute_f16_distance_field,
        compute_f32_distance_field
    };
}


use prelude::*;
use crate::distance_field::DistanceStorage;
use crate::binary_image::BinaryImage;

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

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

/// Compute the signed distance field of the specified binary image with an `f32` distance storage.
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(2048, 2048, 0.05, is_inside_circle(128, 128, 64));
    }

    #[test]
    pub fn reconstruct_dot(){
        reconstruct_binary_image(2048, 2048, 0.05, is_inside_circle(1024, 1024, 4));
    }

    #[test]
    pub fn reconstruct_top_left(){
        reconstruct_binary_image(2048, 2048, 0.05, is_inside_circle(0, 0, 14));
    }

    #[test]
    pub fn reconstruct_top_right(){
        reconstruct_binary_image(2048, 2048, 0.05, is_inside_circle(2048, 0, 14));
    }

    #[test]
    pub fn reconstruct_rectangle(){
        reconstruct_binary_image(2048, 2048, 0.05, is_inside_rectangle(179, 179, 37, 37));
    }

    #[test]
    pub fn reconstruct_stripes(){
        reconstruct_binary_image(2048, 2048, 0.07, is_inside_checker(179, 37));
    }


    fn reconstruct_binary_image(
        width: usize, height: usize, tolerance: f32,
        image: impl Fn(usize, usize) -> bool
    ) {
        use crate::binary_image::BinaryImage;
        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 = binary_image::of_byte_slice(
            &binary_image_buffer, width as u16, height as u16
        );

        let distance_field_16 = compute_f32_distance_field(&binary_image);
        let distance_field_32 = compute_f16_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 distance_16 = distance_field_16.get_distance(x, y);
                let distance_32 = distance_field_32.get_distance(x, y);
                let distance = (distance_16 + distance_32) / 2.0;

                if distance.is_infinite() {
                    panic!("no shape in binary image");
                }

                let reconstructed = distance < 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.min(y)
        }
    }

    #[test]
    pub fn reconstruct_circle_distance_field(){
        reconstruct_distance_field(2048, 2048, 2.0, circle_distance(128, 128, 128));
    }

    #[test]
    pub fn reconstruct_dot_distance_field(){
        reconstruct_distance_field(2048, 2048, 2.0, circle_distance(128, 128, 4));
    }

    #[test]
    pub fn reconstruct_rectangle_distance_field(){
        reconstruct_distance_field(2048, 2048, 2.0, rectangle_distance(1023, 179, 137, 137));
    }

    #[test]
    pub fn reconstruct_large_rectangle_distance_field(){ // TODO reduce error further?
        reconstruct_distance_field(2048, 2048, 25.0, rectangle_distance(1024, 1023, 613, 673));
    }

    #[test]
    pub fn reconstruct_small_rectangle_distance_field(){
        reconstruct_distance_field(2048, 2048, 2.0, rectangle_distance(179, 1023, 4, 7));
    }

    pub fn reconstruct_distance_field(
        width: usize, height: usize, tolerance: f32,
        image: impl Fn(usize, usize) -> f32
    ) {
        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 = binary_image::of_byte_slice(
            &binary_image_buffer, width as u16, height as u16
        );

        let distance_field_16 = compute_f32_distance_field(&binary_image);
        let distance_field_32 = compute_f16_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_16 = distance_field_16.get_distance(x, y);
                let reconstructed_32 = distance_field_32.get_distance(x, y);
                let reconstructed = (reconstructed_16 + reconstructed_32) / 2.0;

                if reconstructed.is_infinite() {
                    panic!("no shape in binary image");
                }

                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");
    }
}