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,64 +1,26 @@
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)]
pub struct Angle {
degrees: f64,
}
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),
}
}
}
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 /// Get the angle between these points
pub fn between(a: Point2d, b: Point2d) -> Self { pub fn between(a: Point2d, b: Point2d) -> Angle {
let x = b.x - a.x; let x = b.x - a.x;
let y = b.y - a.y; let y = b.y - a.y;
Self::from_radians(y.atan2(x)).normalize() normalize(Angle::from_radians(y.atan2(x)))
} }
/// Normalize the angle /// Normalize the angle
pub fn normalize(self) -> Self { pub fn normalize(angle: Angle) -> Angle {
let angle = self.degrees(); let deg = angle.degrees();
let result = ((angle % 360.0) + 360.0) % 360.0; let result = ((deg % 360.0) + 360.0) % 360.0;
Self::from_degrees(if result > 180.0 { result - 360.0 } else { result }) Angle::from_degrees(if result > 180.0 { result - 360.0 } else { result })
} }
/// Gives the ▲-angle between from and to angles (shortest path), use radians. /// Gives the ▲-angle between from and to angles (shortest path), use radians.
/// ///
/// Sign of the returned angle denotes direction, positive means counterClockwise 🔄 /// Sign of the returned angle denotes direction, positive means counterClockwise 🔄
@ -75,7 +37,7 @@ impl Angle {
/// ); /// );
/// ``` /// ```
#[allow(dead_code)] #[allow(dead_code)]
pub fn delta(from_angle: Self, to_angle: Self) -> Self { pub fn delta(from_angle: Angle, to_angle: Angle) -> Angle {
let norm_from_angle = normalize_rad(from_angle.radians()); let norm_from_angle = normalize_rad(from_angle.radians());
let norm_to_angle = normalize_rad(to_angle.radians()); let norm_to_angle = normalize_rad(to_angle.radians());
let provisional = norm_to_angle - norm_from_angle; let provisional = norm_to_angle - norm_from_angle;
@ -91,7 +53,6 @@ impl Angle {
} }
Angle::ZERO Angle::ZERO
} }
}
#[allow(dead_code)] #[allow(dead_code)]
pub fn clockwise_sign(points: &[Point2d]) -> i32 { pub fn clockwise_sign(points: &[Point2d]) -> i32 {