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 enclosure model #88

Closed
wants to merge 4 commits into from
Closed
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: 1 addition & 2 deletions crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ impl ToShape for fj::Difference2d {
// 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));
[a, b].map(|shape| shape.to_shape(tolerance, debug_info));

// Check preconditions.
//
Expand Down
75 changes: 75 additions & 0 deletions crates/fj-operations/src/difference_3d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use fj_interop::debug::DebugInfo;
use fj_kernel::{algorithms::Tolerance, shape::Shape};
use fj_math::Aabb;

use super::ToShape;

impl ToShape for fj::Difference3d {
fn to_shape(
&self,
tolerance: Tolerance,
debug_info: &mut DebugInfo,
) -> Shape {
// 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_ref = [a, b];
let shapes =
shapes_ref.map(|shape| shape.to_shape(tolerance, debug_info));

// Can be cleaned up, once `each_mut` is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.each_mut
let [mut a, mut b] = shapes;
let [a, b] = [&mut a, &mut b];

// TASK: Implement algorithm from "Boundary Representation Modelling
// Techniques", section 6.1.1 (pages 127 ff.).

// Check the faces of both shapes for intersections.
for face_a in a.topology().faces() {
for face_b in b.topology().faces() {
let surface_a = face_a.get().surface();
let surface_b = face_b.get().surface();

// TASK: Check `surface_a` and `surface_b` for intersection. If
// that results in an intersection curve, continue.
// TASK: Check intersection curve against each of `face_a` and
// `face_b`. If the two resulting list of intersections
// are not empty, continue.
// TASK: Compare the two lists of intersections. If the
// resulting list of common intersections is not empty,
// continue.

// TASK: Create edges from the common intersections. Also
// shorten edges where those common intersections
// intersect existing edges of the face. Create new
// vertices as boundaries of those new and shortened
// edges.
//
// Do all of this in a way that doesn't create duplicate
// vertices. This not only goes for for the two faces
// we're looking at here, it also goes for other faces we
// haven't even compared to yet.
//
// At this point, we probably need API to query for all
// faces that an edge is part of, so we can update
// everything properly?

// TASK: Implement.
let _ = surface_a;
let _ = surface_b;
todo!()
}
}

// TASK: Implement.
todo!()
}

fn bounding_volume(&self) -> Aabb<3> {
// 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.shapes()[0].bounding_volume()
}
}
11 changes: 9 additions & 2 deletions crates/fj-operations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod shape_processor;

mod circle;
mod difference_2d;
mod difference_3d;
mod group;
mod sketch;
mod sweep;
Expand All @@ -32,7 +33,11 @@ use fj_math::Aabb;
/// Implemented for all operations from the [`fj`] crate
pub trait ToShape {
/// Compute the boundary representation of the shape
fn to_shape(&self, tolerance: Tolerance, debug: &mut DebugInfo) -> Shape;
fn to_shape(
&self,
tolerance: Tolerance,
debug_info: &mut DebugInfo,
) -> Shape;

/// Access the axis-aligned bounding box of a shape
///
Expand Down Expand Up @@ -70,6 +75,8 @@ macro_rules! dispatch {
$(
fn $method(&self, $($arg_name: $arg_ty,)*) -> $ret {
match self {
Self::Difference(shape) =>
shape.$method($($arg_name,)*),
Self::Group(shape) => shape.$method($($arg_name,)*),
Self::Sweep(shape) => shape.$method($($arg_name,)*),
Self::Transform(shape) => shape.$method($($arg_name,)*),
Expand All @@ -83,7 +90,7 @@ macro_rules! dispatch {
dispatch! {
to_shape(
tolerance: Tolerance,
debug: &mut DebugInfo,
debug_info: &mut DebugInfo,
) -> Shape;
bounding_volume() -> Aabb<3>;
}
34 changes: 34 additions & 0 deletions crates/fj/src/shape_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::{Shape, Shape2d};
#[derive(Clone, Debug)]
#[repr(C)]
pub enum Shape3d {
/// A difference between two shapes
Difference(Box<Difference3d>),

/// A group of two 3-dimensional shapes
Group(Box<Group>),

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

/// A difference between two shapes
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Difference3d {
shapes: [Shape3d; 2],
}

impl Difference3d {
/// Create a `Difference3d` from two shapes
pub fn from_shapes(shapes: [Shape3d; 2]) -> Self {
Self { shapes }
}

/// Access the shapes that make up the difference
pub fn shapes(&self) -> &[Shape3d; 2] {
&self.shapes
}
}

impl From<Difference3d> for Shape {
fn from(shape: Difference3d) -> Self {
Self::Shape3d(shape.into())
}
}

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

/// A group of two 3-dimensional shapes
///
/// A group is a collection of disjoint shapes. It is not a union, in that the
Expand Down
10 changes: 10 additions & 0 deletions models/enclosure/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "enclosure"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies.fj]
path = "../../fj"
10 changes: 10 additions & 0 deletions models/enclosure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Fornjot - Enclosure

An attempt to replicate my [Prusa Mini Enclosure](https://github.com/hannobraun/prusa-mini-enclosure), originally modeled using [libfive Studio](https://libfive.com/studio/), with Fornjot.

As of this writing, the model is not complete, and Fornjot is lacking features required to complete it.

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