voronoid 0.0.2

3D Voronoi tessellation in Rust: a fast, feature-rich grid-based approach ready for WASM and TypeScript.
Documentation

voronoid

crates.io npm tests examples docs

Rust library for D-dimensional Voronoi tessellations, designed to be used in Rust as well as compiled to WebAssembly (with a TypeScript interface). It provides a flexible and feature-rich implementation to calculate the individual cells by a clipping procedure based on the generating points, the bounding box and possible walls. The tessellation struct takes a spatial algorithm to calculate the nearest neighbours efficiently and a cell struct which manages cell data and the clipping algorithm. The combination of spatial algorithm and cell can then be matched to the specific application and distribution of generators. A few interactive examples are shown below.

WebAssembly and TypeScript API

This library is designed to directly compile to WASM, using wasm-pack, and is compatible with TypeScript. The package is published on npm and can be installed with:

npm install voronoid

Consult the www folder for interactive examples and more details on how to use with TypeScript and in a web environment.

To build the project for web usage:

wasm-pack build --target web

which can then be added as a local dependency. To prepare the generated package for publication on npm run the patch_npm_pkg script.

node patch_npm_pkg.js

Usage & Documentation

The library, with documentation, can also be direclty used in Rust by installing it with:

cargo add voronoid

For a small usage example we generate a Voronoi tessellation for a 3D box with randomly positioned generators and calculate the total volume.

use voronoid::{BoundingBox, Tessellation, Algorithm3DGrid, Cell3DFaces, Wall, WALL_ID_MAX};

fn main() {
  let size = 10.0;
  let nr_bins = 10;
  // Creates a bounding box of length, widht , height = size.
  let bounds = BoundingBox::new([0.0, 0.0, 0.0], [size, size, size]);
  // Initializes a 3D tessellation with a grid algorithm and the bounding box.
  let mut tess = Tessellation::<3, Cell3DFaces, _>::new(bounds.clone(), Algorithm3DGrid::new(nr_bins, nr_bins, nr_bins, &bounds));
  // Add a spherical wall that spans the box.
  let r = size / 2.0;
  tess.add_wall(Wall::new(WALL_ID_MAX, Box::new(SphereGeometry::new([size/2.0, size/2.0, size/2.0], r))));
  // Fill the tessellation with random generators (automatically confined to the walls).
  tess.random_generators(1000);
  // Calculate the tessellation.
  tess.calculate();
  // Calculate the total volume of all cells.
  let total_volume: f64 = (0..tess.count_cells())
    .map(|i| tess.get_cell(i).unwrap().volume())
    .sum();
  // Compare the theoretical value.
  let mut sphere_volume = 04.0 / 3.0 * std::f64::consts::PI * 4.0f64.powi(3);
  println!("total cell volume: {}", total_volume);
  println!("theoretical sphere volume: {}", sphere_volume);
}

Development

More information on the tests, benchmarks and examples is in their respective directories. They can be run by:

cargo test
cargo bench
cargo example --example <example>

Contributing is highly appreciated via issues and pull requests.

License

Licensed under either of

at your option.