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

Add convenient syntax for fj::Difference2d #372

Merged
merged 3 commits into from
Mar 17, 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
3 changes: 2 additions & 1 deletion fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ mod syntax;

pub mod prelude {
pub use crate::syntax::{
Group as _, Rotate as _, Sketch as _, Sweep as _, Translate as _,
Difference as _, Group as _, Rotate as _, Sketch as _, Sweep as _,
Translate as _,
};
}

Expand Down
63 changes: 42 additions & 21 deletions fj/src/syntax.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
pub trait Difference {
fn difference<Other>(&self, other: &Other) -> crate::Difference2d
where
Other: Clone + Into<crate::Shape2d>;
}

impl<T> Difference for T
where
T: Clone + Into<crate::Shape2d>,
{
fn difference<Other>(&self, other: &Other) -> crate::Difference2d
where
Other: Clone + Into<crate::Shape2d>,
{
let a = self.clone().into();
let b = other.clone().into();

crate::Difference2d::from_objects(a, b)
}
}

pub trait Group {
fn group<Other>(&self, other: &Other) -> crate::Group
where
Other: Clone + Into<crate::Shape3d>;
}

impl<T> Group for T
where
T: Clone + Into<crate::Shape3d>,
{
fn group<Other>(&self, other: &Other) -> crate::Group
where
Other: Clone + Into<crate::Shape3d>,
{
let a = self.clone().into();
let b = other.clone().into();

crate::Group { a, b }
}
}

pub trait Rotate {
/// Create a rotation
///
Expand Down Expand Up @@ -69,24 +111,3 @@ where
}
}
}

pub trait Group {
fn group<Other>(&self, other: &Other) -> crate::Group
where
Other: Clone + Into<crate::Shape3d>;
}

impl<T> Group for T
where
T: Clone + Into<crate::Shape3d>,
{
fn group<Other>(&self, other: &Other) -> crate::Group
where
Other: Clone + Into<crate::Shape3d>,
{
let a = self.clone().into();
let b = other.clone().into();

crate::Group { a, b }
}
}
8 changes: 4 additions & 4 deletions models/spacer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::HashMap;

use fj::prelude::*;

#[no_mangle]
pub extern "C" fn model(args: &HashMap<String, String>) -> fj::Shape {
let outer = args
Expand All @@ -22,10 +24,8 @@ pub extern "C" fn model(args: &HashMap<String, String>) -> fj::Shape {
fj::Circle::from_radius(outer).with_color([0, 0, 255, 255]);
let inner_edge = fj::Circle::from_radius(inner);

let footprint =
fj::Difference2d::from_objects(outer_edge.into(), inner_edge.into());

let spacer = fj::Sweep::from_shape_and_length(footprint.into(), height);
let footprint = outer_edge.difference(&inner_edge);
let spacer = footprint.sweep(height);

spacer.into()
}