From 764fe447e7b3d4d09fd875b39521ff60207d2c1f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 1 Sep 2022 17:29:15 +0200 Subject: [PATCH] Add some validation code to `PolyChain` --- crates/fj-math/src/poly_chain.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/fj-math/src/poly_chain.rs b/crates/fj-math/src/poly_chain.rs index 0c762226d..c1bf6bbdc 100644 --- a/crates/fj-math/src/poly_chain.rs +++ b/crates/fj-math/src/poly_chain.rs @@ -20,7 +20,21 @@ impl PolyChain { pub fn from_points( points: impl IntoIterator>>, ) -> Self { - let points = points.into_iter().map(Into::into).collect(); + let points = points.into_iter().map(Into::into).collect::>(); + + // 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 } }