From 9f309b65052f899e96e5e6b2188d398dac105e45 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 19 Jun 2023 12:08:35 +0200 Subject: [PATCH 1/2] Add `BuildRegion::empty` --- crates/fj-core/src/operations/build/region.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/fj-core/src/operations/build/region.rs b/crates/fj-core/src/operations/build/region.rs index eacc85711..51c616e8f 100644 --- a/crates/fj-core/src/operations/build/region.rs +++ b/crates/fj-core/src/operations/build/region.rs @@ -8,6 +8,15 @@ use crate::{ /// Build a [`Region`] pub trait BuildRegion { + /// Build an empty region + fn empty(services: &mut Services) -> Region { + let exterior = Cycle::empty().insert(services); + let interiors = []; + let color = None; + + Region::new(exterior, interiors, color) + } + /// Build a circle fn circle( center: impl Into>, From 4bdf3949214abbe0ecb47727974acac78b2f13f4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Mon, 19 Jun 2023 12:21:23 +0200 Subject: [PATCH 2/2] Add `star` model --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + models/star/Cargo.toml | 7 ++++++ models/star/src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++ models/star/src/main.rs | 7 ++++++ 5 files changed, 75 insertions(+) create mode 100644 models/star/Cargo.toml create mode 100644 models/star/src/lib.rs create mode 100644 models/star/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index aba1d14f0..d9e84050a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2737,6 +2737,13 @@ dependencies = [ "num-traits", ] +[[package]] +name = "star" +version = "0.1.0" +dependencies = [ + "fj", +] + [[package]] name = "static_assertions" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index cced879a7..f2459e714 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ members = [ "models/cuboid", "models/spacer", + "models/star", "tools/autolib", "tools/automator", diff --git a/models/star/Cargo.toml b/models/star/Cargo.toml new file mode 100644 index 000000000..0279bef41 --- /dev/null +++ b/models/star/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "star" +version = "0.1.0" +edition = "2021" + +[dependencies.fj] +path = "../../crates/fj" diff --git a/models/star/src/lib.rs b/models/star/src/lib.rs new file mode 100644 index 000000000..9910cbead --- /dev/null +++ b/models/star/src/lib.rs @@ -0,0 +1,53 @@ +use std::f64::consts::PI; + +use fj::{ + core::{ + algorithms::sweep::Sweep, + objects::{Cycle, Region, Sketch, Solid}, + operations::{ + BuildCycle, BuildRegion, BuildSketch, Insert, Reverse, + UpdateRegion, UpdateSketch, + }, + services::Services, + storage::Handle, + }, + math::Vector, +}; + +pub fn model(num_points: u64, r1: f64, r2: f64, h: f64) -> Handle { + let mut services = Services::new(); + + let num_vertices = num_points * 2; + let vertex_iter = (0..num_vertices).map(|i| { + let angle_rad = 2. * PI / num_vertices as f64 * i as f64; + let radius = if i % 2 == 0 { r1 } else { r2 }; + (angle_rad, radius) + }); + + let mut outer_points = Vec::new(); + let mut inner_points = Vec::new(); + + for (angle_rad, radius) in vertex_iter { + let (sin, cos) = angle_rad.sin_cos(); + + let x = cos * radius; + let y = sin * radius; + + outer_points.push([x, y]); + inner_points.push([x / 2., y / 2.]); + } + + let sketch = Sketch::empty() + .add_region( + Region::polygon(outer_points, &mut services) + .add_interiors([Cycle::polygon(inner_points, &mut services) + .reverse(&mut services) + .insert(&mut services)]) + .insert(&mut services), + ) + .insert(&mut services); + + let surface = services.objects.surfaces.xy_plane(); + let path = Vector::from([0., 0., h]); + (sketch, surface).sweep(path, &mut services) +} diff --git a/models/star/src/main.rs b/models/star/src/main.rs new file mode 100644 index 000000000..c6f11d59c --- /dev/null +++ b/models/star/src/main.rs @@ -0,0 +1,7 @@ +use fj::handle_model; + +fn main() -> fj::Result { + let model = star::model(5, 1., 2., 1.); + handle_model(model)?; + Ok(()) +}