Skip to content

Commit

Permalink
Merge pull request #1890 from hannobraun/star
Browse files Browse the repository at this point in the history
Restore `star` model
  • Loading branch information
hannobraun authored Jun 19, 2023
2 parents d6240d8 + 4bdf394 commit 4232b5a
Show file tree
Hide file tree
Showing 6 changed files with 84 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
9 changes: 9 additions & 0 deletions crates/fj-core/src/operations/build/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point<2>>,
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 4232b5a

Please sign in to comment.