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]] [[package]]
name = "kittycad" name = "kittycad"
version = "0.2.40" version = "0.2.41"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f51500d4d8e7a4eb4b9002e65bff51b3ee85d2c0ac137c6b2ccd8381d81e6423" checksum = "874914cd40bfd43674406683bb3f0924d41780698a4ade96f2e180a73678bdd1"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

View File

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

View File

@ -7,9 +7,11 @@ use schemars::JsonSchema;
use crate::{ use crate::{
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
executor::{MemoryItem, SketchGroup}, executor::{MemoryItem, SketchGroup},
std::{utils::Angle, Args}, std::Args,
}; };
use super::utils::between;
/// Returns the segment end of x. /// Returns the segment end of x.
pub async fn segment_end_x(args: Args) -> Result<MemoryItem, KclError> { pub async fn segment_end_x(args: Args) -> Result<MemoryItem, KclError> {
let (segment_name, sketch_group) = args.get_segment_name_sketch_group()?; 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 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()) Ok(result.degrees())
} }

View File

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

View File

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