From dba842f57d00e5725622ad6ed48578ffa065096a Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Wed, 28 Aug 2024 12:04:47 -0500 Subject: [PATCH] Remove tangentialArc(to), add tangentialArcToRelative --- src/wasm-lib/kcl/src/std/mod.rs | 1 + src/wasm-lib/kcl/src/std/sketch.rs | 82 +++++++++++++++++-- .../tests/executor/inputs/i_shape.kcl | 37 +++++---- src/wasm-lib/tests/executor/main.rs | 4 +- 4 files changed, 97 insertions(+), 27 deletions(-) diff --git a/src/wasm-lib/kcl/src/std/mod.rs b/src/wasm-lib/kcl/src/std/mod.rs index 7b1c57f7d..3317e003a 100644 --- a/src/wasm-lib/kcl/src/std/mod.rs +++ b/src/wasm-lib/kcl/src/std/mod.rs @@ -81,6 +81,7 @@ lazy_static! { Box::new(crate::std::sketch::Arc), Box::new(crate::std::sketch::TangentialArc), Box::new(crate::std::sketch::TangentialArcTo), + Box::new(crate::std::sketch::TangentialArcToRelative), Box::new(crate::std::sketch::BezierCurve), Box::new(crate::std::sketch::Hole), Box::new(crate::std::patterns::PatternLinear2D), diff --git a/src/wasm-lib/kcl/src/std/sketch.rs b/src/wasm-lib/kcl/src/std/sketch.rs index 0f19d060d..6258ddab9 100644 --- a/src/wasm-lib/kcl/src/std/sketch.rs +++ b/src/wasm-lib/kcl/src/std/sketch.rs @@ -1634,8 +1634,6 @@ pub enum TangentialArcData { /// Offset of the arc, in degrees. offset: f64, }, - /// A point where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position. - Point([f64; 2]), } /// Draw a tangential arc. @@ -1728,13 +1726,6 @@ async fn inner_tangential_arc( .await?; (center, to.into(), ccw) } - TangentialArcData::Point(to) => { - args.batch_modeling_cmd(id, tan_arc_to(&sketch_group, &to)).await?; - // TODO: Figure out these calculations. - let ccw = false; - let center = Point2d { x: 0.0, y: 0.0 }; - (center, to, ccw) - } }; let current_path = Path::TangentialArc { @@ -1804,6 +1795,24 @@ pub async fn tangential_arc_to(args: Args) -> Result { Ok(KclValue::new_user_val(new_sketch_group.meta.clone(), new_sketch_group)) } +/// Draw a tangential arc to a a specified distance. +pub async fn tangential_arc_to_relative(args: Args) -> Result { + let src = args.source_range; + + // Get arguments to function call + let mut it = args.args.iter(); + let to: [f64; 2] = get_arg(&mut it, src)?.get_json()?; + let sketch_group: SketchGroup = get_arg(&mut it, src)?.get_json()?; + let tag = if let Ok(memory_item) = get_arg(&mut it, src) { + memory_item.get_json_opt()? + } else { + None + }; + + let new_sketch_group = inner_tangential_arc_to_relative(to, sketch_group, tag, args).await?; + Ok(KclValue::new_user_val(new_sketch_group.meta.clone(), new_sketch_group)) +} + /// Starting at the current sketch's origin, draw a curved line segment along /// some part of an imaginary circle until it reaches the desired (x, y) /// coordinates. @@ -1837,6 +1846,61 @@ async fn inner_tangential_arc_to( } else { tangent_info.center_or_tangent_point }; + do_tan_arc_to(to, sketch_group, tag, args, tan_previous_point, from).await +} + +/// Starting at the current path pen location, draw a curved line segment along +/// some part of an imaginary circle until it reaches the desired (current.x + x, current.y + y) +/// coordinates. +/// +/// ```no_run +/// const exampleSketch = startSketchOn('XZ') +/// |> startProfileAt([0, 0], %) +/// |> angledLine({ +/// angle: 60, +/// length: 10, +/// }, %) +/// |> tangentialArcToRelative([15, 15], %) +/// |> line([10, -15], %) +/// |> close(%) +/// +/// const example = extrude(10, exampleSketch) +/// ``` +#[stdlib { + name = "tangentialArcToRelative", +}] +async fn inner_tangential_arc_to_relative( + to: [f64; 2], + sketch_group: SketchGroup, + tag: Option, + args: Args, +) -> Result { + let from: Point2d = sketch_group.current_pen_position()?; + let tangent_info = sketch_group.get_tangential_info_from_paths(); + let tan_previous_point = if tangent_info.is_center { + get_tangent_point_from_previous_arc(tangent_info.center_or_tangent_point, tangent_info.ccw, from.into()) + } else { + tangent_info.center_or_tangent_point + }; + do_tan_arc_to( + [to[0] + from.x, to[1] + from.y], + sketch_group, + tag, + args, + tan_previous_point, + from, + ) + .await +} + +async fn do_tan_arc_to( + to: [f64; 2], + sketch_group: SketchGroup, + tag: Option, + args: Args, + tan_previous_point: [f64; 2], + from: Point2d, +) -> Result { let [to_x, to_y] = to; let result = get_tangential_arc_to_info(TangentialArcInfoInput { arc_start_point: [from.x, from.y], diff --git a/src/wasm-lib/tests/executor/inputs/i_shape.kcl b/src/wasm-lib/tests/executor/inputs/i_shape.kcl index e35de7f12..0af465a67 100644 --- a/src/wasm-lib/tests/executor/inputs/i_shape.kcl +++ b/src/wasm-lib/tests/executor/inputs/i_shape.kcl @@ -4,45 +4,50 @@ let length = 120.0 let hand_thickness = 24.0 let corner_radius = 5.0 +fn tarc = (p, sg) => { + let p2 = [p[0] * corner_radius, p[1] * corner_radius] + return tangentialArcToRelative(p2, sg) +} + // At first I thought this was going to be symmetric, // but I measured intentionally to not be symmetric, // because your wrist isn't a perfect cylindrical surface let brace_base = startSketchAt([corner_radius, 0]) |> line([width - corner_radius, 0.0], %) - |> tangentialArc([corner_radius, corner_radius], %) + |> tarc([1, 1], %) |> yLine(25.0 - corner_radius, %) - |> tangentialArc([-corner_radius, corner_radius], %) + |> tarc([-1, 1], %) |> xLine(-(d_wrist_circumference[0] - (corner_radius * 2)), %) - |> tangentialArc([-corner_radius, corner_radius], %) + |> tarc([-1, 1], %) |> yLine(length - 25.0 - 23.0 - (corner_radius * 2), %) - |> tangentialArc([corner_radius, corner_radius], %) + |> tarc([1, 1], %) |> xLine(15.0 - (corner_radius * 2), %) - |> tangentialArc([corner_radius, corner_radius], %) + |> tarc([1, 1], %) |> yLine(23.0 - corner_radius, %) - |> tangentialArc([-corner_radius, corner_radius], %) + |> tarc([-1, 1], %) |> xLine(-(hand_thickness + 15.0 + 15.0 - (corner_radius * 2)), %) - |> tangentialArc([-corner_radius, -corner_radius], %) + |> tarc([-1, -1], %) |> yLine(-(23.0 - corner_radius), %) - |> tangentialArc([corner_radius, -corner_radius], %) + |> tarc([1, -1], %) |> xLine(15.0 - (corner_radius * 2), %) - |> tangentialArc([corner_radius, -corner_radius], %) + |> tarc([1, -1], %) |> yLine(-(length - 25.0 - 23.0 - (corner_radius * 2)), %) - |> tangentialArc([-corner_radius, -corner_radius], %) + |> tarc([-1, -1], %) |> xLine(-(d_wrist_circumference[1] + d_wrist_circumference[2] + d_wrist_circumference[3] - hand_thickness - corner_radius), %) - |> tangentialArc([-corner_radius, -corner_radius], %) + |> tarc([-1, -1], %) |> yLine(-(25.0 - corner_radius), %) - |> tangentialArc([corner_radius, -corner_radius], %) + |> tarc([1, -1], %) |> close(%) let inner = startSketchAt([0, 0]) |> xLine(1.0, %) - |> tangentialArc([corner_radius, corner_radius], %) + |> tarc([1, 1], %) |> yLine(25.0 - (corner_radius * 2), %) - |> tangentialArc([-corner_radius, corner_radius], %) + |> tarc([-1, 1], %) |> xLine(-1.0, %) - |> tangentialArc([-corner_radius, -corner_radius], %) + |> tarc([-1, -1], %) |> yLine(-(25.0 - (corner_radius * 2)), %) - |> tangentialArc([corner_radius, -corner_radius], %) + |> tarc([1, -1], %) |> close(%) let final = brace_base diff --git a/src/wasm-lib/tests/executor/main.rs b/src/wasm-lib/tests/executor/main.rs index f0e70b740..c4f905e8b 100644 --- a/src/wasm-lib/tests/executor/main.rs +++ b/src/wasm-lib/tests/executor/main.rs @@ -186,7 +186,7 @@ async fn kcl_test_negative_args() { async fn kcl_test_basic_tangential_arc_with_point() { let code = r#"const boxSketch = startSketchAt([0, 0]) |> line([0, 10], %) - |> tangentialArc([-5, 5], %) + |> tangentialArcToRelative([-5, 5], %) |> line([5, -15], %) |> extrude(10, %) "#; @@ -715,7 +715,7 @@ async fn kcl_test_error_sketch_on_arc_face() { let code = r#"fn cube = (pos, scale) => { const sg = startSketchOn('XY') |> startProfileAt(pos, %) - |> tangentialArc([0, scale], %, $here) + |> tangentialArcToRelative([0, scale], %, $here) |> line([scale, 0], %) |> line([0, -scale], %)