diff --git a/src/along_with.scad b/src/along_with.scad index a8fd9e34..6108cbda 100644 --- a/src/along_with.scad +++ b/src/along_with.scad @@ -31,52 +31,67 @@ module along_with(points, angles, twist = 0, scale = 1.0) { scale[2] == undef ? 0 : (scale[2] - 1) / leng_points_minus_one ] : scale_step(); - end_i = $children == 1 ? leng_points_minus_one : $children - 1; - - function _path_angles(pts, i = 0) = - i == end_i ? - [] : - concat( - [__angy_angz(pts[i], pts[i + 1])], - _path_angles(pts, i + 1) - ); - - function path_angles() = - let( - pts = len(points[0]) == 3 ? points : [for(pt = points) __to3d(pt)], - angs = _path_angles(pts) - ) - concat( - [[0, -angs[0][0], angs[0][1]]], - [for(a = angs) [0, -a[0], a[1]]] - ); - - angles_defined = angles != undef; - angs = angles_defined ? angles : path_angles(points); - - module align(i) { + module align_with_pts_angles(i) { translate(points[i]) - rotate(angs[i]) - rotate(angles_defined ? [0, 0, 0] : [90, 0, -90]) - rotate(twist_step_a * i) - scale([1, 1, 1] + scale_step_vt * i) - children(0); + rotate(angles[i]) + rotate(twist_step_a * i) + scale([1, 1, 1] + scale_step_vt * i) + children(0); } - if($children == 1) { - for(i = [0:leng_points_minus_one]) { - align(i) children(0); - } - } else { - for(i = [0:min(leng_points, $children) - 1]) { - align(i) children(i); - } + module align_with_pts_init(a, s) { + angleyz = __angy_angz(points[0], points[1]); + rotate([0, -angleyz[0], angleyz[1]]) + rotate([90, 0, -90]) + rotate(a) + scale(s) + children(0); } - - // hook for testing - test_along_with_angles(angs); -} - -module test_along_with_angles(angles) { + module align_with_pts_local_rotate(j, init_a, init_s) { + if(j == 0) { // first child + align_with_pts_init(init_a, init_s) + children(0); + } + else { + vt0 = points[j] - points[j - 1]; + vt1 = points[j + 1] - points[j]; + a = acos((vt0 * vt1) / (norm(vt0) * norm(vt1))); + rotate(a, cross(vt0, vt1)) + align_with_pts_local_rotate(j - 1, init_a, init_s) + children(0); + } + } + + if(angles != undef) { + if($children == 1) { + for(i = [0:leng_points_minus_one]) { + align_with_pts_angles(i) children(0); + } + } else { + for(i = [0:min(leng_points, $children) - 1]) { + align_with_pts_angles(i) children(i); + } + } + } + else { + translate(points[0]) + align_with_pts_local_rotate(0, 0, [1, 1, 1]) + children(0); + + if($children == 1) { + for(i = [0:leng_points - 2]) { + translate(points[i + 1]) + align_with_pts_local_rotate(i, i * twist_step_a, [1, 1, 1] + scale_step_vt * i) + children(0); + } + } else { + for(i = [0:min(leng_points, $children) - 2]) { + translate(points[i + 1]) + align_with_pts_local_rotate(i, i * twist_step_a, [1, 1, 1] + scale_step_vt * i) + children(i + 1); + } + } + } + } \ No newline at end of file diff --git a/test/test_all.scad b/test/test_all.scad index d2a19af0..2cc38b45 100644 --- a/test/test_all.scad +++ b/test/test_all.scad @@ -16,7 +16,6 @@ include ; include ; // Transformation -include ; include ; // Function diff --git a/test/test_along_with.scad b/test/test_along_with.scad deleted file mode 100644 index b8a9569c..00000000 --- a/test/test_along_with.scad +++ /dev/null @@ -1,58 +0,0 @@ -include ; - -module test_along_with_default_angles() { - echo("==== test_along_with_default_angles ===="); - - include ; - include ; - - $fn = 24; - - points = circle_path(radius = 50); - - module test_along_with_angles(angles) { - expected_angles = [[0, 0, 97.5], [0, 0, 97.5], [0, 0, 112.5], [0, 0, 127.5], [0, 0, 142.5], [0, 0, 157.5], [0, 0, 172.5], [0, 0, -172.5], [0, 0, -157.5], [0, 0, -142.5], [0, 0, -127.5], [0, 0, -112.5], [0, 0, -97.5], [0, 0, -82.5], [0, 0, -67.5], [0, 0, -52.5], [0, 0, -37.5], [0, 0, -22.5], [0, 0, -7.5], [0, 0, 7.5], [0, 0, 22.5], [0, 0, 37.5], [0, 0, 52.5], [0, 0, 67.5]]; - - assertEqual($fn, len(angles)); - assertEqualPoints(expected_angles, angles); - - } - - along_with(points) - sphere(5, center = true); - -} - -module test_along_with_children() { - echo("==== test_along_with_children ===="); - - include ; - include ; - - $fn = 24; - - points = circle_path(radius = 50); - - module test_along_with_angles(angles) { - expected_angles = [[0, 0, 97.5], [0, 0, 97.5], [0, 0, 112.5], [0, 0, 127.5], [0, 0, 142.5], [0, 0, 157.5], [0, 0, 172.5], [0, 0, -172.5]]; - - assertEqual(8, len(angles)); - assertEqualPoints(expected_angles, angles); - - } - - along_with(points) { - linear_extrude(10, center = true) text("A", valign = "center", halign = "center"); - linear_extrude(5, center = true) circle(2); - sphere(1); - cube(5); - linear_extrude(10, center = true) text("A", valign = "center", halign = "center"); - linear_extrude(5, center = true) circle(2); - sphere(1); - cube(5); - } - -} - -test_along_with_default_angles(); -test_along_with_children(); \ No newline at end of file