Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shuffle around some code in fj::Sketch #1368

Merged
merged 5 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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],
},
}