Skip to content

Commit

Permalink
Merge pull request #366 from hannobraun/group
Browse files Browse the repository at this point in the history
Rename `fj::Union` to `fj::Group`
  • Loading branch information
hannobraun authored Mar 16, 2022
2 parents 1aa0981 + 45a19f8 commit 4addd99
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 66 deletions.
2 changes: 1 addition & 1 deletion fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod syntax;

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

Expand Down
72 changes: 33 additions & 39 deletions fj/src/shape_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::{Shape, Shape2d};
#[derive(Clone, Debug)]
#[repr(C)]
pub enum Shape3d {
/// A group of two 3-dimensional shapes
Group(Box<Group>),

/// A sweep of 2-dimensional shape along the z-axis
Sweep(Sweep),

/// A transformed 3-dimensional shape
Transform(Box<Transform>),

/// The union of two 3-dimensional shapes
Union(Box<Union>),
}

impl From<Shape3d> for Shape {
Expand All @@ -20,6 +20,36 @@ impl From<Shape3d> for Shape {
}
}

/// A group of two 3-dimensional shapes
///
/// A group is a collection of disjoint shapes. It is not a union, in that the
/// shapes in the group are not allowed to touch or overlap.
///
/// # Limitations
///
/// Whether the shapes in the group touch or overlap is not currently checked.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Group {
/// The first of the shapes
pub a: Shape3d,

/// The second of the shapes
pub b: Shape3d,
}

impl From<Group> for Shape {
fn from(shape: Group) -> Self {
Self::Shape3d(Shape3d::Group(Box::new(shape)))
}
}

impl From<Group> for Shape3d {
fn from(shape: Group) -> Self {
Self::Group(Box::new(shape))
}
}

/// A transformed 3-dimensional shape
///
/// # Limitations
Expand Down Expand Up @@ -97,39 +127,3 @@ impl From<Sweep> for Shape3d {
Self::Sweep(shape)
}
}

/// The union of two 3-dimensional shapes
///
/// # Limitations
///
/// Support for unions is somewhat limited right now. A union of 2 distinct
/// shapes doesn't really create a new shape, but just an aggregation of the
/// two original shapes.
///
/// This means, for example, that generating the triangle mesh of the union does
/// not result in a proper triangle mesh, but rather the two, possibly
/// intersecting, triangle meshes of the original shapes.
///
/// See issue:
/// <https://github.com/hannobraun/Fornjot/issues/42>
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Union {
/// The first of the shapes
pub a: Shape3d,

/// The second of the shapes
pub b: Shape3d,
}

impl From<Union> for Shape {
fn from(shape: Union) -> Self {
Self::Shape3d(Shape3d::Union(Box::new(shape)))
}
}

impl From<Union> for Shape3d {
fn from(shape: Union) -> Self {
Self::Union(Box::new(shape))
}
}
10 changes: 5 additions & 5 deletions fj/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ where
}
}

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

impl<T> Union for T
impl<T> Group for T
where
T: Clone + Into<crate::Shape3d>,
{
fn union<Other>(&self, other: &Other) -> crate::Union
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::Union { a, b }
crate::Group { a, b }
}
}
10 changes: 0 additions & 10 deletions models/csg-union-disjoint/README.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "csg-union-disjoint"
name = "group"
version = "0.1.0"
edition = "2021"

Expand Down
10 changes: 10 additions & 0 deletions models/group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Fornjot - Group

A model that demonstrates groups of two disjoint bodies.

To display this model, run the following from the repository root:
``` sh
cargo run -- --model group
```

![Screenshot of the group model](group.png)
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub extern "C" fn model(_: &HashMap<String, String>) -> fj::Shape {
let cube_a = fj::Sketch::from_points(vertices).sweep(1.0);
let cube_b = cube_a.clone().translate([1.5, 0., 0.5]);

let disjoint_union = cube_a.union(&cube_b);
let group = cube_a.group(&cube_b);

disjoint_union.into()
group.into()
}
7 changes: 1 addition & 6 deletions src/kernel/shapes/union.rs → src/kernel/shapes/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@ use crate::{

use super::ToShape;

impl ToShape for fj::Union {
impl ToShape for fj::Group {
fn to_shape(&self, tolerance: Scalar, debug_info: &mut DebugInfo) -> Shape {
let mut shape = Shape::new();

let a = self.a.to_shape(tolerance, debug_info);
let b = self.b.to_shape(tolerance, debug_info);

// This doesn't create a true union, as it doesn't eliminate, merge, or
// split faces.
//
// See issue:
// https://github.com/hannobraun/Fornjot/issues/42
copy_shape(a, &mut shape);
copy_shape(b, &mut shape);

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/shapes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub mod circle;
pub mod difference_2d;
pub mod group;
pub mod sketch;
pub mod sweep;
pub mod transform;
pub mod union;

use crate::{
debug::DebugInfo,
Expand Down Expand Up @@ -53,9 +53,9 @@ macro_rules! dispatch {
$(
fn $method(&self, $($arg_name: $arg_ty,)*) -> $ret {
match self {
Self::Group(shape) => shape.$method($($arg_name,)*),
Self::Sweep(shape) => shape.$method($($arg_name,)*),
Self::Transform(shape) => shape.$method($($arg_name,)*),
Self::Union(shape) => shape.$method($($arg_name,)*),
}
}
)*
Expand Down

0 comments on commit 4addd99

Please sign in to comment.