diff --git a/src/query/split/split_trimesh.rs b/src/query/split/split_trimesh.rs index 88d8a1d7..fa0e96df 100644 --- a/src/query/split/split_trimesh.rs +++ b/src/query/split/split_trimesh.rs @@ -2,11 +2,8 @@ use crate::bounding_volume::Aabb; use crate::math::{Isometry, Point, Real, UnitVector, Vector}; use crate::query::visitors::BoundingVolumeIntersectionsVisitor; use crate::query::{IntersectResult, PointQuery, SplitResult}; -use crate::shape::{ - Cuboid, FeatureId, Polyline, Segment, Shape, TriMesh, TriMeshBuilderError, TriMeshFlags, - Triangle, -}; -use crate::transformation::{self, intersect_meshes, MeshIntersectionError}; +use crate::shape::{Cuboid, FeatureId, Polyline, Segment, Shape, TriMesh, TriMeshFlags, Triangle}; +use crate::transformation::{intersect_meshes, MeshIntersectionError}; use crate::utils::{hashmap::HashMap, SortedPair, WBasis}; use spade::{handles::FixedVertexHandle, ConstrainedDelaunayTriangulation, Triangulation as _}; use std::cmp::Ordering; diff --git a/src/shape/shared_shape.rs b/src/shape/shared_shape.rs index f9f928cc..eb2beeff 100644 --- a/src/shape/shared_shape.rs +++ b/src/shape/shared_shape.rs @@ -209,10 +209,10 @@ impl SharedShape { vertices: Vec>, indices: Vec<[u32; 3]>, flags: TriMeshFlags, - ) -> Self { - SharedShape(Arc::new( - TriMesh::with_flags(vertices, indices, flags).unwrap(), - )) + ) -> Result { + Ok(SharedShape(Arc::new(TriMesh::with_flags( + vertices, indices, flags, + )?))) } /// Initializes a compound shape obtained from the decomposition of the given trimesh (in 3D) or diff --git a/src/shape/trimesh.rs b/src/shape/trimesh.rs index e6d0738c..923f7059 100644 --- a/src/shape/trimesh.rs +++ b/src/shape/trimesh.rs @@ -22,11 +22,13 @@ use crate::query::details::NormalConstraints; use rkyv::{bytecheck, CheckBytes}; /// Indicated an inconsistency in the topology of a triangle mesh. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(thiserror::Error, Copy, Clone, Debug, PartialEq, Eq)] pub enum TopologyError { /// Found a triangle with two or three identical vertices. + #[error("the triangle {0} has at least two identical vertices.")] BadTriangle(u32), /// At least two adjacent triangles have opposite orientations. + #[error("the triangles {triangle1} and {triangle2} sharing the edge {:?} have opposite orientations.", edge)] BadAdjacentTrianglesOrientation { /// The first triangle, with an orientation opposite to the second triangle. triangle1: u32, @@ -37,45 +39,17 @@ pub enum TopologyError { }, } -impl fmt::Display for TopologyError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::BadTriangle(fid) => { - f.pad(&format!("the triangle {fid} has at least two identical vertices.")) - } - Self::BadAdjacentTrianglesOrientation { - triangle1, - triangle2, - edge, - } => f.pad(&format!("the triangles {triangle1} and {triangle2} sharing the edge {:?} have opposite orientations.", edge)), - } - } -} - -impl std::error::Error for TopologyError {} - /// Indicated an inconsistency while building a triangle mesh. -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(thiserror::Error, Copy, Clone, Debug, PartialEq, Eq)] pub enum TriMeshBuilderError { /// A triangle mesh must contain at least one triangle. + #[error("A triangle mesh must contain at least one triangle.")] EmptyIndices, /// Indicated an inconsistency in the topology of a triangle mesh. + #[error("Topology Error: {0}")] TopologyError(TopologyError), } -impl fmt::Display for TriMeshBuilderError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::EmptyIndices => f.pad(&format!( - "A triangle mesh must contain at least one triangle." - )), - Self::TopologyError(topology_error) => topology_error.fmt(f), - } - } -} - -impl std::error::Error for TriMeshBuilderError {} - /// The set of pseudo-normals of a triangle mesh. /// /// These pseudo-normals are used for the inside-outside test of a diff --git a/src/transformation/mesh_intersection/mesh_intersection_error.rs b/src/transformation/mesh_intersection/mesh_intersection_error.rs index d067fcc2..13904c3b 100644 --- a/src/transformation/mesh_intersection/mesh_intersection_error.rs +++ b/src/transformation/mesh_intersection/mesh_intersection_error.rs @@ -1,38 +1,28 @@ -use core::fmt; - use crate::shape::TriMeshBuilderError; /// Error indicating that a query is not supported between certain shapes -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(thiserror::Error, Debug, Copy, Clone, Eq, PartialEq)] pub enum MeshIntersectionError { + /// At least one of the meshes is missing its topology information. Call `mesh.compute_topology` on the mesh + #[error("at least one of the meshes is missing its topology information. Call `mesh.compute_topology` on the mesh")] MissingTopology, + /// At least one of the meshes is missing its pseudo-normals. Call `mesh.compute_pseudo_normals` on the mesh + #[error("at least one of the meshes is missing its pseudo-normals. Call `mesh.compute_pseudo_normals` on the mesh")] MissingPseudoNormals, + /// Internal failure while intersecting two triangles + #[error("internal failure while intersecting two triangles")] TriTriError, + /// Internal failure while merging faces resulting from intersections + #[error("internal failure while merging faces resulting from intersections")] DuplicateVertices, + /// Internal failure while triangulating an intersection face + #[error("internal failure while triangulating an intersection face")] TriangulationError, + /// See [`TriMeshBuilderError`] + #[error("TriMeshBuilderError: {0}")] TriMeshBuilderError(TriMeshBuilderError), } -impl fmt::Display for MeshIntersectionError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::MissingTopology => { - f.pad("at least one of the meshes is missing its topology information. Call `mesh.compute_topology` on the mesh") - } - Self::MissingPseudoNormals => { - f.pad("at least one of the meshes is missing its pseudo-normals. Call `mesh.compute_pseudo_normals` on the mesh") - } - Self::TriTriError => f.pad("internal failure while intersecting two triangles"), - Self::DuplicateVertices => f.pad("internal failure while merging faces resulting from intersections"), - Self::TriangulationError => f.pad("internal failure while triangulating an intersection face"), - Self::TriMeshBuilderError(error) => error.fmt(f), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for MeshIntersectionError {} - impl From for MeshIntersectionError { fn from(value: TriMeshBuilderError) -> Self { MeshIntersectionError::TriMeshBuilderError(value)