add polar function (#3158)
* add polarCoords fn Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates; Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -33,7 +33,7 @@ const example = extrude(10, exampleSketch)
|
|||||||
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
// The angle of the line.
|
// The angle of the line (in degrees).
|
||||||
angle: number,
|
angle: number,
|
||||||
// The length of the line.
|
// The length of the line.
|
||||||
length: number,
|
length: number,
|
||||||
|
@ -32,7 +32,7 @@ const extrusion = extrude(10, sketch001)
|
|||||||
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
// The angle of the line.
|
// The angle of the line (in degrees).
|
||||||
angle: number,
|
angle: number,
|
||||||
// The length of the line.
|
// The length of the line.
|
||||||
length: number,
|
length: number,
|
||||||
|
@ -34,7 +34,7 @@ const example = extrude(10, exampleSketch)
|
|||||||
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
|
||||||
```js
|
```js
|
||||||
{
|
{
|
||||||
// The angle of the line.
|
// The angle of the line (in degrees).
|
||||||
angle: number,
|
angle: number,
|
||||||
// The length of the line.
|
// The length of the line.
|
||||||
length: number,
|
length: number,
|
||||||
|
@ -62,6 +62,7 @@ layout: manual
|
|||||||
* [`patternLinear3d`](kcl/patternLinear3d)
|
* [`patternLinear3d`](kcl/patternLinear3d)
|
||||||
* [`patternTransform`](kcl/patternTransform)
|
* [`patternTransform`](kcl/patternTransform)
|
||||||
* [`pi`](kcl/pi)
|
* [`pi`](kcl/pi)
|
||||||
|
* [`polar`](kcl/polar)
|
||||||
* [`pow`](kcl/pow)
|
* [`pow`](kcl/pow)
|
||||||
* [`profileStart`](kcl/profileStart)
|
* [`profileStart`](kcl/profileStart)
|
||||||
* [`profileStartX`](kcl/profileStartX)
|
* [`profileStartX`](kcl/profileStartX)
|
||||||
|
48
docs/kcl/polar.md
Normal file
48
docs/kcl/polar.md
Normal file
File diff suppressed because one or more lines are too long
@ -7642,7 +7642,7 @@
|
|||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"angle": {
|
"angle": {
|
||||||
"description": "The angle of the line.",
|
"description": "The angle of the line (in degrees).",
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"format": "double"
|
"format": "double"
|
||||||
},
|
},
|
||||||
@ -14307,7 +14307,7 @@
|
|||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"angle": {
|
"angle": {
|
||||||
"description": "The angle of the line.",
|
"description": "The angle of the line (in degrees).",
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"format": "double"
|
"format": "double"
|
||||||
},
|
},
|
||||||
@ -20972,7 +20972,7 @@
|
|||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"angle": {
|
"angle": {
|
||||||
"description": "The angle of the line.",
|
"description": "The angle of the line (in degrees).",
|
||||||
"type": "number",
|
"type": "number",
|
||||||
"format": "double"
|
"format": "double"
|
||||||
},
|
},
|
||||||
@ -171849,6 +171849,58 @@
|
|||||||
"const circumference = 70\n\nconst exampleSketch = startSketchOn(\"XZ\")\n |> circle([0, 0], circumference / (2 * pi()), %)\n\nconst example = extrude(5, exampleSketch)"
|
"const circumference = 70\n\nconst exampleSketch = startSketchOn(\"XZ\")\n |> circle([0, 0], circumference / (2 * pi()), %)\n\nconst example = extrude(5, exampleSketch)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "polar",
|
||||||
|
"summary": "Convert from polar/sphere coordinates to cartesian coordinates.",
|
||||||
|
"description": "",
|
||||||
|
"tags": [],
|
||||||
|
"args": [
|
||||||
|
{
|
||||||
|
"name": "data",
|
||||||
|
"type": "PolarCoordsData",
|
||||||
|
"schema": {
|
||||||
|
"description": "Data for polar coordinates.",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"angle",
|
||||||
|
"length"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"angle": {
|
||||||
|
"description": "The angle of the line (in degrees).",
|
||||||
|
"type": "number",
|
||||||
|
"format": "double"
|
||||||
|
},
|
||||||
|
"length": {
|
||||||
|
"description": "The length of the line.",
|
||||||
|
"type": "number",
|
||||||
|
"format": "double"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"returnValue": {
|
||||||
|
"name": "",
|
||||||
|
"type": "[number]",
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "number",
|
||||||
|
"format": "double"
|
||||||
|
},
|
||||||
|
"maxItems": 2,
|
||||||
|
"minItems": 2
|
||||||
|
},
|
||||||
|
"required": true
|
||||||
|
},
|
||||||
|
"unpublished": false,
|
||||||
|
"deprecated": false,
|
||||||
|
"examples": [
|
||||||
|
"const exampleSketch = startSketchOn('XZ')\n |> startProfileAt([0, 0], %)\n |> line(polar({ angle: 30, length: 5 }), %, $thing)\n |> line([0, 5], %)\n |> line([segEndX(thing), 0], %)\n |> line([-20, 10], %)\n |> close(%)\n\nconst example = extrude(5, exampleSketch)"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "pow",
|
"name": "pow",
|
||||||
"summary": "Computes the number to a power.",
|
"summary": "Computes the number to a power.",
|
||||||
|
2
src/wasm-lib/Cargo.lock
generated
2
src/wasm-lib/Cargo.lock
generated
@ -1391,7 +1391,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kcl-lib"
|
name = "kcl-lib"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"approx",
|
"approx",
|
||||||
|
@ -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.2.1"
|
version = "0.2.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
repository = "https://github.com/KittyCAD/modeling-app"
|
repository = "https://github.com/KittyCAD/modeling-app"
|
||||||
|
@ -188,6 +188,21 @@ impl Args {
|
|||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn make_user_val_from_f64_array(&self, f: Vec<f64>) -> Result<MemoryItem, KclError> {
|
||||||
|
let mut arr = Vec::new();
|
||||||
|
for n in f {
|
||||||
|
arr.push(serde_json::Value::Number(serde_json::Number::from_f64(n).ok_or_else(
|
||||||
|
|| {
|
||||||
|
KclError::Type(KclErrorDetails {
|
||||||
|
message: format!("Failed to convert `{}` to a number", n),
|
||||||
|
source_ranges: vec![self.source_range],
|
||||||
|
})
|
||||||
|
},
|
||||||
|
)?));
|
||||||
|
}
|
||||||
|
self.make_user_val_from_json(serde_json::Value::Array(arr))
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn get_number(&self) -> Result<f64, KclError> {
|
pub(crate) fn get_number(&self) -> Result<f64, KclError> {
|
||||||
FromArgs::from_args(self, 0)
|
FromArgs::from_args(self, 0)
|
||||||
}
|
}
|
||||||
@ -586,6 +601,7 @@ impl_from_arg_via_json!(super::fillet::FilletData);
|
|||||||
impl_from_arg_via_json!(super::revolve::RevolveData);
|
impl_from_arg_via_json!(super::revolve::RevolveData);
|
||||||
impl_from_arg_via_json!(super::sketch::SketchData);
|
impl_from_arg_via_json!(super::sketch::SketchData);
|
||||||
impl_from_arg_via_json!(crate::std::import::ImportFormat);
|
impl_from_arg_via_json!(crate::std::import::ImportFormat);
|
||||||
|
impl_from_arg_via_json!(crate::std::polar::PolarCoordsData);
|
||||||
impl_from_arg_via_json!(FaceTag);
|
impl_from_arg_via_json!(FaceTag);
|
||||||
impl_from_arg_via_json!(String);
|
impl_from_arg_via_json!(String);
|
||||||
impl_from_arg_via_json!(u32);
|
impl_from_arg_via_json!(u32);
|
||||||
|
@ -11,6 +11,7 @@ pub mod import;
|
|||||||
pub mod kcl_stdlib;
|
pub mod kcl_stdlib;
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod patterns;
|
pub mod patterns;
|
||||||
|
pub mod polar;
|
||||||
pub mod revolve;
|
pub mod revolve;
|
||||||
pub mod segment;
|
pub mod segment;
|
||||||
pub mod shapes;
|
pub mod shapes;
|
||||||
@ -117,6 +118,7 @@ lazy_static! {
|
|||||||
Box::new(crate::std::math::Ln),
|
Box::new(crate::std::math::Ln),
|
||||||
Box::new(crate::std::math::ToDegrees),
|
Box::new(crate::std::math::ToDegrees),
|
||||||
Box::new(crate::std::math::ToRadians),
|
Box::new(crate::std::math::ToRadians),
|
||||||
|
Box::new(crate::std::polar::Polar),
|
||||||
Box::new(crate::std::assert::Assert),
|
Box::new(crate::std::assert::Assert),
|
||||||
Box::new(crate::std::assert::AssertLessThan),
|
Box::new(crate::std::assert::AssertLessThan),
|
||||||
Box::new(crate::std::assert::AssertGreaterThan),
|
Box::new(crate::std::assert::AssertGreaterThan),
|
||||||
|
50
src/wasm-lib/kcl/src/std/polar.rs
Normal file
50
src/wasm-lib/kcl/src/std/polar.rs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
//! Functions related to polar coordinates.
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
use derive_docs::stdlib;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::{errors::KclError, executor::MemoryItem, std::Args};
|
||||||
|
|
||||||
|
/// Data for polar coordinates.
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
|
#[ts(export)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct PolarCoordsData {
|
||||||
|
/// The angle of the line (in degrees).
|
||||||
|
pub angle: f64,
|
||||||
|
/// The length of the line.
|
||||||
|
pub length: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert from polar/sphere coordinates to cartesian coordinates.
|
||||||
|
pub async fn polar(args: Args) -> Result<MemoryItem, KclError> {
|
||||||
|
let data: PolarCoordsData = args.get_data()?;
|
||||||
|
let result = inner_polar(&data)?;
|
||||||
|
|
||||||
|
args.make_user_val_from_f64_array(result.to_vec())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert from polar/sphere coordinates to cartesian coordinates.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// const exampleSketch = startSketchOn('XZ')
|
||||||
|
/// |> startProfileAt([0, 0], %)
|
||||||
|
/// |> line(polar({angle: 30, length: 5}), %, $thing)
|
||||||
|
/// |> line([0, 5], %)
|
||||||
|
/// |> line([segEndX(thing), 0], %)
|
||||||
|
/// |> line([-20, 10], %)
|
||||||
|
/// |> close(%)
|
||||||
|
///
|
||||||
|
/// const example = extrude(5, exampleSketch)
|
||||||
|
/// ```
|
||||||
|
#[stdlib {
|
||||||
|
name = "polar",
|
||||||
|
}]
|
||||||
|
fn inner_polar(data: &PolarCoordsData) -> Result<[f64; 2], KclError> {
|
||||||
|
let angle = data.angle.to_radians();
|
||||||
|
let x = data.length * angle.cos();
|
||||||
|
let y = data.length * angle.sin();
|
||||||
|
Ok([x, y])
|
||||||
|
}
|
@ -413,7 +413,7 @@ async fn inner_y_line(
|
|||||||
pub enum AngledLineData {
|
pub enum AngledLineData {
|
||||||
/// An angle and length with explicitly named parameters
|
/// An angle and length with explicitly named parameters
|
||||||
AngleAndLengthNamed {
|
AngleAndLengthNamed {
|
||||||
/// The angle of the line.
|
/// The angle of the line (in degrees).
|
||||||
angle: f64,
|
angle: f64,
|
||||||
/// The length of the line.
|
/// The length of the line.
|
||||||
length: f64,
|
length: f64,
|
||||||
|
BIN
src/wasm-lib/kcl/tests/outputs/serial_test_example_polar0.png
Normal file
BIN
src/wasm-lib/kcl/tests/outputs/serial_test_example_polar0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 129 KiB |
Reference in New Issue
Block a user