mesh_to_sdf
⚠️ This crate is still in its early stages. Expect the API to change.
This crate provides two entry points:
- [
generate_sdf]: computes the signed distance field for the mesh defined byverticesandindicesat the pointsquery_points. - [
generate_grid_sdf]: computes the signed distance field for the mesh defined byverticesandindiceson a [Grid].
// vertices are [f32; 3], but can be cgmath::Vector3<f32>, glam::Vec3, etc.
let vertices: = vec!;
let indices: = vec!;
// query points must be of the same type as vertices
let query_points: = vec!;
// Query points are expected to be in the same space as the mesh.
let sdf: = generate_sdf;
for point in query_points.iter.zip
// if you can, use generate_grid_sdf instead of generate_sdf as it's optimized and much faster.
let bounding_box_min = ;
let bounding_box_max = ;
let cell_count = ;
let grid = from_bounding_box;
let sdf: = generate_grid_sdf;
for x in 0..cell_count
Mesh Topology
Indices can be of any type that implements Into<u32>, e.g. u16 and u32. Topology can be list or strip.
If the indices are not provided, they are supposed to be 0..vertices.len().
For vertices, this library aims to be as generic as possible by providing a trait Point that can be implemented for any type.
Implementations for most common math libraries are gated behind feature flags. By default, only [f32; 3] is provided.
If you do not find your favorite library, feel free to implement the trait for it and submit a PR or open an issue.
Using your favorite library
To use your favorite math library with mesh_to_sdf, you need to add it to mesh_to_sdf dependency. For example, to use glam:
[]
= { = "0.1", = ["glam"] }
Currently, the following libraries are supported:
cgmath(cgmath::Vector3<f32>)glam(glam::Vec3)mint(mint::Vector3<f32>andmint::Point3<f32>)nalgebra(nalgebra::Vector3<f32>andnalgebra::Point3<f32>)- and
[f32; 3]
Determining inside/outside
As of now, sign is computed by checking the normals of the triangles. This is not robust and might lead to negative distances leaking outside the mesh in pyramidal shapes. A more robust solution is planned for the future.
Benchmarks
[generate_grid_sdf] is much faster than [generate_sdf] and should be used whenever possible.
[generate_sdf] does not allocate memory (except for the result array) but is slow. A faster implementation is planned for the future.
License: MIT OR Apache-2.0