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

Add some validation code to PolyChain #1027

Merged
merged 1 commit into from
Sep 1, 2022
Merged
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
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