Skip to content

Commit

Permalink
feat: upgrades to manifold v3.0.0
Browse files Browse the repository at this point in the history
feat: adds ManifoldError
feat: adds Manifold#revolve
refactor: moves constructors to impl blocks
refactor: improves Angle struct
  • Loading branch information
NickUfer committed Nov 18, 2024
1 parent 958d7f4 commit 01c73e1
Show file tree
Hide file tree
Showing 10 changed files with 501 additions and 316 deletions.
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ name = "manifold3d"
description = "Bindings for Manifold - a Geometry library for topological robustness"
homepage = "https://github.com/NickUfer/manifold3d-rs"
license = "Apache-2.0"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

[dependencies]
thiserror = "1.0.64"
thiserror = "2.0"
num-traits = "0.2.19"
manifold-sys = { version = "0.0.2" }
manifold3d-sys = { version = "0.0.3" }
nalgebra = { version = "0.33.0", optional = true }

[features]
nalgebra_interop = ["nalgebra"]
nalgebra_interop = ["nalgebra"]
export = ["manifold3d-sys/export"]
parallel = ["manifold3d-sys/parallel"]
40 changes: 20 additions & 20 deletions src/bounding_box.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::{Matrix4x3, Point3, Vec3};
use manifold_sys::{
use manifold3d_sys::{
manifold_alloc_box, manifold_box, manifold_box_center, manifold_box_contains_box,
manifold_box_contains_pt, manifold_box_dimensions, manifold_box_does_overlap_box,
manifold_box_does_overlap_pt, manifold_box_include_pt, manifold_box_is_finite,
Expand All @@ -11,27 +11,27 @@ use std::os::raw::c_void;

pub struct BoundingBox(*mut ManifoldBox);

pub fn new(min_point: Point3, max_point: Point3) -> BoundingBox {
let manifold_box_ptr = unsafe { manifold_alloc_box() };
unsafe {
manifold_box(
manifold_box_ptr as *mut c_void,
min_point.x,
min_point.y,
min_point.z,
max_point.x,
max_point.y,
max_point.z,
)
};
BoundingBox(manifold_box_ptr)
}
impl BoundingBox {
pub fn new(min_point: Point3, max_point: Point3) -> BoundingBox {
let manifold_box_ptr = unsafe { manifold_alloc_box() };
unsafe {
manifold_box(
manifold_box_ptr as *mut c_void,
min_point.x,
min_point.y,
min_point.z,
max_point.x,
max_point.y,
max_point.z,
)
};
BoundingBox(manifold_box_ptr)
}

pub(crate) fn from_ptr(ptr: *mut ManifoldBox) -> BoundingBox {
BoundingBox(ptr)
}
pub(crate) fn from_ptr(ptr: *mut ManifoldBox) -> BoundingBox {
BoundingBox(ptr)
}

impl BoundingBox {
pub fn min_point(&self) -> Point3 {
unsafe { Point3::from(manifold_box_min(self.0)) }
}
Expand Down
86 changes: 86 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::manifold::Manifold;
use manifold3d_sys::{
manifold_status, ManifoldError, ManifoldError_MANIFOLD_FACE_ID_WRONG_LENGTH,
ManifoldError_MANIFOLD_INVALID_CONSTRUCTION, ManifoldError_MANIFOLD_MERGE_INDEX_OUT_OF_BOUNDS,
ManifoldError_MANIFOLD_MERGE_VECTORS_DIFFERENT_LENGTHS,
ManifoldError_MANIFOLD_MISSING_POSITION_PROPERTIES, ManifoldError_MANIFOLD_NON_FINITE_VERTEX,
ManifoldError_MANIFOLD_NOT_MANIFOLD, ManifoldError_MANIFOLD_NO_ERROR,
ManifoldError_MANIFOLD_PROPERTIES_WRONG_LENGTH, ManifoldError_MANIFOLD_RUN_INDEX_WRONG_LENGTH,
ManifoldError_MANIFOLD_TRANSFORM_WRONG_LENGTH,
ManifoldError_MANIFOLD_VERTEX_INDEX_OUT_OF_BOUNDS,
};

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
#[non_exhaustive]
pub enum Error {
NoError,
NonFiniteVertex,
NotManifold,
VertexIndexOutOfBounds,
PropertiesWrongLength,
MissingPositionProperties,
MergeVectorsDifferentLengths,
MergeIndexOutOfBounds,
TransformWrongLength,
RunIndexWrongLength,
FaceIdWrongLength,
InvalidConstruction,
Unknown(u32),
}

impl From<u32> for Error {
fn from(value: u32) -> Self {
#[allow(non_upper_case_globals)]
match value {
ManifoldError_MANIFOLD_NO_ERROR => Error::NoError,
ManifoldError_MANIFOLD_NON_FINITE_VERTEX => Error::NonFiniteVertex,
ManifoldError_MANIFOLD_NOT_MANIFOLD => Error::NotManifold,
ManifoldError_MANIFOLD_VERTEX_INDEX_OUT_OF_BOUNDS => Error::VertexIndexOutOfBounds,
ManifoldError_MANIFOLD_PROPERTIES_WRONG_LENGTH => Error::PropertiesWrongLength,
ManifoldError_MANIFOLD_MISSING_POSITION_PROPERTIES => Error::MissingPositionProperties,
ManifoldError_MANIFOLD_MERGE_VECTORS_DIFFERENT_LENGTHS => {
Error::MergeVectorsDifferentLengths
}
ManifoldError_MANIFOLD_MERGE_INDEX_OUT_OF_BOUNDS => Error::MergeIndexOutOfBounds,
ManifoldError_MANIFOLD_TRANSFORM_WRONG_LENGTH => Error::TransformWrongLength,
ManifoldError_MANIFOLD_RUN_INDEX_WRONG_LENGTH => Error::RunIndexWrongLength,
ManifoldError_MANIFOLD_FACE_ID_WRONG_LENGTH => Error::FaceIdWrongLength,
ManifoldError_MANIFOLD_INVALID_CONSTRUCTION => Error::InvalidConstruction,
value => Error::Unknown(value),
}
}
}

pub trait ManifoldErrorExt {
fn is_error(&self) -> bool;
}

pub fn check_error(manifold: Manifold) -> Result<Manifold, Error> {
match Error::from(unsafe { manifold_status(manifold.ptr()) }) {
Error::NoError => Ok(manifold),
e => Err(e),
}
}

impl ManifoldErrorExt for ManifoldError {
fn is_error(&self) -> bool {
*self != 0
}
}

mod tests {
use crate::error::Error;
use manifold3d_sys::{
ManifoldError_MANIFOLD_NON_FINITE_VERTEX, ManifoldError_MANIFOLD_NO_ERROR,
};

#[test]
fn test_error_from_u32() {
// Checks whether the error discrimination works at all
assert_eq!(Error::from(ManifoldError_MANIFOLD_NO_ERROR), Error::NoError);
assert_eq!(
Error::from(ManifoldError_MANIFOLD_NON_FINITE_VERTEX),
Error::NonFiniteVertex
);
}
}
24 changes: 17 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#![allow(dead_code)]

pub mod bounding_box;
pub mod manifold;
pub mod mesh_gl;
pub mod polygons;
pub mod quality;
pub mod simple_polygon;
pub mod types;
mod bounding_box;
mod error;
mod manifold;
mod mesh_gl;
mod polygons;
mod quality;
mod simple_polygon;
mod types;

pub use bounding_box::*;
pub use error::*;
pub use manifold::*;
pub use mesh_gl::*;
pub use polygons::*;
pub use quality::*;
pub use simple_polygon::*;
pub use types::*;
Loading

0 comments on commit 01c73e1

Please sign in to comment.