Skip to content

Commit

Permalink
Empty segments & points (#1585)
Browse files Browse the repository at this point in the history
* Made from_points and from_segments return Option

* Updated models

* Fixed doctests
  • Loading branch information
tmayoff authored Feb 16, 2023
1 parent 04ea2d0 commit dc43d54
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/fj/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::Shape;
/// Convenient syntax for this operation is available through [`crate::syntax`].
///
/// ``` rust
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]);
/// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]);
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]).unwrap();
/// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]).unwrap();
/// use fj::syntax::*;
///
/// // `a` and `b` can be anything that converts to `fj::Shape`
Expand Down
30 changes: 20 additions & 10 deletions crates/fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl Shape2d {
/// Convenient syntax for this operation is available through [`crate::syntax`].
///
/// ``` rust
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]);
/// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]);
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]).unwrap();
/// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]).unwrap();
/// use fj::syntax::*;
///
/// // `a` and `b` can be anything that converts to `fj::Shape2d`
Expand Down Expand Up @@ -102,18 +102,28 @@ pub struct Sketch {

impl Sketch {
/// Create a sketch made of sketch segments
pub fn from_segments(segments: Vec<SketchSegment>) -> Self {
Self {
chain: Chain::PolyChain(PolyChain::from_segments(segments)),
color: [255, 0, 0, 255],
pub fn from_segments(segments: Vec<SketchSegment>) -> Option<Self> {
// TODO Returning an option is just a temporary solution, see: https://github.com/hannobraun/Fornjot/issues/1507
if segments.is_empty() {
None
} else {
Some(Self {
chain: Chain::PolyChain(PolyChain::from_segments(segments)),
color: [255, 0, 0, 255],
})
}
}

/// Create a sketch made of straight lines from a bunch of points
pub fn from_points(points: Vec<[f64; 2]>) -> Self {
Self {
chain: Chain::PolyChain(PolyChain::from_points(points)),
color: [255, 0, 0, 255],
pub fn from_points(points: Vec<[f64; 2]>) -> Option<Self> {
if points.is_empty() {
// TODO Returning an option is just a temporary solution, see: https://github.com/hannobraun/Fornjot/issues/1507
None
} else {
Some(Self {
chain: Chain::PolyChain(PolyChain::from_points(points)),
color: [255, 0, 0, 255],
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Shape, Shape2d};
/// Convenient syntax for this operation is available through [`crate::syntax`].
///
/// ``` rust
/// # let shape = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]);
/// # let shape = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]).unwrap();
/// use fj::syntax::*;
///
/// // `shape` can be anything that converts to `fj::Shape2d`
Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
T: AsRef<[[f64; 2]]>,
{
fn sketch(&self) -> crate::Sketch {
crate::Sketch::from_points(self.as_ref().to_vec())
crate::Sketch::from_points(self.as_ref().to_vec()).unwrap()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fj/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{Angle, Shape};
/// Convenient syntax for this operation is available through [`crate::syntax`].
///
/// ``` rust
/// # let shape = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]);
/// # let shape = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 1.]]).unwrap();
/// use fj::syntax::*;
///
/// // `shape` can be anything that converts to `fj::Shape`
Expand Down
2 changes: 1 addition & 1 deletion models/cuboid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn model(
[ x / 2., -y / 2.],
[ x / 2., y / 2.],
[-x / 2., y / 2.],
]).with_color([100,255,0,200]);
]).unwrap().with_color([100,255,0,200]);

let cuboid = fj::Sweep::from_path(rectangle.into(), [0., 0., z]);

Expand Down
4 changes: 2 additions & 2 deletions models/star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub fn model(
inner.push([x / 2., y / 2.]);
}

let outer = fj::Sketch::from_points(outer);
let inner = fj::Sketch::from_points(inner);
let outer = fj::Sketch::from_points(outer).unwrap();
let inner = fj::Sketch::from_points(inner).unwrap();

let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]);

Expand Down
4 changes: 2 additions & 2 deletions models/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ fn star(
}
}

let outer = fj::Sketch::from_segments(outer).with_color(color);
let inner = fj::Sketch::from_segments(inner);
let outer = fj::Sketch::from_segments(outer).unwrap().with_color(color);
let inner = fj::Sketch::from_segments(inner).unwrap();

let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]);

Expand Down

0 comments on commit dc43d54

Please sign in to comment.