From 0a41e6bcc1039d92d868185478e9f033a05a092c Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sun, 6 Mar 2022 17:18:11 +0800 Subject: [PATCH] refactor --- src/_impl/_shape_path_extend_impl.scad | 25 ++--------- src/matrix/_impl/_m_determinant_impl.scad | 3 +- src/matrix/_impl/_m_scaling_impl.scad | 6 +-- src/matrix/_impl/_m_translation_impl.scad | 11 +++-- src/matrix/m_mirror.scad | 12 +++--- src/maze/mz_hex_walls.scad | 24 +++-------- src/maze/mz_wang_tiles.scad | 3 +- src/noise/_impl/_nz_cell_impl.scad | 6 +-- src/noise/_impl/_nz_worley2_impl.scad | 13 +++--- src/noise/_impl/_nz_worley3_impl.scad | 32 +++++++------- src/noise/_impl/_nz_worley_comm.scad | 7 +-- src/surface/sf_solidify.scad | 52 +++++++++++------------ src/torus_knot.scad | 13 +++--- 13 files changed, 80 insertions(+), 127 deletions(-) diff --git a/src/_impl/_shape_path_extend_impl.scad b/src/_impl/_shape_path_extend_impl.scad index 588976de..c84dac76 100644 --- a/src/_impl/_shape_path_extend_impl.scad +++ b/src/_impl/_shape_path_extend_impl.scad @@ -6,16 +6,8 @@ function __polytransversals(transversals) = let( leng_trs = len(transversals), leng_tr = len(transversals[0]), - lefts = [ - for(i = 1; i < leng_trs - 1; i = i + 1) - let(tr = transversals[leng_trs - i]) - tr[0] - ], - rights = [ - for(i = 1; i < leng_trs - 1; i = i + 1) - let(tr = transversals[i]) - tr[leng_tr - 1] - ] + lefts = [for(i = 1; i < leng_trs - 1; i = i + 1) transversals[leng_trs - i][0]], + rights = [for(i = 1; i < leng_trs - 1; i = i + 1) transversals[i][leng_tr - 1]] ) concat( transversals[0], rights, @@ -23,13 +15,7 @@ function __polytransversals(transversals) = lefts ); -function _shape_path_extend_az(p1, p2) = - let( - x1 = p1[0], - y1 = p1[1], - x2 = p2[0], - y2 = p2[1] - ) -90 + atan2((y2 - y1), (x2 - x1)); +function _shape_path_extend_az(p1, p2) = -90 + atan2((p2.y - p1.y), (p2.x - p1.x)); function _shape_path_first_stroke(stroke_pts, path_pts) = let( @@ -37,10 +23,7 @@ function _shape_path_first_stroke(stroke_pts, path_pts) = p2 = path_pts[1], a = _shape_path_extend_az(p1, p2) ) - [ - for(p = stroke_pts) - ptf_rotate(p, a) + p1 - ]; + [for(p = stroke_pts) ptf_rotate(p, a) + p1]; function _shape_path_extend_stroke(stroke_pts, p1, p2, scale_step, i) = let( diff --git a/src/matrix/_impl/_m_determinant_impl.scad b/src/matrix/_impl/_m_determinant_impl.scad index 24e14f5e..18a21391 100644 --- a/src/matrix/_impl/_m_determinant_impl.scad +++ b/src/matrix/_impl/_m_determinant_impl.scad @@ -18,5 +18,4 @@ function _m_determinant_sub(matrix, leng, fc) = function _m_determinant(matrix) = let(leng = len(matrix)) leng == 2 ? matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1] : - let(indices = [for(i = [0:leng - 1]) i]) - sum([for(fc = indices) _m_determinant_sub(matrix, leng, fc)]); \ No newline at end of file + sum([for(fc = [0:leng - 1]) _m_determinant_sub(matrix, leng, fc)]); \ No newline at end of file diff --git a/src/matrix/_impl/_m_scaling_impl.scad b/src/matrix/_impl/_m_scaling_impl.scad index 265b5df5..d36d6f4a 100644 --- a/src/matrix/_impl/_m_scaling_impl.scad +++ b/src/matrix/_impl/_m_scaling_impl.scad @@ -8,8 +8,8 @@ function __m_scaling_to_scaling_vect(s) = is_num(s) ? [s, s, s] : __m_scaling_to function _m_scaling_impl(s) = let(v = __m_scaling_to_scaling_vect(s)) [ - [v[0], 0, 0, 0], - [0, v[1], 0, 0], - [0, 0, v[2], 0], + [v.x, 0, 0, 0], + [0, v.y, 0, 0], + [0, 0, v.z, 0], [0, 0, 0, 1] ]; \ No newline at end of file diff --git a/src/matrix/_impl/_m_translation_impl.scad b/src/matrix/_impl/_m_translation_impl.scad index 0504e24c..3c026576 100644 --- a/src/matrix/_impl/_m_translation_impl.scad +++ b/src/matrix/_impl/_m_translation_impl.scad @@ -1,16 +1,15 @@ function _to_3_elems_translation_vect(v) = let(leng = len(v)) - leng == 3 ? v : ( - leng == 2 ? [v[0], v[1], 0] : [v[0], 0, 0] - ); + leng == 3 ? v : + leng == 2 ? [v[0], v[1], 0] : [v[0], 0, 0]; function _to_translation_vect(v) = is_num(v) ? [v, 0, 0] : _to_3_elems_translation_vect(v); function _m_translation_impl(v) = let(vt = _to_translation_vect(v)) [ - [1, 0, 0, vt[0]], - [0, 1, 0, vt[1]], - [0, 0, 1, vt[2]], + [1, 0, 0, vt.x], + [0, 1, 0, vt.y], + [0, 0, 1, vt.z], [0, 0, 0, 1] ]; \ No newline at end of file diff --git a/src/matrix/m_mirror.scad b/src/matrix/m_mirror.scad index 6112ec84..4ba186ac 100644 --- a/src/matrix/m_mirror.scad +++ b/src/matrix/m_mirror.scad @@ -11,12 +11,12 @@ function m_mirror(v) = let( nv = v / norm(v), - txx = -2* nv[0] * nv[0], - txy = -2* nv[0] * nv[1], - txz = -2* nv[0] * nv[2], - tyy = -2* nv[1] * nv[1], - tyz = -2* nv[1] * nv[2], - tzz = -2* nv[2] * nv[2] + txx = -2* nv.x * nv.x, + txy = -2* nv.x * nv.y, + txz = -2* nv.x * nv.z, + tyy = -2* nv.y * nv.y, + tyz = -2* nv.y * nv.z, + tzz = -2* nv.z * nv.z ) [ [1 + txx, txy, txz, 0], diff --git a/src/maze/mz_hex_walls.scad b/src/maze/mz_hex_walls.scad index d861e6d9..960d5730 100644 --- a/src/maze/mz_hex_walls.scad +++ b/src/maze/mz_hex_walls.scad @@ -12,11 +12,7 @@ use <_impl/_mz_hex_walls.scad>; function mz_hex_walls(cells, rows, columns, cell_radius, left_border = true, bottom_border = true) = let( - walls = [ - for(cell = cells) - for(wall = _build_cell(cell_radius, cell)) - wall - ], + walls = [for(cell = cells, wall = _build_cell(cell_radius, cell)) wall], left_pair_walls = left_border ? [ for(y = [0:rows - 1]) let( @@ -29,22 +25,15 @@ function mz_hex_walls(cells, rows, columns, cell_radius, left_border = true, bot [walls2[0] + cell_p, walls2[1] + cell_p] ] ] : [], - left_border_walls = [ - for(pair = left_pair_walls) - for(wall = pair) - wall - ], + left_border_walls = [for(pair = left_pair_walls) each pair], bottom_pair_walls = bottom_border ? [ for(x = [0:columns - 1]) let( cell_p = _cell_position(cell_radius, x, 0), walls1 = _bottom(cell_radius), walls2 = [ - for( - pair = (x % 2 == 0 ? [_bottom_left(cell_radius), _bottom_right(cell_radius)] : []), - wall = pair - ) - wall + for(pair = (x % 2 == 0 ? [_bottom_left(cell_radius), _bottom_right(cell_radius)] : [])) + each pair ] ) walls2 == [] ? @@ -56,9 +45,6 @@ function mz_hex_walls(cells, rows, columns, cell_radius, left_border = true, bot [walls2[0] + cell_p, walls2[1] + cell_p] ] ] : [], - bottom_border_walls = [ - for(pair = bottom_pair_walls, wall = pair) - wall - ] + bottom_border_walls = [for(pair = bottom_pair_walls) each pair] ) concat(walls, left_border_walls, bottom_border_walls); \ No newline at end of file diff --git a/src/maze/mz_wang_tiles.scad b/src/maze/mz_wang_tiles.scad index 4b4d4b54..31615360 100644 --- a/src/maze/mz_wang_tiles.scad +++ b/src/maze/mz_wang_tiles.scad @@ -40,7 +40,6 @@ function mz_wang_tiles(rows, columns, start = [0, 0], init_cells, seed) = dot_pts = dedup(sort(all, by = "vt")) ) [ - for(y = [0:rows - 1]) - for(x = [0:columns - 1]) + for(y = [0:rows - 1], x = [0:columns - 1]) [x, y, _mz_wang_tile_type(dot_pts, x, y)] ]; \ No newline at end of file diff --git a/src/noise/_impl/_nz_cell_impl.scad b/src/noise/_impl/_nz_cell_impl.scad index 56f9dfe0..89d05409 100644 --- a/src/noise/_impl/_nz_cell_impl.scad +++ b/src/noise/_impl/_nz_cell_impl.scad @@ -1,9 +1,5 @@ use <../../util/sort.scad>; - -function _sum_impl(lt, leng, i = 0) = - i >= leng - 1 ? lt[i] : (lt[i] + _sum_impl(lt, leng, i + 1)); - -function sum(lt) = _sum_impl(lt, len(lt)); +use <../../util/sum.scad>; function _manhattan(v) = sum([for(d = v) abs(d)]); diff --git a/src/noise/_impl/_nz_worley2_impl.scad b/src/noise/_impl/_nz_worley2_impl.scad index 36228d8d..4e42d435 100644 --- a/src/noise/_impl/_nz_worley2_impl.scad +++ b/src/noise/_impl/_nz_worley2_impl.scad @@ -1,19 +1,20 @@ use <_nz_worley_comm.scad>; use <../../util/sort.scad>; -function _neighbors(fcord, seed, grid_w) = [ - for(y = [-1:1]) - for(x = [-1:1]) +function _neighbors(fcord, seed, grid_w) = + let(range = [-1:1]) + [ + for(y = range, x = range) let( - nx = fcord[0] + x, - ny = fcord[1] + y, + nx = fcord.x + x, + ny = fcord.y + y, sd_base = abs(nx + ny * grid_w), sd1 = _lookup_noise_table(seed + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base), nbr = [(nx + sd1) * grid_w, (ny + sd2) * grid_w] ) nbr -]; + ]; function _nz_worley2_classic(p, nbrs, dist) = let( diff --git a/src/noise/_impl/_nz_worley3_impl.scad b/src/noise/_impl/_nz_worley3_impl.scad index d924dbec..ca4c4fd9 100644 --- a/src/noise/_impl/_nz_worley3_impl.scad +++ b/src/noise/_impl/_nz_worley3_impl.scad @@ -1,22 +1,22 @@ use <_nz_worley_comm.scad>; use <../../util/sort.scad>; -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 -]; +function _neighbors(fcord, seed, grid_w) = + 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 + ]; function _nz_worley3_classic(p, nbrs, dist) = let( diff --git a/src/noise/_impl/_nz_worley_comm.scad b/src/noise/_impl/_nz_worley_comm.scad index bca4cdd4..bd8e9c6f 100644 --- a/src/noise/_impl/_nz_worley_comm.scad +++ b/src/noise/_impl/_nz_worley_comm.scad @@ -1,10 +1,7 @@ +use <../../util/sum.scad>; + _noise_table = [0.592157, 0.627451, 0.537255, 0.356863, 0.352941, 0.0588235, 0.513725, 0.0509804, 0.788235, 0.372549, 0.376471, 0.207843, 0.760784, 0.913725, 0.027451, 0.882353, 0.54902, 0.141176, 0.403922, 0.117647, 0.270588, 0.556863, 0.0313725, 0.388235, 0.145098, 0.941176, 0.0823529, 0.0392157, 0.0901961, 0.745098, 0.0235294, 0.580392, 0.968627, 0.470588, 0.917647, 0.294118, 0, 0.101961, 0.772549, 0.243137, 0.368627, 0.988235, 0.858824, 0.796078, 0.458824, 0.137255, 0.0431373, 0.12549, 0.223529, 0.694118, 0.129412, 0.345098, 0.929412, 0.584314, 0.219608, 0.341176, 0.682353, 0.0784314, 0.490196, 0.533333, 0.670588, 0.658824, 0.266667, 0.686275, 0.290196, 0.647059, 0.278431, 0.52549, 0.545098, 0.188235, 0.105882, 0.65098, 0.301961, 0.572549, 0.619608, 0.905882, 0.32549, 0.435294, 0.898039, 0.478431, 0.235294, 0.827451, 0.521569, 0.901961, 0.862745, 0.411765, 0.360784, 0.160784, 0.215686, 0.180392, 0.960784, 0.156863, 0.956863, 0.4, 0.560784, 0.211765, 0.254902, 0.0980392, 0.247059, 0.631373, 0.00392157, 0.847059, 0.313725, 0.286275, 0.819608, 0.298039, 0.517647, 0.733333, 0.815686, 0.34902, 0.0705882, 0.662745, 0.784314, 0.768627, 0.529412, 0.509804, 0.454902, 0.737255, 0.623529, 0.337255, 0.643137, 0.392157, 0.427451, 0.776471, 0.678431, 0.729412, 0.0117647, 0.25098, 0.203922, 0.85098, 0.886275, 0.980392, 0.486275, 0.482353, 0.0196078, 0.792157, 0.14902, 0.576471, 0.462745, 0.494118, 1, 0.321569, 0.333333, 0.831373, 0.811765, 0.807843, 0.231373, 0.890196, 0.184314, 0.0627451, 0.227451, 0.0666667, 0.713725, 0.741176, 0.109804, 0.164706, 0.87451, 0.717647, 0.666667, 0.835294, 0.466667, 0.972549, 0.596078, 0.00784314, 0.172549, 0.603922, 0.639216, 0.27451, 0.866667, 0.6, 0.396078, 0.607843, 0.654902, 0.168627, 0.67451, 0.0352941, 0.505882, 0.0862745, 0.152941, 0.992157, 0.0745098, 0.384314, 0.423529, 0.431373, 0.309804, 0.443137, 0.878431, 0.909804, 0.698039, 0.72549, 0.439216, 0.407843, 0.854902, 0.964706, 0.380392, 0.894118, 0.984314, 0.133333, 0.94902, 0.756863, 0.933333, 0.823529, 0.564706, 0.0470588, 0.74902, 0.701961, 0.635294, 0.945098, 0.317647, 0.2, 0.568627, 0.921569, 0.976471, 0.054902, 0.937255, 0.419608, 0.192157, 0.752941, 0.839216, 0.121569, 0.709804, 0.780392, 0.415686, 0.615686, 0.721569, 0.329412, 0.8, 0.690196, 0.45098, 0.47451, 0.196078, 0.176471, 0.498039, 0.0156863, 0.588235, 0.996078, 0.541176, 0.92549, 0.803922, 0.364706, 0.870588, 0.447059, 0.262745, 0.113725, 0.0941176, 0.282353, 0.952941, 0.552941, 0.501961, 0.764706, 0.305882, 0.258824, 0.843137, 0.239216, 0.611765, 0.705882]; -function _sum_impl(lt, leng, i = 0) = - i >= leng - 1 ? lt[i] : (lt[i] + _sum_impl(lt, leng, i + 1)); - -function sum(lt) = _sum_impl(lt, len(lt)); - function _lookup_noise_table(i) = _noise_table[i % 256]; function _manhattan(v) = sum([for(d = v) abs(d)]); diff --git a/src/surface/sf_solidify.scad b/src/surface/sf_solidify.scad index 0262446d..103a9cdd 100644 --- a/src/surface/sf_solidify.scad +++ b/src/surface/sf_solidify.scad @@ -27,39 +27,35 @@ module sf_solidify(surface1, surface2, slicing = "SLASH", convexity = 1) { leng_pts = len(flatted_sf1); sf1_tri_faces1 = slicing == "SLASH" ? [ - for(yi = yi_range) - for(xi = xi_range) - [ - xy_to_index(xi, yi, columns), - xy_to_index(xi + 1, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] + for(yi = yi_range, xi = xi_range) + [ + xy_to_index(xi, yi, columns), + xy_to_index(xi + 1, yi + 1, columns), + xy_to_index(xi + 1, yi, columns) + ] ] : [ - for(yi = yi_range) - for(xi = xi_range) - [ - xy_to_index(xi, yi, columns), - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] + for(yi = yi_range, xi = xi_range) + [ + xy_to_index(xi, yi, columns), + xy_to_index(xi, yi + 1, columns), + xy_to_index(xi + 1, yi, columns) + ] ]; sf1_tri_faces2 = slicing == "SLASH" ? [ - for(yi = yi_range) - for(xi = xi_range) - [ - xy_to_index(xi, yi, columns), - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi + 1, columns) - ] + for(yi = yi_range, xi = xi_range) + [ + xy_to_index(xi, yi, columns), + xy_to_index(xi, yi + 1, columns), + xy_to_index(xi + 1, yi + 1, columns) + ] ] : [ - for(yi = yi_range) - for(xi = xi_range) - [ - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] + for(yi = yi_range, xi = xi_range) + [ + xy_to_index(xi, yi + 1, columns), + xy_to_index(xi + 1, yi + 1, columns), + xy_to_index(xi + 1, yi, columns) + ] ]; offset_v = [leng_pts, leng_pts, leng_pts]; diff --git a/src/torus_knot.scad b/src/torus_knot.scad index 87cee095..c1bf1a7a 100644 --- a/src/torus_knot.scad +++ b/src/torus_knot.scad @@ -11,15 +11,12 @@ use ; function torus_knot(p, q, phi_step) = - let(tau = PI * 2) [ - for(phi = 0; phi < tau; phi = phi + phi_step) + for(phi = 0; phi < 6.283185307179586; phi = phi + phi_step) let( - degree = degrees(phi), - r = cos(q * degree) + 2, - x = r * cos(p * degree), - y = r * sin(p * degree), - z = -sin(q * degree) + deg_qp = degrees(q * phi), + deg_pp = degrees(p * phi), + r = cos(deg_qp) + 2 ) - [x, y, z] + [r * cos(deg_pp), r * sin(deg_pp), -sin(deg_qp)] ]; \ No newline at end of file