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(()) +}