math u32 fix (#2218)
* math fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * empty * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * empty --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
2
src/wasm-lib/Cargo.lock
generated
2
src/wasm-lib/Cargo.lock
generated
@ -1854,7 +1854,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kcl-lib"
|
name = "kcl-lib"
|
||||||
version = "0.1.49"
|
version = "0.1.50"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"approx 0.5.1",
|
"approx 0.5.1",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "kcl-lib"
|
name = "kcl-lib"
|
||||||
description = "KittyCAD Language implementation and tools"
|
description = "KittyCAD Language implementation and tools"
|
||||||
version = "0.1.49"
|
version = "0.1.50"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/KittyCAD/modeling-app"
|
repository = "https://github.com/KittyCAD/modeling-app"
|
||||||
|
@ -323,7 +323,12 @@ pub fn get_type_string_from_schema(schema: &schemars::schema::Schema) -> Result<
|
|||||||
if let Some(format) = &o.format {
|
if let Some(format) = &o.format {
|
||||||
if format == "uuid" {
|
if format == "uuid" {
|
||||||
return Ok((Primitive::Uuid.to_string(), false));
|
return Ok((Primitive::Uuid.to_string(), false));
|
||||||
} else if format == "double" || format == "uint" || format == "int64" || format == "uint32" {
|
} else if format == "double"
|
||||||
|
|| format == "uint"
|
||||||
|
|| format == "int64"
|
||||||
|
|| format == "uint32"
|
||||||
|
|| format == "uint64"
|
||||||
|
{
|
||||||
return Ok((Primitive::Number.to_string(), false));
|
return Ok((Primitive::Number.to_string(), false));
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("unknown format: {}", format);
|
anyhow::bail!("unknown format: {}", format);
|
||||||
@ -460,7 +465,12 @@ pub fn get_autocomplete_snippet_from_schema(
|
|||||||
if let Some(format) = &o.format {
|
if let Some(format) = &o.format {
|
||||||
if format == "uuid" {
|
if format == "uuid" {
|
||||||
return Ok(Some((index, format!(r#"${{{}:"tag_or_edge_fn"}}"#, index))));
|
return Ok(Some((index, format!(r#"${{{}:"tag_or_edge_fn"}}"#, index))));
|
||||||
} else if format == "double" || format == "uint" || format == "int64" || format == "uint32" {
|
} else if format == "double"
|
||||||
|
|| format == "uint"
|
||||||
|
|| format == "int64"
|
||||||
|
|| format == "uint32"
|
||||||
|
|| format == "uint64"
|
||||||
|
{
|
||||||
return Ok(Some((index, format!(r#"${{{}:3.14}}"#, index))));
|
return Ok(Some((index, format!(r#"${{{}:3.14}}"#, index))));
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("unknown format: {}", format);
|
anyhow::bail!("unknown format: {}", format);
|
||||||
@ -614,7 +624,12 @@ pub fn get_autocomplete_string_from_schema(schema: &schemars::schema::Schema) ->
|
|||||||
if let Some(format) = &o.format {
|
if let Some(format) = &o.format {
|
||||||
if format == "uuid" {
|
if format == "uuid" {
|
||||||
return Ok(Primitive::Uuid.to_string());
|
return Ok(Primitive::Uuid.to_string());
|
||||||
} else if format == "double" || format == "uint" || format == "int64" || format == "uint32" {
|
} else if format == "double"
|
||||||
|
|| format == "uint"
|
||||||
|
|| format == "int64"
|
||||||
|
|| format == "uint32"
|
||||||
|
|| format == "uint64"
|
||||||
|
{
|
||||||
return Ok(Primitive::Number.to_string());
|
return Ok(Primitive::Number.to_string());
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("unknown format: {}", format);
|
anyhow::bail!("unknown format: {}", format);
|
||||||
|
@ -11,6 +11,7 @@ pub mod revolve;
|
|||||||
pub mod segment;
|
pub mod segment;
|
||||||
pub mod shapes;
|
pub mod shapes;
|
||||||
pub mod sketch;
|
pub mod sketch;
|
||||||
|
pub mod types;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use crate::{
|
use crate::{
|
||||||
errors::{KclError, KclErrorDetails},
|
errors::{KclError, KclErrorDetails},
|
||||||
executor::{ExtrudeGroup, Geometries, Geometry, MemoryItem, SketchGroup, SketchGroupSet},
|
executor::{ExtrudeGroup, Geometries, Geometry, MemoryItem, SketchGroup, SketchGroupSet},
|
||||||
std::Args,
|
std::{types::Uint, Args},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Data for a linear pattern on a 2D sketch.
|
/// Data for a linear pattern on a 2D sketch.
|
||||||
@ -20,7 +20,7 @@ pub struct LinearPattern2dData {
|
|||||||
/// The number of repetitions. Must be greater than 0.
|
/// The number of repetitions. Must be greater than 0.
|
||||||
/// This excludes the original entity. For example, if `repetitions` is 1,
|
/// This excludes the original entity. For example, if `repetitions` is 1,
|
||||||
/// the original entity will be copied once.
|
/// the original entity will be copied once.
|
||||||
pub repetitions: u32,
|
pub repetitions: Uint,
|
||||||
/// The distance between each repetition. This can also be referred to as spacing.
|
/// The distance between each repetition. This can also be referred to as spacing.
|
||||||
pub distance: f64,
|
pub distance: f64,
|
||||||
/// The axis of the pattern. This is a 2D vector.
|
/// The axis of the pattern. This is a 2D vector.
|
||||||
@ -35,7 +35,7 @@ pub struct LinearPattern3dData {
|
|||||||
/// The number of repetitions. Must be greater than 0.
|
/// The number of repetitions. Must be greater than 0.
|
||||||
/// This excludes the original entity. For example, if `repetitions` is 1,
|
/// This excludes the original entity. For example, if `repetitions` is 1,
|
||||||
/// the original entity will be copied once.
|
/// the original entity will be copied once.
|
||||||
pub repetitions: u32,
|
pub repetitions: Uint,
|
||||||
/// The distance between each repetition. This can also be referred to as spacing.
|
/// The distance between each repetition. This can also be referred to as spacing.
|
||||||
pub distance: f64,
|
pub distance: f64,
|
||||||
/// The axis of the pattern.
|
/// The axis of the pattern.
|
||||||
@ -57,8 +57,8 @@ impl LinearPattern {
|
|||||||
|
|
||||||
pub fn repetitions(&self) -> u32 {
|
pub fn repetitions(&self) -> u32 {
|
||||||
match self {
|
match self {
|
||||||
LinearPattern::TwoD(lp) => lp.repetitions,
|
LinearPattern::TwoD(lp) => lp.repetitions.u32(),
|
||||||
LinearPattern::ThreeD(lp) => lp.repetitions,
|
LinearPattern::ThreeD(lp) => lp.repetitions.u32(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,6 +185,19 @@ async fn inner_pattern_linear_3d(
|
|||||||
|
|
||||||
async fn pattern_linear(data: LinearPattern, geometry: Geometry, args: Args) -> Result<Geometries, KclError> {
|
async fn pattern_linear(data: LinearPattern, geometry: Geometry, args: Args) -> Result<Geometries, KclError> {
|
||||||
let id = uuid::Uuid::new_v4();
|
let id = uuid::Uuid::new_v4();
|
||||||
|
println!(
|
||||||
|
"id: {:#?}",
|
||||||
|
ModelingCmd::EntityLinearPattern {
|
||||||
|
axis: kittycad::types::Point3D {
|
||||||
|
x: data.axis()[0],
|
||||||
|
y: data.axis()[1],
|
||||||
|
z: data.axis()[2],
|
||||||
|
},
|
||||||
|
entity_id: geometry.id(),
|
||||||
|
num_repetitions: data.repetitions(),
|
||||||
|
spacing: data.distance(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
let resp = args
|
let resp = args
|
||||||
.send_modeling_cmd(
|
.send_modeling_cmd(
|
||||||
@ -244,7 +257,7 @@ pub struct CircularPattern2dData {
|
|||||||
/// The number of repetitions. Must be greater than 0.
|
/// The number of repetitions. Must be greater than 0.
|
||||||
/// This excludes the original entity. For example, if `repetitions` is 1,
|
/// This excludes the original entity. For example, if `repetitions` is 1,
|
||||||
/// the original entity will be copied once.
|
/// the original entity will be copied once.
|
||||||
pub repetitions: u32,
|
pub repetitions: Uint,
|
||||||
/// The center about which to make the pattern. This is a 2D vector.
|
/// The center about which to make the pattern. This is a 2D vector.
|
||||||
pub center: [f64; 2],
|
pub center: [f64; 2],
|
||||||
/// The arc angle (in degrees) to place the repetitions. Must be greater than 0.
|
/// The arc angle (in degrees) to place the repetitions. Must be greater than 0.
|
||||||
@ -261,7 +274,7 @@ pub struct CircularPattern3dData {
|
|||||||
/// The number of repetitions. Must be greater than 0.
|
/// The number of repetitions. Must be greater than 0.
|
||||||
/// This excludes the original entity. For example, if `repetitions` is 1,
|
/// This excludes the original entity. For example, if `repetitions` is 1,
|
||||||
/// the original entity will be copied once.
|
/// the original entity will be copied once.
|
||||||
pub repetitions: u32,
|
pub repetitions: Uint,
|
||||||
/// The axis around which to make the pattern. This is a 3D vector.
|
/// The axis around which to make the pattern. This is a 3D vector.
|
||||||
pub axis: [f64; 3],
|
pub axis: [f64; 3],
|
||||||
/// The center about which to make the pattern. This is a 3D vector.
|
/// The center about which to make the pattern. This is a 3D vector.
|
||||||
@ -294,8 +307,8 @@ impl CircularPattern {
|
|||||||
|
|
||||||
pub fn repetitions(&self) -> u32 {
|
pub fn repetitions(&self) -> u32 {
|
||||||
match self {
|
match self {
|
||||||
CircularPattern::TwoD(lp) => lp.repetitions,
|
CircularPattern::TwoD(lp) => lp.repetitions.u32(),
|
||||||
CircularPattern::ThreeD(lp) => lp.repetitions,
|
CircularPattern::ThreeD(lp) => lp.repetitions.u32(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
src/wasm-lib/kcl/src/std/types.rs
Normal file
40
src/wasm-lib/kcl/src/std/types.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//! Custom types for various standard library types.
|
||||||
|
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// A Uint that allows us to do math but rounds to a uint.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, ts_rs::TS)]
|
||||||
|
#[ts(export)]
|
||||||
|
pub struct Uint(f64);
|
||||||
|
|
||||||
|
impl Uint {
|
||||||
|
pub fn new(value: f64) -> Self {
|
||||||
|
if value < 0.0 {
|
||||||
|
panic!("Uint cannot be negative");
|
||||||
|
}
|
||||||
|
Self(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(&self) -> f64 {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn u32(&self) -> u32 {
|
||||||
|
self.0.round() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn u64(&self) -> u64 {
|
||||||
|
self.0.round() as u64
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl JsonSchema for Uint {
|
||||||
|
fn schema_name() -> String {
|
||||||
|
"Uint".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
|
||||||
|
gen.subschema_for::<u32>()
|
||||||
|
}
|
||||||
|
}
|
@ -939,11 +939,32 @@ async fn serial_test_top_level_expression() {
|
|||||||
twenty_twenty::assert_image("tests/executor/outputs/top_level_expression.png", &result, 0.999);
|
twenty_twenty::assert_image("tests/executor/outputs/top_level_expression.png", &result, 0.999);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
|
async fn serial_test_patterns_linear_basic_with_math() {
|
||||||
|
let code = r#"const num = 12
|
||||||
|
const distance = 5
|
||||||
|
const part = startSketchOn('XY')
|
||||||
|
|> circle([0,0], 2, %)
|
||||||
|
|> patternLinear2d({axis: [0,1], repetitions: num -1, distance: distance - 1}, %)
|
||||||
|
|> extrude(1, %)
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let result = execute_and_snapshot(code, kittycad::types::UnitLength::Mm)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
twenty_twenty::assert_image(
|
||||||
|
"tests/executor/outputs/patterns_linear_basic_with_math.png",
|
||||||
|
&result,
|
||||||
|
0.999,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread")]
|
#[tokio::test(flavor = "multi_thread")]
|
||||||
async fn serial_test_patterns_linear_basic() {
|
async fn serial_test_patterns_linear_basic() {
|
||||||
let code = r#"const part = startSketchOn('XY')
|
let code = r#"const part = startSketchOn('XY')
|
||||||
|> circle([0,0], 2, %)
|
|> circle([0,0], 2, %)
|
||||||
|> patternLinear2d({axis: [0,1], repetitions: 12, distance: 2}, %)
|
|> patternLinear2d({axis: [0,1], repetitions: 12, distance: 4}, %)
|
||||||
|
|> extrude(1, %)
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let result = execute_and_snapshot(code, kittycad::types::UnitLength::Mm)
|
let result = execute_and_snapshot(code, kittycad::types::UnitLength::Mm)
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 114 KiB |
Binary file not shown.
After Width: | Height: | Size: 112 KiB |
Reference in New Issue
Block a user