Compare commits

...

5 Commits

Author SHA1 Message Date
a91d8c07b6 Regenerate docs 2023-09-18 10:45:14 -05:00
d8102252e1 Remove dbg 2023-09-18 10:28:14 -05:00
1d8b0cb12f Use angle object in more places 2023-09-18 10:19:39 -05:00
88685c24cc More conversions 2023-09-18 10:16:28 -05:00
118b642cc5 Start using rich angles 2023-09-18 10:14:15 -05:00
5 changed files with 260 additions and 84 deletions

View File

@ -514,10 +514,18 @@
], ],
"returnValue": { "returnValue": {
"name": "", "name": "",
"type": "number", "type": "Angle",
"schema": { "schema": {
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"required": true "required": true
}, },
@ -983,10 +991,18 @@
], ],
"returnValue": { "returnValue": {
"name": "", "name": "",
"type": "number", "type": "Angle",
"schema": { "schema": {
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"required": true "required": true
}, },
@ -1016,8 +1032,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"length": { "length": {
"description": "The length of the line.", "description": "The length of the line.",
@ -1033,10 +1057,24 @@
{ {
"description": "An angle and length.", "description": "An angle and length.",
"type": "array", "type": "array",
"items": { "items": [
"type": "number", {
"format": "double" "type": "object",
}, "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
},
{
"type": "number",
"format": "double"
}
],
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
@ -1937,8 +1975,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"length": { "length": {
"description": "The length of the line.", "description": "The length of the line.",
@ -1954,10 +2000,24 @@
{ {
"description": "An angle and length.", "description": "An angle and length.",
"type": "array", "type": "array",
"items": { "items": [
"type": "number", {
"format": "double" "type": "object",
}, "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
},
{
"type": "number",
"format": "double"
}
],
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
@ -2858,8 +2918,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"length": { "length": {
"description": "The length of the line.", "description": "The length of the line.",
@ -2875,10 +2943,24 @@
{ {
"description": "An angle and length.", "description": "An angle and length.",
"type": "array", "type": "array",
"items": { "items": [
"type": "number", {
"format": "double" "type": "object",
}, "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
},
{
"type": "number",
"format": "double"
}
],
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
@ -3775,8 +3857,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"intersectTag": { "intersectTag": {
"description": "The tag of the line to intersect with.", "description": "The tag of the line to intersect with.",
@ -4690,8 +4780,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"tag": { "tag": {
"description": "The tag.", "description": "The tag.",
@ -4707,10 +4805,24 @@
{ {
"description": "An angle and point to draw to.", "description": "An angle and point to draw to.",
"type": "array", "type": "array",
"items": { "items": [
"type": "number", {
"format": "double" "type": "object",
}, "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
},
{
"type": "number",
"format": "double"
}
],
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
@ -5611,8 +5723,16 @@
"properties": { "properties": {
"angle": { "angle": {
"description": "The angle of the line.", "description": "The angle of the line.",
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"tag": { "tag": {
"description": "The tag.", "description": "The tag.",
@ -5628,10 +5748,24 @@
{ {
"description": "An angle and point to draw to.", "description": "An angle and point to draw to.",
"type": "array", "type": "array",
"items": { "items": [
"type": "number", {
"format": "double" "type": "object",
}, "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
},
{
"type": "number",
"format": "double"
}
],
"maxItems": 2, "maxItems": 2,
"minItems": 2 "minItems": 2
} }
@ -13889,10 +14023,18 @@
], ],
"returnValue": { "returnValue": {
"name": "", "name": "",
"type": "number", "type": "Angle",
"schema": { "schema": {
"type": "number", "type": "object",
"format": "double" "required": [
"degrees"
],
"properties": {
"degrees": {
"type": "number",
"format": "double"
}
}
}, },
"required": true "required": true
}, },

View File

@ -106,7 +106,7 @@ Returns the angle to match the given length for x.
``` ```
angleToMatchLengthX(segment_name: string, to: number, sketch_group: SketchGroup) -> number angleToMatchLengthX(segment_name: string, to: number, sketch_group: SketchGroup) -> Angle
``` ```
#### Arguments #### Arguments
@ -179,7 +179,12 @@ angleToMatchLengthX(segment_name: string, to: number, sketch_group: SketchGroup)
#### Returns #### Returns
* `number` * `Angle`
```
{
degrees: number,
}
```
@ -190,7 +195,7 @@ Returns the angle to match the given length for y.
``` ```
angleToMatchLengthY(segment_name: string, to: number, sketch_group: SketchGroup) -> number angleToMatchLengthY(segment_name: string, to: number, sketch_group: SketchGroup) -> Angle
``` ```
#### Arguments #### Arguments
@ -263,7 +268,12 @@ angleToMatchLengthY(segment_name: string, to: number, sketch_group: SketchGroup)
#### Returns #### Returns
* `number` * `Angle`
```
{
degrees: number,
}
```
@ -283,13 +293,15 @@ angledLine(data: AngledLineData, sketch_group: SketchGroup) -> SketchGroup
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The length of the line. // The length of the line.
length: number, length: number,
// The tag. // The tag.
tag: string, tag: string,
} | } |
[number] string
``` ```
* `sketch_group`: `SketchGroup` - A sketch group is a collection of paths. * `sketch_group`: `SketchGroup` - A sketch group is a collection of paths.
``` ```
@ -439,13 +451,15 @@ angledLineOfXLength(data: AngledLineData, sketch_group: SketchGroup) -> SketchGr
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The length of the line. // The length of the line.
length: number, length: number,
// The tag. // The tag.
tag: string, tag: string,
} | } |
[number] string
``` ```
* `sketch_group`: `SketchGroup` - A sketch group is a collection of paths. * `sketch_group`: `SketchGroup` - A sketch group is a collection of paths.
``` ```
@ -595,13 +609,15 @@ angledLineOfYLength(data: AngledLineData, sketch_group: SketchGroup) -> SketchGr
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The length of the line. // The length of the line.
length: number, length: number,
// The tag. // The tag.
tag: string, tag: string,
} | } |
[number] string
``` ```
* `sketch_group`: `SketchGroup` - A sketch group is a collection of paths. * `sketch_group`: `SketchGroup` - A sketch group is a collection of paths.
``` ```
@ -751,7 +767,9 @@ angledLineThatIntersects(data: AngeledLineThatIntersectsData, sketch_group: Sket
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The tag of the line to intersect with. // The tag of the line to intersect with.
intersectTag: string, intersectTag: string,
// The offset from the intersecting line. // The offset from the intersecting line.
@ -908,13 +926,15 @@ angledLineToX(data: AngledLineToData, sketch_group: SketchGroup) -> SketchGroup
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The tag. // The tag.
tag: string, tag: string,
// The point to draw to. // The point to draw to.
to: number, to: number,
} | } |
[number] string
``` ```
* `sketch_group`: `SketchGroup` - A sketch group is a collection of paths. * `sketch_group`: `SketchGroup` - A sketch group is a collection of paths.
``` ```
@ -1064,13 +1084,15 @@ angledLineToY(data: AngledLineToData, sketch_group: SketchGroup) -> SketchGroup
``` ```
{ {
// The angle of the line. // The angle of the line.
angle: number, angle: {
degrees: number,
},
// The tag. // The tag.
tag: string, tag: string,
// The point to draw to. // The point to draw to.
to: number, to: number,
} | } |
[number] string
``` ```
* `sketch_group`: `SketchGroup` - A sketch group is a collection of paths. * `sketch_group`: `SketchGroup` - A sketch group is a collection of paths.
``` ```
@ -2680,7 +2702,7 @@ Returns the angle of the segment.
``` ```
segAng(segment_name: string, sketch_group: SketchGroup) -> number segAng(segment_name: string, sketch_group: SketchGroup) -> Angle
``` ```
#### Arguments #### Arguments
@ -2752,7 +2774,12 @@ segAng(segment_name: string, sketch_group: SketchGroup) -> number
#### Returns #### Returns
* `number` * `Angle`
```
{
degrees: number,
}
```

View File

@ -155,14 +155,20 @@ pub fn segment_angle(args: &mut 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()?;
let result = inner_segment_angle(&segment_name, sketch_group, args)?; let result = inner_segment_angle(&segment_name, sketch_group, args)?;
args.make_user_val_from_f64(result) let j = serde_json::to_value(result).map_err(|e| {
KclError::Type(KclErrorDetails {
message: format!("Could not serialize KCL value {result:?}: {e}"),
source_ranges: vec![args.source_range],
})
})?;
args.make_user_val_from_json(j)
} }
/// Returns the angle of the segment. /// Returns the angle of the segment.
#[stdlib { #[stdlib {
name = "segAng", name = "segAng",
}] }]
fn inner_segment_angle(segment_name: &str, sketch_group: SketchGroup, args: &mut Args) -> Result<f64, KclError> { fn inner_segment_angle(segment_name: &str, sketch_group: SketchGroup, args: &mut Args) -> Result<Angle, KclError> {
let path = sketch_group.get_path_by_name(segment_name).ok_or_else(|| { let path = sketch_group.get_path_by_name(segment_name).ok_or_else(|| {
KclError::Type(KclErrorDetails { KclError::Type(KclErrorDetails {
message: format!( message: format!(
@ -174,9 +180,7 @@ fn inner_segment_angle(segment_name: &str, sketch_group: SketchGroup, args: &mut
})?; })?;
let line = path.get_base(); let line = path.get_base();
let result = Angle::between(line.from.into(), line.to.into()); Ok(Angle::between(line.from.into(), line.to.into()))
Ok(result.degrees())
} }
/// Returns the angle to match the given length for x. /// Returns the angle to match the given length for x.

View File

@ -297,14 +297,14 @@ pub enum AngledLineData {
/// An angle and length with a tag. /// An angle and length with a tag.
AngleWithTag { AngleWithTag {
/// The angle of the line. /// The angle of the line.
angle: f64, angle: Angle,
/// The length of the line. /// The length of the line.
length: f64, length: f64,
/// The tag. /// The tag.
tag: String, tag: String,
}, },
/// An angle and length. /// An angle and length.
AngleAndLength([f64; 2]), AngleAndLength(Angle, f64),
} }
/// Draw an angled line. /// Draw an angled line.
@ -327,11 +327,11 @@ fn inner_angled_line(
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, length) = match &data { let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length), AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]), AngledLineData::AngleAndLength(angle, length) => (*angle, *length),
}; };
let to: [f64; 2] = [ let to: [f64; 2] = [
from.x + length * f64::cos(angle.to_radians()), from.x + length * f64::cos(angle.radians()),
from.y + length * f64::sin(angle.to_radians()), from.y + length * f64::sin(angle.radians()),
]; ];
let id = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4();
@ -390,10 +390,10 @@ fn inner_angled_line_of_x_length(
) -> Result<SketchGroup, KclError> { ) -> Result<SketchGroup, KclError> {
let (angle, length) = match &data { let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length), AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]), AngledLineData::AngleAndLength(angle, length) => (*angle, *length),
}; };
let to = get_y_component(Angle::from_degrees(angle), length); let to = get_y_component(angle, length);
let new_sketch_group = inner_line( let new_sketch_group = inner_line(
if let AngledLineData::AngleWithTag { tag, .. } = data { if let AngledLineData::AngleWithTag { tag, .. } = data {
@ -416,14 +416,14 @@ pub enum AngledLineToData {
/// An angle and point with a tag. /// An angle and point with a tag.
AngleWithTag { AngleWithTag {
/// The angle of the line. /// The angle of the line.
angle: f64, angle: Angle,
/// The point to draw to. /// The point to draw to.
to: f64, to: f64,
/// The tag. /// The tag.
tag: String, tag: String,
}, },
/// An angle and point to draw to. /// An angle and point to draw to.
AngleAndPoint([f64; 2]), AngleAndPoint(Angle, f64),
} }
/// Draw an angled line to a given x coordinate. /// Draw an angled line to a given x coordinate.
@ -446,11 +446,11 @@ fn inner_angled_line_to_x(
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, x_to) = match &data { let (angle, x_to) = match &data {
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to), AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]), AngledLineToData::AngleAndPoint(angle, to) => (*angle, *to),
}; };
let x_component = x_to - from.x; let x_component = x_to - from.x;
let y_component = x_component * f64::tan(angle.to_radians()); let y_component = x_component * f64::tan(angle.radians());
let y_to = from.y + y_component; let y_to = from.y + y_component;
let new_sketch_group = inner_line_to( let new_sketch_group = inner_line_to(
@ -485,10 +485,10 @@ fn inner_angled_line_of_y_length(
) -> Result<SketchGroup, KclError> { ) -> Result<SketchGroup, KclError> {
let (angle, length) = match &data { let (angle, length) = match &data {
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length), AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]), AngledLineData::AngleAndLength(angle, length) => (*angle, *length),
}; };
let to = get_x_component(Angle::from_degrees(angle), length); let to = get_x_component(angle, length);
let new_sketch_group = inner_line( let new_sketch_group = inner_line(
if let AngledLineData::AngleWithTag { tag, .. } = data { if let AngledLineData::AngleWithTag { tag, .. } = data {
@ -523,11 +523,11 @@ fn inner_angled_line_to_y(
let from = sketch_group.get_coords_from_paths()?; let from = sketch_group.get_coords_from_paths()?;
let (angle, y_to) = match &data { let (angle, y_to) = match &data {
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to), AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]), AngledLineToData::AngleAndPoint(angle, to) => (*angle, *to),
}; };
let y_component = y_to - from.y; let y_component = y_to - from.y;
let x_component = y_component / f64::tan(angle.to_radians()); let x_component = y_component / f64::tan(angle.radians());
let x_to = from.x + x_component; let x_to = from.x + x_component;
let new_sketch_group = inner_line_to( let new_sketch_group = inner_line_to(
@ -549,7 +549,7 @@ fn inner_angled_line_to_y(
// TODO: make sure the docs on the args below are correct. // TODO: make sure the docs on the args below are correct.
pub struct AngeledLineThatIntersectsData { pub struct AngeledLineThatIntersectsData {
/// The angle of the line. /// The angle of the line.
pub angle: f64, pub angle: Angle,
/// The tag of the line to intersect with. /// The tag of the line to intersect with.
pub intersect_tag: String, pub intersect_tag: String,
/// The offset from the intersecting line. /// The offset from the intersecting line.

View File

@ -5,7 +5,10 @@ use crate::{
executor::{Point2d, SourceRange}, executor::{Point2d, SourceRange},
}; };
#[derive(Clone, Copy, Default, PartialOrd, PartialEq, Debug)] use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Default, PartialOrd, PartialEq, Debug, ts_rs::TS, Serialize, Deserialize, JsonSchema)]
pub struct Angle { pub struct Angle {
degrees: f64, degrees: f64,
} }
@ -125,10 +128,10 @@ pub fn distance_between_points(point_a: Point2d, point_b: Point2d) -> f64 {
((y2 - y1).powi(2) + (x2 - x1).powi(2)).sqrt() ((y2 - y1).powi(2) + (x2 - x1).powi(2)).sqrt()
} }
pub fn calculate_intersection_of_two_lines(line1: &[Point2d; 2], line2_angle: f64, line2_point: Point2d) -> Point2d { pub fn calculate_intersection_of_two_lines(line1: &[Point2d; 2], line2_angle: Angle, line2_point: Point2d) -> Point2d {
let line2_point_b = Point2d { let line2_point_b = Point2d {
x: line2_point.x + f64::cos(line2_angle.to_radians()) * 10.0, x: line2_point.x + f64::cos(line2_angle.radians()) * 10.0,
y: line2_point.y + f64::sin(line2_angle.to_radians()) * 10.0, y: line2_point.y + f64::sin(line2_angle.radians()) * 10.0,
}; };
intersect(line1[0], line1[1], line2_point, line2_point_b) intersect(line1[0], line1[1], line2_point, line2_point_b)
} }
@ -159,7 +162,7 @@ pub fn intersect(p1: Point2d, p2: Point2d, p3: Point2d, p4: Point2d) -> Point2d
pub fn intersection_with_parallel_line( pub fn intersection_with_parallel_line(
line1: &[Point2d; 2], line1: &[Point2d; 2],
line1_offset: f64, line1_offset: f64,
line2_angle: f64, line2_angle: Angle,
line2_point: Point2d, line2_point: Point2d,
) -> Point2d { ) -> Point2d {
calculate_intersection_of_two_lines(&offset_line(line1_offset, line1[0], line1[1]), line2_angle, line2_point) calculate_intersection_of_two_lines(&offset_line(line1_offset, line1[0], line1[1]), line2_angle, line2_point)