////////////////////////////////////////////////////////////////////// // 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) = let( steps = len(curve)*3, path = [for (i=[0:1:steps]) let(v=(end_u-u)*(i/steps)+u) [v, bez_point(curve, v)]], bracketed = concat([path[0]], path, [path[len(path)-1]]), minima_ranges = [ for (pts = triplet(bracketed)) let( d1=norm(pts.x.y-pt), d2=norm(pts.y.y-pt), d3=norm(pts.z.y-pt) ) if(d2<=d1 && d2<=d3) [pts.x.x,pts.z.x] ] ) len(minima_ranges)>1? ( let( min_us = [ for (minima = minima_ranges) bezier_segment_closest_point(curve, pt, max_err=max_err, u=minima.x, end_u=minima.y) ], dists = [for (v=min_us) norm(bez_point(curve,v)-pt)], min_i = min_index(dists) ) min_us[min_i] ) : let( minima = minima_ranges[0], p1 = bez_point(curve, minima.x), p2 = bez_point(curve, minima.y), err = norm(p2-p1) ) err= 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 = unit(p0-p1), v1 = unit(p2-p1), midv = unit((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], [vnf]); // Description: // Calculate vertices and faces for forming a partial polyhedron from the given bezier rectangular // or triangular patch. Returns a [VNF structure](vnf.scad): 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. For rectangular patches you can specify [XSTEPS,YSTEPS]. Default: 16 // vnf = Vertices'n'Faces [VNF structure](vnf.scad) to add new vertices and faces to. Default: empty VNF // 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); // vnf_polyhedron(vnf); // 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); // vnf_polyhedron(vnf); // Example(3DFlatSpin): Chaining Patches // patch = [ // [[0, 0,0], [33, 0, 0], [67, 0, 0], [100, 0,0]], // [[0, 33,0], [33, 33, 33], [67, 33, 33], [100, 33,0]], // [[0, 67,0], [33, 67, 33], [67, 67, 33], [100, 67,0]], // [[0,100,0], [33,100, 0], [67,100, 0], [100,100,0]], // ]; // vnf1 = bezier_patch(translate(p=patch,[-50,-50,50])); // vnf2 = bezier_patch(vnf=vnf1, rot(a=[90,0,0],p=translate(p=patch,[-50,-50,50]))); // vnf3 = bezier_patch(vnf=vnf2, rot(a=[-90,0,0],p=translate(p=patch,[-50,-50,50]))); // vnf4 = bezier_patch(vnf=vnf3, rot(a=[180,0,0],p=translate(p=patch,[-50,-50,50]))); // vnf5 = bezier_patch(vnf=vnf4, rot(a=[0,90,0],p=translate(p=patch,[-50,-50,50]))); // vnf6 = bezier_patch(vnf=vnf5, rot(a=[0,-90,0],p=translate(p=patch,[-50,-50,50]))); // vnf_polyhedron(vnf6); // Example(3D): Chaining Patches with Asymmetric Splinesteps // steps = 8; // edge_patch = [ // [[-60, 0,-40], [0, 0,-40], [60, 0,-40]], // [[-60, 0, 0], [0, 0, 0], [60, 0, 0]], // [[-60,40, 0], [0,40, 0], [60,40, 0]], // ]; // corner_patch = [ // [[40, 0,-40], [ 0, 0,-40], [ 0, 40,-40]], // [[40, 0, 0], [ 0, 0, 0], [ 0, 40, 0]], // [[40, 40, 0], [40, 40, 0], [40, 40, 0]] // ]; // face_patch = bezier_patch_flat([120,120],orient=LEFT); // edges = [ // for (axrot=[[0,0,0],[0,90,0],[0,0,90]], xang=[-90:90:180]) // bezier_patch( // splinesteps=[1,steps], // rot(a=axrot, // p=rot(a=[xang,0,0], // p=translate(v=[0,-100,100],p=edge_patch) // ) // ) // ) // ]; // corners = [ // for (zang=[0,180], xang=[-90:90:180]) // bezier_patch( // splinesteps=steps, // rot(a=[xang,0,zang], // p=translate(v=[-100,-100,100],p=corner_patch) // ) // ) // ]; // faces = [ // for (axrot=[[0,0,0],[0,90,0],[0,0,90]], zang=[0,180]) // bezier_patch( // splinesteps=1, // rot(a=axrot, // p=rot(a=[0,0,zang], // p=translate(v=[-100,0,0], p=face_patch) // ) // ) // ) // ]; // vnf_polyhedron(concat(edges,corners,faces)); function bezier_patch(patch, splinesteps=16, vnf=[[],[]]) = assert(is_num(splinesteps)||is_list(splinesteps)) is_tripatch(patch)? _bezier_triangle(patch, splinesteps=splinesteps, vnf=vnf) : let( splinesteps = is_list(splinesteps)? splinesteps : [splinesteps, splinesteps], pts = [for (v=[0:1:splinesteps.y], u=[0:1:splinesteps.x]) bezier_patch_point(patch, u/splinesteps.x, v/splinesteps.y)], faces = [ for ( v=[0:1:splinesteps.y-1], u=[0:1:splinesteps.x-1] ) let ( v1 = u+v*(splinesteps.x+1), v2 = v1 + 1, v3 = v1 + splinesteps.x + 1, v4 = v3 + 1 ) each [[v1,v3,v2], [v2,v3,v4]] ] ) vnf_merge([vnf, [pts, faces]]); function _tri_count(n) = (n*(1+n))/2; function _bezier_triangle(tri, splinesteps=16, vnf=[[],[]]) = assert(is_num(splinesteps)) let( pts = [ for ( u=[0:1:splinesteps], v=[0:1:splinesteps-u] ) bezier_triangle_point(tri, u/splinesteps, v/splinesteps) ], tricnt = _tri_count(splinesteps+1), faces = [ for ( u=[0:1:splinesteps-1], v=[0:1:splinesteps-u-1] ) let ( v1 = v + (tricnt - _tri_count(splinesteps+1-u)), v2 = v1 + 1, v3 = v + (tricnt - _tri_count(splinesteps-u)), v4 = v3 + 1, allfaces = concat( [[v1,v2,v3]], ((u= len(patches))? vnf : bezier_patch(patches[i], splinesteps=splinesteps, vnf=vnf) ) (i >= len(patches))? vnf : bezier_surface(patches=patches, splinesteps=splinesteps, i=i+1, vnf=vnf); // Section: Bezier Surface Modules // Module: bezier_polyhedron() // Useage: // bezier_polyhedron(patches, [splinesteps], [vnf]) // 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 // vnf = Vertices'n'Faces [VNF structure](vnf.scad) to add extra vertices and faces to. Default: empty VNF // 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, vnf=[[],[]]) { vnf_polyhedron( bezier_surface(patches=patches, splinesteps=splinesteps, vnf=vnf) ); } // 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