-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverted model implementations to main
- Loading branch information
1 parent
c43bcfd
commit dd356cc
Showing
4 changed files
with
74 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,18 @@ | ||
use fj::{ | ||
ArgumentMetadata, ContextExt, HostExt, Metadata, Model, ModelMetadata, | ||
}; | ||
|
||
fj::register_model!(|host| { | ||
host.register_model(Cuboid); | ||
|
||
Ok(Metadata::new( | ||
env!("CARGO_PKG_NAME"), | ||
env!("CARGO_PKG_VERSION"), | ||
)) | ||
}); | ||
|
||
pub struct Cuboid; | ||
|
||
impl Model for Cuboid { | ||
fn shape( | ||
&self, | ||
ctx: &dyn fj::Context, | ||
) -> Result<fj::Shape, Box<dyn std::error::Error + Send + Sync>> { | ||
let x: f64 = ctx.parse_optional_argument("x")?.unwrap_or(3.0); | ||
let y: f64 = ctx.parse_optional_argument("y")?.unwrap_or(2.0); | ||
let z: f64 = ctx.parse_optional_argument("z")?.unwrap_or(1.0); | ||
|
||
#[rustfmt::skip] | ||
let rectangle = fj::Sketch::from_points(vec![ | ||
[-x / 2., -y / 2.], | ||
[ x / 2., -y / 2.], | ||
[ x / 2., y / 2.], | ||
[-x / 2., y / 2.], | ||
]).with_color([100,255,0,200]); | ||
|
||
let cuboid = fj::Sweep::from_path(rectangle.into(), [0., 0., z]); | ||
|
||
Ok(cuboid.into()) | ||
} | ||
|
||
fn metadata(&self) -> ModelMetadata { | ||
ModelMetadata::new("Cuboid") | ||
.with_argument(ArgumentMetadata::new("x").with_default_value("3.0")) | ||
.with_argument(ArgumentMetadata::new("y").with_default_value("2.0")) | ||
.with_argument(ArgumentMetadata::new("z").with_default_value("1.0")) | ||
} | ||
#[fj::model] | ||
pub fn model( | ||
#[param(default = 3.0)] x: f64, | ||
#[param(default = 2.0)] y: f64, | ||
#[param(default = 1.0)] z: f64, | ||
) -> fj::Shape { | ||
#[rustfmt::skip] | ||
let rectangle = fj::Sketch::from_points(vec![ | ||
[-x / 2., -y / 2.], | ||
[ x / 2., -y / 2.], | ||
[ x / 2., y / 2.], | ||
[-x / 2., y / 2.], | ||
]).with_color([100,255,0,200]); | ||
|
||
let cuboid = fj::Sweep::from_path(rectangle.into(), [0., 0., z]); | ||
|
||
cuboid.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,16 @@ | ||
use fj::{ | ||
syntax::*, ArgumentMetadata, ContextExt, HostExt, Metadata, Model, | ||
ModelMetadata, | ||
}; | ||
use fj::syntax::*; | ||
|
||
fj::register_model!(|host| { | ||
host.register_model(Spacer); | ||
#[fj::model] | ||
pub fn model( | ||
#[param(default = 1.0, min = inner * 1.01)] outer: f64, | ||
#[param(default = 0.5, max = outer * 0.99)] inner: f64, | ||
#[param(default = 1.0)] height: f64, | ||
) -> fj::Shape { | ||
let outer_edge = fj::Sketch::from_circle(fj::Circle::from_radius(outer)); | ||
let inner_edge = fj::Sketch::from_circle(fj::Circle::from_radius(inner)); | ||
|
||
Ok(Metadata::new( | ||
env!("CARGO_PKG_NAME"), | ||
env!("CARGO_PKG_VERSION"), | ||
)) | ||
}); | ||
let footprint = outer_edge.difference(&inner_edge); | ||
let spacer = footprint.sweep([0., 0., height]); | ||
|
||
struct Spacer; | ||
|
||
impl Model for Spacer { | ||
fn shape( | ||
&self, | ||
ctx: &dyn fj::Context, | ||
) -> Result<fj::Shape, Box<dyn std::error::Error + Send + Sync>> { | ||
let outer: f64 = ctx.parse_optional_argument("outer")?.unwrap_or(1.0); | ||
let inner: f64 = ctx.parse_optional_argument("inner")?.unwrap_or(0.5); | ||
let height: f64 = ctx.parse_optional_argument("height")?.unwrap_or(1.0); | ||
|
||
if outer < inner * 1.01 { | ||
todo!("Return a suitable error"); | ||
} | ||
if inner > outer * 0.99 { | ||
todo!("Return a suitable error"); | ||
} | ||
|
||
let outer_edge = | ||
fj::Sketch::from_circle(fj::Circle::from_radius(outer)); | ||
let inner_edge = | ||
fj::Sketch::from_circle(fj::Circle::from_radius(inner)); | ||
|
||
let footprint = outer_edge.difference(&inner_edge); | ||
let spacer = footprint.sweep([0., 0., height]); | ||
|
||
Ok(spacer.into()) | ||
} | ||
|
||
fn metadata(&self) -> ModelMetadata { | ||
ModelMetadata::new("Spacer") | ||
.with_argument( | ||
ArgumentMetadata::new("outer").with_default_value("1.0"), | ||
) | ||
.with_argument( | ||
ArgumentMetadata::new("inner").with_default_value("0.5"), | ||
) | ||
.with_argument( | ||
ArgumentMetadata::new("height").with_default_value("1.0"), | ||
) | ||
} | ||
spacer.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,40 @@ | ||
use std::f64::consts::PI; | ||
|
||
use fj::{ | ||
ArgumentMetadata, ContextExt, HostExt, Metadata, Model, ModelMetadata, | ||
}; | ||
|
||
fj::register_model!(|host| { | ||
host.register_model(Star); | ||
|
||
Ok(Metadata::new( | ||
env!("CARGO_PKG_NAME"), | ||
env!("CARGO_PKG_VERSION"), | ||
)) | ||
}); | ||
|
||
struct Star; | ||
|
||
impl Model for Star { | ||
fn shape( | ||
&self, | ||
ctx: &dyn fj::Context, | ||
) -> Result<fj::Shape, Box<dyn std::error::Error + Send + Sync>> { | ||
let num_points: u64 = | ||
ctx.parse_optional_argument("num_points")?.unwrap_or(5); | ||
let r1: f64 = ctx.parse_optional_argument("r1")?.unwrap_or(1.0); | ||
let r2: f64 = ctx.parse_optional_argument("r2")?.unwrap_or(2.0); | ||
let h: f64 = ctx.parse_optional_argument("h")?.unwrap_or(1.0); | ||
|
||
if num_points < 3 { | ||
todo!(); | ||
} | ||
if r1 < 1.0 { | ||
todo!(); | ||
} | ||
if r2 < 2.0 { | ||
todo!(); | ||
} | ||
|
||
let num_vertices = num_points * 2; | ||
let vertex_iter = (0..num_vertices).map(|i| { | ||
let angle = | ||
fj::Angle::from_rad(2. * PI / num_vertices as f64 * i as f64); | ||
let radius = if i % 2 == 0 { r1 } else { r2 }; | ||
(angle, radius) | ||
}); | ||
|
||
// Now that we got that iterator prepared, generating the vertices is just a | ||
// bit of trigonometry. | ||
let mut outer = Vec::new(); | ||
let mut inner = Vec::new(); | ||
for (angle, radius) in vertex_iter { | ||
let (sin, cos) = angle.rad().sin_cos(); | ||
|
||
let x = cos * radius; | ||
let y = sin * radius; | ||
|
||
outer.push([x, y]); | ||
inner.push([x / 2., y / 2.]); | ||
} | ||
|
||
let outer = fj::Sketch::from_points(outer); | ||
let inner = fj::Sketch::from_points(inner); | ||
#[fj::model] | ||
pub fn model( | ||
#[param(default = 5, min = 3)] num_points: u64, | ||
#[param(default = 1.0, min = 1.0)] r1: f64, | ||
#[param(default = 2.0, min = 2.0)] r2: f64, | ||
#[param(default = 1.0)] h: f64, | ||
) -> fj::Shape { | ||
let num_vertices = num_points * 2; | ||
let vertex_iter = (0..num_vertices).map(|i| { | ||
let angle = | ||
fj::Angle::from_rad(2. * PI / num_vertices as f64 * i as f64); | ||
let radius = if i % 2 == 0 { r1 } else { r2 }; | ||
(angle, radius) | ||
}); | ||
|
||
// Now that we got that iterator prepared, generating the vertices is just a | ||
// bit of trigonometry. | ||
let mut outer = Vec::new(); | ||
let mut inner = Vec::new(); | ||
for (angle, radius) in vertex_iter { | ||
let (sin, cos) = angle.rad().sin_cos(); | ||
|
||
let x = cos * radius; | ||
let y = sin * radius; | ||
|
||
outer.push([x, y]); | ||
inner.push([x / 2., y / 2.]); | ||
} | ||
|
||
let footprint = | ||
fj::Difference2d::from_shapes([outer.into(), inner.into()]); | ||
let outer = fj::Sketch::from_points(outer); | ||
let inner = fj::Sketch::from_points(inner); | ||
|
||
let star = fj::Sweep::from_path(footprint.into(), [0., 0., h]); | ||
let footprint = fj::Difference2d::from_shapes([outer.into(), inner.into()]); | ||
|
||
Ok(star.into()) | ||
} | ||
let star = fj::Sweep::from_path(footprint.into(), [0., 0., h]); | ||
|
||
fn metadata(&self) -> ModelMetadata { | ||
ModelMetadata::new("Star") | ||
.with_argument( | ||
ArgumentMetadata::new("num_points").with_default_value("5"), | ||
) | ||
.with_argument( | ||
ArgumentMetadata::new("r1").with_default_value("1.0"), | ||
) | ||
.with_argument( | ||
ArgumentMetadata::new("r2").with_default_value("2.0"), | ||
) | ||
.with_argument(ArgumentMetadata::new("h").with_default_value("1.0")) | ||
} | ||
star.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters