From 573c50774bfb2edae25a415ca864abb39c4c1bcf Mon Sep 17 00:00:00 2001 From: SmoothieAq Date: Sun, 14 Mar 2021 12:48:14 +0100 Subject: [PATCH] changes after review --- utils/maths.scad | 1 - utils/rounded_polygon.scad | 35 +++++++---------------------------- vitamins/belt.scad | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/utils/maths.scad b/utils/maths.scad index 46d52e9..0074f7d 100644 --- a/utils/maths.scad +++ b/utils/maths.scad @@ -155,7 +155,6 @@ function circle_intersect(c1, r1, c2, r2) = //! Calculate one point where tw a = atan2(v.z, v.x) - acos((sqr(d) + sqr(r2) - sqr(r1)) / (2 * d * r2)) // Cosine rule to find angle from c2 ) c2 + r2 * [cos(a), 0, sin(a)]; // Point on second circle -function slice(v, range) = [ for (i = range) v[i] ]; //! slice a section of a vector v, takes elements from v with index in the range function map(v, func) = [ for (e = v) func(e) ]; //! make a new vector where the func function argument is applied to each element of the vector v function mapi(v, func) = [ for (i = [0:len(v)-1]) func(i,v[i]) ]; //! make a new vector where the func function argument is applied to each element of the vector v. The func will get the index number as first argument, and the element as second argument. function reduce(v, func, unity) = let ( r = function(i,val) i == len(v) ? val : r(i + 1, func(val, v[i])) ) r(0, unity); //! reduce a vector v to a single entity by applying the func function recursivly to the reduced value so far and the next element, starting with unity as the inital reduced value diff --git a/utils/rounded_polygon.scad b/utils/rounded_polygon.scad index 8c04dbf..48f853c 100644 --- a/utils/rounded_polygon.scad +++ b/utils/rounded_polygon.scad @@ -41,8 +41,8 @@ function rounded_polygon_arcs(points, tangents) = //! Compute the arcs at the po len = len(points) ) [ for (i = [0: len-1]) let( - p1 = tangents[(i - 1 + len) % len].y, - p2 = tangents[i].x, + p1 = tangents[(i - 1 + len) % len][1], + p2 = tangents[i][0], p = points[i], v1 = p1 - p, v2 = p2 - p, @@ -57,44 +57,23 @@ function rounded_polygon_arcs(points, tangents) = //! Compute the arcs at the po ) [a, v, l] ]; -// we might want to remove the old rounded_polygon_tangents and to change rounded_polygon_length to use the v2 tangents -function rounded_polygon_tangents_v2(points) = //! Compute the straight sections between a point and the next point, for each section [start_point, end_point, length] +function rounded_polygon_tangents(points) = //! Compute the straight sections between a point and the next point, for each section [start_point, end_point, length] let(len = len(points)) [ for(i = [0 : len - 1]) let(ends = circle_tangent(points[i], points[(i + 1) % len])) - [ends.x, ends.y, norm(ends.x - ends.y)] + [ends[0], ends[1], norm(ends[0] - ends[1])] ]; -function rounded_polygon_tangents(points) = //! Compute the straight sections between a point and the next point, needed to draw and to compute the lengths - let(len = len(points)) - [for(i = [0 : len - 1]) - let(ends = circle_tangent(points[i], points[(i + 1) % len])) - for(end = [0, 1]) - ends[end]]; - -//function sumv(v, i = 0, sum = 0) = i == len(v) ? sum : sumv(v, i + 1, sum + v[i]); // moved to maths.scad - // the cross product of 2D vectors is the area of the parallelogram between them. We use the sign of this to decide if the angle is bigger than 180. function rounded_polygon_length(points, tangents) = //! Calculate the length given the point list and the list of tangents computed by ` rounded_polygon_tangents` let( - len = len(points), - indices = [0 : len - 1], - straights = [for(i = indices) norm(tangents[2 * i] - tangents[2 * i + 1])], - arcs = [for(i = indices) let(p1 = tangents[2 * i + 1], - p2 = tangents[(2 * i + 2) % (2 * len)], - corner = points[(i + 1) % len], - c = [corner.x, corner.y], - v1 = p1 - c, - v2 = p2 - c, - r = abs(corner.z), - a = acos((v1 * v2) / sqr(r))) r ? PI * (cross(v1, v2) <= 0 ? a : 360 - a) * r / 180 : 0] - ) - sumv(concat(straights, arcs)); + arcs = rounded_polygon_arcs(points, tangents) + ) sumv( map( concat(tangents, arcs), function(e) e[2] ) ); module rounded_polygon(points, _tangents = undef) { //! Draw the rounded polygon from the point list, can pass the tangent list to save it being calculated len = len(points); indices = [0 : len - 1]; - tangents = _tangents ? _tangents : rounded_polygon_tangents(points); + tangents = [ for (t = _tangents ? _tangents : rounded_polygon_tangents(points)) each [t.x, t.y] ]; difference(convexity = points) { union() { diff --git a/vitamins/belt.scad b/vitamins/belt.scad index b8cd64d..aaa8ecc 100644 --- a/vitamins/belt.scad +++ b/vitamins/belt.scad @@ -65,7 +65,7 @@ module belt(type, points, gap = 0, gap_pos = undef, belt_colour = grey(20), toot pointsx = info[2]; // array of [x,y,r], r is negative if left-angle (points may have pulleys as third element, but pointsx have radi) tangents = info[3]; arcs = info[4]; - length = _belt_length(type, info, open, gap); + length = ceil(_belt_length(type, info, open, gap) / pitch) * pitch; part = str(type[0],pitch); vitamin(str("xbelt(", no_point(part), "x", width, ", ", points, "): Belt ", part," x ", width, "mm x ", length, "mm")); @@ -101,10 +101,10 @@ module belt(type, points, gap = 0, gap_pos = undef, belt_colour = grey(20), toot for (i = [(open?1:0):len-(open?2:1)]) { p = pointsx[i]; arc = arcs[i]; - translate([p.x,p.y,0]) rotate([0,0,arc.y]) { - mirrored = !xor(twisted[i], p.z < 0) ? 1 : 0; - color(tooth_colour) rotate_extrude(angle=arc.x) translate([abs(p.z),0,0]) mirror([mirrored,0,0]) beltp(); - color(belt_colour) rotate_extrude(angle=arc.x) translate([abs(p.z),0,0]) mirror([mirrored,0,0]) beltb(); + translate([p.x,p.y,0]) rotate([0,0,arc[1]]) { + mirrored = xor(twisted[i], p[2] < 0) ? 0 : 1; + color(tooth_colour) rotate_extrude(angle=arc[0]) translate([abs(p[2]),0,0]) mirror([mirrored,0,0]) beltp(); + color(belt_colour) rotate_extrude(angle=arc[0]) translate([abs(p[2]),0,0]) mirror([mirrored,0,0]) beltb(); } } @@ -121,7 +121,7 @@ let( dotwist = function(i,istwisted) let( in = (i + 1) % len ) is_list(twist) ? twist[i] : !is_undef(twist) ? i == twist : - open && is_list(points[in].z) && auto_twist ? !pulley_teeth(points[in].z) && !xor(isleft(in),istwisted) : + open && is_list(points[in][2]) && auto_twist ? !pulley_teeth(points[in][2]) && !xor(isleft(in),istwisted) : false, twisted = [ for ( i = 0, @@ -134,13 +134,13 @@ let( twist = dotwist(i,istwisted), nexttwisted = xor(twist,istwisted) ) [twist,istwisted] ], - pointsx = mapi(points, function(i, p) !is_list(p.z) ? p : [p.x, p.y, let( // if p.z is not a list it is just r, otherwise it is taken to be a pulley and we calculate r + pointsx = mapi(points, function(i, p) !is_list(p[2]) ? p : [p.x, p.y, let( // if p[2] is not a list it is just r, otherwise it is taken to be a pulley and we calculate r isleft = isleft(i), - r = belt_pulley_pr(type, p.z, twisted=!xor(pulley_teeth(p.z),xor(isleft, twisted[i].y))) + r = belt_pulley_pr(type, p[2], twisted=!xor(pulley_teeth(p[2]),xor(isleft, twisted[i][1]))) ) isleft ? -r : r ] ), - tangents = rounded_polygon_tangents_v2(pointsx), + tangents = rounded_polygon_tangents(pointsx), arcs = rounded_polygon_arcs(pointsx, tangents) -) [ [ for (t = twisted) t.x ], [ for (t = twisted) t.y ], pointsx, tangents, arcs]; +) [ [ for (t = twisted) t[0] ], [ for (t = twisted) t[1] ], pointsx, tangents, arcs]; function belt_pulley_pr(type, pulley, twisted=false) = //! Pitch radius. Default it expects the belt tooth to be against a toothed pulley an the backside to be against a smooth pulley (an idler). If `twisted` is true, the the belt is the other way around. let( @@ -157,8 +157,8 @@ function _belt_length(type, info, open, gap) = let( len = len(info[0]), pitch = belt_pitch(type), d = open ? 1 : 0, - tangents = slice(info[3], [0:len - 1 - d]) , - arcs = slice(info[4], [d:len - 1 - d]), - beltl = sumv( map( concat(tangents, arcs), function(e) e.z ) ), + tangents = slice(info[3], 0, len - d) , + arcs = slice(info[4], d, len - d), + beltl = sumv( map( concat(tangents, arcs), function(e) e[2] ) ), gapl = is_list(gap) ? gap.x : is_undef(gap) ? 0 : gap -) ceil((beltl - gapl) / pitch) * pitch; +) beltl - gapl;