From 4eb05f7b4267b0b8ce8508ef1f8d1ae44ef56360 Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Sun, 6 Oct 2024 11:03:22 +0300 Subject: [PATCH 1/2] Create constructor for ConvexPolyline that does not remove any vertices Fix format --- src/shape/convex_polygon.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/shape/convex_polygon.rs b/src/shape/convex_polygon.rs index 8a5825ac..06ba7157 100644 --- a/src/shape/convex_polygon.rs +++ b/src/shape/convex_polygon.rs @@ -30,6 +30,7 @@ impl ConvexPolygon { /// describe a counter-clockwise convex polyline. /// /// Convexity of the input polyline is not checked. + /// Removes some points if they are collinear with the previous one. /// Returns `None` if all points form an almost flat line. pub fn from_convex_polyline(mut points: Vec>) -> Option { if points.is_empty() { @@ -73,6 +74,29 @@ impl ConvexPolygon { } } + /// Creates a new 2D convex polygon from a set of points assumed to + /// describe a counter-clockwise convex polyline. + /// + /// Convexity of the input polyline is not checked. + /// Does not remove any points. + pub fn from_points(points: Vec>) -> Option { + if points.is_empty() { + return None; + } + let mut normals = Vec::with_capacity(points.len()); + // First, compute all normals. + for i1 in 0..points.len() { + let i2 = (i1 + 1) % points.len(); + normals.push(utils::ccw_face_normal([&points[i1], &points[i2]])?); + } + + if points.len() > 2 { + Some(ConvexPolygon { points, normals }) + } else { + None + } + } + /// The vertices of this convex polygon. #[inline] pub fn points(&self) -> &[Point] { From 7ae9145618a672492c89d0c3c4ef6c6b798f44db Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Mon, 14 Oct 2024 17:34:46 +0200 Subject: [PATCH 2/2] Update shared_shape.rs --- src/shape/shared_shape.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/shape/shared_shape.rs b/src/shape/shared_shape.rs index eb2beeff..78f88589 100644 --- a/src/shape/shared_shape.rs +++ b/src/shape/shared_shape.rs @@ -307,6 +307,15 @@ impl SharedShape { ConvexPolygon::from_convex_polyline(points).map(|ch| SharedShape(Arc::new(ch))) } + /// Creates a new shared shape that is a convex polygon formed by the + /// given set of points assumed to form a convex polyline (no convex-hull will be automatically + /// computed and no points will be removed). + /// Does not remove any points. + #[cfg(feature = "dim2")] + pub fn convex_polyline_from_points_raw(points: Vec>) -> Option { + ConvexPolygon::from_points(points).map(|ch| SharedShape(Arc::new(ch))) + } + /// Creates a new shared shape that is a convex polyhedron formed by the /// given set of points assumed to form a convex mesh (no convex-hull will be automatically /// computed).