Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore star model #1890

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}