From 14ad6b409050a4b543697a8b3995439c04f33374 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 1 Mar 2022 09:54:48 +0800 Subject: [PATCH] refactor --- src/__comm__/__angy_angz.scad | 10 ++++++---- src/__comm__/__lines_from.scad | 3 +-- src/__comm__/__to_degree.scad | 2 +- src/__comm__/_convex_hull3.scad | 9 +++++---- src/_impl/_shape_liquid_splitting_impl.scad | 5 +++-- src/experimental/_impl/_tiles_wfc_impl.scad | 13 +++++-------- src/util/_impl/_dedup_impl.scad | 1 - src/util/_impl/_sum_impl.scad | 2 -- src/util/degrees.scad | 2 +- src/util/radians.scad | 2 +- src/util/sum.scad | 4 +--- src/voronoi/_impl/_convex_intersection.scad | 14 ++++---------- src/voronoi/_impl/_vrn2_space_cells_impl.scad | 5 +---- 13 files changed, 29 insertions(+), 43 deletions(-) delete mode 100644 src/util/_impl/_sum_impl.scad diff --git a/src/__comm__/__angy_angz.scad b/src/__comm__/__angy_angz.scad index 0a5b8481..59649123 100644 --- a/src/__comm__/__angy_angz.scad +++ b/src/__comm__/__angy_angz.scad @@ -2,7 +2,9 @@ function __angy_angz(p1, p2) = let( dx = p2.x - p1.x, dy = p2.y - p1.y, - dz = p2.z - p1.z, - ya = atan2(dz, sqrt(dx * dx + dy * dy)), - za = atan2(dy, dx) - ) [ya, za]; \ No newline at end of file + dz = p2.z - p1.z + ) + [ + atan2(dz, sqrt(dx ^ 2 + dy ^ 2)), + atan2(dy, dx) + ]; \ No newline at end of file diff --git a/src/__comm__/__lines_from.scad b/src/__comm__/__lines_from.scad index 4f60b936..2c6139f0 100644 --- a/src/__comm__/__lines_from.scad +++ b/src/__comm__/__lines_from.scad @@ -1,7 +1,6 @@ function __lines_from(pts, closed = false) = let( - leng = len(pts), - endi = leng - 1, + endi = len(pts) - 1, lines = [for(i = 0; i < endi; i = i + 1) [pts[i], pts[i + 1]]] ) closed ? [each lines, [pts[endi], pts[0]]] : lines; \ No newline at end of file diff --git a/src/__comm__/__to_degree.scad b/src/__comm__/__to_degree.scad index 963506f5..75b1c447 100644 --- a/src/__comm__/__to_degree.scad +++ b/src/__comm__/__to_degree.scad @@ -1 +1 @@ -function __to_degree(phi) = 180 / PI * phi; \ No newline at end of file +function __to_degree(radians) = (radians * 180) / PI; \ No newline at end of file diff --git a/src/__comm__/_convex_hull3.scad b/src/__comm__/_convex_hull3.scad index 58f698fa..77b7ec69 100644 --- a/src/__comm__/_convex_hull3.scad +++ b/src/__comm__/_convex_hull3.scad @@ -115,11 +115,12 @@ function _convex_hull3(pts) = [v2, v1, v3], [v0, v2, v3] ], - init_vis = [for(i = [0:leng - 1]) [for(j = [0:leng - 1]) 0]], + zeros = [for(j = [0:leng - 1]) 0], + init_vis = [for(i = [0:leng - 1]) zeros], faces = _all_faces(v0, v1, v2, v3, sorted, leng, init_vis, fst_tetrahedron), // counter-clockwise - reversed = [for(face = faces) // OpenSCAD prefers clockwise. - [for(i = 2; i >= 0; i = i - 1) - face[i]] + reversed = [ + for(face = faces) // OpenSCAD requires clockwise. + [for(i = 2; i >= 0; i = i - 1) face[i]] ] ) [ diff --git a/src/_impl/_shape_liquid_splitting_impl.scad b/src/_impl/_shape_liquid_splitting_impl.scad index ebee6a6f..e97892e9 100644 --- a/src/_impl/_shape_liquid_splitting_impl.scad +++ b/src/_impl/_shape_liquid_splitting_impl.scad @@ -6,11 +6,12 @@ function _liquid_splitting_pie_curve(radius, centre_dist, tangent_angle) = let( begin_ang = 90 + tangent_angle, shape_pts = shape_pie(radius, [-begin_ang, begin_ang]), - leng = len(shape_pts) + leng = len(shape_pts), + offset_p = [centre_dist / 2, 0] ) [ for(i = 1; i < leng; i = i + 1) - shape_pts[i] + [centre_dist / 2, 0] + shape_pts[i] + offset_p ]; function _liquid_splitting_bezier(radius, centre_dist, tangent_angle, t_step, ctrl_p1) = diff --git a/src/experimental/_impl/_tiles_wfc_impl.scad b/src/experimental/_impl/_tiles_wfc_impl.scad index b4d78736..1fc575e8 100644 --- a/src/experimental/_impl/_tiles_wfc_impl.scad +++ b/src/experimental/_impl/_tiles_wfc_impl.scad @@ -106,8 +106,7 @@ function wf_entropy(wf, x, y) = let( states = wf_eigenstates_at(wf, x, y), weights = wf_weights(wf), - state_leng = len(states), - sumOfWeights_sumOfWeightLogWeights = _wf_entropy(weights, states, state_leng, 0, 0), + sumOfWeights_sumOfWeightLogWeights = _wf_entropy(weights, states, len(states), 0, 0), sumOfWeights = sumOfWeights_sumOfWeightLogWeights[0], sumOfWeightLogWeights = sumOfWeights_sumOfWeightLogWeights[1] ) @@ -241,8 +240,8 @@ function tilemap_generate(tm) = wf_is_all_collapsed(wf) ? collapsed_tiles(wf) : let( coord = wf_coord_min_entropy(wf), - x = coord[0], - y = coord[1] + x = coord.x, + y = coord.y ) tilemap_generate(tilemap_propagate([ tilemap_width(tm), @@ -269,10 +268,8 @@ function compatibilities_of_tiles(sample) = height = len(sample) ) hashset([ - for(y = [0:height - 1]) - for(x = [0:width - 1]) - for(c = neighbor_compatibilities(sample, x, y, width, height)) - c + for(y = [0:height - 1], x = [0:width - 1]) + each neighbor_compatibilities(sample, x, y, width, height) ], number_of_buckets = width * height); function collapsed_tiles(wf) = diff --git a/src/util/_impl/_dedup_impl.scad b/src/util/_impl/_dedup_impl.scad index 79f5ff38..93ade9c4 100644 --- a/src/util/_impl/_dedup_impl.scad +++ b/src/util/_impl/_dedup_impl.scad @@ -6,7 +6,6 @@ function _dedup(elems, leng, buckets, eq, hash, bucket_numbers, i = 0) = function _dedup_add(buckets, i_elem, eq, hash, bucket_numbers) = let( - i = i_elem[0], elem = i_elem[1], b_idx = hash(elem) % bucket_numbers, bucket = buckets[b_idx] diff --git a/src/util/_impl/_sum_impl.scad b/src/util/_impl/_sum_impl.scad deleted file mode 100644 index 7acb2a8d..00000000 --- a/src/util/_impl/_sum_impl.scad +++ /dev/null @@ -1,2 +0,0 @@ -function _sum_impl(lt, leng, i = 0) = - i >= leng - 1 ? lt[i] : (lt[i] + _sum_impl(lt, leng, i + 1)); diff --git a/src/util/degrees.scad b/src/util/degrees.scad index 036e60ce..42108523 100644 --- a/src/util/degrees.scad +++ b/src/util/degrees.scad @@ -8,4 +8,4 @@ * **/ -function degrees(radians) = 180 / PI * radians; \ No newline at end of file +function degrees(radians) = (radians * 180) / PI; \ No newline at end of file diff --git a/src/util/radians.scad b/src/util/radians.scad index bef07398..d781871d 100644 --- a/src/util/radians.scad +++ b/src/util/radians.scad @@ -8,4 +8,4 @@ * **/ -function radians(degrees) = PI / 180 * degrees; \ No newline at end of file +function radians(degrees) = (degrees * PI) / 180; \ No newline at end of file diff --git a/src/util/sum.scad b/src/util/sum.scad index 762afe92..28d3a99e 100644 --- a/src/util/sum.scad +++ b/src/util/sum.scad @@ -8,6 +8,4 @@ * **/ -use <_impl/_sum_impl.scad>; - -function sum(lt) = _sum_impl(lt, len(lt)); \ No newline at end of file +function sum(lt) = [for(i = [0:len(lt) - 1]) 1] * lt; \ No newline at end of file diff --git a/src/voronoi/_impl/_convex_intersection.scad b/src/voronoi/_impl/_convex_intersection.scad index 1d404ba5..9c9b5147 100644 --- a/src/voronoi/_impl/_convex_intersection.scad +++ b/src/voronoi/_impl/_convex_intersection.scad @@ -17,26 +17,20 @@ function _in_convex(convex_pts, pt) = function _intersection_ps(shape, line_pts, epsilon) = - let( - leng = len(shape), - pts = [each shape, shape[0]] - ) + let(pts = [each shape, shape[0]]) dedup([ - for(i = [0:leng - 1]) + for(i = [0:len(shape) - 1]) let(p = lines_intersection(line_pts, [pts[i], pts[i + 1]], epsilon = epsilon)) if(p != []) p ]); function _convex_intersection(shape1, shape2, epsilon = 0.0001) = (shape1 == [] || shape2 == []) ? [] : - let( - leng = len(shape1), - pts = [each shape1, shape1[0]] - ) + let(pts = [each shape1, shape1[0]]) _convex_ct_clk_order( concat( [for(p = shape1) if(_in_convex(shape2, p)) p], [for(p = shape2) if(_in_convex(shape1, p)) p], - [for(i = [0:leng - 1]) each _intersection_ps(shape2, [pts[i], pts[i + 1]], epsilon)] + [for(i = [0:len(shape1) - 1]) each _intersection_ps(shape2, [pts[i], pts[i + 1]], epsilon)] ) ); \ 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 7e3eb7a2..31519ca4 100644 --- a/src/voronoi/_impl/_vrn2_space_cells_impl.scad +++ b/src/voronoi/_impl/_vrn2_space_cells_impl.scad @@ -22,10 +22,7 @@ function cell_pt(fcord, grid_w, seed, x, y, gw, gh) = function _neighbors(fcord, seed, grid_w, gw, gh) = let(range = [-1:1]) concat( - [ - for(y = range, x = range) - cell_pt(fcord, grid_w, seed, x, y, gw, gh) - ], + [for(y = range, x = range) cell_pt(fcord, grid_w, seed, x, 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)],