Skip to content

Commit

Permalink
use thiserror more
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Sep 9, 2024
1 parent 30e5d06 commit 4b4a467
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 64 deletions.
7 changes: 2 additions & 5 deletions src/query/split/split_trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/shape/shared_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ impl SharedShape {
vertices: Vec<Point<Real>>,
indices: Vec<[u32; 3]>,
flags: TriMeshFlags,
) -> Self {
SharedShape(Arc::new(
TriMesh::with_flags(vertices, indices, flags).unwrap(),
))
) -> Result<Self, TriMeshBuilderError> {
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
Expand Down
38 changes: 6 additions & 32 deletions src/shape/trimesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
36 changes: 13 additions & 23 deletions src/transformation/mesh_intersection/mesh_intersection_error.rs
Original file line number Diff line number Diff line change
@@ -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<TriMeshBuilderError> for MeshIntersectionError {
fn from(value: TriMeshBuilderError) -> Self {
MeshIntersectionError::TriMeshBuilderError(value)
Expand Down

0 comments on commit 4b4a467

Please sign in to comment.