diff --git a/crates/fj-operations/src/sketch.rs b/crates/fj-operations/src/sketch.rs index 377c79f7c..33db32a8e 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_segments() + .into_iter() + .map(|fj::SketchSegment::LineTo { point }| point) + .map(Point::from); Face::partial() .with_surface(surface) @@ -68,8 +71,9 @@ 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) .map(Point::to_xyz), ), 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(); diff --git a/crates/fj/src/shape_2d.rs b/crates/fj/src/shape_2d.rs index 96c72cbe2..87a933870 100644 --- a/crates/fj/src/shape_2d.rs +++ b/crates/fj/src/shape_2d.rs @@ -184,18 +184,35 @@ impl Circle { #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[repr(C)] pub struct PolyChain { - points: ffi_safe::Vec<[f64; 2]>, + segments: 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(); - Self { points } + let points = points + .into_iter() + .map(|point| SketchSegment::LineTo { point }) + .collect(); + Self { segments: points } } /// Return the points that define the polygonal chain - pub fn to_points(&self) -> Vec<[f64; 2]> { - self.points.clone().into() + pub fn to_segments(&self) -> Vec { + self.segments.clone().into() } } + +/// 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], + }, +}