Skip to content

Commit

Permalink
Merge pull request #505 from hannobraun/sweep
Browse files Browse the repository at this point in the history
Support sweeping in arbitrary directions
  • Loading branch information
hannobraun authored Apr 26, 2022
2 parents c847da2 + 580cbec commit c4a240f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 22 deletions.
9 changes: 4 additions & 5 deletions crates/fj-operations/src/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fj_kernel::{
algorithms::{sweep_shape, Tolerance},
shape::Shape,
};
use fj_math::{Aabb, Vector};
use fj_math::{Aabb, Point, Vector};

use super::ToShape;

Expand All @@ -15,15 +15,14 @@ impl ToShape for fj::Sweep {
) -> Shape {
sweep_shape(
self.shape().to_shape(tolerance, debug_info),
Vector::from([0., 0., self.length()]),
Vector::from(self.path()),
tolerance,
self.shape().color(),
)
}

fn bounding_volume(&self) -> Aabb<3> {
let mut aabb = self.shape().bounding_volume();
aabb.max.z = self.length().into();
aabb
let target = Point::origin() + self.path();
self.shape().bounding_volume().include_point(&target)
}
}
18 changes: 9 additions & 9 deletions crates/fj/src/shape_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,31 +87,31 @@ impl From<Transform> for Shape3d {
}
}

/// A sweep of a 2-dimensional shape along the z-axis
/// A sweep of a 2-dimensional shape along straight path
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Sweep {
/// The 2-dimensional shape being swept
shape: Shape2d,

/// The length of the sweep
length: f64,
/// The length and direction of the sweep
path: [f64; 3],
}

impl Sweep {
/// Create a `Sweep` from a shape and a length
pub fn from_shape_and_length(shape: Shape2d, length: f64) -> Self {
Self { shape, length }
/// Create a `Sweep` along a straight path
pub fn from_path(shape: Shape2d, path: [f64; 3]) -> Self {
Self { shape, path }
}

/// Access the shape being swept
pub fn shape(&self) -> &Shape2d {
&self.shape
}

/// Access the length of the sweep
pub fn length(&self) -> f64 {
self.length
/// Access the path of the sweep
pub fn path(&self) -> [f64; 3] {
self.path
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/fj/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ where
///
/// [`fj::Sweep`]: crate::Sweep
pub trait Sweep {
/// Sweep `self` along the z-axis by `length`
fn sweep(&self, length: f64) -> crate::Sweep;
/// Sweep `self` along a straight path
fn sweep(&self, path: [f64; 3]) -> crate::Sweep;
}

impl<T> Sweep for T
where
T: Clone + Into<crate::Shape2d>,
{
fn sweep(&self, length: f64) -> crate::Sweep {
fn sweep(&self, path: [f64; 3]) -> crate::Sweep {
let shape = self.clone().into();
crate::Sweep::from_shape_and_length(shape, length)
crate::Sweep::from_path(shape, path)
}
}

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 @@ -14,7 +14,7 @@ pub extern "C" fn model(args: &HashMap<String, String>) -> fj::Shape {
[-x / 2., y / 2.],
]).with_color([100,255,0,200]);

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

cuboid.into()
}
2 changes: 1 addition & 1 deletion models/group/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub extern "C" fn model(_: &HashMap<String, String>) -> fj::Shape {
[-0.5, 0.5],
];

let cube_a = fj::Sketch::from_points(vertices).sweep(1.0);
let cube_a = fj::Sketch::from_points(vertices).sweep([0., 0., 1.]);
let cube_b = cube_a.translate([1.5, 0., 0.5]);

let group = cube_a.group(&cube_b);
Expand Down
2 changes: 1 addition & 1 deletion models/spacer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub extern "C" fn model(args: &HashMap<String, String>) -> fj::Shape {
let inner_edge = fj::Circle::from_radius(inner);

let footprint = outer_edge.difference(&inner_edge);
let spacer = footprint.sweep(height);
let spacer = footprint.sweep([0., 0., height]);

spacer.into()
}
2 changes: 1 addition & 1 deletion models/star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub extern "C" fn model(args: &HashMap<String, String>) -> fj::Shape {

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

let star = fj::Sweep::from_shape_and_length(footprint.into(), h);
let star = fj::Sweep::from_path(footprint.into(), [0., 0., h]);

star.into()
}

0 comments on commit c4a240f

Please sign in to comment.