From 7d407d8cda80e81639ebd169168683107645923e Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 27 Feb 2022 18:50:19 +0800 Subject: [PATCH] use [each lt, v] to replace concat(lt, [v]) --- src/__comm__/__lines_from.scad | 9 ++---- src/__comm__/__pie_for_rounding.scad | 20 +++++-------- src/__comm__/_convex_hull2.scad | 6 ++-- src/__comm__/_convex_hull3.scad | 30 +++++++++---------- src/__comm__/_vertex_normals.scad | 2 +- src/voronoi/_impl/_convex_intersection.scad | 4 +-- src/voronoi/_impl/_vrn2_space_cells_impl.scad | 12 ++++---- src/voronoi/vrn_sphere.scad | 2 +- 8 files changed, 38 insertions(+), 47 deletions(-) diff --git a/src/__comm__/__lines_from.scad b/src/__comm__/__lines_from.scad index 59720652..4f60b936 100644 --- a/src/__comm__/__lines_from.scad +++ b/src/__comm__/__lines_from.scad @@ -1,10 +1,7 @@ function __lines_from(pts, closed = false) = let( leng = len(pts), - endi = leng - 1 + endi = leng - 1, + lines = [for(i = 0; i < endi; i = i + 1) [pts[i], pts[i + 1]]] ) - concat( - [for(i = 0; i < endi; i = i + 1) [pts[i], pts[i + 1]]], - closed ? [[pts[len(pts) - 1], pts[0]]] : [] - ); - \ No newline at end of file + closed ? [each lines, [pts[endi], pts[0]]] : lines; \ No newline at end of file diff --git a/src/__comm__/__pie_for_rounding.scad b/src/__comm__/__pie_for_rounding.scad index c67024b6..c2d3ed11 100644 --- a/src/__comm__/__pie_for_rounding.scad +++ b/src/__comm__/__pie_for_rounding.scad @@ -2,17 +2,11 @@ function __pie_for_rounding(r, begin_a, end_a, frags) = let( sector_angle = end_a - begin_a, step_a = sector_angle / frags, - is_integer = frags % 1 == 0 + is_integer = frags % 1 == 0, + pie = [ + for(ang = begin_a; ang <= end_a; ang = ang + step_a) + [r * cos(ang), r * sin(ang)] + ] ) - r < 0.00005 ? [[0, 0]] : concat([ - for(ang = begin_a; ang <= end_a; ang = ang + step_a) - [ - r * cos(ang), - r * sin(ang) - ] - ], - is_integer ? [] : [[ - r * cos(end_a), - r * sin(end_a) - ]] - ); \ No newline at end of file + r < 0.00005 ? [[0, 0]] : + is_integer ? pie : [each pie, [r * cos(end_a), r * sin(end_a)]]; \ No newline at end of file diff --git a/src/__comm__/_convex_hull2.scad b/src/__comm__/_convex_hull2.scad index dc1249c9..f2703b81 100644 --- a/src/__comm__/_convex_hull2.scad +++ b/src/__comm__/_convex_hull2.scad @@ -11,7 +11,7 @@ function _convex_hull_sort_by_xy(lt) = before = [for(j = 1; j < leng; j = j + 1) if(_convex_hull_lt_than_by_xy(lt[j], pivot)) lt[j]], after = [for(j = 1; j < leng; j = j + 1) if(!_convex_hull_lt_than_by_xy(lt[j], pivot)) lt[j]] ) - concat(_convex_hull_sort_by_xy(before), [pivot], _convex_hull_sort_by_xy(after)); + [each _convex_hull_sort_by_xy(before), pivot, each _convex_hull_sort_by_xy(after)]; // oa->ob ct_clk : greater than 0 function _convex_hull_impl_dir(o, a, b) = @@ -28,7 +28,7 @@ function _convex_hull_lower_chain(points, leng, chain, m, i) = _convex_hull_lower_chain( points, leng, - concat(slice(chain, 0, current_m), [points[i]]), + [each slice(chain, 0, current_m), points[i]], current_m + 1, i + 1 ); @@ -44,7 +44,7 @@ function _convex_hull_upper_chain(points, chain, m, t, i) = ) _convex_hull_upper_chain( points, - concat(slice(chain, 0, current_m), [points[i]]), + [each slice(chain, 0, current_m), points[i]], current_m + 1, t, i - 1 diff --git a/src/__comm__/_convex_hull3.scad b/src/__comm__/_convex_hull3.scad index d34d8207..529044d5 100644 --- a/src/__comm__/_convex_hull3.scad +++ b/src/__comm__/_convex_hull3.scad @@ -13,7 +13,7 @@ function _convex_hull_sort_by_xyz(pts) = before = [for(j = 1; j < leng; j = j + 1) if(_cmp(pts[j], pivot)) pts[j]], after = [for(j = 1; j < leng; j = j + 1) if(!_cmp(pts[j], pivot)) pts[j]] ) - concat(_convex_hull_sort_by_xyz(before), [pivot], _convex_hull_sort_by_xyz(after)); + [each _convex_hull_sort_by_xyz(before), pivot, each _convex_hull_sort_by_xyz(after)]; function normal(pts, f) = cross(pts[f[1]] - pts[f[0]], pts[f[2]] - pts[f[0]]); @@ -33,25 +33,25 @@ function m_assign(m, i, j, v) = let( lt = m[i], leng_lt = len(lt), - nlt = concat( - j == 0 ? [] : [for(idx = [0:j - 1]) lt[idx]], - [v], - j == leng_lt - 1 ? [] : [for(idx = [j + 1:leng_lt - 1]) lt[idx]] - ), + nlt = [ + each (j == 0 ? [] : [for(idx = [0:j - 1]) lt[idx]]), + v, + each (j == leng_lt - 1 ? [] : [for(idx = [j + 1:leng_lt - 1]) lt[idx]]) + ], leng_m = len(m) ) - concat( - i == 0 ? [] : [for(idx = [0:i - 1]) m[idx]], - [nlt], - i == leng_m - 1 ? [] : [for(idx = [i + 1:leng_m - 1]) m[idx]] - ); + [ + each (i == 0 ? [] : [for(idx = [0:i - 1]) m[idx]]), + nlt, + each (i == leng_m - 1 ? [] : [for(idx = [i + 1:leng_m - 1]) m[idx]]) + ]; function next_vis(i, pts, cur_faces, cur_faces_leng, next, vis, j = 0) = j == cur_faces_leng ? [next, vis] : let( f = cur_faces[j], d = (pts[f[0]] - pts[i]) * normal(pts, f), - nx = d >= 0 ? concat(next, [f]) : next, + nx = d >= 0 ? [each next, f] : next, s = d > 0 ? 1 : d < 0 ? -1 : 0, vis1 = m_assign(vis, f[0], f[1], s), @@ -68,11 +68,11 @@ function next2(i, cur_faces, cur_faces_leng, vis, next, j = 0) = b = f[1], c = f[2], nx1 = vis[a][b] < 0 && vis[a][b] != vis[b][a] ? - concat(next, [[a, b, i]]) : next, + [each next, [a, b, i]] : next, nx2 = vis[b][c] < 0 && vis[b][c] != vis[c][b] ? - concat(nx1, [[b, c, i]]) : nx1, + [each nx1, [b, c, i]] : nx1, nx3 = vis[c][a] < 0 && vis[c][a] != vis[a][c] ? - concat(nx2, [[c, a, i]]) : nx2 + [each nx2, [c, a, i]] : nx2 ) next2(i, cur_faces, cur_faces_leng, vis, nx3, j + 1); diff --git a/src/__comm__/_vertex_normals.scad b/src/__comm__/_vertex_normals.scad index c475be86..66d29d49 100644 --- a/src/__comm__/_vertex_normals.scad +++ b/src/__comm__/_vertex_normals.scad @@ -20,7 +20,7 @@ function _connected_faces(faces, leng, leng_pts, cnt_faces, i = 0) = facei = sort(faces[i], by = ascending), n_cnt_faces = [ for(k = [0:leng_pts - 1]) - find_index(facei, function(e) e == k) != -1 ? concat(cnt_faces[k], [faces[i]]) : cnt_faces[k] + find_index(facei, function(e) e == k) != -1 ? [each cnt_faces[k], faces[i]] : cnt_faces[k] ] ) _connected_faces(faces, leng, leng_pts, n_cnt_faces, i + 1); diff --git a/src/voronoi/_impl/_convex_intersection.scad b/src/voronoi/_impl/_convex_intersection.scad index 49e5a3fe..1d404ba5 100644 --- a/src/voronoi/_impl/_convex_intersection.scad +++ b/src/voronoi/_impl/_convex_intersection.scad @@ -19,7 +19,7 @@ function _in_convex(convex_pts, pt) = function _intersection_ps(shape, line_pts, epsilon) = let( leng = len(shape), - pts = concat(shape, [shape[0]]) + pts = [each shape, shape[0]] ) dedup([ for(i = [0:leng - 1]) @@ -31,7 +31,7 @@ function _convex_intersection(shape1, shape2, epsilon = 0.0001) = (shape1 == [] || shape2 == []) ? [] : let( leng = len(shape1), - pts = concat(shape1, [shape1[0]]) + pts = [each shape1, shape1[0]] ) _convex_ct_clk_order( concat( diff --git a/src/voronoi/_impl/_vrn2_space_cells_impl.scad b/src/voronoi/_impl/_vrn2_space_cells_impl.scad index 79f1cd75..7e3eb7a2 100644 --- a/src/voronoi/_impl/_vrn2_space_cells_impl.scad +++ b/src/voronoi/_impl/_vrn2_space_cells_impl.scad @@ -20,16 +20,16 @@ function cell_pt(fcord, grid_w, seed, x, y, gw, gh) = // 21-nearest-neighbor function _neighbors(fcord, seed, grid_w, gw, gh) = + let(range = [-1:1]) concat( [ - for(y = [-1:1]) - for(x = [-1:1]) + for(y = range, x = range) cell_pt(fcord, grid_w, seed, x, y, gw, gh) ], - [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, -2, gw, gh)], - [for(x = [-1:1]) cell_pt(fcord, grid_w, seed, x, 2, gw, gh)], - [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, -2, y, gw, gh)], - [for(y = [-1:1]) cell_pt(fcord, grid_w, seed, 2, y, gw, gh)] + [for(x = range) cell_pt(fcord, grid_w, seed, x, -2, gw, gh)], + [for(x = range) cell_pt(fcord, grid_w, seed, x, 2, gw, gh)], + [for(y = range) cell_pt(fcord, grid_w, seed, -2, y, gw, gh)], + [for(y = range) cell_pt(fcord, grid_w, seed, 2, y, gw, gh)] ); function _cells_lt_before_intersection(shape, size, points, pt, half_region_size) = diff --git a/src/voronoi/vrn_sphere.scad b/src/voronoi/vrn_sphere.scad index cd984162..420e801d 100644 --- a/src/voronoi/vrn_sphere.scad +++ b/src/voronoi/vrn_sphere.scad @@ -6,7 +6,7 @@ function vrn_sphere(points) = r = norm(points[0]), plane_pts = [for(p = points) stereographic_proj_to_plane(p / r)], inifinity = [4e7, 0], - vrn2_cells = vrn2_cells_from(concat(plane_pts, [inifinity])) + vrn2_cells = vrn2_cells_from([each plane_pts, inifinity]) ) [ for(i = [0:len(vrn2_cells) - 2])