From d9c890d8c3d14aa0041a31c9fb0532fedeb57440 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 4 Mar 2022 15:58:33 +0800 Subject: [PATCH] refactor --- src/path_extrude.scad | 16 +++--- src/ring_extrude.scad | 10 +++- src/rounded_cube.scad | 6 +- src/rounded_cylinder.scad | 6 +- src/starburst.scad | 2 +- src/sweep.scad | 43 +++++--------- .../_impl/_tri_delaunay_voronoi_impl.scad | 7 +-- src/triangle/tri_delaunay_voronoi.scad | 14 +++-- src/turtle/footprints2.scad | 7 +-- src/turtle/footprints3.scad | 7 +-- src/util/swap.scad | 6 +- src/voronoi/_impl/_convex_ct_clk_order.scad | 5 +- src/voronoi/_impl/_vrn2_space_cells_impl.scad | 4 +- src/voronoi/vrn2_cells_space.scad | 8 +-- src/voronoi/vrn2_space.scad | 8 +-- src/voronoi/vrn3_space.scad | 57 +++++++++---------- src/voxel/_impl/_vx_cylinder_impl.scad | 5 +- src/voxel/vx_polygon.scad | 8 +-- 18 files changed, 99 insertions(+), 120 deletions(-) diff --git a/src/path_extrude.scad b/src/path_extrude.scad index 55abcde1..93d9f62e 100644 --- a/src/path_extrude.scad +++ b/src/path_extrude.scad @@ -38,7 +38,7 @@ module path_extrude(shape_pts, path_pts, triangles = "SOLID", twist = 0, scale = [ (scale.x - 1) / len_path_pts_minus_one, (scale.y - 1) / len_path_pts_minus_one, - is_undef(scale[2]) ? 0 : (scale.z - 1) / len_path_pts_minus_one + is_undef(scale.z) ? 0 : (scale.z - 1) / len_path_pts_minus_one ]; // get rotation matrice for sections @@ -114,15 +114,17 @@ module path_extrude(shape_pts, path_pts, triangles = "SOLID", twist = 0, scale = let( vt0 = pth_pts[j] - pth_pts[j - 1], vt1 = pth_pts[j + 1] - pth_pts[j], - ms = cumu_rot_matrice[j - 1] + ms = cumu_rot_matrice[j - 1], + ms0 = ms[0], + ms1 = ms[1], + ms2 = ms[2], + ms0p = [ms0[0], ms0[1], ms0[2]], + ms1p = [ms1[0], ms1[1], ms1[2]], + ms2p = [ms2[0], ms2[1], ms2[2]] ) [ for(p = init_section(init_a, init_s)) - [ - [ms[0][0], ms[0][1], ms[0][2]] * p, - [ms[1][0], ms[1][1], ms[1][2]] * p, - [ms[2][0], ms[2][1], ms[2][2]] * p - ] + [ms0p * p, ms1p * p, ms2p * p] ]; sections = diff --git a/src/ring_extrude.scad b/src/ring_extrude.scad index 11cf4760..400154fd 100644 --- a/src/ring_extrude.scad +++ b/src/ring_extrude.scad @@ -31,9 +31,15 @@ module ring_extrude(shape_pts, radius, angle = 360, twist = 0, scale = 1.0, tria begin_r = leng / cos((m - 0.5) * a_step - angles[0]); end_r = leng / cos((n + 0.5) * a_step - angles[1]); - angs = [[90, 0, angles[0]], each (m > n ? [] : [for(i = [m:n]) [90, 0, a_step * i]])]; + angs = [ + [90, 0, angles[0]], + if(m <= n) each [for(i = [m:n]) [90, 0, a_step * i]] + ]; - pts = [__ra_to_xy(begin_r, angles[0]), each (m > n ? [] : [for(i = [m:n]) __ra_to_xy(radius, a_step * i)])]; + pts = [ + __ra_to_xy(begin_r, angles[0]), + if(m <= n) each [for(i = [m:n]) __ra_to_xy(radius, a_step * i)] + ]; is_angle_frag_end = angs[len(angs) - 1][2] == angles[1]; diff --git a/src/rounded_cube.scad b/src/rounded_cube.scad index 0facf39c..b4b917f7 100644 --- a/src/rounded_cube.scad +++ b/src/rounded_cube.scad @@ -30,10 +30,8 @@ module rounded_cube(size, corner_r, center = false) { pair = [1, -1]; corners = [ - for(z = pair) - for(y = pair) - for(x = pair) - [half_l * x, half_w * y, half_h * z] + for(z = pair, y = pair, x = pair) + [half_l * x, half_w * y, half_h * z] ]; module corner(i) { diff --git a/src/rounded_cylinder.scad b/src/rounded_cylinder.scad index b0222afc..ce4ad9e1 100644 --- a/src/rounded_cylinder.scad +++ b/src/rounded_cylinder.scad @@ -12,10 +12,10 @@ use <__comm__/__half_trapezium.scad>; module rounded_cylinder(radius, h, round_r, convexity = 2, center = false) { r_corners = __half_trapezium(radius, h, round_r); - - shape_pts = [[0, -h/2], each r_corners, [0, h/2]]; + half_h = h / 2; + shape_pts = [[0, -half_h], each r_corners, [0, half_h]]; - center_pt = center ? [0, 0, 0] : [0, 0, h/2]; + center_pt = center ? [0, 0, 0] : [0, 0, half_h]; translate(center_pt) rotate(180) diff --git a/src/starburst.scad b/src/starburst.scad index af75abc9..da587f01 100644 --- a/src/starburst.scad +++ b/src/starburst.scad @@ -32,7 +32,7 @@ module starburst(r1, r2, n, height) { module burst() { hull() { half_burst(); - mirror([0, 1,0]) half_burst(); + mirror([0, 1, 0]) half_burst(); } } diff --git a/src/sweep.scad b/src/sweep.scad index ab416684..6329daae 100644 --- a/src/sweep.scad +++ b/src/sweep.scad @@ -21,22 +21,20 @@ module sweep(sections, triangles = "SOLID") { ) concat( [ - for(j = range_j) - for(i = range_i) - [ - j + i, - j + (i + 1) % leng_pts_sect, - j + (i + 1) % leng_pts_sect + leng_pts_sect - ] + for(j = range_j, i = range_i) + [ + j + i, + j + (i + 1) % leng_pts_sect, + j + (i + 1) % leng_pts_sect + leng_pts_sect + ] ], [ - for(j = range_j) - for(i = range_i) - [ - j + i, - j + (i + 1) % leng_pts_sect + leng_pts_sect , - j + i + leng_pts_sect - ] + for(j = range_j, i = range_i) + [ + j + i, + j + (i + 1) % leng_pts_sect + leng_pts_sect , + j + i + leng_pts_sect + ] ] ); @@ -45,21 +43,14 @@ module sweep(sections, triangles = "SOLID") { (p == f_sect[i] ? i : search_at(f_sect, p, leng_pts_sect, i + 1)) : -1; function the_same_after_twisting(f_sect, l_sect, leng_pts_sect) = - let( - found_at_i = search_at(f_sect, l_sect[0], leng_pts_sect) - ) + let(found_at_i = search_at(f_sect, l_sect[0], leng_pts_sect)) found_at_i <= 0 ? false : l_sect == concat( [for(i = found_at_i; i < leng_pts_sect; i = i + 1) f_sect[i]], [for(i = 0; i < found_at_i; i = i + 1) f_sect[i]] ); - function to_v_pts(sects) = - [ - for(sect = sects) - for(pt = sect) - pt - ]; + function to_v_pts(sects) = [for(sect = sects) each sect]; module solid_sections(sects) { @@ -68,11 +59,7 @@ module sweep(sections, triangles = "SOLID") { first_sect = sects[0]; last_sect = sects[leng_sects - 1]; - v_pts = [ - for(sect = sects) - for(pt = sect) - pt - ]; + v_pts = [for(sect = sects) each sect]; begin_end_the_same = first_sect == last_sect || diff --git a/src/triangle/_impl/_tri_delaunay_voronoi_impl.scad b/src/triangle/_impl/_tri_delaunay_voronoi_impl.scad index 9b91bb91..eb4b871a 100644 --- a/src/triangle/_impl/_tri_delaunay_voronoi_impl.scad +++ b/src/triangle/_impl/_tri_delaunay_voronoi_impl.scad @@ -3,12 +3,7 @@ use <../../util/map/hashmap_get.scad>; use <../../util/find_index.scad>; function indicesOfCell(iTris, triIndices) = - let( - vi = iTris[0][0], - indices = [], - leng = len(iTris) - ) - _indicesOfCell(iTris, triIndices, leng, indices, vi); + _indicesOfCell(iTris, triIndices, len(iTris), [], iTris[0][0]); function _indicesOfCell(iTris, triIndices, leng, indices, vi, i = 0) = i == leng ? indices : diff --git a/src/triangle/tri_delaunay_voronoi.scad b/src/triangle/tri_delaunay_voronoi.scad index e4103194..e44469ca 100644 --- a/src/triangle/tri_delaunay_voronoi.scad +++ b/src/triangle/tri_delaunay_voronoi.scad @@ -28,9 +28,10 @@ function tri_delaunay_voronoi(d) = i_rts = [ for(i = i_range) let( - a = tris[i][0], - b = tris[i][1], - c = tris[i][2], + tris_i = tris[i], + a = tris_i[0], + b = tris_i[1], + c = tris_i[2], rt1 = [b, c, a], rt2 = [c, a, b], rt3 = [a, b, c] @@ -44,9 +45,10 @@ function tri_delaunay_voronoi(d) = triIndices = hashmap([ for(i = i_range) let( - a = tris[i][0], - b = tris[i][1], - c = tris[i][2], + tris_i = tris[i], + a = tris_i[0], + b = tris_i[1], + c = tris_i[2], rt1 = [b, c, a], rt2 = [c, a, b], rt3 = [a, b, c] diff --git a/src/turtle/footprints2.scad b/src/turtle/footprints2.scad index 4f7ee152..baa5e46f 100644 --- a/src/turtle/footprints2.scad +++ b/src/turtle/footprints2.scad @@ -12,8 +12,5 @@ use <_impl/_footprints2.scad>; use ; function footprints2(cmds, start = [0, 0]) = - let( - t = turtle2d("create", start.x, start.y, 0), - leng = len(cmds) - ) - [turtle2d("pt", t), each _footprints2(cmds, t, leng)]; \ No newline at end of file + let(t = turtle2d("create", start.x, start.y, 0)) + [turtle2d("pt", t), each _footprints2(cmds, t, len(cmds))]; \ No newline at end of file diff --git a/src/turtle/footprints3.scad b/src/turtle/footprints3.scad index 54e29c22..fe91921b 100644 --- a/src/turtle/footprints3.scad +++ b/src/turtle/footprints3.scad @@ -12,8 +12,5 @@ use <_impl/_footprints3.scad>; use ; function footprints3(cmds, start = [0, 0, 0]) = - let( - t = turtle3d("create", start, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]), - leng = len(cmds) - ) - [turtle3d("pt", t), each _footprints3(cmds, t, leng)]; \ No newline at end of file + let(t = turtle3d("create", start, [[1, 0, 0], [0, 1, 0], [0, 0, 1]])) + [turtle3d("pt", t), each _footprints3(cmds, t, len(cmds))]; \ No newline at end of file diff --git a/src/util/swap.scad b/src/util/swap.scad index 5f36c6e6..5c86e687 100644 --- a/src/util/swap.scad +++ b/src/util/swap.scad @@ -16,9 +16,9 @@ function swap(lt, i, j) = b = max([i, j]) ) [ - each (a == 0 ? [] : [for(idx = [0:a - 1]) lt[idx]]), + if(a != 0) each [for(idx = [0:a - 1]) lt[idx]], lt[b], - each (b - a == 1 ? [] : [for(idx = [a + 1:b - 1]) lt[idx]]), + if(b - a != 1) each [for(idx = [a + 1:b - 1]) lt[idx]], lt[a], - each (b == leng - 1 ? [] : [for(idx = [b + 1:leng - 1]) lt[idx]]) + if(b != leng - 1) each [for(idx = [b + 1:leng - 1]) lt[idx]] ]; \ No newline at end of file diff --git a/src/voronoi/_impl/_convex_ct_clk_order.scad b/src/voronoi/_impl/_convex_ct_clk_order.scad index 14ef3e3e..a25b4c3e 100644 --- a/src/voronoi/_impl/_convex_ct_clk_order.scad +++ b/src/voronoi/_impl/_convex_ct_clk_order.scad @@ -4,7 +4,6 @@ use <_convex_centroid.scad>; function _convex_ct_clk_order(points) = let( cpt = _convex_centroid(points), - pts_as = [for(p = points) [p, atan2(p.y - cpt.y, p.x - cpt.x)]], - sorted = sort(pts_as, by = "idx", idx = 1) + pts_as = [for(p = points) [p, atan2(p.y - cpt.y, p.x - cpt.x)]] ) - [for(v = sorted) v[0]]; \ No newline at end of file + [for(v = sort(pts_as, by = "idx", idx = 1)) v[0]]; \ No newline at end of file diff --git a/src/voronoi/_impl/_vrn2_space_cells_impl.scad b/src/voronoi/_impl/_vrn2_space_cells_impl.scad index 31519ca4..9c051b5f 100644 --- a/src/voronoi/_impl/_vrn2_space_cells_impl.scad +++ b/src/voronoi/_impl/_vrn2_space_cells_impl.scad @@ -6,8 +6,8 @@ function _lookup_noise_table(i) = _noise_table[i % 256]; function cell_pt(fcord, grid_w, seed, x, y, gw, gh) = let( - nx = fcord[0] + x, - ny = fcord[1] + y, + nx = fcord.x + x, + ny = fcord.y + y, sd_x = nx < 0 ? nx + gw : nx >= gw ? nx % gw : nx, sd_y = ny < 0 ? ny + gh : diff --git a/src/voronoi/vrn2_cells_space.scad b/src/voronoi/vrn2_cells_space.scad index de72dfb0..7af04c84 100644 --- a/src/voronoi/vrn2_cells_space.scad +++ b/src/voronoi/vrn2_cells_space.scad @@ -18,10 +18,10 @@ function vrn2_cells_space(size, grid_w, seed) = region_size = grid_w * 3, half_region_size = region_size * 0.5, shape = shape_square(grid_w * 3), - gw = size[0] / grid_w, - gh = size[1] / grid_w, - cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) - for(cx = [-grid_w:grid_w:size[0]]) + gw = size.x / grid_w, + gh = size.y / grid_w, + cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size.y]) + for(cx = [-grid_w:grid_w:size.x]) let( nbrs = _neighbors( [floor(cx / grid_w), floor(cy / grid_w)], diff --git a/src/voronoi/vrn2_space.scad b/src/voronoi/vrn2_space.scad index 8cb18e6a..81a0828f 100644 --- a/src/voronoi/vrn2_space.scad +++ b/src/voronoi/vrn2_space.scad @@ -15,8 +15,8 @@ module vrn2_space(size, grid_w, seed, spacing = 1, r = 0, delta = 0, chamfer = f function cell_pt(fcord, seed, x, y, gw, gh) = let( - nx = fcord[0] + x, - ny = fcord[1] + y, + nx = fcord.x + x, + ny = fcord.y + y, sd_x = nx < 0 ? nx + gw : nx >= gw ? nx % gw : nx, sd_y = ny < 0 ? ny + gh : @@ -59,8 +59,8 @@ module vrn2_space(size, grid_w, seed, spacing = 1, r = 0, delta = 0, chamfer = f gw = size[0] / grid_w; gh = size[1] / grid_w; - cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) - for(cx = [-grid_w:grid_w:size[0]]) + cell_nbrs_lt = [ + for(cy = [-grid_w:grid_w:size.y], cx = [-grid_w:grid_w:size.x]) let( nbrs = _neighbors( [floor(cx / grid_w), floor(cy / grid_w)], diff --git a/src/voronoi/vrn3_space.scad b/src/voronoi/vrn3_space.scad index 0c2d9a66..e0baf93f 100644 --- a/src/voronoi/vrn3_space.scad +++ b/src/voronoi/vrn3_space.scad @@ -17,20 +17,19 @@ module vrn3_space(size, grid_w, seed, spacing = 1) { function _lookup_noise_table(i) = _noise_table[i % 256]; function _neighbors(fcord, seed, grid_w) = [ - for(z = [-1:1]) - for(y = [-1:1]) - for(x = [-1:1]) - let( - nx = fcord[0] + x, - ny = fcord[1] + y, - nz = fcord[2] + z, - sd_base = abs(nx + ny * grid_w + nz * grid_w * grid_w), - sd1 = _lookup_noise_table(seed + sd_base), - sd2 = _lookup_noise_table(sd1 * 255 + sd_base), - sd3 = _lookup_noise_table(sd2 * 255 + sd_base), - nbr = [(nx + sd1) * grid_w, (ny + sd2) * grid_w, (nz + sd3) * grid_w] - ) - nbr + let(range = [-1:1]) + for(z = range, y = range, x = range) + let( + nx = fcord.x + x, + ny = fcord.y + y, + nz = fcord.z + z, + sd_base = abs(nx + ny * grid_w + nz * grid_w * grid_w), + sd1 = _lookup_noise_table(seed + sd_base), + sd2 = _lookup_noise_table(sd1 * 255 + sd_base), + sd3 = _lookup_noise_table(sd2 * 255 + sd_base), + nbr = [(nx + sd1) * grid_w, (ny + sd2) * grid_w, (nz + sd3) * grid_w] + ) + nbr ]; space_size = grid_w * 3; @@ -55,22 +54,20 @@ module vrn3_space(size, grid_w, seed, spacing = 1) { It can be avoided by taking 177-nearest-neighbor cells but the time taken would be unacceptable. */ cell_nbrs_lt = [ - for(cz = [0:grid_w:size[2]]) - for(cy = [0:grid_w:size[1]]) - for(cx = [0:grid_w:size[0]]) - let( - nbrs = _neighbors( - [floor(cx / grid_w), floor(cy / grid_w), floor(cz / grid_w)], - sd, - grid_w - ), - p = nbrs[13], - points = concat( - [for(i = [0:12]) nbrs[i]], - [for(i = [14:len(nbrs) - 1]) nbrs[i]] - ) - ) - [p, points] + for(cz = [0:grid_w:size.z], cy = [0:grid_w:size.y], cx = [0:grid_w:size.x]) + let( + nbrs = _neighbors( + [floor(cx / grid_w), floor(cy / grid_w), floor(cz / grid_w)], + sd, + grid_w + ), + p = nbrs[13], + points = concat( + [for(i = [0:12]) nbrs[i]], + [for(i = [14:len(nbrs) - 1]) nbrs[i]] + ) + ) + [p, points] ]; for(cell_nbrs = cell_nbrs_lt) { diff --git a/src/voxel/_impl/_vx_cylinder_impl.scad b/src/voxel/_impl/_vx_cylinder_impl.scad index 1a82d186..80e38167 100644 --- a/src/voxel/_impl/_vx_cylinder_impl.scad +++ b/src/voxel/_impl/_vx_cylinder_impl.scad @@ -27,7 +27,7 @@ function _vx_cylinder_diff_r(r, h, filled, thickness) = let(r = round(r1 + dr * i)) each [ for(pt = _vx_cylinder_vx_circle(r, filled, thickness)) - [pt.x, pt.y, i] + [each pt, i] ] ]; @@ -36,8 +36,7 @@ function _vx_cylinder_same_r(r, h, filled, thickness) = [ for(i = 0; i < h; i = i + 1) each [ - for(pt = c) - [pt.x, pt.y, i] + for(pt = c) [each pt, i] ] ]; diff --git a/src/voxel/vx_polygon.scad b/src/voxel/vx_polygon.scad index 63aa1c99..f74fa0ac 100644 --- a/src/voxel/vx_polygon.scad +++ b/src/voxel/vx_polygon.scad @@ -8,7 +8,7 @@ function vx_polygon(points, filled = false) = !filled ? contour : let( sortedXY = sort(contour, by = "vt"), - ys = [for(p = sortedXY) p[1]], + ys = [for(p = sortedXY) p.y], rows = [ for(y = [min(ys):max(ys)]) let( @@ -20,9 +20,9 @@ function vx_polygon(points, filled = false) = sortedXY, [ for(row = rows) - let(to = len(row) - 1, y = row[0][1]) - if(to > 0 && (row[0][0] + 1 != row[to][0])) - for(i = [row[0][0] + 1:row[to][0] - 1]) + let(to = len(row) - 1, row0 = row[0], y = row0[1]) + if(to > 0 && (row0[0] + 1 != row[to][0])) + for(i = [row0[0] + 1:row[to][0] - 1]) let(p = [i, y]) if(in_shape(points, p)) p ]