Use kittycad::Angle instead of our own (#934)

This commit is contained in:
Adam Chalmers
2023-10-24 16:32:41 -07:00
committed by GitHub
parent 6dc4fbc808
commit f6f1574982
5 changed files with 50 additions and 88 deletions

View File

@ -1425,9 +1425,9 @@ dependencies = [
[[package]]
name = "kittycad"
version = "0.2.40"
version = "0.2.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f51500d4d8e7a4eb4b9002e65bff51b3ee85d2c0ac137c6b2ccd8381d81e6423"
checksum = "874914cd40bfd43674406683bb3f0924d41780698a4ade96f2e180a73678bdd1"
dependencies = [
"anyhow",
"async-trait",

View File

@ -54,7 +54,7 @@ members = [
]
[workspace.dependencies]
kittycad = { version = "0.2.40", default-features = false, features = ["js"] }
kittycad = { version = "0.2.41", default-features = false, features = ["js"] }
[[test]]
name = "executor"

View File

@ -7,9 +7,11 @@ use schemars::JsonSchema;
use crate::{
errors::{KclError, KclErrorDetails},
executor::{MemoryItem, SketchGroup},
std::{utils::Angle, Args},
std::Args,
};
use super::utils::between;
/// Returns the segment end of x.
pub async fn segment_end_x(args: Args) -> Result<MemoryItem, KclError> {
let (segment_name, sketch_group) = args.get_segment_name_sketch_group()?;
@ -174,7 +176,7 @@ fn inner_segment_angle(segment_name: &str, sketch_group: Box<SketchGroup>, args:
})?;
let line = path.get_base();
let result = Angle::between(line.from.into(), line.to.into());
let result = between(line.from.into(), line.to.into());
Ok(result.degrees())
}

View File

@ -2,11 +2,10 @@
use anyhow::Result;
use derive_docs::stdlib;
use kittycad::types::{ModelingCmd, Point3D};
use kittycad::types::{Angle, ModelingCmd, Point3D};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use super::utils::Angle;
use crate::{
errors::{KclError, KclErrorDetails},
executor::{
@ -1033,8 +1032,8 @@ async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) ->
segment: kittycad::types::PathSegment::Arc {
angle_start: angle_start.degrees(),
angle_end: angle_end.degrees(),
start: Some(angle_start.into()),
end: Some(angle_end.into()),
start: Some(angle_start),
end: Some(angle_end),
center: center.into(),
radius,
relative: false,

View File

@ -1,96 +1,57 @@
use std::f64::consts::PI;
use kittycad::types::UnitAngle;
use kittycad::types::Angle;
use crate::{
errors::{KclError, KclErrorDetails},
executor::{Point2d, SourceRange},
};
#[derive(Clone, Copy, Default, PartialOrd, PartialEq, Debug)]
pub struct Angle {
degrees: f64,
/// Get the angle between these points
pub fn between(a: Point2d, b: Point2d) -> Angle {
let x = b.x - a.x;
let y = b.y - a.y;
normalize(Angle::from_radians(y.atan2(x)))
}
impl From<kittycad::types::Angle> for Angle {
fn from(angle: kittycad::types::Angle) -> Self {
match angle.unit {
kittycad::types::UnitAngle::Degrees => Self::from_degrees(angle.value),
kittycad::types::UnitAngle::Radians => Self::from_radians(angle.value),
}
}
/// Normalize the angle
pub fn normalize(angle: Angle) -> Angle {
let deg = angle.degrees();
let result = ((deg % 360.0) + 360.0) % 360.0;
Angle::from_degrees(if result > 180.0 { result - 360.0 } else { result })
}
impl From<Angle> for kittycad::types::Angle {
fn from(value: Angle) -> Self {
Self {
unit: UnitAngle::Degrees,
value: value.degrees,
}
}
}
impl Angle {
const ZERO: Self = Self { degrees: 0.0 };
/// Make an angle of the given degrees.
pub fn from_degrees(degrees: f64) -> Self {
Self { degrees }
}
/// Make an angle of the given radians.
pub fn from_radians(radians: f64) -> Self {
Self::from_degrees(radians.to_degrees())
}
/// Get the angle in degrees
pub fn degrees(&self) -> f64 {
self.degrees
}
/// Get the angle in radians
pub fn radians(&self) -> f64 {
self.degrees.to_radians()
}
/// Get the angle between these points
pub fn between(a: Point2d, b: Point2d) -> Self {
let x = b.x - a.x;
let y = b.y - a.y;
Self::from_radians(y.atan2(x)).normalize()
}
/// Normalize the angle
pub fn normalize(self) -> Self {
let angle = self.degrees();
let result = ((angle % 360.0) + 360.0) % 360.0;
Self::from_degrees(if result > 180.0 { result - 360.0 } else { result })
}
/// Gives the ▲-angle between from and to angles (shortest path), use radians.
///
/// Sign of the returned angle denotes direction, positive means counterClockwise 🔄
/// # Examples
///
/// ```
/// use std::f64::consts::PI;
///
/// use kcl_lib::std::utils::Angle;
///
/// assert_eq!(
/// Angle::delta(Angle::from_radians(PI / 8.0), Angle::from_radians(PI / 4.0)),
/// Angle::from_radians(PI / 8.0)
/// );
/// ```
#[allow(dead_code)]
pub fn delta(from_angle: Self, to_angle: Self) -> Self {
let norm_from_angle = normalize_rad(from_angle.radians());
let norm_to_angle = normalize_rad(to_angle.radians());
let provisional = norm_to_angle - norm_from_angle;
/// Gives the ▲-angle between from and to angles (shortest path), use radians.
///
/// Sign of the returned angle denotes direction, positive means counterClockwise 🔄
/// # Examples
///
/// ```
/// use std::f64::consts::PI;
///
/// use kcl_lib::std::utils::Angle;
///
/// assert_eq!(
/// Angle::delta(Angle::from_radians(PI / 8.0), Angle::from_radians(PI / 4.0)),
/// Angle::from_radians(PI / 8.0)
/// );
/// ```
#[allow(dead_code)]
pub fn delta(from_angle: Angle, to_angle: Angle) -> Angle {
let norm_from_angle = normalize_rad(from_angle.radians());
let norm_to_angle = normalize_rad(to_angle.radians());
let provisional = norm_to_angle - norm_from_angle;
if provisional > -PI && provisional <= PI {
return Angle::from_radians(provisional);
}
if provisional > PI {
return Angle::from_radians(provisional - 2.0 * PI);
}
if provisional < -PI {
return Angle::from_radians(provisional + 2.0 * PI);
}
Angle::ZERO
if provisional > -PI && provisional <= PI {
return Angle::from_radians(provisional);
}
if provisional > PI {
return Angle::from_radians(provisional - 2.0 * PI);
}
if provisional < -PI {
return Angle::from_radians(provisional + 2.0 * PI);
}
Angle::ZERO
}
#[allow(dead_code)]