////////////////////////////////////////////////////////////////////// // LibFile: beziers.scad // Bezier functions and modules. // To use, add the following lines to the beginning of your file: // ``` // include // use // ``` ////////////////////////////////////////////////////////////////////// /* BSD 2-Clause License Copyright (c) 2017, Revar Desmera All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ include use use use // Section: Terminology // **Polyline**: A series of points joined by straight line segements. // // **Bezier Curve**: A mathematical curve that joins two endpoints, following a curve determined by one or more control points. // // **Endpoint**: A point that is on the end of a bezier segment. This point lies on the bezier curve. // // **Control Point**: A point that influences the shape of the curve that connects two endpoints. This is often *NOT* on the bezier curve. // // **Degree**: The number of control points, plus one endpoint, needed to specify a bezier segment. Most beziers are cubic (degree 3). // // **Bezier Segment**: A list consisting of an endpoint, one or more control points, and a final endpoint. The number of control points is one less than the degree of the bezier. A cubic (degree 3) bezier segment looks something like: // `[endpt1, cp1, cp2, endpt2]` // // **Bezier Path**: A list of bezier segments flattened out into a list of points, where each segment shares the endpoint of the previous segment as a start point. A cubic Bezier Path looks something like: // `[endpt1, cp1, cp2, endpt2, cp3, cp4, endpt3]` // **NOTE**: A bezier path is *NOT* a polyline. It is only the points and controls used to define the curve. // // **Bezier Patch**: A surface defining grid of (N+1) by (N+1) bezier points. If a Bezier Segment defines a curved line, a Bezier Patch defines a curved surface. // // **Bezier Surface**: A surface defined by a list of one or more bezier patches. // // **Spline Steps**: The number of straight-line segments to split a bezier segment into, to approximate the bezier curve. The more spline steps, the closer the approximation will be to the curve, but the slower it will be to generate. Usually defaults to 16. // Section: Segment Functions // Function: bez_point() // Usage: // bez_point(curve, u) // Description: // Formula to calculate points on a bezier curve. The degree of // the curve, N, is one less than the number of points in `curve`. // Arguments: // curve = The list of endpoints and control points for this bezier segment. // u = The proportion of the way along the curve to find the point of. 0<=`u`<=1 // Example(2D): Quadratic (Degree 2) Bezier. // bez = [[0,0], [30,30], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.3)) color("red") sphere(1); // Example(2D): Cubic (Degree 3) Bezier // bez = [[0,0], [5,35], [60,-25], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.4)) color("red") sphere(1); // Example(2D): Degree 4 Bezier. // bez = [[0,0], [5,15], [40,20], [60,-15], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bez_point(bez, 0.8)) color("red") sphere(1); function bez_point(curve,u)= (len(curve) <= 1) ? curve[0] : bez_point( [for(i=[0:len(curve)-2]) curve[i]*(1-u)+curve[i+1]*u], u ); // Function: bezier_segment_closest_point() // Usage: // bezier_segment_closest_point(bezier,pt) // Description: // Finds the closest part of the given bezier segment to point `pt`. // The degree of the curve, N, is one less than the number of points in `curve`. // Returns `u` for the shortest position on the bezier segment to the given point `pt`. // Arguments: // curve = The list of endpoints and control points for this bezier segment. // pt = The point to find the closest curve point to. // max_err = The maximum allowed error when approximating the closest approach. // Example(2D): // pt = [40,15]; // bez = [[0,0], [20,40], [60,-25], [80,0]]; // u = bezier_segment_closest_point(bez, pt); // trace_bezier(bez, N=len(bez)-1); // color("red") translate(pt) sphere(r=1); // color("blue") translate(bez_point(bez,u)) sphere(r=1); function bezier_segment_closest_point(curve, pt, max_err=0.01, u=0, end_u=1, step_u=undef, min_dist=undef, min_u=undef) = let( step = step_u == undef? (end_u-u)/(len(curve)*2) : step_u, t_u = min(u, end_u), dist = norm(bez_point(curve, t_u)-pt), md = (min_dist==undef || dist(end_u-step/2))? ( (step= 0.125 || defl > max_deflect)? ( bezier_segment_length(curve, start_u, mid_u, max_deflect) + bezier_segment_length(curve, mid_u, end_u, max_deflect) ) : norm(ep-sp); // Function: fillet3pts() // Usage: // fillet3pts(p0, p1, p2, r); // Description: // Takes three points, defining two line segments, and works out the // cubic (degree 3) bezier segment (and surrounding control points) // needed to approximate a rounding of the corner with radius `r`. // If there isn't room for a radius `r` rounding, uses the largest // radius that will fit. Returns [cp1, endpt1, cp2, cp3, endpt2, cp4] // Arguments: // p0 = The starting point. // p1 = The middle point. // p2 = The ending point. // r = The radius of the fillet/rounding. // maxerr = Max amount bezier curve should diverge from actual radius curve. Default: 0.1 // Example(2D): // p0 = [40, 0]; // p1 = [0, 0]; // p2 = [30, 30]; // trace_polyline([p0,p1,p2], showpts=true, size=0.5, color="green"); // fbez = fillet3pts(p0,p1,p2, 10); // trace_bezier(slice(fbez, 1, -2), size=1); function fillet3pts(p0, p1, p2, r, maxerr=0.1, w=0.5, dw=0.25) = let( v0 = normalize(p0-p1), v1 = normalize(p2-p1), midv = normalize((v0+v1)/2), a = vector_angle(v0,v1), tanr = min(r/tan(a/2), norm(p0-p1)*0.99, norm(p2-p1)*0.99), tp0 = p1+v0*tanr, tp1 = p1+v1*tanr, cp = p1 + midv * tanr / cos(a/2), cp0 = lerp(tp0, p1, w), cp1 = lerp(tp1, p1, w), cpr = norm(cp-tp0), bp = bez_point([tp0, cp0, cp1, tp1], 0.5), tdist = norm(cp-bp) ) (abs(tdist-cpr) <= maxerr)? [tp0, tp0, cp0, cp1, tp1, tp1] : (tdist= len(patches)? vnf : bezier_surface_vertices_and_faces(patches, splinesteps=splinesteps, i=i+1, vertices=vnf[0], faces=vnf[1]); // Section: Path Functions // Function: bezier_path_point() // Usage: // bezier_path_point(path, seg, u, [N]) // Description: Returns the coordinates of bezier path segment `seg` at position `u`. // Arguments: // path = A bezier path to approximate. // seg = Segment number along the path. Each segment is N points long. // u = The proportion of the way along the segment to find the point of. 0<=`u`<=1 // N = The degree of the bezier curves. Cubic beziers have N=3. Default: 3 function bezier_path_point(path, seg, u, N=3) = bez_point(select(path,seg*N,(seg+1)*N), u); // Function: bezier_path_closest_point() // Usage: // bezier_path_closest_point(bezier,pt) // Description: // Finds the closest part of the given bezier path to point `pt`. // Returns [segnum, u] for the closest position on the bezier path to the given point `pt`. // Arguments: // path = A bezier path to approximate. // pt = The point to find the closest curve point to. // N = The degree of the bezier curves. Cubic beziers have N=3. Default: 3 // max_err = The maximum allowed error when approximating the closest approach. // Example(2D): // pt = [100,0]; // bez = [[0,0], [20,40], [60,-25], [80,0], [100,25], [140,25], [160,0]]; // pos = bezier_path_closest_point(bez, pt); // xy = bezier_path_point(bez,pos[0],pos[1]); // echo(pos=pos); // trace_bezier(bez, N=3); // color("red") translate(pt) sphere(r=1); // color("blue") translate(xy) sphere(r=1); function bezier_path_closest_point(path, pt, N=3, max_err=0.01, seg=0, min_seg=undef, min_u=undef, min_dist=undef) = let(curve = select(path,seg*N,(seg+1)*N)) (seg*N+1 >= len(path))? ( let(curve = select(path, min_seg*N, (min_seg+1)*N)) [min_seg, bezier_segment_closest_point(curve, pt, max_err=max_err)] ) : ( let( curve = select(path,seg*N,(seg+1)*N), u = bezier_segment_closest_point(curve, pt, max_err=0.05), dist = norm(bez_point(curve, u)-pt), mseg = (min_dist==undef || dist