////////////////////////////////////////////////////////////////////// // LibFile: beziers.scad // Bezier functions and modules. // To use, add the following lines to the beginning of your file: // ``` // include // include // ``` ////////////////////////////////////////////////////////////////////// include // 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:1:len(curve)-2]) curve[i]*(1-u)+curve[i+1]*u], u ); // Function: bezier_curve() // Usage: // bezier_curve(curve, n); // Description: // Takes a list of bezier curve control points, and a count of path points to generate. The points // returned will be along the curve, starting at the first control point, then about every `1/n`th // of the way along the curve, ending about `1/n`th of the way *before* the final control point. // The distance between the points will *not* be equidistant. 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. // n = The number of points to generate along the bezier curve. // Example(2D): Quadratic (Degree 2) Bezier. // bez = [[0,0], [30,30], [80,0]]; // place_copies(bezier_curve(bez, 16)) sphere(r=1); // trace_bezier(bez, N=len(bez)-1); // Example(2D): Cubic (Degree 3) Bezier // bez = [[0,0], [5,35], [60,-25], [80,0]]; // place_copies(bezier_curve(bez, 16)) sphere(r=1); // trace_bezier(bez, N=len(bez)-1); // Example(2D): Degree 4 Bezier. // bez = [[0,0], [5,15], [40,20], [60,-15], [80,0]]; // place_copies(bezier_curve(bez, 16)) sphere(r=1); // trace_bezier(bez, N=len(bez)-1); function bezier_curve(curve,n) = [for(i=[0:1:n-1]) bez_point(curve, i/(n-1))]; // 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(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 || dist1 && len(x[len(x)-1])==1; // Function: is_rectpatch() // Description: // Returns true if the given item is a rectangular bezier patch. function is_rectpatch(x) = is_list(x) && is_list(x[0]) && is_vector(x[0][0]) && len(x[0]) == len(x[len(x)-1]); // Function: is_patch() // Description: // Returns true if the given item is a bezier patch. function is_patch(x) = is_tripatch(x) || is_rectpatch(x); // Function: bezier_patch() // Usage: // bezier_patch(patch, [splinesteps], [vertices], [faces]); // Description: // Calculate vertices and faces for forming a partial polyhedron from the given bezier rectangular // or triangular patch. Returns a list containing two elements. The first is the list of unique // vertices. The second is the list of faces, where each face is a list of indices into the list of // vertices. You can chain calls to this, to add more vertices and faces for multiple bezier // patches, to stitch them together into a complete polyhedron. // Arguments: // patch = The rectangular or triangular array of endpoints and control points for this bezier patch. // splinesteps = Number of steps to divide each bezier segment into. Default: 16 // vertices = Vertex list to add new points to. Default: [] // faces = Face list to add new faces to. Default: [] // Example(3D): // patch = [ // [[-50, 50, 0], [-16, 50, -20], [ 16, 50, 20], [50, 50, 0]], // [[-50, 16, 20], [-16, 16, -20], [ 16, 16, 20], [50, 16, 20]], // [[-50,-16, 20], [-16,-16, 20], [ 16,-16, -20], [50,-16, 20]], // [[-50,-50, 0], [-16,-50, 20], [ 16,-50, -20], [50,-50, 0]] // ]; // vnf = bezier_patch(patch, splinesteps=16); // polyhedron(points=vnf[0], faces=vnf[1]); // Example(3D): // tri = [ // [[-50,-33,0], [-25,16,50], [0,66,0]], // [[0,-33,50], [25,16,50]], // [[50,-33,0]] // ]; // vnf = bezier_patch(tri, splinesteps=16); // polyhedron(points=vnf[0], faces=vnf[1]); function bezier_patch(patch, splinesteps=16, vertices=[], faces=[]) = is_tripatch(patch)? _bezier_triangle(patch, splinesteps=splinesteps, vertices=vertices, faces=faces) : let( base = len(vertices), pts = [for (v=[0:1:splinesteps], u=[0:1:splinesteps]) bezier_patch_point(patch, u/splinesteps, v/splinesteps)], new_vertices = concat(vertices, pts), new_faces = [ for ( v=[0:1:splinesteps-1], u=[0:1:splinesteps-1], i=[0,1] ) let ( v1 = u+v*(splinesteps+1) + base, v2 = v1 + 1, v3 = v1 + splinesteps + 1, v4 = v3 + 1, face = i? [v1,v3,v2] : [v2,v3,v4] ) face ] ) [new_vertices, concat(faces, new_faces)]; function _tri_count(n) = (n*(1+n))/2; function _bezier_triangle(tri, splinesteps=16, vertices=[], faces=[]) = let( base = len(vertices), pts = [ for ( u=[0:1:splinesteps], v=[0:1:splinesteps-u] ) bezier_triangle_point(tri, u/splinesteps, v/splinesteps) ], new_vertices = concat(vertices, pts), patchlen = len(tri), tricnt = _tri_count(splinesteps+1), new_faces = [ for ( u=[0:1:splinesteps-1], v=[0:1:splinesteps-u-1] ) let ( v1 = v + (tricnt - _tri_count(splinesteps+1-u)) + base, v2 = v1 + 1, v3 = v + (tricnt - _tri_count(splinesteps-u)) + base, v4 = v3 + 1, allfaces = concat( [[v1,v2,v3]], ((u= len(patches))? [vertices, faces] : bezier_patch(patches[i], splinesteps=splinesteps, vertices=vertices, faces=faces) ) (i >= len(patches))? vnf : bezier_surface(patches=patches, splinesteps=splinesteps, i=i+1, vertices=vnf[0], faces=vnf[1]); // Section: Bezier Surface Modules // Module: bezier_polyhedron() // Useage: // bezier_polyhedron(patches, [splinesteps], [vertices], [faces]) // Description: // Takes a list of two or more bezier patches and attempts to make a complete polyhedron from them. // Arguments: // patches = A list of triangular and/or rectangular bezier patches. // splinesteps = Number of steps to divide each bezier segment into. Default: 16 // vertices = Vertex list for additional non-bezier faces. Default: [] // faces = Additional non-bezier faces. Default: [] // Example: // patch1 = [ // [[18,18,0], [33, 0, 0], [ 67, 0, 0], [ 82, 18,0]], // [[ 0,40,0], [ 0, 0, 20], [100, 0, 20], [100, 40,0]], // [[ 0,60,0], [ 0,100, 20], [100,100,100], [100, 60,0]], // [[18,82,0], [33,100, 0], [ 67,100, 0], [ 82, 82,0]], // ]; // patch2 = [ // [[18,18,0], [33, 0, 0], [ 67, 0, 0], [ 82, 18,0]], // [[ 0,40,0], [ 0, 0,-50], [100, 0,-50], [100, 40,0]], // [[ 0,60,0], [ 0,100,-50], [100,100,-50], [100, 60,0]], // [[18,82,0], [33,100, 0], [ 67,100, 0], [ 82, 82,0]], // ]; // bezier_polyhedron([patch1, patch2], splinesteps=8); module bezier_polyhedron(patches=[], splinesteps=16, vertices=[], faces=[]) { sfc = bezier_surface(patches=patches, splinesteps=splinesteps, vertices=vertices, faces=faces); polyhedron(points=sfc[0], faces=sfc[1]); } // Module: trace_bezier_patches() // Usage: // trace_bezier_patches(patches, [size], [showcps], [splinesteps]); // Description: // Shows the surface, and optionally, control points of a list of bezier patches. // Arguments: // patches = A list of rectangular bezier patches. // splinesteps = Number of steps to divide each bezier segment into. default=16 // showcps = If true, show the controlpoints as well as the surface. // size = Size to show control points and lines. // Example: // patch1 = [ // [[15,15,0], [33, 0, 0], [ 67, 0, 0], [ 85, 15,0]], // [[ 0,33,0], [33, 33, 50], [ 67, 33, 50], [100, 33,0]], // [[ 0,67,0], [33, 67, 50], [ 67, 67, 50], [100, 67,0]], // [[15,85,0], [33,100, 0], [ 67,100, 0], [ 85, 85,0]], // ]; // patch2 = [ // [[15,15,0], [33, 0, 0], [ 67, 0, 0], [ 85, 15,0]], // [[ 0,33,0], [33, 33,-50], [ 67, 33,-50], [100, 33,0]], // [[ 0,67,0], [33, 67,-50], [ 67, 67,-50], [100, 67,0]], // [[15,85,0], [33,100, 0], [ 67,100, 0], [ 85, 85,0]], // ]; // trace_bezier_patches(patches=[patch1, patch2], splinesteps=8, showcps=true); module trace_bezier_patches(patches=[], size=1, showcps=false, splinesteps=16) { if (showcps) { for (patch = patches) { place_copies(flatten(patch)) color("red") sphere(d=size*2); color("cyan") if (is_tripatch(patch)) { for (i=[0:1:len(patch)-2], j=[0:1:len(patch[i])-2]) { extrude_from_to(patch[i][j], patch[i+1][j]) circle(d=size); extrude_from_to(patch[i][j], patch[i][j+1]) circle(d=size); extrude_from_to(patch[i+1][j], patch[i][j+1]) circle(d=size); } } else { for (i=[0:1:len(patch)-1], j=[0:1:len(patch[i])-1]) { if (i