From 96ec1e0c261714d4fe68ed19b888ec9e812fcc55 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 1 Mar 2022 15:52:17 +0800 Subject: [PATCH] refactor --- src/along_with.scad | 2 +- src/triangle/_impl/_tri_delaunay_impl.scad | 9 +- .../_impl/_tri_ear_clipping_impl.scad | 12 +- src/triangle/tri_circumcenter.scad | 4 +- src/triangle/tri_delaunay.scad | 4 +- src/util/polar_coordinate.scad | 2 +- src/util/spherical_coordinate.scad | 4 +- src/util/sum.scad | 4 +- src/voxel/_impl/_vx_contour_impl.scad | 2 +- src/voxel/_impl/_vx_cylinder_impl.scad | 26 ++-- src/voxel/_impl/_vx_line_impl.scad | 144 ++++++------------ src/voxel/vx_circle.scad | 3 +- src/voxel/vx_curve.scad | 25 ++- src/voxel/vx_sphere.scad | 22 ++- 14 files changed, 96 insertions(+), 167 deletions(-) diff --git a/src/along_with.scad b/src/along_with.scad index f4867ef4..ac0fde78 100644 --- a/src/along_with.scad +++ b/src/along_with.scad @@ -49,7 +49,7 @@ module along_with(points, angles, twist = 0, scale = 1.0, method = "AXIS_ANGLE") let( vt0 = pts[i] - pts[i - 1], vt1 = pts[i + 1] - pts[i], - a = acos((vt0 * vt1) / (norm(vt0) * norm(vt1))), + a = acos((vt0 * vt1) / sqrt((vt0 * vt0) * (vt1 * vt1))), v = cross(vt0, vt1) ) [a, v] diff --git a/src/triangle/_impl/_tri_delaunay_impl.scad b/src/triangle/_impl/_tri_delaunay_impl.scad index ec350627..2c5da112 100644 --- a/src/triangle/_impl/_tri_delaunay_impl.scad +++ b/src/triangle/_impl/_tri_delaunay_impl.scad @@ -23,8 +23,8 @@ function _tri_circumcircle(shape_pts) = ) det == 0 ? undef : let( - x = (d1 * v0[1] - d0 * v1[1]) / det, - y = (d0 * v1[0] - d1 * v0[0]) / det, + x = (d1 * v0.y - d0 * v1.y) / det, + y = (d0 * v1.x - d1 * v0.x) / det, center = [x, y], v = p0 - center ) @@ -179,10 +179,9 @@ function delaunayBadTriangles(d, p, _indices_hash) = function inCircumcircle(t, p, circles, _indices_hash) = let( c = hashmap_get(circles, t, hash = _indices_hash), - ct = cc_center(c), - rr = (ct[0] - p[0]) ^ 2 + (ct[1] - p[1]) ^ 2 + v = cc_center(c) - p ) - rr <= cc_rr(c); + v * v <= cc_rr(c); function delaunayBoundaries(d, badTriangles, _indices_hash) = let( diff --git a/src/triangle/_impl/_tri_ear_clipping_impl.scad b/src/triangle/_impl/_tri_ear_clipping_impl.scad index 77ee933d..38d48ab1 100644 --- a/src/triangle/_impl/_tri_ear_clipping_impl.scad +++ b/src/triangle/_impl/_tri_ear_clipping_impl.scad @@ -14,13 +14,7 @@ function _triangulate_snipable(shape_pts, u, v, w, n, indices, epsilon = 0.0001) a = shape_pts[indices[u]], b = shape_pts[indices[v]], c = shape_pts[indices[w]], - ax = a[0], - ay = a[1], - bx = b[0], - by = b[1], - cx = c[0], - cy = c[1], - determinant = cross([bx - ax, by - ay], [cx - ax, cy - ay]) + determinant = cross([b.x - a.x, b.y - a.y], [c.x - a.x, c.y - a.y]) ) epsilon > determinant ? false : _triangulate_snipable_sub(shape_pts, n, u, v, w, a, b, c, indices); @@ -35,9 +29,7 @@ function _triangulate_snipable_sub(shape_pts, n, u, v, w, a, b, c, indices, p = // remove the elem at idx v from indices function _triangulate_remove_v(indices, v, num_of_vertices) = - let( - nv_minuns_one = num_of_vertices - 1 - ) + let(nv_minuns_one = num_of_vertices - 1) v == 0 ? [for(i = 1; i <= nv_minuns_one; i = i + 1) indices[i]] : ( v == nv_minuns_one ? [for(i = 0; i < v; i = i + 1) indices[i]] : concat( [for(i = 0; i < v; i = i + 1) indices[i]], diff --git a/src/triangle/tri_circumcenter.scad b/src/triangle/tri_circumcenter.scad index 12de63b8..2129a751 100644 --- a/src/triangle/tri_circumcenter.scad +++ b/src/triangle/tri_circumcenter.scad @@ -20,6 +20,6 @@ function tri_circumcenter(shape_pts) = det = -cross(v0 , v1) ) det == 0 ? undef : [ - (d1 * v0[1] - d0 * v1[1]) / det, - (d0 * v1[0] - d1 * v0[0]) / det + (d1 * v0.y - d0 * v1.y) / det, + (d0 * v1.x - d1 * v0.x) / det ]; \ No newline at end of file diff --git a/src/triangle/tri_delaunay.scad b/src/triangle/tri_delaunay.scad index b0afa7d9..4699e748 100644 --- a/src/triangle/tri_delaunay.scad +++ b/src/triangle/tri_delaunay.scad @@ -24,8 +24,8 @@ function tri_delaunay(points, ret = "TRI_INDICES") = max_y = max(ys), min_y = min(ys), center = [max_x + min_x, max_y + min_y] / 2, - width = abs(max_x - center[0]) * 4, - height = abs(max_y - center[1]) * 4, + width = abs(max_x - center.x) * 4, + height = abs(max_y - center.y) * 4, leng_pts = len(points), d = _tri_delaunay( delaunay_init(center, width, height, leng_pts, _indices_hash), diff --git a/src/util/polar_coordinate.scad b/src/util/polar_coordinate.scad index 0c6ca55d..f3a24591 100644 --- a/src/util/polar_coordinate.scad +++ b/src/util/polar_coordinate.scad @@ -10,4 +10,4 @@ use <../__comm__/__angy_angz.scad>; -function polar_coordinate(point) = [norm(point), atan2(point[1], point[0])]; // r, theta \ No newline at end of file +function polar_coordinate(point) = [norm(point), atan2(point.y, point.x)]; // r, theta \ No newline at end of file diff --git a/src/util/spherical_coordinate.scad b/src/util/spherical_coordinate.scad index d69ab747..07b0bcfa 100644 --- a/src/util/spherical_coordinate.scad +++ b/src/util/spherical_coordinate.scad @@ -12,6 +12,6 @@ function spherical_coordinate(point) = // mathematics [r, theta, phi] [ norm(point), - atan2(point[1], point[0]), - atan2(sqrt(point[0]^2 + point[1]^2), point[2]) + atan2(point.y, point.x), + atan2(sqrt(point.x ^ 2 + point.y ^ 2), point.z) ]; diff --git a/src/util/sum.scad b/src/util/sum.scad index 28d3a99e..1bc28a97 100644 --- a/src/util/sum.scad +++ b/src/util/sum.scad @@ -8,4 +8,6 @@ * **/ -function sum(lt) = [for(i = [0:len(lt) - 1]) 1] * lt; \ No newline at end of file +function sum(lt) = + let(leng = len(lt)) + [for(i = 0; i < leng; i = i + 1) 1] * lt; \ No newline at end of file diff --git a/src/voxel/_impl/_vx_contour_impl.scad b/src/voxel/_impl/_vx_contour_impl.scad index f4e9025c..75aa7cad 100644 --- a/src/voxel/_impl/_vx_contour_impl.scad +++ b/src/voxel/_impl/_vx_contour_impl.scad @@ -26,7 +26,7 @@ _vx_contour_nxt_offset = [ ]; function _vx_contour_travel(pts, p, fst) = let( - dir_i = _vx_contour_dir(_vx_contour_corner_value(pts, p[0], p[1])), + dir_i = _vx_contour_dir(_vx_contour_corner_value(pts, p.x, p.y)), nxt_p = p + _vx_contour_nxt_offset[dir_i] ) nxt_p == fst ? [p] : [p, each _vx_contour_travel(pts, nxt_p, fst)]; diff --git a/src/voxel/_impl/_vx_cylinder_impl.scad b/src/voxel/_impl/_vx_cylinder_impl.scad index de3d2bb9..1a82d186 100644 --- a/src/voxel/_impl/_vx_cylinder_impl.scad +++ b/src/voxel/_impl/_vx_cylinder_impl.scad @@ -1,20 +1,18 @@ function _vx_cylinder_vx_circle(radius, filled, thickness) = let(range = [-radius: radius - 1]) filled ? [ - for(y = range) - for(x = range) - let(v = [x, y]) - if(norm(v) < radius) v + for(y = range, x = range) + let(v = [x, y]) + if(norm(v) < radius) v ] : - let(ishell = radius * radius - 2 * thickness * radius) + let(ishell = radius ^ 2 - 2 * thickness * radius) [ - for(y = range) - for(x = range) - let( - v = [x, y], - leng = norm(v) - ) - if(leng < radius && (leng * leng) > ishell) v + for(y = range, x = range) + let( + v = [x, y], + leng = norm(v) + ) + if(leng < radius && (leng ^ 2) > ishell) v ]; function _vx_cylinder_diff_r(r, h, filled, thickness) = @@ -29,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[0], pt[1], i] + [pt.x, pt.y, i] ] ]; @@ -39,7 +37,7 @@ function _vx_cylinder_same_r(r, h, filled, thickness) = for(i = 0; i < h; i = i + 1) each [ for(pt = c) - [pt[0], pt[1], i] + [pt.x, pt.y, i] ] ]; diff --git a/src/voxel/_impl/_vx_line_impl.scad b/src/voxel/_impl/_vx_line_impl.scad index 732d94e5..fe4634fe 100644 --- a/src/voxel/_impl/_vx_line_impl.scad +++ b/src/voxel/_impl/_vx_line_impl.scad @@ -11,55 +11,37 @@ function _vx_line_xdominant_z(z, zd, sz) = zd >= 0 ? z + sz : z; function _vx_line_xdominant_zd(zd, ax, az) = (zd >= 0 ? zd - ax : zd) + az; function _vx_line_xdominant(start, end, a, s) = - let( - x = start[0], - y = start[1], - z = start[2], - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2], - shrx = floor(ax / 2), - yd = ay - shrx, - zd = az - shrx, - endx = end[0] + let( + shrx = floor(a.x / 2), + yd = a.y - shrx, + zd = a.z - shrx ) [ start, each _vx_line_xdominant_sub( - x + sx, - _vx_line_xdominant_y(y, yd, sy), - _vx_line_xdominant_z(z, zd, sz), - endx, + start.x + s.x, + _vx_line_xdominant_y(start.y, yd, s.y), + _vx_line_xdominant_z(start.z, zd, s.z), + end.x, a, s, - _vx_line_xdominant_yd(yd, ax, ay), - _vx_line_xdominant_zd(zd, ax, az) + _vx_line_xdominant_yd(yd, a.x, a.y), + _vx_line_xdominant_zd(zd, a.x, a.z) ) ]; function _vx_line_xdominant_sub(x, y, z, endx, a, s, yd, zd) = - let( - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2] - ) x == endx ? [] : [ [x, y, z], each _vx_line_xdominant_sub( - x + sx, - _vx_line_xdominant_y(y, yd, sy), - _vx_line_xdominant_z(z, zd, sz), + x + s.x, + _vx_line_xdominant_y(y, yd, s.y), + _vx_line_xdominant_z(z, zd, s.z), endx, a, s, - _vx_line_xdominant_yd(yd, ax, ay), - _vx_line_xdominant_zd(zd, ax, az) + _vx_line_xdominant_yd(yd, a.x, a.y), + _vx_line_xdominant_zd(zd, a.x, a.z) ) ]; @@ -70,55 +52,37 @@ function _vx_line_ydominant_z(z, zd, sz) = zd >= 0 ? z + sz : z; function _vx_line_ydominant_zd(zd, ay, az) = (zd >= 0 ? zd - ay : zd) + az; function _vx_line_ydominant(start, end, a, s) = - let( - x = start[0], - y = start[1], - z = start[2], - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2], - shry = floor(ay / 2), - xd = ax - shry, - zd = az - shry, - endy = end[1] + let( + shry = floor(a.y / 2), + xd = a.x - shry, + zd = a.z - shry ) [ start, each _vx_line_ydominant_sub( - _vx_line_ydominant_x(x, xd, sx), - y + sy, - _vx_line_ydominant_z(z, zd, sz), - endy, + _vx_line_ydominant_x(start.x, xd, s.x), + start.y + s.y, + _vx_line_ydominant_z(start.z, zd, s.z), + end.y, a, s, - _vx_line_ydominant_xd(xd, ax, ay), - _vx_line_ydominant_zd(zd, ay, az) + _vx_line_ydominant_xd(xd, a.x, a.y), + _vx_line_ydominant_zd(zd, a.y, a.z) ) ]; function _vx_line_ydominant_sub(x, y, z, endy, a, s, xd, zd) = - let( - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2] - ) y == endy ? [] : [ [x, y, z], each _vx_line_ydominant_sub( - _vx_line_ydominant_x(x, xd, sx), - y + sy, - _vx_line_ydominant_z(z, zd, sz), + _vx_line_ydominant_x(x, xd, s.x), + y + s.y, + _vx_line_ydominant_z(z, zd, s.z), endy, a, s, - _vx_line_ydominant_xd(xd, ax, ay), - _vx_line_ydominant_zd(zd, ay, az) + _vx_line_ydominant_xd(xd, a.x, a.y), + _vx_line_ydominant_zd(zd, a.y, a.z) ) ]; @@ -130,55 +94,37 @@ function _vx_line_zdominant_y(y, yd, sy) = yd >= 0 ? y + sy : y; function _vx_line_zdominant_yd(yd, ay, az) = (yd >= 0 ? yd - az : yd) + ay; function _vx_line_zdominant(start, end, a, s) = - let( - x = start[0], - y = start[1], - z = start[2], - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2], - shrz = floor(az / 2), - xd = ax - shrz, - yd = ay - shrz, - endz = end[2] + let( + shrz = floor(a.z / 2), + xd = a.x - shrz, + yd = a.y - shrz ) [ start, each _vx_line_zdominant_sub( - _vx_line_zdominant_x(x, xd, sx), - _vx_line_zdominant_y(y, yd, sy), - z + sz, - endz, + _vx_line_zdominant_x(start.x, xd, s.x), + _vx_line_zdominant_y(start.y, yd, s.y), + start.z + s.z, + end.z, a, s, - _vx_line_zdominant_xd(xd, ax, az), - _vx_line_zdominant_yd(yd, ay, az) + _vx_line_zdominant_xd(xd, a.x, a.z), + _vx_line_zdominant_yd(yd, a.y, a.z) ) ]; function _vx_line_zdominant_sub(x, y, z, endz, a, s, xd, yd) = - let( - ax = a[0], - ay = a[1], - az = a[2], - sx = s[0], - sy = s[1], - sz = s[2] - ) z == endz ? [] : [ [x, y, z], each _vx_line_zdominant_sub( - _vx_line_zdominant_x(x, xd, sx), - _vx_line_zdominant_y(y, yd, sy), - z + sz, + _vx_line_zdominant_x(x, xd, s.x), + _vx_line_zdominant_y(y, yd, s.y), + z + s.z, endz, a, s, - _vx_line_zdominant_xd(xd, ax, az), - _vx_line_zdominant_yd(yd, ay, az) + _vx_line_zdominant_xd(xd, a.x, a.z), + _vx_line_zdominant_yd(yd, a.y, a.z) ) ]; diff --git a/src/voxel/vx_circle.scad b/src/voxel/vx_circle.scad index ce6092bc..8ea90568 100644 --- a/src/voxel/vx_circle.scad +++ b/src/voxel/vx_circle.scad @@ -12,5 +12,4 @@ use <_impl/_vx_circle_impl.scad>; use <../util/dedup.scad>; function vx_circle(radius, filled = false) = - let(all = _vx_circle_impl(radius, filled)) - dedup(all); \ No newline at end of file + dedup(_vx_circle_impl(radius, filled)); \ No newline at end of file diff --git a/src/voxel/vx_curve.scad b/src/voxel/vx_curve.scad index a4971384..09b5cbc2 100644 --- a/src/voxel/vx_curve.scad +++ b/src/voxel/vx_curve.scad @@ -12,17 +12,14 @@ use <_impl/_vx_curve_impl.scad>; use <../util/dedup.scad>; function vx_curve(points, tightness = 0) = - let( - leng = len(points), - pts = [ - each [ - for(i = [0:leng - 4]) - let( - pts = _vx_catmull_rom_spline_4pts([for(j = [i:i + 3]) points[j]], tightness) - ) - for(i = [0:len(pts) - 2]) pts[i] - ], - points[leng - 2] - ] - ) - dedup(pts); \ No newline at end of file + let(leng = len(points)) + dedup([ + each [ + for(i = [0:leng - 4]) + let( + pts = _vx_catmull_rom_spline_4pts([for(j = [i:i + 3]) points[j]], tightness) + ) + for(i = [0:len(pts) - 2]) pts[i] + ], + points[leng - 2] + ]); \ No newline at end of file diff --git a/src/voxel/vx_sphere.scad b/src/voxel/vx_sphere.scad index 3ef1753b..45c7dd66 100644 --- a/src/voxel/vx_sphere.scad +++ b/src/voxel/vx_sphere.scad @@ -11,21 +11,17 @@ function vx_sphere(radius, filled = false, thickness = 1) = let(range = [-radius: radius - 1]) filled ? [ - for(z = range) - for(y = range) - for(x = range) - let(v = [x, y, z]) - if(norm(v) < radius) v + for(z = range, y = range, x = range) + let(v = [x, y, z]) + if(norm(v) < radius) v ] : let(ishell = radius * radius - 2 * thickness * radius) [ - for(z = range) - for(y = range) - for(x = range) - let( - v = [x, y, z], - leng = norm(v) - ) - if(leng < radius && (leng * leng) > ishell) v + for(z = range, y = range, x = range) + let( + v = [x, y, z], + leng = norm(v) + ) + if(leng < radius && (leng * leng) > ishell) v ]; \ No newline at end of file