Skip to content

Commit

Permalink
Merge pull request #412 from hannobraun/fj
Browse files Browse the repository at this point in the history
Make some cleanups in `fj` API
  • Loading branch information
hannobraun authored Mar 29, 2022
2 parents 81e28bd + 13b201b commit 62d79d7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 55 deletions.
10 changes: 7 additions & 3 deletions fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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()
}
}

Expand Down
2 changes: 1 addition & 1 deletion fj-operations/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}

Expand Down
23 changes: 7 additions & 16 deletions fj/src/shape_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,23 @@ impl From<Circle> 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
}
}

Expand Down
5 changes: 0 additions & 5 deletions fj/src/shape_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sweep> for Shape {
Expand Down
48 changes: 19 additions & 29 deletions fj/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down Expand Up @@ -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<T> Rotate for T
where
T: Clone + Into<crate::Shape3d>,
{
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
Expand Down Expand Up @@ -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<T> Translate for T
impl<T> Transform for T
where
T: Clone + Into<crate::Shape3d>,
{
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 {
Expand Down
2 changes: 1 addition & 1 deletion models/star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub extern "C" fn model(args: &HashMap<String, String>) -> 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);

Expand Down

0 comments on commit 62d79d7

Please sign in to comment.