From 1773519bd944eb65cd486402c9537f86dfec39be Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 23 Feb 2024 11:14:39 +0100 Subject: [PATCH] Replace old-style cycle validation check --- crates/fj-core/src/validate/cycle.rs | 109 +------------------------ crates/fj-core/src/validate/mod.rs | 6 +- crates/fj-core/src/validation/error.rs | 12 +-- 3 files changed, 13 insertions(+), 114 deletions(-) diff --git a/crates/fj-core/src/validate/cycle.rs b/crates/fj-core/src/validate/cycle.rs index 035d46781..a1d628605 100644 --- a/crates/fj-core/src/validate/cycle.rs +++ b/crates/fj-core/src/validate/cycle.rs @@ -1,9 +1,6 @@ -use fj_math::{Point, Scalar}; - use crate::{ - objects::{Cycle, HalfEdge}, - storage::Handle, - validation::{ValidationConfig, ValidationError}, + objects::Cycle, + validation::{ValidationCheck, ValidationConfig, ValidationError}, }; use super::Validate; @@ -14,106 +11,6 @@ impl Validate for Cycle { config: &ValidationConfig, errors: &mut Vec, ) { - CycleValidationError::check_half_edge_connections(self, config, errors); - } -} - -/// [`Cycle`] validation failed -#[derive(Clone, Debug, thiserror::Error)] -pub enum CycleValidationError { - /// [`Cycle`]'s edges are not connected - #[error( - "Adjacent `HalfEdge`s are not connected\n\ - - End position of first `HalfEdge`: {end_of_first:?}\n\ - - Start position of second `HalfEdge`: {start_of_second:?}\n\ - - Distance between vertices: {distance}\n\ - - `HalfEdge`s: {half_edges:#?}" - )] - HalfEdgesNotConnected { - /// The end position of the first [`HalfEdge`] - end_of_first: Point<2>, - - /// The start position of the second [`HalfEdge`] - start_of_second: Point<2>, - - /// The distance between the two vertices - distance: Scalar, - - /// The edges - half_edges: [Handle; 2], - }, -} - -impl CycleValidationError { - fn check_half_edge_connections( - cycle: &Cycle, - config: &ValidationConfig, - errors: &mut Vec, - ) { - for (first, second) in cycle.half_edges().pairs() { - let end_of_first = { - let [_, end] = first.boundary().inner; - first.path().point_from_path_coords(end) - }; - let start_of_second = second.start_position(); - - let distance = (end_of_first - start_of_second).magnitude(); - - if distance > config.identical_max_distance { - errors.push( - Self::HalfEdgesNotConnected { - end_of_first, - start_of_second, - distance, - half_edges: [first.clone(), second.clone()], - } - .into(), - ); - } - } - } -} - -#[cfg(test)] -mod tests { - - use crate::{ - assert_contains_err, - objects::{Cycle, HalfEdge}, - operations::{ - build::{BuildCycle, BuildHalfEdge}, - update::UpdateCycle, - }, - validate::{cycle::CycleValidationError, Validate}, - validation::ValidationError, - Core, - }; - - #[test] - fn edges_connected() -> anyhow::Result<()> { - let mut core = Core::new(); - - let valid = - Cycle::polygon([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]], &mut core); - - valid.validate_and_return_first_error()?; - - let disconnected = { - let edges = [ - HalfEdge::line_segment([[0., 0.], [1., 0.]], None, &mut core), - HalfEdge::line_segment([[0., 0.], [1., 0.]], None, &mut core), - ]; - - Cycle::empty().add_half_edges(edges, &mut core) - }; - - assert_contains_err!( - disconnected, - ValidationError::Cycle( - CycleValidationError::HalfEdgesNotConnected { .. } - ) - ); - - Ok(()) + errors.extend(self.check(config).map(Into::into)); } } diff --git a/crates/fj-core/src/validate/mod.rs b/crates/fj-core/src/validate/mod.rs index 295ca5224..2c3b3d56c 100644 --- a/crates/fj-core/src/validate/mod.rs +++ b/crates/fj-core/src/validate/mod.rs @@ -76,9 +76,9 @@ mod vertex; use crate::validation::{ValidationConfig, ValidationError}; pub use self::{ - cycle::CycleValidationError, edge::EdgeValidationError, - face::FaceValidationError, shell::ShellValidationError, - sketch::SketchValidationError, solid::SolidValidationError, + edge::EdgeValidationError, face::FaceValidationError, + shell::ShellValidationError, sketch::SketchValidationError, + solid::SolidValidationError, }; /// Assert that some object has a validation error which matches a specific diff --git a/crates/fj-core/src/validation/error.rs b/crates/fj-core/src/validation/error.rs index 01d28103a..353ac6409 100644 --- a/crates/fj-core/src/validation/error.rs +++ b/crates/fj-core/src/validation/error.rs @@ -1,16 +1,18 @@ use std::{convert::Infallible, fmt}; use crate::validate::{ - CycleValidationError, EdgeValidationError, FaceValidationError, - ShellValidationError, SketchValidationError, SolidValidationError, + EdgeValidationError, FaceValidationError, ShellValidationError, + SketchValidationError, SolidValidationError, }; +use super::checks::AdjacentHalfEdgesNotConnected; + /// An error that can occur during a validation #[derive(Clone, Debug, thiserror::Error)] pub enum ValidationError { - /// `Cycle` validation error - #[error("`Cycle` validation error")] - Cycle(#[from] CycleValidationError), + /// `HalfEdge`s in `Cycle` not connected + #[error(transparent)] + HalfEdgesInCycleNotConnected(#[from] AdjacentHalfEdgesNotConnected), /// `Edge` validation error #[error("`Edge` validation error")]