Skip to content

Commit

Permalink
Add high-level bindings for meshopt_simplifyScale
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastLe9enD committed Mar 24, 2024
1 parent 39e6180 commit 5d243a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/packing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl FromVertex for PackedVertexOct {
#[repr(C)]
/// A basic Vertex type that can be used with most mesh processing functions.
/// You don't _need_ to use this type, you can use your own type by implementing
/// the DecodePosition trait and making a [`VertexDataAdapter`] from slices of it.
/// the `DecodePosition` trait and making a [`VertexDataAdapter`] from slices of it.
pub struct Vertex {
pub p: [f32; 3],
pub n: [f32; 3],
Expand Down
33 changes: 33 additions & 0 deletions src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,36 @@ pub fn simplify_sloppy_decoder<T: DecodePosition>(
result.resize(index_count, 0u32);
result
}

/// Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
///
/// Absolute error must be *divided* by the scaling factor before passing it to `simplify` as `target_error`
/// Relative error returned by `simplify` via `result_error` must be *multiplied* by the scaling factor to get absolute error.
pub fn simplify_scale(vertices: &VertexDataAdapter<'_>) -> f32 {
unsafe {
ffi::meshopt_simplifyScale(
vertices.pos_ptr(),
vertices.vertex_count,
vertices.vertex_stride,
)
}
}

/// Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
///
/// Absolute error must be *divided* by the scaling factor before passing it to `simplify` as `target_error`
/// Relative error returned by `simplify` via `result_error` must be *multiplied* by the scaling factor to get absolute error.
pub fn simplify_scale_decoder<T: DecodePosition>(vertices: &[T]) -> f32 {
let positions = vertices
.iter()
.map(|vertex| vertex.decode_position())
.collect::<Vec<[f32; 3]>>();

unsafe {
ffi::meshopt_simplifyScale(
positions.as_ptr().cast(),
positions.len(),
mem::size_of::<f32>() * 3,
)
}
}

0 comments on commit 5d243a8

Please sign in to comment.