Skip to content

Commit

Permalink
Merge pull request #746 from hannobraun/validation
Browse files Browse the repository at this point in the history
Fix vertex uniqueness validation
  • Loading branch information
hannobraun authored Jun 29, 2022
2 parents c5395ab + 5d06c69 commit d9983a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
28 changes: 21 additions & 7 deletions crates/fj-kernel/src/validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(())
Expand Down
5 changes: 4 additions & 1 deletion crates/fj-kernel/src/validation/uniqueness.rs
Original file line number Diff line number Diff line change
@@ -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<Vertex>,
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),
});
Expand Down

0 comments on commit d9983a9

Please sign in to comment.