Skip to content

Commit

Permalink
Add star model
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Jun 19, 2023
1 parent 9f309b6 commit 4bdf394
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
7 changes: 7 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/cuboid",
"models/spacer",
"models/star",

"tools/autolib",
"tools/automator",
Expand Down
7 changes: 7 additions & 0 deletions models/star/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "star"
version = "0.1.0"
edition = "2021"

[dependencies.fj]
path = "../../crates/fj"
53 changes: 53 additions & 0 deletions models/star/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Solid> {
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)
}
7 changes: 7 additions & 0 deletions models/star/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use fj::handle_model;

fn main() -> fj::Result {
let model = star::model(5, 1., 2., 1.);
handle_model(model)?;
Ok(())
}

0 comments on commit 4bdf394

Please sign in to comment.