voronoid
v0.0.2
Published
3D Voronoi tessellation in Rust: a fast, feature-rich grid-based approach ready for WASM and TypeScript.
Downloads
25
Maintainers
Readme
voronoid
Rust library for 3D Voronoi tessellations, designed to be used in Rust as well as compiled to WebAssembly (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.
Installation
npm install voronoidUsage
import {init, Tessellation, BoundingBox, Wall } from 'voronoid';
async function run() {
await init();
// Create tessellation from bounding box and bins per axis.
const bounds = new BoundingBox(0, 0, 0, 100, 100, 100);
const bin_cnt = 10;
const tess = new Tessellation(bounds, bin_cnt, bin_cnt, bin_cnt);
// Optional: add a wall to constrain the tessellation.
tess.add_wall(Wall.new_sphere(50, 50, 50, 40, -1000));
// Set the generators randomly, explicit setting via set_generators
// or read_generators is also possible.
const generator_cnt = 1000;
tess.random_generators(generator_cnt);
// Perform the Voronoi tessellation.
tess.calculate();
// Evaluate the results: print average number of faces per cell.
let total_faces = 0;
const cell_count = tess.count_cells;
for (let i = 0; i < cell_count; i++) {
const cell = tess.get_cell(i);
if (cell) {
total_faces += cell.faces().length;
}
}
console.log(`Average faces per cell: ${total_faces / cell_count}`);
}
run();License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
