diff --git a/fj-operations/src/difference_2d.rs b/fj-operations/src/difference_2d.rs index 89178c598..bf0cd58fe 100644 --- a/fj-operations/src/difference_2d.rs +++ b/fj-operations/src/difference_2d.rs @@ -16,8 +16,12 @@ impl ToShape for fj::Difference2d { let mut shape = Shape::new(); - let [mut a, mut b] = [&self.a(), &self.b()] - .map(|shape| shape.to_shape(tolerance, debug_info)); + // Can be cleaned up, once `each_ref` is stable: + // https://doc.rust-lang.org/std/primitive.array.html#method.each_ref + let [a, b] = self.shapes(); + let shapes = [&a, &b]; + let [mut a, mut b] = + shapes.map(|shape| shape.to_shape(tolerance, debug_info)); // Check preconditions. // @@ -76,7 +80,7 @@ impl ToShape for fj::Difference2d { // This is a conservative estimate of the bounding box: It's never going // to be bigger than the bounding box of the original shape that another // is being subtracted from. - self.a().bounding_volume() + self.shapes()[0].bounding_volume() } } diff --git a/fj-operations/src/sweep.rs b/fj-operations/src/sweep.rs index 15f816fc8..0a2dab176 100644 --- a/fj-operations/src/sweep.rs +++ b/fj-operations/src/sweep.rs @@ -10,7 +10,7 @@ impl ToShape for fj::Sweep { self.shape().to_shape(tolerance, debug_info), Vector::from([0., 0., self.length()]), tolerance, - self.color(), + self.shape().color(), ) } diff --git a/fj/src/shape_2d.rs b/fj/src/shape_2d.rs index 50aae3b8f..f5116d824 100644 --- a/fj/src/shape_2d.rs +++ b/fj/src/shape_2d.rs @@ -84,32 +84,23 @@ impl From for Shape2d { #[derive(Clone, Debug)] #[repr(C)] pub struct Difference2d { - /// The original shape - a: Shape2d, - - /// The shape being subtracted - b: Shape2d, + shapes: [Shape2d; 2], } impl Difference2d { /// Create a `Difference2d` from two shapes - pub fn from_objects(a: Shape2d, b: Shape2d) -> Self { - Self { a, b } + pub fn from_shapes(shapes: [Shape2d; 2]) -> Self { + Self { shapes } } /// Get the rendering color of the larger object in RGBA pub fn color(&self) -> [u8; 4] { - self.a.color() - } - - /// Access the original shape - pub fn a(&self) -> &Shape2d { - &self.a + self.shapes[0].color() } - /// Access the shape being subtracted - pub fn b(&self) -> &Shape2d { - &self.b + /// Access the shapes that make up the difference + pub fn shapes(&self) -> &[Shape2d; 2] { + &self.shapes } } diff --git a/fj/src/shape_3d.rs b/fj/src/shape_3d.rs index be6ae2ba1..2962afc27 100644 --- a/fj/src/shape_3d.rs +++ b/fj/src/shape_3d.rs @@ -113,11 +113,6 @@ impl Sweep { pub fn length(&self) -> f64 { self.length } - - /// Access the color of the shape being swept - pub fn color(&self) -> [u8; 4] { - self.shape().color() - } } impl From for Shape { diff --git a/fj/src/syntax.rs b/fj/src/syntax.rs index 0422cb0ce..6dee78e22 100644 --- a/fj/src/syntax.rs +++ b/fj/src/syntax.rs @@ -24,7 +24,7 @@ where let a = self.clone().into(); let b = other.clone().into(); - crate::Difference2d::from_objects(a, b) + crate::Difference2d::from_shapes([a, b]) } } @@ -53,32 +53,6 @@ where } } -/// Convenient syntax to create an [`fj::Transform`] -/// -/// [`fj::Transform`]: crate::Transform -pub trait Rotate { - /// Create a rotation - /// - /// Create a rotation that rotates `shape` by `angle` around an axis defined - /// by `axis`. - fn rotate(&self, axis: [f64; 3], angle: f64) -> crate::Transform; -} - -impl Rotate for T -where - T: Clone + Into, -{ - fn rotate(&self, axis: [f64; 3], angle: f64) -> crate::Transform { - let shape = self.clone().into(); - crate::Transform { - shape, - axis, - angle, - offset: [0.; 3], - } - } -} - /// Convenient syntax to create an [`fj::Sketch`] /// /// [`fj::Sketch`]: crate::Sketch @@ -120,17 +94,33 @@ where /// Convenient syntax to create an [`fj::Transform`] /// /// [`fj::Transform`]: crate::Transform -pub trait Translate { +pub trait Transform { + /// Create a rotation + /// + /// Create a rotation that rotates `shape` by `angle` around an axis defined + /// by `axis`. + fn rotate(&self, axis: [f64; 3], angle: f64) -> crate::Transform; + /// Create a translation /// /// Create a translation that translates `shape` by `offset`. fn translate(&self, offset: [f64; 3]) -> crate::Transform; } -impl Translate for T +impl Transform for T where T: Clone + Into, { + fn rotate(&self, axis: [f64; 3], angle: f64) -> crate::Transform { + let shape = self.clone().into(); + crate::Transform { + shape, + axis, + angle, + offset: [0.; 3], + } + } + fn translate(&self, offset: [f64; 3]) -> crate::Transform { let shape = self.clone().into(); crate::Transform { diff --git a/models/star/src/lib.rs b/models/star/src/lib.rs index 8367c1544..70f200e31 100644 --- a/models/star/src/lib.rs +++ b/models/star/src/lib.rs @@ -53,7 +53,7 @@ pub extern "C" fn model(args: &HashMap) -> fj::Shape { let outer = fj::Sketch::from_points(outer).with_color([0, 255, 0, 200]); let inner = fj::Sketch::from_points(inner); - let footprint = fj::Difference2d::from_objects(outer.into(), inner.into()); + let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]); let star = fj::Sweep::from_shape_and_length(footprint.into(), h);