Implement polar std function in KCL (#6180)
Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -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()
|
||||
|
@ -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),
|
||||
|
@ -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])
|
||||
}
|
Reference in New Issue
Block a user