From bec82b5690aa11738b6b921945c0232a2c73f99e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 7 Nov 2022 15:49:29 +0100 Subject: [PATCH 1/5] Prepare `PolyChain` for supporting more than lines --- crates/fj/src/shape_2d.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/crates/fj/src/shape_2d.rs b/crates/fj/src/shape_2d.rs index 96c72cbe2..eb4a9337e 100644 --- a/crates/fj/src/shape_2d.rs +++ b/crates/fj/src/shape_2d.rs @@ -184,18 +184,39 @@ impl Circle { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(C)] pub struct PolyChain { - points: ffi_safe::Vec<[f64; 2]>, + points: ffi_safe::Vec, } impl PolyChain { /// Construct an instance from a list of points pub fn from_points(points: Vec<[f64; 2]>) -> Self { - let points = points.into(); + let points = points + .into_iter() + .map(|point| SketchSegment::LineTo { point }) + .collect(); Self { points } } /// Return the points that define the polygonal chain pub fn to_points(&self) -> Vec<[f64; 2]> { - self.points.clone().into() + let segments: Vec<_> = self.points.clone().into(); + segments + .into_iter() + .map(|SketchSegment::LineTo { point }| point) + .collect() } } + +/// A segment of a sketch +/// +/// Each segment starts at the previous point of the sketch. +#[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[repr(C)] +pub enum SketchSegment { + /// A line to a point + LineTo { + /// The destination point of the line + point: [f64; 2], + }, +} From fc9d071b84f60514cb2b98100cefc0453789a34e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 7 Nov 2022 16:00:18 +0100 Subject: [PATCH 2/5] Update struct field name --- crates/fj/src/shape_2d.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj/src/shape_2d.rs b/crates/fj/src/shape_2d.rs index eb4a9337e..7707614ef 100644 --- a/crates/fj/src/shape_2d.rs +++ b/crates/fj/src/shape_2d.rs @@ -184,7 +184,7 @@ impl Circle { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(C)] pub struct PolyChain { - points: ffi_safe::Vec, + segments: ffi_safe::Vec, } impl PolyChain { @@ -194,12 +194,12 @@ impl PolyChain { .into_iter() .map(|point| SketchSegment::LineTo { point }) .collect(); - Self { points } + Self { segments: points } } /// Return the points that define the polygonal chain pub fn to_points(&self) -> Vec<[f64; 2]> { - let segments: Vec<_> = self.points.clone().into(); + let segments: Vec<_> = self.segments.clone().into(); segments .into_iter() .map(|SketchSegment::LineTo { point }| point) From d09ee952b4a9a5ce516fe3deef20ba3b14e6b3d8 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 9 Nov 2022 15:10:33 +0100 Subject: [PATCH 3/5] Return `SketchSegment` from `PolyChain` --- crates/fj-operations/src/sketch.rs | 8 ++++++-- crates/fj/src/shape_2d.rs | 8 ++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index 377c79f7c..edc725c43 100644 --- a/crates/fj-operations/src/sketch.rs +++ b/crates/fj-operations/src/sketch.rs @@ -44,8 +44,11 @@ impl Shape for fj::Sketch { .insert(objects)? } fj::Chain::PolyChain(poly_chain) => { - let points = - poly_chain.to_points().into_iter().map(Point::from); + let points = poly_chain + .to_points() + .into_iter() + .map(|fj::SketchSegment::LineTo { point }| point) + .map(Point::from); Face::partial() .with_surface(surface) @@ -70,6 +73,7 @@ impl Shape for fj::Sketch { poly_chain .to_points() .into_iter() + .map(|fj::SketchSegment::LineTo { point }| point) .map(Point::from) .map(Point::to_xyz), ), diff --git a/crates/fj/src/shape_2d.rs b/crates/fj/src/shape_2d.rs index 7707614ef..77b1e27a9 100644 --- a/crates/fj/src/shape_2d.rs +++ b/crates/fj/src/shape_2d.rs @@ -198,12 +198,8 @@ impl PolyChain { } /// Return the points that define the polygonal chain - pub fn to_points(&self) -> Vec<[f64; 2]> { - let segments: Vec<_> = self.segments.clone().into(); - segments - .into_iter() - .map(|SketchSegment::LineTo { point }| point) - .collect() + pub fn to_points(&self) -> Vec { + self.segments.clone().into() } } From f2eff7a8ae91e6e99810e6832ed1b238689fd2d8 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 9 Nov 2022 15:11:09 +0100 Subject: [PATCH 4/5] Update method name --- crates/fj-operations/src/sketch.rs | 4 ++-- crates/fj/src/shape_2d.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index edc725c43..33db32a8e 100644 --- a/crates/fj-operations/src/sketch.rs +++ b/crates/fj-operations/src/sketch.rs @@ -45,7 +45,7 @@ impl Shape for fj::Sketch { } fj::Chain::PolyChain(poly_chain) => { let points = poly_chain - .to_points() + .to_segments() .into_iter() .map(|fj::SketchSegment::LineTo { point }| point) .map(Point::from); @@ -71,7 +71,7 @@ impl Shape for fj::Sketch { }, fj::Chain::PolyChain(poly_chain) => Aabb::<3>::from_points( poly_chain - .to_points() + .to_segments() .into_iter() .map(|fj::SketchSegment::LineTo { point }| point) .map(Point::from) diff --git a/crates/fj/src/shape_2d.rs b/crates/fj/src/shape_2d.rs index 77b1e27a9..87a933870 100644 --- a/crates/fj/src/shape_2d.rs +++ b/crates/fj/src/shape_2d.rs @@ -198,7 +198,7 @@ impl PolyChain { } /// Return the points that define the polygonal chain - pub fn to_points(&self) -> Vec { + pub fn to_segments(&self) -> Vec { self.segments.clone().into() } } From ba939790a04190c77d2fa3d1c9628684038ddbdd Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Nov 2022 17:15:21 +0100 Subject: [PATCH 5/5] Implement `Default` for `ffi_safe::Vec` --- crates/fj/src/abi/ffi_safe.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/fj/src/abi/ffi_safe.rs b/crates/fj/src/abi/ffi_safe.rs index 12f51a6fa..b0965f8f0 100644 --- a/crates/fj/src/abi/ffi_safe.rs +++ b/crates/fj/src/abi/ffi_safe.rs @@ -78,6 +78,12 @@ impl From> for Box<[T]> { } } +impl Default for Vec { + fn default() -> Self { + std::vec::Vec::default().into() + } +} + impl FromIterator for Vec { fn from_iter>(iter: I) -> Self { let vec: std::vec::Vec = iter.into_iter().collect();