From dd356cc5161420642057ab9f6b6e458be604f696 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 9 Aug 2022 14:39:21 +0800 Subject: [PATCH] Reverted model implementations to main --- models/cuboid/src/lib.rs | 60 ++++++--------------- models/spacer/src/lib.rs | 64 +++++----------------- models/star/src/lib.rs | 111 ++++++++++++--------------------------- models/test/src/lib.rs | 47 +++++------------ 4 files changed, 74 insertions(+), 208 deletions(-) diff --git a/models/cuboid/src/lib.rs b/models/cuboid/src/lib.rs index 39800822e..6505e3612 100644 --- a/models/cuboid/src/lib.rs +++ b/models/cuboid/src/lib.rs @@ -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> { - 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() } diff --git a/models/spacer/src/lib.rs b/models/spacer/src/lib.rs index 7e9c691bf..98724904d 100644 --- a/models/spacer/src/lib.rs +++ b/models/spacer/src/lib.rs @@ -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> { - 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() } diff --git a/models/star/src/lib.rs b/models/star/src/lib.rs index 34fa9bab4..3d1775680 100644 --- a/models/star/src/lib.rs +++ b/models/star/src/lib.rs @@ -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> { - 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() } diff --git a/models/test/src/lib.rs b/models/test/src/lib.rs index 11abd4883..76f955cf6 100644 --- a/models/test/src/lib.rs +++ b/models/test/src/lib.rs @@ -1,41 +1,18 @@ use std::f64::consts::PI; -use fj::{syntax::*, Angle, HostExt, Metadata, ModelMetadata}; - -fj::register_model!(|host| { - host.register_model(Test); - - Ok( - Metadata::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) - .with_short_description(env!("CARGO_PKG_DESCRIPTION")) - .with_description(include_str!("../README.md")) - .with_homepage(env!("CARGO_PKG_HOMEPAGE")) - .with_repository(env!("CARGO_PKG_REPOSITORY")) - .with_license(env!("CARGO_PKG_LICENSE")), - ) -}); - -struct Test; - -impl fj::Model for Test { - fn shape( - &self, - _ctx: &dyn fj::Context, - ) -> Result> { - let a = star(4, 1., [0, 255, 0, 200]); - let b = star(5, -1., [255, 0, 0, 255]) - .rotate([1., 1., 1.], Angle::from_deg(45.)) - .translate([3., 3., 1.]); - let c = spacer().translate([6., 6., 1.]); - - let group = a.group(&b).group(&c); - - Ok(group.into()) - } +use fj::{syntax::*, Angle}; - fn metadata(&self) -> ModelMetadata { - ModelMetadata::new("Test") - } +#[fj::model] +pub fn model() -> fj::Shape { + let a = star(4, 1., [0, 255, 0, 200]); + let b = star(5, -1., [255, 0, 0, 255]) + .rotate([1., 1., 1.], Angle::from_deg(45.)) + .translate([3., 3., 1.]); + let c = spacer().translate([6., 6., 1.]); + + let group = a.group(&b).group(&c); + + group.into() } fn star(num_points: u64, height: f64, color: [u8; 4]) -> fj::Shape {