Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Geometry available to ValidationCheck implementations #2272

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/fj-core/src/validate/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ impl Validate for Cycle {
&self,
config: &ValidationConfig,
errors: &mut Vec<ValidationError>,
_: &Geometry,
geometry: &Geometry,
) {
errors.extend(
AdjacentHalfEdgesNotConnected::check(self, config).map(Into::into),
AdjacentHalfEdgesNotConnected::check(self, geometry, config)
.map(Into::into),
);
}
}
12 changes: 10 additions & 2 deletions crates/fj-core/src/validation/checks/half_edge_connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fj_math::{Point, Scalar};

use crate::{
geometry::Geometry,
objects::{Cycle, HalfEdge},
storage::Handle,
validation::{validation_check::ValidationCheck, ValidationConfig},
Expand Down Expand Up @@ -40,6 +41,7 @@ pub struct AdjacentHalfEdgesNotConnected {
impl ValidationCheck<Cycle> for AdjacentHalfEdgesNotConnected {
fn check(
object: &Cycle,
_: &Geometry,
config: &ValidationConfig,
) -> impl Iterator<Item = Self> {
object.half_edges().pairs().filter_map(|(first, second)| {
Expand Down Expand Up @@ -87,7 +89,10 @@ mod tests {
let mut core = Core::new();

let valid = Cycle::polygon([[0., 0.], [1., 0.], [1., 1.]], &mut core);
AdjacentHalfEdgesNotConnected::check_and_return_first_error(&valid)?;
AdjacentHalfEdgesNotConnected::check_and_return_first_error(
&valid,
&core.layers.geometry,
)?;

let invalid = valid.update_half_edge(
valid.half_edges().first(),
Expand All @@ -96,7 +101,10 @@ mod tests {
},
&mut core,
);
AdjacentHalfEdgesNotConnected::check_and_expect_one_error(&invalid);
AdjacentHalfEdgesNotConnected::check_and_expect_one_error(
&invalid,
&core.layers.geometry,
);

Ok(())
}
Expand Down
14 changes: 10 additions & 4 deletions crates/fj-core/src/validation/validation_check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::Display;

use crate::geometry::Geometry;

use super::ValidationConfig;

/// Run a specific validation check on an object
Expand All @@ -10,16 +12,20 @@ pub trait ValidationCheck<T>: Sized {
/// Run the validation check on the implementing object
fn check(
object: &T,
geometry: &Geometry,
config: &ValidationConfig,
) -> impl Iterator<Item = Self>;

/// Convenience method to run the check return the first error
///
/// This method is designed for convenience over flexibility (it is intended
/// for use in unit tests), and thus always uses the default configuration.
fn check_and_return_first_error(object: &T) -> Result<(), Self> {
fn check_and_return_first_error(
object: &T,
geometry: &Geometry,
) -> Result<(), Self> {
let config = ValidationConfig::default();
let mut errors = Self::check(object, &config);
let mut errors = Self::check(object, geometry, &config);

if let Some(err) = errors.next() {
return Err(err);
Expand All @@ -32,12 +38,12 @@ pub trait ValidationCheck<T>: Sized {
///
/// This method is designed for convenience over flexibility (it is intended
/// for use in unit tests), and thus always uses the default configuration.
fn check_and_expect_one_error(object: &T) -> Self
fn check_and_expect_one_error(object: &T, geometry: &Geometry) -> Self
where
Self: Display,
{
let config = ValidationConfig::default();
let mut errors = Self::check(object, &config).peekable();
let mut errors = Self::check(object, geometry, &config).peekable();

let err = errors
.next()
Expand Down