From dbf6df4dff1a5ea2bcfa34291bb86f9616e9975a Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Thu, 11 Feb 2021 14:31:28 +0800 Subject: [PATCH] add angle_threshold param --- src/_impl/_bezier_smooth_impl.scad | 17 ++++++++++------- src/bezier_smooth.scad | 4 ++-- test/test_bezier_smooth.scad | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/_impl/_bezier_smooth_impl.scad b/src/_impl/_bezier_smooth_impl.scad index 834477e3..b56572d2 100644 --- a/src/_impl/_bezier_smooth_impl.scad +++ b/src/_impl/_bezier_smooth_impl.scad @@ -2,6 +2,7 @@ use <../__comm__/__to3d.scad>; use <../__comm__/__to2d.scad>; use <../__comm__/__angy_angz.scad>; use <../bezier_curve.scad>; +use <../angle_between.scad>; function _corner_ctrl_pts(round_d, p1, p2, p3) = let( @@ -24,32 +25,34 @@ function _corner_ctrl_pts(round_d, p1, p2, p3) = p2 - [dx2, dy2, dz2] ]; - function _bezier_corner(round_d, t_step, p1, p2, p3) = bezier_curve(t_step, _corner_ctrl_pts(round_d, p1, p2, p3)); -function _recursive_bezier_smooth(pts, round_d, t_step, leng) = +function _recursive_bezier_smooth(pts, round_d, t_step, leng, angle_threshold) = let(end_i = leng - 2) [ for(i = 0; i < end_i; i = i + 1) - each _bezier_corner(round_d, t_step, pts[i], pts[i + 1], pts[i + 2]) + each + angle_between(pts[i] - pts[i + 1], pts[i + 1] - pts[i + 2]) > angle_threshold ? + _bezier_corner(round_d, t_step, pts[i], pts[i + 1], pts[i + 2]) : + [pts[i + 1]] ]; -function _bezier_smooth_impl(path_pts, round_d, t_step, closed) = +function _bezier_smooth_impl(path_pts, round_d, t_step, closed, angle_threshold) = let( pts = len(path_pts[0]) == 3 ? path_pts : [for(p = path_pts) __to3d(p)], leng = len(pts), - middle_pts = _recursive_bezier_smooth(pts, round_d, t_step, leng), + middle_pts = _recursive_bezier_smooth(pts, round_d, t_step, leng, angle_threshold), pth_pts = closed ? concat( _recursive_bezier_smooth( [pts[leng - 1], pts[0], pts[1]], - round_d, t_step, 3 + round_d, t_step, 3, angle_threshold ), middle_pts, _recursive_bezier_smooth( [pts[leng - 2], pts[leng - 1], pts[0]], - round_d, t_step, 3 + round_d, t_step, 3, angle_threshold ) ) : concat( diff --git a/src/bezier_smooth.scad b/src/bezier_smooth.scad index 9ea01a34..baba9243 100644 --- a/src/bezier_smooth.scad +++ b/src/bezier_smooth.scad @@ -10,5 +10,5 @@ use <_impl/_bezier_smooth_impl.scad>; -function bezier_smooth(path_pts, round_d, t_step = 0.1, closed = false) = - _bezier_smooth_impl(path_pts, round_d, t_step, closed); \ No newline at end of file +function bezier_smooth(path_pts, round_d, t_step = 0.1, closed = false, angle_threshold = 0) = + _bezier_smooth_impl(path_pts, round_d, t_step, closed, angle_threshold); \ No newline at end of file diff --git a/test/test_bezier_smooth.scad b/test/test_bezier_smooth.scad index f30cac66..83c2ad0c 100644 --- a/test/test_bezier_smooth.scad +++ b/test/test_bezier_smooth.scad @@ -39,5 +39,21 @@ module test_bezier_smooth_closed() { assertEqualPoints(expected, actual); } +module test_bezier_smooth_angle_threshold() { + echo("==== test_bezier_smooth_angle_threshold ===="); + + round_d = 15; + + path_pts = [ + [0, 0, 0], + [0, 40, 0], + [0, 60, 0], + [0, 70, 1] + ]; + + assert(path_pts == bezier_smooth(path_pts, round_d, angle_threshold = 15)); +} + test_bezier_smooth_no_closed(); -test_bezier_smooth_closed(); \ No newline at end of file +test_bezier_smooth_closed(); +test_bezier_smooth_angle_threshold(); \ No newline at end of file