From 5d06c69b164fefd2bb1c7e60aab9e4aa4446e5b9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 29 Jun 2022 15:54:19 +0200 Subject: [PATCH] Fix vertex uniqueness validation Not sure when that broke, but broken it was. --- crates/fj-kernel/src/validation/mod.rs | 28 ++++++++++++++----- crates/fj-kernel/src/validation/uniqueness.rs | 5 +++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/crates/fj-kernel/src/validation/mod.rs b/crates/fj-kernel/src/validation/mod.rs index 6be3e84a0..eec968753 100644 --- a/crates/fj-kernel/src/validation/mod.rs +++ b/crates/fj-kernel/src/validation/mod.rs @@ -50,7 +50,11 @@ where let mut vertices = HashSet::new(); for vertex in object.vertex_iter() { - uniqueness::validate_vertex(&vertex, &vertices)?; + uniqueness::validate_vertex( + &vertex, + &vertices, + config.distinct_min_distance, + )?; vertices.insert(vertex); } @@ -186,15 +190,25 @@ mod tests { fn uniqueness_vertex() -> anyhow::Result<()> { let mut shape = Shape::new(); - let point = Point::from([0., 0., 0.]); + let deviation = Scalar::from_f64(0.25); + + let a = Point::from([0., 0., 0.]); + + let mut b = a; + b.x += deviation; + + let config = ValidationConfig { + distinct_min_distance: deviation * 2., + ..ValidationConfig::default() + }; // Adding a vertex should work. - shape.insert(Vertex { point }); - validate(shape.clone(), &ValidationConfig::default())?; + shape.insert(Vertex { point: a }); + validate(shape.clone(), &config)?; - // Adding a second vertex with the same point should fail. - shape.insert(Vertex { point }); - let result = validate(shape, &ValidationConfig::default()); + // Adding a second vertex that is considered identical should fail. + shape.insert(Vertex { point: b }); + let result = validate(shape, &config); assert!(matches!(result, Err(ValidationError::Uniqueness(_)))); Ok(()) diff --git a/crates/fj-kernel/src/validation/uniqueness.rs b/crates/fj-kernel/src/validation/uniqueness.rs index a253a55c2..311e21e1b 100644 --- a/crates/fj-kernel/src/validation/uniqueness.rs +++ b/crates/fj-kernel/src/validation/uniqueness.rs @@ -1,13 +1,16 @@ use std::{collections::HashSet, fmt}; +use fj_math::Scalar; + use crate::objects::Vertex; pub fn validate_vertex( vertex: &Vertex, vertices: &HashSet, + min_distance: Scalar, ) -> Result<(), UniquenessIssues> { for existing in vertices { - if existing == vertex { + if (existing.point - vertex.point).magnitude() < min_distance { return Err(UniquenessIssues { duplicate_vertex: Some(*existing), });