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:
Jess Frazelle
2024-07-28 22:45:40 -07:00
committed by GitHub
parent 57d4204f47
commit 8fe91259fa
13 changed files with 178 additions and 9 deletions

View File

@ -33,7 +33,7 @@ const example = extrude(10, exampleSketch)
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
```js
{
// The angle of the line.
// The angle of the line (in degrees).
angle: number,
// The length of the line.
length: number,

View File

@ -32,7 +32,7 @@ const extrusion = extrude(10, sketch001)
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
```js
{
// The angle of the line.
// The angle of the line (in degrees).
angle: number,
// The length of the line.
length: number,

View File

@ -34,7 +34,7 @@ const example = extrude(10, exampleSketch)
* `data`: `AngledLineData` - Data to draw an angled line. (REQUIRED)
```js
{
// The angle of the line.
// The angle of the line (in degrees).
angle: number,
// The length of the line.
length: number,

View File

@ -62,6 +62,7 @@ layout: manual
* [`patternLinear3d`](kcl/patternLinear3d)
* [`patternTransform`](kcl/patternTransform)
* [`pi`](kcl/pi)
* [`polar`](kcl/polar)
* [`pow`](kcl/pow)
* [`profileStart`](kcl/profileStart)
* [`profileStartX`](kcl/profileStartX)

48
docs/kcl/polar.md Normal file

File diff suppressed because one or more lines are too long

View File

@ -7642,7 +7642,7 @@
],
"properties": {
"angle": {
"description": "The angle of the line.",
"description": "The angle of the line (in degrees).",
"type": "number",
"format": "double"
},
@ -14307,7 +14307,7 @@
],
"properties": {
"angle": {
"description": "The angle of the line.",
"description": "The angle of the line (in degrees).",
"type": "number",
"format": "double"
},
@ -20972,7 +20972,7 @@
],
"properties": {
"angle": {
"description": "The angle of the line.",
"description": "The angle of the line (in degrees).",
"type": "number",
"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)"
]
},
{
"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",
"summary": "Computes the number to a power.",

View File

@ -1391,7 +1391,7 @@ dependencies = [
[[package]]
name = "kcl-lib"
version = "0.2.1"
version = "0.2.2"
dependencies = [
"anyhow",
"approx",

View File

@ -1,7 +1,7 @@
[package]
name = "kcl-lib"
description = "KittyCAD Language implementation and tools"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
license = "MIT"
repository = "https://github.com/KittyCAD/modeling-app"

View File

@ -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> {
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::sketch::SketchData);
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!(String);
impl_from_arg_via_json!(u32);

View File

@ -11,6 +11,7 @@ pub mod import;
pub mod kcl_stdlib;
pub mod math;
pub mod patterns;
pub mod polar;
pub mod revolve;
pub mod segment;
pub mod shapes;
@ -117,6 +118,7 @@ lazy_static! {
Box::new(crate::std::math::Ln),
Box::new(crate::std::math::ToDegrees),
Box::new(crate::std::math::ToRadians),
Box::new(crate::std::polar::Polar),
Box::new(crate::std::assert::Assert),
Box::new(crate::std::assert::AssertLessThan),
Box::new(crate::std::assert::AssertGreaterThan),

View 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])
}

View File

@ -413,7 +413,7 @@ async fn inner_y_line(
pub enum AngledLineData {
/// An angle and length with explicitly named parameters
AngleAndLengthNamed {
/// The angle of the line.
/// The angle of the line (in degrees).
angle: f64,
/// The length of the line.
length: f64,

Binary file not shown.

After

Width:  |  Height:  |  Size: 129 KiB