////////////////////////////////////////////////////////////////////// // 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: bezier_points() // Usage: // bezier_points(curve, u) // Description: // Computes bezier points for bezier with control points specified by `curve` at parameter values specified by `u`, which can be a scalar or a list. // This function uses an optimized method which is best when `u` is a long list and the bezier degree is 10 or less. // The degree of the bezier curve given is `len(curve)-1`. // 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 If given as a list or range, returns a list of point, one for each u value. // Example(2D): Quadratic (Degree 2) Bezier. // bez = [[0,0], [30,30], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // translate(bezier_points(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(bezier_points(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(bezier_points(bez, 0.8)) color("red") sphere(1); // Example(2D): Giving a List of `u` // bez = [[0,0], [5,35], [60,-25], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // pts = bezier_points(bez, [0, 0.2, 0.3, 0.7, 0.8, 1]); // rainbow(pts) move($item) sphere(1.5, $fn=12); // Example(2D): Giving a Range of `u` // bez = [[0,0], [5,35], [60,-25], [80,0]]; // trace_bezier(bez, N=len(bez)-1); // pts = bezier_points(bez, [0:0.2:1]); // rainbow(pts) move($item) sphere(1.5, $fn=12); // Ugly but speed optimized code for computing bezier curves using the matrix representation // See https://pomax.github.io/bezierinfo/#matrix for explanation. // // All of the loop unrolling makes and the use of the matrix lookup table make a big difference // in the speed of execution. For orders 10 and below this code is 10-20 times faster than // the recursive code using the de Casteljau method depending on the bezier order and the // number of points evaluated in one call (more points is faster). For orders 11 and above without the // lookup table or hard coded powers list the code is about twice as fast as the recursive method. // Note that everything I tried to simplify or tidy this code made is slower, sometimes a lot slower. function bezier_points(curve, u) = is_num(u) ? bezier_points(curve,[u])[0] : let( N = len(curve)-1, M = _bezier_matrix(N)*curve ) N==0 ? [for(uval=u)[1]*M] : N==1 ? [for(uval=u)[1, uval]*M] : N==2 ? [for(uval=u)[1, uval, uval*uval]*M] : N==3 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval]*M] : // It appears that pow() is as fast or faster for powers 5 or above N==4 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval]*M] : N==5 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5)]*M] : N==6 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5),pow(uval,6)]*M] : N==7 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5),pow(uval,6), pow(uval,7)]*M] : N==8 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5),pow(uval,6), pow(uval,7), pow(uval,8)]*M] : N==9 ? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5),pow(uval,6), pow(uval,7), pow(uval,8), pow(uval,9)]*M] : N==10? [for(uval=u)[1, uval, uval*uval, uval*uval*uval, uval*uval*uval*uval, pow(uval,5),pow(uval,6), pow(uval,7), pow(uval,8), pow(uval,9), pow(uval,10)]*M] : /* N>=11 */ [for(uval=u)[for (i=[0:1:N]) pow(uval,i)]*M]; // Not public. function _signed_pascals_triangle(N,tri=[[-1]]) = len(tri)==N+1 ? tri : let(last=tri[len(tri)-1]) _signed_pascals_triangle(N,concat(tri,[[-1, for(i=[0:1:len(tri)-2]) (i%2==1?-1:1)*(abs(last[i])+abs(last[i+1])),len(last)%2==0? -1:1]])); // Not public. function _compute_bezier_matrix(N) = let(tri = _signed_pascals_triangle(N)) [for(i=[0:N]) concat(tri[N][i]*tri[i], repeat(0,N-i))]; // The bezier matrix, which is related to Pascal's triangle, enables nonrecursive computation // of bezier points. This method is much faster than the recursive de Casteljau method // in OpenScad, but we have to precompute the matrices to reap the full benefit. // Not public. _bezier_matrix_table = [ [[1]], [[ 1, 0], [-1, 1]], [[1, 0, 0], [-2, 2, 0], [1, -2, 1]], [[ 1, 0, 0, 0], [-3, 3, 0, 0], [ 3,-6, 3, 0], [-1, 3,-3, 1]], [[ 1, 0, 0, 0, 0], [-4, 4, 0, 0, 0], [ 6,-12, 6, 0, 0], [-4, 12,-12, 4, 0], [ 1, -4, 6,-4, 1]], [[ 1, 0, 0, 0, 0, 0], [ -5, 5, 0, 0, 0, 0], [ 10,-20, 10, 0, 0, 0], [-10, 30,-30, 10, 0, 0], [ 5,-20, 30,-20, 5, 0], [ -1, 5,-10, 10,-5, 1]], [[ 1, 0, 0, 0, 0, 0, 0], [ -6, 6, 0, 0, 0, 0, 0], [ 15,-30, 15, 0, 0, 0, 0], [-20, 60,-60, 20, 0, 0, 0], [ 15,-60, 90,-60, 15, 0, 0], [ -6, 30,-60, 60,-30, 6, 0], [ 1, -6, 15,-20, 15,-6, 1]], [[ 1, 0, 0, 0, 0, 0, 0, 0], [ -7, 7, 0, 0, 0, 0, 0, 0], [ 21, -42, 21, 0, 0, 0, 0, 0], [-35, 105,-105, 35, 0, 0, 0, 0], [ 35,-140, 210,-140, 35, 0, 0, 0], [-21, 105,-210, 210,-105, 21, 0, 0], [ 7, -42, 105,-140, 105,-42, 7, 0], [ -1, 7, -21, 35, -35, 21,-7, 1]], [[ 1, 0, 0, 0, 0, 0, 0, 0, 0], [ -8, 8, 0, 0, 0, 0, 0, 0, 0], [ 28, -56, 28, 0, 0, 0, 0, 0, 0], [-56, 168,-168, 56, 0, 0, 0, 0, 0], [ 70,-280, 420,-280, 70, 0, 0, 0, 0], [-56, 280,-560, 560,-280, 56, 0, 0, 0], [ 28,-168, 420,-560, 420,-168, 28, 0, 0], [ -8, 56,-168, 280,-280, 168,-56, 8, 0], [ 1, -8, 28, -56, 70, -56, 28,-8, 1]], [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [-9, 9, 0, 0, 0, 0, 0, 0, 0, 0], [36, -72, 36, 0, 0, 0, 0, 0, 0, 0], [-84, 252, -252, 84, 0, 0, 0, 0, 0, 0], [126, -504, 756, -504, 126, 0, 0, 0, 0, 0], [-126, 630, -1260, 1260, -630, 126, 0, 0, 0, 0], [84, -504, 1260, -1680, 1260, -504, 84, 0, 0, 0], [-36, 252, -756, 1260, -1260, 756, -252, 36, 0, 0], [9, -72, 252, -504, 630, -504, 252, -72, 9, 0], [-1, 9, -36, 84, -126, 126, -84, 36, -9, 1]], [[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [-10, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0], [45, -90, 45, 0, 0, 0, 0, 0, 0, 0, 0], [-120, 360, -360, 120, 0, 0, 0, 0, 0, 0, 0], [210, -840, 1260, -840, 210, 0, 0, 0, 0, 0, 0], [-252, 1260, -2520, 2520, -1260, 252, 0, 0, 0, 0, 0], [210, -1260, 3150, -4200, 3150, -1260, 210, 0, 0, 0, 0], [-120, 840, -2520, 4200, -4200, 2520, -840, 120, 0, 0, 0], [45, -360, 1260, -2520, 3150, -2520, 1260, -360, 45, 0, 0], [-10, 90, -360, 840, -1260, 1260, -840, 360, -90, 10, 0], [1, -10, 45, -120, 210, -252, 210, -120, 45, -10, 1]] ]; // Not public. function _bezier_matrix(N) = N>10 ? _compute_bezier_matrix(N) : _bezier_matrix_table[N]; // Function: bezier_derivative() // Usage: // d = bezier_derivative(curve, u, [order]); // Description: // Finds the `order`th derivative of the bezier segment at the given position `u`. // The degree of the bezier segment 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 derivative of. 0<=`u`<=1 If given as a list or range, returns a list of derivatives, one for each u value. // order = The order of the derivative to return. Default: 1 (for the first derivative) function bezier_derivative(curve, u, order=1) = assert(is_int(order) && order>=0) order==0? bezier_points(curve, u) : let( N = len(curve) - 1, dpts = N * deltas(curve) ) order==1? bezier_points(dpts, u) : bezier_derivative(dpts, u, order-1); // Function: bezier_tangent() // Usage: // tanvec= bezier_tangent(curve, u); // Description: // Returns the unit vector of the tangent at the given position `u` on the bezier segment `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 tangent vector of. 0<=`u`<=1 If given as a list or range, returns a list of tangent vectors, one for each u value. function bezier_tangent(curve, u) = let( res = bezier_derivative(curve, u) ) is_vector(res)? unit(res) : [for (v=res) unit(v)]; // Function: bezier_curvature() // Usage: // crv = bezier_curvature(curve, u); // Description: // Returns the curvature value for the given position `u` on the bezier segment `curve`. // The curvature is the inverse of the radius of the tangent circle at the given point. // Thus, the tighter the curve, the larger the curvature value. Curvature will be 0 for // a position with no curvature, since 1/0 is not a number. // 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 curvature of. 0<=`u`<=1 If given as a list or range, returns a list of curvature values, one for each u value. function bezier_curvature(curve, u) = is_num(u) ? bezier_curvature(curve,[u])[0] : let( d1 = bezier_derivative(curve, u, 1), d2 = bezier_derivative(curve, u, 2) ) [ for(i=idx(d1)) sqrt( sqr(norm(d1[i])*norm(d2[i])) - sqr(d1[i]*d2[i]) ) / pow(norm(d1[i]),3) ]; // 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]]; // move_copies(bezier_curve(bez, 8)) sphere(r=1.5, $fn=12); // trace_bezier(bez, N=len(bez)-1); // Example(2D): Cubic (Degree 3) Bezier // bez = [[0,0], [5,35], [60,-25], [80,0]]; // move_copies(bezier_curve(bez, 8)) sphere(r=1.5, $fn=12); // trace_bezier(bez, N=len(bez)-1); // Example(2D): Degree 4 Bezier. // bez = [[0,0], [5,15], [40,20], [60,-15], [80,0]]; // move_copies(bezier_curve(bez, 8)) sphere(r=1.5, $fn=12); // trace_bezier(bez, N=len(bez)-1); function bezier_curve(curve,n) = bezier_points(curve, [0:1/n:(n-0.5)/n]); // 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(bezier_points(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, uvals = [u, for (i=[0:1:steps]) (end_u-u)*(i/steps)+u, end_u], path = bezier_points(curve,uvals), minima_ranges = [ for (i = [1:1:len(uvals)-2]) let( d1 = norm(path[i-1]-pt), d2 = norm(path[i ]-pt), d3 = norm(path[i+1]-pt) ) if (d2<=d1 && d2<=d3) [uvals[i-1],uvals[i+1]] ] ) 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(bezier_points(curve,v)-pt)], min_i = min_index(dists) ) min_us[min_i] ) : let( minima = minima_ranges[0], pp = bezier_points(curve, minima), err = norm(pp[1]-pp[0]) ) err= 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(bezier_points(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], [style]); // 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 // style = The style of subdividing the quads into faces. Valid options are "default", "alt", and "quincunx". // Example(3D): // patch = [ // // u=0,v=0 u=1,v=0 // [[-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]], // // u=0,v=1 u=1,v=1 // ]; // 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 = [ // // u=0,v=0 u=1,v=0 // [[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]], // // u=0,v=1 u=1,v=1 // ]; // 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): Connecting Patches with Asymmetric Splinesteps // steps = 8; // edge_patch = [ // // u=0, v=0 u=1,v=0 // [[-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]], // // u=0, v=1 u=1,v=1 // ]; // corner_patch = [ // // u=0, v=0 u=1,v=0 // [[ 0, 40,-40], [ 0, 0,-40], [40, 0,-40]], // [[ 0, 40, 0], [ 0, 0, 0], [40, 0, 0]], // [[40, 40, 0], [40, 40, 0], [40, 40, 0]], // // u=0, v=1 u=1,v=1 // ]; // 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=[steps,1], // rot(a=axrot, // p=rot(a=[xang,0,0], // p=translate(v=[0,-100,100],p=edge_patch) // ) // ) // ) // ]; // corners = [ // for (xang=[0,180], zang=[-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=move([-100,0,0], p=face_patch) // ) // ) // ) // ]; // vnf_polyhedron(concat(edges,corners,faces)); function bezier_patch(patch, splinesteps=16, vnf=EMPTY_VNF, style="default") = assert(is_num(splinesteps) || is_vector(splinesteps,2)) is_tripatch(patch)? _bezier_triangle(patch, splinesteps=splinesteps, vnf=vnf) : let( splinesteps = is_list(splinesteps) ? splinesteps : [splinesteps,splinesteps], uvals = [ for(step=[0:1:splinesteps.x]) step/splinesteps.x ], vvals = [ for(step=[0:1:splinesteps.y]) 1-step/splinesteps.y ], pts = bezier_patch_points(patch, uvals, vvals), vnf = vnf_vertex_array(pts, style=style, vnf=vnf, reverse=false) ) vnf; function _tri_count(n) = (n*(1+n))/2; function _bezier_triangle(tri, splinesteps=16, vnf=EMPTY_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, style=style) ) (i >= len(patches))? vnf : bezier_surface(patches=patches, splinesteps=splinesteps, vnf=vnf, style=style, i=i+1); // Section: Bezier Surface Modules // Module: bezier_polyhedron() // Useage: // bezier_polyhedron(patches, [splinesteps], [vnf], [style], [convexity]) // 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 // style = The style of subdividing the quads into faces. Valid options are "default", "alt", and "quincunx". // convexity = Max number of times a line could intersect a wall of the shape. // 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,82,0], [33,100, 0], [ 67,100, 0], [ 82, 82,0]], // [[ 0,60,0], [ 0,100,-50], [100,100,-50], [100, 60,0]], // [[ 0,40,0], [ 0, 0,-50], [100, 0,-50], [100, 40,0]], // [[18,18,0], [33, 0, 0], [ 67, 0, 0], [ 82, 18,0]], // ]; // bezier_polyhedron([patch1, patch2], splinesteps=8); module bezier_polyhedron(patches=[], splinesteps=16, vnf=EMPTY_VNF, style="default", convexity=10) { vnf_polyhedron( bezier_surface(patches=patches, splinesteps=splinesteps, vnf=vnf, style=style), convexity=convexity ); } // Module: trace_bezier_patches() // Usage: // trace_bezier_patches(patches, [size], [splinesteps], [showcps], [showdots], [showpatch], [convexity], [style]); // 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. Default: true. // showdots = If true, shows the calculated surface vertices. Default: false. // showpatch = If true, shows the surface faces. Default: true. // size = Size to show control points and lines. // style = The style of subdividing the quads into faces. Valid options are "default", "alt", and "quincunx". // convexity = Max number of times a line could intersect a wall of the shape. // 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,85,0], [33,100, 0], [ 67,100, 0], [ 85, 85,0]], // [[ 0,67,0], [33, 67,-50], [ 67, 67,-50], [100, 67,0]], // [[ 0,33,0], [33, 33,-50], [ 67, 33,-50], [100, 33,0]], // [[15,15,0], [33, 0, 0], [ 67, 0, 0], [ 85, 15,0]], // ]; // trace_bezier_patches(patches=[patch1, patch2], splinesteps=8, showcps=true); module trace_bezier_patches(patches=[], size, splinesteps=16, showcps=true, showdots=false, showpatch=true, convexity=10, style="default") { assert(is_undef(size)||is_num(size)); assert(is_int(splinesteps) && splinesteps>0); assert(is_list(patches) && all([for (patch=patches) is_patch(patch)])); assert(is_bool(showcps)); assert(is_bool(showdots)); assert(is_bool(showpatch)); assert(is_int(convexity) && convexity>0); vnfs = [ for (patch = patches) bezier_patch(patch, splinesteps=splinesteps, style=style) ]; if (showcps || showdots) { for (patch = patches) { size = is_num(size)? size : let( bounds = pointlist_bounds(flatten(patch)) ) max(bounds[1]-bounds[0])*0.01; if (showcps) { move_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