From f67b0dbfc558261ebbcde6db048c039a3b38abfc Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Thu, 5 Mar 2020 17:05:23 -0500 Subject: [PATCH] Added support for rounding factor k to path_to_bezier and smooth_path. --- beziers.scad | 22 ++++++++++++++++++---- rounding.scad | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/beziers.scad b/beziers.scad index f9f31f0..bcfdd92 100644 --- a/beziers.scad +++ b/beziers.scad @@ -318,26 +318,40 @@ function bezier_polyline(bezier, splinesteps=16, N=3) = let( ); + // Function: path_to_bezier() // Usage: -// path_to_bezier(path,[tangent],[closed]); +// path_to_bezier(path,[tangent],[k],[closed]); // Description: // Given an input path and optional path of tangent vectors, computes a cubic (degree 3) bezier path that passes // through every point on the input path and matches the tangent vectors. If you do not supply // the tangent it will be computed using path_tangents. If the path is closed specify this -// by setting closed=true. +// by setting closed=true. If you specify the curvature parameter k it scales the tangent vectors, +// which will increase or decrease the curvature of the interpolated bezier. Negative values of k create loops at the corners, +// so they are not allowed. Sufficiently large k values will also produce loops. // Arguments: // path = path of points to define the bezier // tangents = optional list of tangent vectors at every point +// k = curvature parameter, a scalar or vector to adjust curvature at each point // closed = set to true for a closed path. Default: false -function path_to_bezier(path, tangents, closed=false) = +function path_to_bezier(path, tangents, k, closed=false) = assert(is_path(path,dim=undef),"Input path is not a valid path") assert(is_undef(tangents) || is_path(tangents,dim=len(path[0])),"Tangents must be a path of the same dimension as the input path") + assert(is_undef(tangents) || len(path)==len(tangents), "Input tangents must be the same length as the input path") + let( + k = is_undef(k) ? repeat(1, len(path)) : + is_list(k) ? k : repeat(k, len(path)), + k_bad = [for(entry=k) if (entry<0) entry] + ) + assert(len(k)==len(path), "Curvature parameter k must have the same length as the path") + assert(k_bad==[], "Curvature parameter k must be a nonnegative number or list of nonnegative numbers") let( tangents = is_def(tangents)? tangents : deriv(path, closed=closed), lastpt = len(path) - (closed?0:1) ) - [for(i=[0:lastpt-1]) each [path[i], path[i]+tangents[i]/3, select(path,i+1)-select(tangents,i+1)/3], + [for(i=[0:lastpt-1]) each [path[i], + path[i]+k[i]*tangents[i]/3, + select(path,i+1)-select(k,i+1)*select(tangents,i+1)/3], select(path,lastpt)]; diff --git a/rounding.scad b/rounding.scad index 4987f21..2ccf7a2 100644 --- a/rounding.scad +++ b/rounding.scad @@ -9,10 +9,10 @@ // ``` ////////////////////////////////////////////////////////////////////// -include -include -include -include +include +include +include +include // CommonCode: @@ -409,16 +409,23 @@ function _rounding_offsets(edgespec,z_dir=1) = // Function: smooth_path() // Usage: -// smooth_path(path, [tangents], [splinesteps], [closed] +// smooth_path(path, [tangents], [k], [splinesteps], [closed] // Description: // Smooths the input path using a cubic spline. Every segment of the path will be replaced by a cubic curve // with `splinesteps` points. The cubic interpolation will pass through every input point on the path // and will match the tangents at every point. If you do not specify tangents they will be computed using -// deriv(). Note that the magnitude of the tangents affects the result. See also path_to_bezier(). +// deriv(). See also path_to_bezier(). +// +// Note that the magnitude of the tangents affects the result. If you increase it you will get a blunter +// corner with a larger radius of curvature. Decreasing it will produce a sharp corner. You can specify +// the curvature factor `k` to adjust the curvature. It can be a scalar or a vector the same length as +// the path and is used to scale the tangent vectors. Negative values of k create loops at the corners, +// so they are not allowed. Sufficiently large k values will also produce loops. // Arguments: // path = path to smooth // tangents = tangent vectors of the path // splinesteps = number of points to insert between the path points. Default: 10 +// k = curvature parameter, a scalar or vector to adjust curvature at each point // closed = set to true for a closed path. Default: false // Example(2D): Original path in green, smoothed path in yellow: // color("green")stroke(square(4), width=0.1); @@ -428,21 +435,30 @@ function _rounding_offsets(edgespec,z_dir=1) = // Example(2D): A more interesting shape: // path = [[0,0], [4,0], [7,14], [-3,12]]; // polygon(smooth_path(path,closed=true)); -// Example(2D): Scaling the tangent data can decrease or increase the amount of smoothing: -// shape = square(4); -// polygon(smooth_path(shape, tangents=0.5*deriv(shape, closed=true),closed=true)); +// Example(2D): Scaling the tangent data using the curvature parameter k can decrease or increase the amount of smoothing. Note this is the same +// as just multiplying the deriv(square(4)) by k. +// polygon(smooth_path(square(4), k=0.5,closed=true)); // Example(2D): Or you can specify your own tangent values to alter the shape of the curve // polygon(smooth_path(square(4),tangents=1.25*[[-2,-1], [-2,1], [1,2], [2,-1]],closed=true)); // Example(FlatSpin): Works on 3d paths as well // path = [[0,0,0],[3,3,2],[6,0,1],[9,9,0]]; // trace_polyline(smooth_path(path),size=.3); -function smooth_path(path, tangents, splinesteps=10, closed=false) = - let( - bez = path_to_bezier(path, tangents=tangents, closed=closed) +// Example(2D): The curve passes through all the points, but features some unexpected wiggles. These occur because the curvature is too low to change fast enough. +// path = [[0,0], [0,3], [.5,2.8], [1,2.2], [1,0]]; +// polygon(smooth_path(path)); +// color("red") place_copies(path)circle(r=.1,$fn=16); +// Example(2D): Using the k parameter is one way to fix this problem. By allowing sharper curvature (k<1) at the two points next to the problematic point we can achieve a smoother result. The other fix is to move your points. +// path = [[0,0], [0,3], [.5,2.8], [1,2.2], [1,0]]; +// polygon(smooth_path(path,k=[1,.5,1,.5,1])); +// color("red") place_copies(path)circle(r=.1,$fn=16); +function smooth_path(path, tangents, k, splinesteps=10, closed=false) = + let ( + bez = path_to_bezier(path, tangents, k=k, closed=closed) ) bezier_polyline(bez,splinesteps=splinesteps); + // Module: offset_sweep() // // Description: