Implement polar std function in KCL (#6180)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-04-08 08:16:43 +12:00
committed by GitHub
parent 1bf7a2abd4
commit ababe24b97
10 changed files with 67 additions and 212 deletions

View File

@ -101,7 +101,6 @@ layout: manual
* [`patternLinear3d`](kcl/patternLinear3d)
* [`patternTransform`](kcl/patternTransform)
* [`patternTransform2d`](kcl/patternTransform2d)
* [`polar`](kcl/polar)
* [`polygon`](kcl/polygon)
* [`pop`](kcl/pop)
* [`pow`](kcl/pow)
@ -143,6 +142,7 @@ layout: manual
* [`PI`](kcl/consts/std-math-PI)
* [`TAU`](kcl/consts/std-math-TAU)
* [`cos`](kcl/std-math-cos)
* [`polar`](kcl/std-math-polar)
* [`sin`](kcl/std-math-sin)
* [`tan`](kcl/std-math-tan)
* **std::sketch**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -201537,67 +201537,6 @@
"circumference = 70\n\nexampleSketch = startSketchOn(XZ)\n |> circle(center = [0, 0], radius = circumference / (2 * pi()))\n\nexample = extrude(exampleSketch, length = 5)"
]
},
{
"name": "polar",
"summary": "Convert polar/sphere (azimuth, elevation, distance) coordinates to cartesian (x/y/z grid) coordinates.",
"description": "",
"tags": [],
"keywordArguments": false,
"args": [
{
"name": "data",
"type": "PolarCoordsData",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "PolarCoordsData",
"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,
"includeInSnippet": true,
"labelRequired": true
}
],
"returnValue": {
"name": "",
"type": "[number]",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "Array_size_2_of_double",
"type": "array",
"items": {
"type": "number",
"format": "double"
},
"maxItems": 2,
"minItems": 2
},
"required": true,
"includeInSnippet": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"exampleSketch = startSketchOn(XZ)\n |> startProfileAt([0, 0], %)\n |> line(end = polar({ angle = 30, length = 5 }), tag = $thing)\n |> line(end = [0, 5])\n |> line(end = [segEndX(thing), 0])\n |> line(end = [-20, 10])\n |> close()\n\nexample = extrude(exampleSketch, length = 5)"
]
},
{
"name": "polygon",
"summary": "Create a regular polygon with the specified number of sides that is either inscribed or circumscribed around a circle of the specified radius.",

View File

@ -1,22 +0,0 @@
---
title: "PolarCoordsData"
excerpt: "Data for polar coordinates."
layout: manual
---
Data for polar coordinates.
**Type:** `object`
## Properties
| Property | Type | Description | Required |
|----------|------|-------------|----------|
| `angle` |[`number`](/docs/kcl/types/number)| The angle of the line (in degrees). | No |
| `length` |[`number`](/docs/kcl/types/number)| The length of the line. | No |

View File

@ -542,25 +542,6 @@ impl Args {
)
}
pub(super) fn make_user_val_from_f64_array(&self, f: Vec<f64>, ty: &NumericType) -> Result<KclValue, KclError> {
let array = f
.into_iter()
.map(|n| KclValue::Number {
value: n,
meta: vec![Metadata {
source_range: self.source_range,
}],
ty: ty.clone(),
})
.collect::<Vec<_>>();
Ok(KclValue::MixedArray {
value: array,
meta: vec![Metadata {
source_range: self.source_range,
}],
})
}
pub(crate) fn get_number(&self) -> Result<f64, KclError> {
FromArgs::from_args(self, 0)
}
@ -1170,15 +1151,6 @@ impl<'a> FromKclValue<'a> for super::shapes::PolygonData {
}
}
impl<'a> FromKclValue<'a> for crate::std::polar::PolarCoordsData {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
let obj = arg.as_object()?;
let_field_of!(obj, angle);
let_field_of!(obj, length);
Some(Self { angle, length })
}
}
impl<'a> FromKclValue<'a> for crate::execution::Plane {
fn from_kcl_val(arg: &'a KclValue) -> Option<Self> {
arg.as_plane().cloned()

View File

@ -18,7 +18,6 @@ pub mod math;
pub mod mirror;
pub mod patterns;
pub mod planes;
pub mod polar;
pub mod revolve;
pub mod segment;
pub mod shapes;
@ -145,7 +144,6 @@ lazy_static! {
Box::new(crate::std::units::M),
Box::new(crate::std::units::Cm),
Box::new(crate::std::units::Yd),
Box::new(crate::std::polar::Polar),
Box::new(crate::std::assert::Assert),
Box::new(crate::std::assert::AssertEqual),
Box::new(crate::std::assert::AssertLessThan),

View File

@ -1,55 +0,0 @@
//! Functions related to polar coordinates.
use anyhow::Result;
use kcl_derive_docs::stdlib;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
errors::KclError,
execution::{ExecState, KclValue},
std::args::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(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let data: PolarCoordsData = args.get_data()?;
let result = inner_polar(&data)?;
args.make_user_val_from_f64_array(result.to_vec(), &crate::execution::types::NumericType::Unknown)
}
/// Convert polar/sphere (azimuth, elevation, distance) coordinates to
/// cartesian (x/y/z grid) coordinates.
///
/// ```no_run
/// exampleSketch = startSketchOn('XZ')
/// |> startProfileAt([0, 0], %)
/// |> line(end = polar({angle: 30, length: 5}), tag = $thing)
/// |> line(end = [0, 5])
/// |> line(end = [segEndX(thing), 0])
/// |> line(end = [-20, 10])
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
#[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])
}

View File

@ -95,3 +95,25 @@ export fn sin(@num: number(rad)): number(_) {}
/// ```
@(impl = std_rust)
export fn tan(@num: number(rad)): number(_) {}
/// Convert polar/sphere (azimuth, elevation, distance) coordinates to
/// cartesian (x/y/z grid) coordinates.
///
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfileAt([0, 0], %)
/// |> line(end = polar(angle = 30, length = 5), tag = $thing)
/// |> line(end = [0, 5])
/// |> line(end = [segEndX(thing), 0])
/// |> line(end = [-20, 10])
/// |> close()
///
/// example = extrude(exampleSketch, length = 5)
/// ```
export fn polar(angle: number(deg), length: number(mm)): [number(mm); 2] {
// TODO could be done by implicit conversion when UoM coercions are activated.
rads = toRadians(angle)
x = length * cos(rads)
y = length * sin(rads)
return [x, y]
}

View File

Before

Width:  |  Height:  |  Size: 59 KiB

After

Width:  |  Height:  |  Size: 59 KiB