Skip to content

Commit

Permalink
Merge pull request #1892 from hannobraun/examples
Browse files Browse the repository at this point in the history
Add `all` model
  • Loading branch information
hannobraun authored Jun 19, 2023
2 parents 1d233e7 + 90976d3 commit 9633746
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 33 deletions.
10 changes: 10 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 @@ -9,6 +9,7 @@ members = [
"crates/fj-viewer",
"crates/fj-window",

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

[dependencies.fj]
path = "../../crates/fj"

[dependencies.cuboid]
path = "../cuboid"

[dependencies.spacer]
path = "../spacer"

[dependencies.star]
path = "../star"
32 changes: 32 additions & 0 deletions models/all/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use fj::{
core::{
algorithms::transform::TransformObject,
objects::Solid,
operations::{Insert, Merge},
services::Services,
storage::Handle,
},
math::{Scalar, Vector},
};

pub fn model(services: &mut Services) -> Handle<Solid> {
// Just combine all the other models using offsets/rotations that won't
// result in neat vertex positions or axis-aligned edges/faces. This is
// useful for testing.

let offset = Vector::from([5., 5., 5.]);
let axis = Vector::from([1., 1., 1.]).normalize();
let angle_rad = Scalar::PI / 6.;

let cuboid = cuboid::model(1., 2., 3., services)
.translate(offset * 1., services)
.rotate(axis * angle_rad * 1., services);
let spacer = spacer::model(2., 1., 1., services)
.translate(offset * 2., services)
.rotate(axis * angle_rad * 2., services);
let star = star::model(5, 2., 1., 1., services)
.translate(offset * 3., services)
.rotate(axis * angle_rad * 3., services);

cuboid.merge(&spacer).merge(&star).insert(services)
}
7 changes: 7 additions & 0 deletions models/all/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use fj::{core::services::Services, handle_model};

fn main() -> fj::Result {
let model = all::model(&mut Services::new());
handle_model(model)?;
Ok(())
}
12 changes: 5 additions & 7 deletions models/cuboid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use fj::{
math::Vector,
};

pub fn model(x: f64, y: f64, z: f64) -> Handle<Solid> {
let mut services = Services::new();

pub fn model(x: f64, y: f64, z: f64, services: &mut Services) -> Handle<Solid> {
let sketch = Sketch::empty()
.add_region(
Region::polygon(
Expand All @@ -21,13 +19,13 @@ pub fn model(x: f64, y: f64, z: f64) -> Handle<Solid> {
[x / 2., y / 2.],
[-x / 2., y / 2.],
],
&mut services,
services,
)
.insert(&mut services),
.insert(services),
)
.insert(&mut services);
.insert(services);

let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., z]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}
4 changes: 2 additions & 2 deletions models/cuboid/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};

fn main() -> fj::Result {
let model = cuboid::model(3., 2., 1.);
let model = cuboid::model(3., 2., 1., &mut Services::new());
handle_model(model)?;
Ok(())
}
23 changes: 13 additions & 10 deletions models/spacer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ use fj::{
math::{Point, Vector},
};

pub fn model(outer: f64, inner: f64, height: f64) -> Handle<Solid> {
let mut services = Services::new();

pub fn model(
outer: f64,
inner: f64,
height: f64,
services: &mut Services,
) -> Handle<Solid> {
let sketch = Sketch::empty()
.add_region(
Region::circle(Point::origin(), outer, &mut services)
Region::circle(Point::origin(), outer, services)
.add_interiors([Cycle::circle(
Point::origin(),
inner,
&mut services,
services,
)
.reverse(&mut services)
.insert(&mut services)])
.insert(&mut services),
.reverse(services)
.insert(services)])
.insert(services),
)
.insert(&mut services);
.insert(services);

let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., height]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}
4 changes: 2 additions & 2 deletions models/spacer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};

fn main() -> fj::Result {
let model = spacer::model(1., 0.5, 1.);
let model = spacer::model(1., 0.5, 1., &mut Services::new());
handle_model(model)?;
Ok(())
}
24 changes: 14 additions & 10 deletions models/star/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ use fj::{
math::Vector,
};

pub fn model(num_points: u64, r1: f64, r2: f64, h: f64) -> Handle<Solid> {
let mut services = Services::new();

pub fn model(
num_points: u64,
r1: f64,
r2: f64,
h: f64,
services: &mut Services,
) -> Handle<Solid> {
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;
Expand All @@ -39,15 +43,15 @@ pub fn model(num_points: u64, r1: f64, r2: f64, h: f64) -> Handle<Solid> {

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),
Region::polygon(outer_points, services)
.add_interiors([Cycle::polygon(inner_points, services)
.reverse(services)
.insert(services)])
.insert(services),
)
.insert(&mut services);
.insert(services);

let surface = services.objects.surfaces.xy_plane();
let path = Vector::from([0., 0., h]);
(sketch, surface).sweep(path, &mut services)
(sketch, surface).sweep(path, services)
}
4 changes: 2 additions & 2 deletions models/star/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fj::handle_model;
use fj::{core::services::Services, handle_model};

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

0 comments on commit 9633746

Please sign in to comment.