Skip to content

Commit

Permalink
Merge pull request #2128 from hannobraun/holes
Browse files Browse the repository at this point in the history
Add operation for adding blind holes
  • Loading branch information
hannobraun authored Dec 7, 2023
2 parents 4daf3b7 + 061a58d commit f6734e2
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [

"models/all",
"models/cuboid",
"models/holes",
"models/spacer",
"models/split",
"models/star",
Expand Down
76 changes: 76 additions & 0 deletions crates/fj-core/src/operations/holes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Add holes to shapes

use fj_math::{Point, Vector};

use crate::{
objects::{Cycle, Face, HalfEdge, Region, Shell},
services::Services,
storage::Handle,
};

use super::{
build::{BuildCycle, BuildHalfEdge, BuildRegion},
insert::Insert,
join::JoinCycle,
sweep::{SweepCache, SweepRegion},
update::{UpdateCycle, UpdateFace, UpdateRegion, UpdateShell},
};

/// Add a hole to a [`Shell`]
pub trait AddHole {
/// Add a blind hole to the provided face of the shell
fn add_blind_hole(
&self,
face: &Handle<Face>,
position: impl Into<Point<2>>,
path: impl Into<Vector<3>>,
services: &mut Services,
) -> Self;
}

impl AddHole for Shell {
fn add_blind_hole(
&self,
face: &Handle<Face>,
position: impl Into<Point<2>>,
path: impl Into<Vector<3>>,
services: &mut Services,
) -> Self {
let half_edge =
HalfEdge::circle(position, 0.25, services).insert(services);
let hole = Region::empty(services)
.update_exterior(|_| {
Cycle::empty()
.add_half_edges([half_edge.clone()])
.insert(services)
})
.sweep_region(
face.surface(),
path,
&mut SweepCache::default(),
services,
)
.into_iter()
.map(|face| face.insert(services))
.collect::<Vec<_>>();

self.update_face(face, |face| {
face.update_region(|region| {
region
.add_interiors([Cycle::empty()
.add_joined_edges(
[(
half_edge.clone(),
half_edge.path(),
half_edge.boundary(),
)],
services,
)
.insert(services)])
.insert(services)
})
.insert(services)
})
.add_faces(hole)
}
}
1 change: 1 addition & 0 deletions crates/fj-core/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
//! send a pull request!

pub mod build;
pub mod holes;
pub mod insert;
pub mod join;
pub mod merge;
Expand Down
10 changes: 10 additions & 0 deletions models/holes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "holes"
version = "0.1.0"
edition = "2021"

[dependencies.fj]
path = "../../crates/fj"

[dependencies.cuboid]
path = "../cuboid"
24 changes: 24 additions & 0 deletions models/holes/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use fj::core::{
objects::Solid,
operations::{holes::AddHole, insert::Insert, update::UpdateSolid},
services::Services,
storage::Handle,
};

pub fn model(services: &mut Services) -> Handle<Solid> {
let cuboid = cuboid::model(1., 1., 1., services);

let shell = cuboid.shells().first();
let bottom_face = shell.faces().first();

let hole_position = [0., 0.];
let hole_path = [0., 0., 0.5];

cuboid
.update_shell(shell, |shell| {
shell
.add_blind_hole(bottom_face, hole_position, hole_path, services)
.insert(services)
})
.insert(services)
}
8 changes: 8 additions & 0 deletions models/holes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use fj::{core::services::Services, handle_model};

fn main() -> fj::Result {
let mut services = Services::new();
let model = holes::model(&mut services);
handle_model(model, services)?;
Ok(())
}

0 comments on commit f6734e2

Please sign in to comment.