use manifold_csg_sys::*;
pub struct MeshGL {
ptr: *mut ManifoldMeshGL,
}
unsafe impl Send for MeshGL {}
unsafe impl Sync for MeshGL {}
impl Drop for MeshGL {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { manifold_delete_meshgl(self.ptr) };
}
}
}
impl MeshGL {
#[must_use]
pub fn new(vert_props: &[f32], n_props: usize, tri_indices: &[u32]) -> Self {
assert!(n_props >= 3, "n_props must be >= 3");
assert!(
vert_props.len() % n_props == 0,
"vert_props length must be divisible by n_props"
);
assert!(
tri_indices.len() % 3 == 0,
"tri_indices length must be divisible by 3"
);
let n_verts = vert_props.len() / n_props;
let n_tris = tri_indices.len() / 3;
let ptr = unsafe { manifold_alloc_meshgl() };
unsafe {
manifold_meshgl(
ptr,
vert_props.as_ptr(),
n_verts,
n_props,
tri_indices.as_ptr(),
n_tris,
);
}
Self { ptr }
}
#[must_use]
pub fn new_with_tangents(
vert_props: &[f32],
n_props: usize,
tri_indices: &[u32],
halfedge_tangent: &[f32],
) -> Self {
assert!(n_props >= 3, "n_props must be >= 3");
assert!(vert_props.len() % n_props == 0);
assert!(tri_indices.len() % 3 == 0);
let n_verts = vert_props.len() / n_props;
let n_tris = tri_indices.len() / 3;
let ptr = unsafe { manifold_alloc_meshgl() };
unsafe {
manifold_meshgl_w_tangents(
ptr,
vert_props.as_ptr(),
n_verts,
n_props,
tri_indices.as_ptr(),
n_tris,
halfedge_tangent.as_ptr(),
);
}
Self { ptr }
}
#[must_use]
pub fn num_vert(&self) -> usize {
unsafe { manifold_meshgl_num_vert(self.ptr) }
}
#[must_use]
pub fn num_tri(&self) -> usize {
unsafe { manifold_meshgl_num_tri(self.ptr) }
}
#[must_use]
pub fn num_prop(&self) -> usize {
unsafe { manifold_meshgl_num_prop(self.ptr) }
}
#[must_use]
pub fn vert_properties(&self) -> Vec<f32> {
let len = unsafe { manifold_meshgl_vert_properties_length(self.ptr) };
let mut buf = vec![0.0f32; len];
unsafe { manifold_meshgl_vert_properties(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn tri_verts(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_tri_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_tri_verts(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn merge(&self) -> Self {
let ptr = unsafe { manifold_alloc_meshgl() };
unsafe { manifold_meshgl_merge(ptr, self.ptr) };
Self { ptr }
}
#[must_use]
pub fn merge_from_vert(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_merge_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_merge_from_vert(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn merge_to_vert(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_merge_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_merge_to_vert(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_index(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_run_index_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_run_index(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_original_id(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_run_original_id_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_run_original_id(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_transform(&self) -> Vec<f32> {
let len = unsafe { manifold_meshgl_run_transform_length(self.ptr) };
let mut buf = vec![0.0f32; len];
unsafe { manifold_meshgl_run_transform(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn face_id(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl_face_id_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl_face_id(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn halfedge_tangent(&self) -> Vec<f32> {
let len = unsafe { manifold_meshgl_tangent_length(self.ptr) };
let mut buf = vec![0.0f32; len];
unsafe { manifold_meshgl_halfedge_tangent(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn tolerance(&self) -> f32 {
unsafe { manifold_meshgl_tolerance(self.ptr) }
}
#[must_use]
pub fn num_run(&self) -> usize {
unsafe { manifold_meshgl_num_run(self.ptr) }
}
#[must_use]
pub fn run_flags(&self) -> Vec<u8> {
let len = unsafe { manifold_meshgl_run_flags_length(self.ptr) };
let mut buf = vec![0u8; len];
unsafe { manifold_meshgl_run_flags(buf.as_mut_ptr(), self.ptr) };
buf
}
pub fn update_normals(&mut self, normal_idx: i32) {
unsafe { manifold_meshgl_update_normals(self.ptr, normal_idx) };
}
}
impl Clone for MeshGL {
fn clone(&self) -> Self {
let ptr = unsafe { manifold_alloc_meshgl() };
unsafe { manifold_meshgl_copy(ptr, self.ptr) };
Self { ptr }
}
}
pub struct MeshGL64 {
ptr: *mut ManifoldMeshGL64,
}
unsafe impl Send for MeshGL64 {}
unsafe impl Sync for MeshGL64 {}
impl Drop for MeshGL64 {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { manifold_delete_meshgl64(self.ptr) };
}
}
}
impl MeshGL64 {
#[must_use]
pub fn new(vert_props: &[f64], n_props: usize, tri_indices: &[u64]) -> Self {
assert!(n_props >= 3, "n_props must be >= 3");
assert!(
vert_props.len() % n_props == 0,
"vert_props length must be divisible by n_props"
);
assert!(
tri_indices.len() % 3 == 0,
"tri_indices length must be divisible by 3"
);
let n_verts = vert_props.len() / n_props;
let n_tris = tri_indices.len() / 3;
let ptr = unsafe { manifold_alloc_meshgl64() };
unsafe {
manifold_meshgl64(
ptr,
vert_props.as_ptr(),
n_verts,
n_props,
tri_indices.as_ptr(),
n_tris,
);
}
Self { ptr }
}
#[must_use]
pub fn new_with_tangents(
vert_props: &[f64],
n_props: usize,
tri_indices: &[u64],
halfedge_tangent: &[f64],
) -> Self {
assert!(n_props >= 3, "n_props must be >= 3");
assert!(vert_props.len() % n_props == 0);
assert!(tri_indices.len() % 3 == 0);
let n_verts = vert_props.len() / n_props;
let n_tris = tri_indices.len() / 3;
let ptr = unsafe { manifold_alloc_meshgl64() };
unsafe {
manifold_meshgl64_w_tangents(
ptr,
vert_props.as_ptr(),
n_verts,
n_props,
tri_indices.as_ptr(),
n_tris,
halfedge_tangent.as_ptr(),
);
}
Self { ptr }
}
#[must_use]
pub fn num_vert(&self) -> usize {
unsafe { manifold_meshgl64_num_vert(self.ptr) }
}
#[must_use]
pub fn num_tri(&self) -> usize {
unsafe { manifold_meshgl64_num_tri(self.ptr) }
}
#[must_use]
pub fn num_prop(&self) -> usize {
unsafe { manifold_meshgl64_num_prop(self.ptr) }
}
#[must_use]
pub fn vert_properties(&self) -> Vec<f64> {
let len = unsafe { manifold_meshgl64_vert_properties_length(self.ptr) };
let mut buf = vec![0.0f64; len];
unsafe { manifold_meshgl64_vert_properties(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn tri_verts(&self) -> Vec<u64> {
let len = unsafe { manifold_meshgl64_tri_length(self.ptr) };
let mut buf = vec![0u64; len];
unsafe { manifold_meshgl64_tri_verts(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn merge(&self) -> Self {
let ptr = unsafe { manifold_alloc_meshgl64() };
unsafe { manifold_meshgl64_merge(ptr, self.ptr) };
Self { ptr }
}
#[must_use]
pub fn merge_from_vert(&self) -> Vec<u64> {
let len = unsafe { manifold_meshgl64_merge_length(self.ptr) };
let mut buf = vec![0u64; len];
unsafe { manifold_meshgl64_merge_from_vert(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn merge_to_vert(&self) -> Vec<u64> {
let len = unsafe { manifold_meshgl64_merge_length(self.ptr) };
let mut buf = vec![0u64; len];
unsafe { manifold_meshgl64_merge_to_vert(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_index(&self) -> Vec<u64> {
let len = unsafe { manifold_meshgl64_run_index_length(self.ptr) };
let mut buf = vec![0u64; len];
unsafe { manifold_meshgl64_run_index(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_original_id(&self) -> Vec<u32> {
let len = unsafe { manifold_meshgl64_run_original_id_length(self.ptr) };
let mut buf = vec![0u32; len];
unsafe { manifold_meshgl64_run_original_id(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn run_transform(&self) -> Vec<f64> {
let len = unsafe { manifold_meshgl64_run_transform_length(self.ptr) };
let mut buf = vec![0.0f64; len];
unsafe { manifold_meshgl64_run_transform(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn face_id(&self) -> Vec<u64> {
let len = unsafe { manifold_meshgl64_face_id_length(self.ptr) };
let mut buf = vec![0u64; len];
unsafe { manifold_meshgl64_face_id(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn halfedge_tangent(&self) -> Vec<f64> {
let len = unsafe { manifold_meshgl64_tangent_length(self.ptr) };
let mut buf = vec![0.0f64; len];
unsafe { manifold_meshgl64_halfedge_tangent(buf.as_mut_ptr(), self.ptr) };
buf
}
#[must_use]
pub fn tolerance(&self) -> f64 {
unsafe { manifold_meshgl64_tolerance(self.ptr) }
}
#[must_use]
pub fn num_run(&self) -> usize {
unsafe { manifold_meshgl64_num_run(self.ptr) }
}
#[must_use]
pub fn run_flags(&self) -> Vec<u8> {
let len = unsafe { manifold_meshgl64_run_flags_length(self.ptr) };
let mut buf = vec![0u8; len];
unsafe { manifold_meshgl64_run_flags(buf.as_mut_ptr(), self.ptr) };
buf
}
pub fn update_normals(&mut self, normal_idx: i32) {
unsafe { manifold_meshgl64_update_normals(self.ptr, normal_idx) };
}
pub fn from_obj(obj_content: &str) -> Result<Self, crate::types::CsgError> {
let c_str = std::ffi::CString::new(obj_content).map_err(|_| {
crate::types::CsgError::InvalidInput("OBJ content contains null byte".into())
})?;
let ptr = unsafe { manifold_alloc_meshgl64() };
unsafe { manifold_meshgl64_read_obj(ptr, c_str.as_ptr()) };
Ok(Self { ptr })
}
#[must_use]
pub fn to_obj(&self) -> String {
let mut result = String::new();
unsafe extern "C" fn callback(data: *mut std::ffi::c_char, ctx: *mut std::ffi::c_void) {
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let result = unsafe { &mut *(ctx as *mut String) };
let c_str = unsafe { std::ffi::CStr::from_ptr(data) };
*result = c_str.to_string_lossy().into_owned();
}));
}
let ctx = &mut result as *mut String as *mut std::ffi::c_void;
unsafe { manifold_meshgl64_write_obj(self.ptr, Some(callback), ctx) };
result
}
}
impl Clone for MeshGL64 {
fn clone(&self) -> Self {
let ptr = unsafe { manifold_alloc_meshgl64() };
unsafe { manifold_meshgl64_copy(ptr, self.ptr) };
Self { ptr }
}
}