Empty segments & points (#1585)

* Made from_points and from_segments return Option

* Updated models

* Fixed doctests
This commit is contained in:
Tyler 2023-02-16 06:30:52 -05:00 committed by GitHub
parent 04ea2d009c
commit dc43d54dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 20 deletions

View File

@ -10,8 +10,8 @@ use crate::Shape;
/// Convenient syntax for this operation is available through [`crate::syntax`]. /// Convenient syntax for this operation is available through [`crate::syntax`].
/// ///
/// ``` rust /// ``` rust
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 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.]]); /// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]).unwrap();
/// use fj::syntax::*; /// use fj::syntax::*;
/// ///
/// // `a` and `b` can be anything that converts to `fj::Shape` /// // `a` and `b` can be anything that converts to `fj::Shape`

View File

@ -29,8 +29,8 @@ impl Shape2d {
/// Convenient syntax for this operation is available through [`crate::syntax`]. /// Convenient syntax for this operation is available through [`crate::syntax`].
/// ///
/// ``` rust /// ``` rust
/// # let a = fj::Sketch::from_points(vec![[0., 0.], [1., 0.], [0., 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.]]); /// # let b = fj::Sketch::from_points(vec![[2., 0.], [3., 0.], [2., 1.]]).unwrap();
/// use fj::syntax::*; /// use fj::syntax::*;
/// ///
/// // `a` and `b` can be anything that converts to `fj::Shape2d` /// // `a` and `b` can be anything that converts to `fj::Shape2d`
@ -102,18 +102,28 @@ pub struct Sketch {
impl Sketch { impl Sketch {
/// Create a sketch made of sketch segments /// Create a sketch made of sketch segments
pub fn from_segments(segments: Vec<SketchSegment>) -> Self { pub fn from_segments(segments: Vec<SketchSegment>) -> Option<Self> {
Self { // TODO Returning an option is just a temporary solution, see: https://github.com/hannobraun/Fornjot/issues/1507
chain: Chain::PolyChain(PolyChain::from_segments(segments)), if segments.is_empty() {
color: [255, 0, 0, 255], 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 /// Create a sketch made of straight lines from a bunch of points
pub fn from_points(points: Vec<[f64; 2]>) -> Self { pub fn from_points(points: Vec<[f64; 2]>) -> Option<Self> {
Self { if points.is_empty() {
chain: Chain::PolyChain(PolyChain::from_points(points)), // TODO Returning an option is just a temporary solution, see: https://github.com/hannobraun/Fornjot/issues/1507
color: [255, 0, 0, 255], None
} else {
Some(Self {
chain: Chain::PolyChain(PolyChain::from_points(points)),
color: [255, 0, 0, 255],
})
} }
} }

View File

@ -7,7 +7,7 @@ use crate::{Shape, Shape2d};
/// Convenient syntax for this operation is available through [`crate::syntax`]. /// Convenient syntax for this operation is available through [`crate::syntax`].
/// ///
/// ``` rust /// ``` 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::*; /// use fj::syntax::*;
/// ///
/// // `shape` can be anything that converts to `fj::Shape2d` /// // `shape` can be anything that converts to `fj::Shape2d`

View File

@ -69,7 +69,7 @@ where
T: AsRef<[[f64; 2]]>, T: AsRef<[[f64; 2]]>,
{ {
fn sketch(&self) -> crate::Sketch { fn sketch(&self) -> crate::Sketch {
crate::Sketch::from_points(self.as_ref().to_vec()) crate::Sketch::from_points(self.as_ref().to_vec()).unwrap()
} }
} }

View File

@ -7,7 +7,7 @@ use crate::{Angle, Shape};
/// Convenient syntax for this operation is available through [`crate::syntax`]. /// Convenient syntax for this operation is available through [`crate::syntax`].
/// ///
/// ``` rust /// ``` 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::*; /// use fj::syntax::*;
/// ///
/// // `shape` can be anything that converts to `fj::Shape` /// // `shape` can be anything that converts to `fj::Shape`

View File

@ -10,7 +10,7 @@ pub fn model(
[ x / 2., -y / 2.], [ x / 2., -y / 2.],
[ x / 2., y / 2.], [ 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]); let cuboid = fj::Sweep::from_path(rectangle.into(), [0., 0., z]);

View File

@ -29,8 +29,8 @@ pub fn model(
inner.push([x / 2., y / 2.]); inner.push([x / 2., y / 2.]);
} }
let outer = fj::Sketch::from_points(outer); let outer = fj::Sketch::from_points(outer).unwrap();
let inner = fj::Sketch::from_points(inner); let inner = fj::Sketch::from_points(inner).unwrap();
let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]); let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]);

View File

@ -69,8 +69,8 @@ fn star(
} }
} }
let outer = fj::Sketch::from_segments(outer).with_color(color); let outer = fj::Sketch::from_segments(outer).unwrap().with_color(color);
let inner = fj::Sketch::from_segments(inner); let inner = fj::Sketch::from_segments(inner).unwrap();
let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]); let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]);