marching-squares 0.1.1

Parallelized marching squares algorithm for constructing closed isolines / contour lines
Documentation
  • Coverage
  • 66.67%
    8 out of 12 items documented1 out of 5 items with examples
  • Size
  • Source code size: 20.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 358.02 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • fschutt/marching-squares
    4 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • fschutt

marching-squares

This crate provides an (optionally parallelizable) marching squares algorithm to generate isolines from a heightmap of Vec<Vec<i16>> values.

Adapted from https://github.com/d-dorazio/marching-squares

Warning

  • The returned lines may only have two points.

Example

use marching_squares::{Field, Point};

fn main() {

    let width = 1600_usize;
    let height = 1600_usize;

    let n_steps = 10_usize;
    let mut min_val = 0;
    let mut max_val = 0;

    // Build the heightmap data (here: randomly generated from a function)
    let z_values = (0..height).map(|y| {
        (0..width).map(|x| {
            let x = (x as f64 - width as f64 / 2.0) / 150.0;
            let y = (y as f64 - height as f64 / 2.0) / 150.0;
            let val = ((1.3 * x).sin() * (0.9 * y).cos() + (0.8 * x).cos() * (1.9 * y).sin() + (y * 0.2 * x).cos()) as i16;
            min_val = min_val.min(val);
            max_val = max_val.max(val);
            val
        }).collect()
    }).collect::<Vec<Vec<i16>>>();

    let field = Field {
        dimensions: (width, height),
        top_left: Point { x: 0.0, y: 0.0 },
        pixel_size: (1.0, 1.0),
        values: &z_values,
    };

    let step_size = (max_val - min_val) as f32 / n_steps as f32;

    // Generate 10 isolines
    // note: you could do this in parallel using rayon
    for step in 0..n_steps {
        let isoline_height = min_val as f32 + (step_size * step as f32);
        println!("{:#?}", field.get_contours(isoline_height as i16));
    }
}

License: MIT