Skip to content

Commit

Permalink
Merge pull request #1368 from hannobraun/arcs
Browse files Browse the repository at this point in the history
Shuffle around some code in `fj::Sketch`
  • Loading branch information
hannobraun authored Nov 18, 2022
2 parents b6759d1 + ba93979 commit 7dc0aac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 7 additions & 3 deletions crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
),
Expand Down
6 changes: 6 additions & 0 deletions crates/fj/src/abi/ffi_safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ impl<T: Copy> From<Vec<T>> for Box<[T]> {
}
}

impl<T> Default for Vec<T> {
fn default() -> Self {
std::vec::Vec::default().into()
}
}

impl<T> FromIterator<T> for Vec<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let vec: std::vec::Vec<T> = iter.into_iter().collect();
Expand Down
27 changes: 22 additions & 5 deletions crates/fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SketchSegment>,
}

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<SketchSegment> {
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],
},
}

0 comments on commit 7dc0aac

Please sign in to comment.