Skip to content

Commit

Permalink
Implement geometric validation of edge vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed May 24, 2022
1 parent 33f19f6 commit 6c3143a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
66 changes: 66 additions & 0 deletions crates/fj-kernel/src/shape/validate/geometric.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
use std::fmt;

use fj_math::Scalar;

use crate::topology::Edge;

pub fn validate_edge(
edge: &Edge<3>,
max_distance: impl Into<Scalar>,
) -> Result<(), GeometricIssues> {
let max_distance = max_distance.into();

// Validate that the local and canonical forms of the vertices match. As a
// side effect, this also happens to validate that the canonical forms of
// the vertices lie on the curve.
if let Some(vertices) = &edge.vertices {
for vertex in vertices {
let local_point =
edge.curve().point_from_curve_coords(*vertex.local());

let distance =
(local_point - vertex.canonical().get().point()).magnitude();

if distance > max_distance {
return Err(GeometricIssues);
}
}
}

Ok(())
}

/// Geometric issues found during validation
///
/// Used by [`ValidationError`].
Expand All @@ -14,3 +44,39 @@ impl fmt::Display for GeometricIssues {
Ok(())
}
}

#[cfg(test)]
mod tests {
use fj_math::Scalar;

use crate::{
shape::{LocalForm, Shape},
topology::Edge,
};

#[test]
fn validate_edge() -> anyhow::Result<()> {
let mut shape = Shape::new();

let deviation = Scalar::from_f64(0.25);

let edge = Edge::builder(&mut shape)
.build_line_segment_from_points([[0., 0., 0.], [1., 0., 0.]])?
.get();
let edge = Edge {
vertices: edge.vertices.clone().map(|vertices| {
vertices.map(|vertex| {
LocalForm::new(
*vertex.local() + [deviation],
vertex.canonical(),
)
})
}),
..edge
};
assert!(super::validate_edge(&edge, deviation * 2.).is_ok());
assert!(super::validate_edge(&edge, deviation / 2.).is_err());

Ok(())
}
}
4 changes: 3 additions & 1 deletion crates/fj-kernel/src/shape/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ impl Validate for Edge<3> {
&self,
_: Option<&Handle<Self>>,
_: Scalar,
_: Scalar,
max_distance: Scalar,
stores: &Stores,
) -> Result<(), ValidationError> {
geometric::validate_edge(self, max_distance)?;
structural::validate_edge(self, stores)?;

Ok(())
}
}
Expand Down

0 comments on commit 6c3143a

Please sign in to comment.