Skip to content

Commit

Permalink
Merge pull request #1027 from hannobraun/poly
Browse files Browse the repository at this point in the history
Add some validation code to `PolyChain`
  • Loading branch information
hannobraun authored Sep 1, 2022
2 parents 3efb6f8 + 764fe44 commit 58091f6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/fj-math/src/poly_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ impl<const D: usize> PolyChain<D> {
pub fn from_points(
points: impl IntoIterator<Item = impl Into<Point<D>>>,
) -> Self {
let points = points.into_iter().map(Into::into).collect();
let points = points.into_iter().map(Into::into).collect::<Vec<_>>();

// Validate that we don't have any neighboring points that are the same.
// This doesn't ensure that the `PolyChain` is fully valid, but it's
// better than nothing.
for points in points.windows(2) {
// Can't panic, as we passed `2` to `windows`.
//
// Can be cleaned up, once `array_windows` is stable"
// https://doc.rust-lang.org/std/primitive.slice.html#method.array_windows
let [a, b] = [&points[0], &points[1]];

assert_ne!(a, b, "Polygonal chain has duplicate point");
}

Self { points }
}

Expand Down

0 comments on commit 58091f6

Please sign in to comment.