Crates.io | signed-distance-field |
lib.rs | signed-distance-field |
version | 0.6.3 |
source | src |
created_at | 2019-02-04 21:35:49.829383 |
updated_at | 2019-02-13 14:28:50.232756 |
description | Fast signed distance fields from binary images using `dead reckoning` |
homepage | |
repository | https://github.com/johannesvollmer/sdf-dead-reckoning |
max_upload_size | |
id | 112771 |
size | 38,252 |
Fast Signed Distance Fields for Rust
This crate approximates a signed distance field, given a binary image. The algorithm is inspired by the paper "The dead reckoning signed distance transform" by George J. Grevara (2004). Don't forget to compile in release mode!
In the process of computing the signed distance field, the algorithm constructs an image with each pixel containing the vectors which point to the nearest edge. This vector distance field is made available after computing the plain distance field and can be used for further processing. Also, the library offers a simple conversion from distance fields to images with integer precision.
Update your Cargo.toml
:
signed-distance-field = { version = "0.6.3", features = [ "piston_image" ] }
use signed_distance_field::prelude::*;
fn main(){
// load data using piston image
let mut gray_image = image::open("sketch.jpg").unwrap().to_luma();
// interpret grayscale image as binary image with any pixel brighter than 80 being 'on'
let binary_image = binary_piston_image::of_gray_image_with_threshold(&gray_image, 80);
// convert the binary image to a distance field
let distance_field = compute_f32_distance_field(&binary_image);
// clip all distances greater than 10px and compress them into a byte array
// so that a distance of -10px is 0 and a distance of 10px is 255
// (edges, having a distance of 0px, will be 128)
let distance_image = distance_field
.normalize_clamped_distances(-10.0, 10.0)
// convert f32 distance field to u8 piston image
.unwrap().to_gray_u8_image();
// save the piston image as png
distance_image.save("sketch_distance.png").unwrap();
}
This library can be configured to offer some
simple conversions to and from piston images.
The feature flag piston_image
unlocks these functions.
The image crate is not required to calculate the
signed distance field, including piston image is truly optional.