Skip to content

Commit

Permalink
Replace old-style cycle validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Feb 23, 2024
1 parent 7e58d27 commit 1773519
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 114 deletions.
109 changes: 3 additions & 106 deletions crates/fj-core/src/validate/cycle.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,106 +11,6 @@ impl Validate for Cycle {
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
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<HalfEdge>; 2],
},
}

impl CycleValidationError {
fn check_half_edge_connections(
cycle: &Cycle,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
) {
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));
}
}
6 changes: 3 additions & 3 deletions crates/fj-core/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions crates/fj-core/src/validation/error.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down

0 comments on commit 1773519

Please sign in to comment.