1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-15 11:14:17 +02:00
This commit is contained in:
Justin Lin
2022-03-04 15:58:33 +08:00
parent 18ecbcee93
commit d9c890d8c3
18 changed files with 99 additions and 120 deletions

View File

@@ -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.x - 1) / len_path_pts_minus_one,
(scale.y - 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 // get rotation matrice for sections
@@ -114,15 +114,17 @@ module path_extrude(shape_pts, path_pts, triangles = "SOLID", twist = 0, scale =
let( let(
vt0 = pth_pts[j] - pth_pts[j - 1], vt0 = pth_pts[j] - pth_pts[j - 1],
vt1 = pth_pts[j + 1] - pth_pts[j], 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)) for(p = init_section(init_a, init_s))
[ [ms0p * p, ms1p * p, ms2p * p]
[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
]
]; ];
sections = sections =

View File

@@ -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]); begin_r = leng / cos((m - 0.5) * a_step - angles[0]);
end_r = leng / cos((n + 0.5) * a_step - angles[1]); 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]; is_angle_frag_end = angs[len(angs) - 1][2] == angles[1];

View File

@@ -30,9 +30,7 @@ module rounded_cube(size, corner_r, center = false) {
pair = [1, -1]; pair = [1, -1];
corners = [ corners = [
for(z = pair) for(z = pair, y = pair, x = pair)
for(y = pair)
for(x = pair)
[half_l * x, half_w * y, half_h * z] [half_l * x, half_w * y, half_h * z]
]; ];

View File

@@ -12,10 +12,10 @@ use <__comm__/__half_trapezium.scad>;
module rounded_cylinder(radius, h, round_r, convexity = 2, center = false) { module rounded_cylinder(radius, h, round_r, convexity = 2, center = false) {
r_corners = __half_trapezium(radius, h, round_r); r_corners = __half_trapezium(radius, h, round_r);
half_h = h / 2;
shape_pts = [[0, -half_h], each r_corners, [0, half_h]];
shape_pts = [[0, -h/2], each r_corners, [0, h/2]]; center_pt = center ? [0, 0, 0] : [0, 0, half_h];
center_pt = center ? [0, 0, 0] : [0, 0, h/2];
translate(center_pt) translate(center_pt)
rotate(180) rotate(180)

View File

@@ -21,8 +21,7 @@ module sweep(sections, triangles = "SOLID") {
) )
concat( concat(
[ [
for(j = range_j) for(j = range_j, i = range_i)
for(i = range_i)
[ [
j + i, j + i,
j + (i + 1) % leng_pts_sect, j + (i + 1) % leng_pts_sect,
@@ -30,8 +29,7 @@ module sweep(sections, triangles = "SOLID") {
] ]
], ],
[ [
for(j = range_j) for(j = range_j, i = range_i)
for(i = range_i)
[ [
j + i, j + i,
j + (i + 1) % leng_pts_sect + leng_pts_sect , j + (i + 1) % leng_pts_sect + 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; (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) = function the_same_after_twisting(f_sect, l_sect, leng_pts_sect) =
let( let(found_at_i = search_at(f_sect, l_sect[0], leng_pts_sect))
found_at_i = search_at(f_sect, l_sect[0], leng_pts_sect)
)
found_at_i <= 0 ? false : found_at_i <= 0 ? false :
l_sect == concat( l_sect == concat(
[for(i = found_at_i; i < leng_pts_sect; i = i + 1) f_sect[i]], [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]] [for(i = 0; i < found_at_i; i = i + 1) f_sect[i]]
); );
function to_v_pts(sects) = function to_v_pts(sects) = [for(sect = sects) each sect];
[
for(sect = sects)
for(pt = sect)
pt
];
module solid_sections(sects) { module solid_sections(sects) {
@@ -68,11 +59,7 @@ module sweep(sections, triangles = "SOLID") {
first_sect = sects[0]; first_sect = sects[0];
last_sect = sects[leng_sects - 1]; last_sect = sects[leng_sects - 1];
v_pts = [ v_pts = [for(sect = sects) each sect];
for(sect = sects)
for(pt = sect)
pt
];
begin_end_the_same = begin_end_the_same =
first_sect == last_sect || first_sect == last_sect ||

View File

@@ -3,12 +3,7 @@ use <../../util/map/hashmap_get.scad>;
use <../../util/find_index.scad>; use <../../util/find_index.scad>;
function indicesOfCell(iTris, triIndices) = function indicesOfCell(iTris, triIndices) =
let( _indicesOfCell(iTris, triIndices, len(iTris), [], iTris[0][0]);
vi = iTris[0][0],
indices = [],
leng = len(iTris)
)
_indicesOfCell(iTris, triIndices, leng, indices, vi);
function _indicesOfCell(iTris, triIndices, leng, indices, vi, i = 0) = function _indicesOfCell(iTris, triIndices, leng, indices, vi, i = 0) =
i == leng ? indices : i == leng ? indices :

View File

@@ -28,9 +28,10 @@ function tri_delaunay_voronoi(d) =
i_rts = [ i_rts = [
for(i = i_range) for(i = i_range)
let( let(
a = tris[i][0], tris_i = tris[i],
b = tris[i][1], a = tris_i[0],
c = tris[i][2], b = tris_i[1],
c = tris_i[2],
rt1 = [b, c, a], rt1 = [b, c, a],
rt2 = [c, a, b], rt2 = [c, a, b],
rt3 = [a, b, c] rt3 = [a, b, c]
@@ -44,9 +45,10 @@ function tri_delaunay_voronoi(d) =
triIndices = hashmap([ triIndices = hashmap([
for(i = i_range) for(i = i_range)
let( let(
a = tris[i][0], tris_i = tris[i],
b = tris[i][1], a = tris_i[0],
c = tris[i][2], b = tris_i[1],
c = tris_i[2],
rt1 = [b, c, a], rt1 = [b, c, a],
rt2 = [c, a, b], rt2 = [c, a, b],
rt3 = [a, b, c] rt3 = [a, b, c]

View File

@@ -12,8 +12,5 @@ use <_impl/_footprints2.scad>;
use <turtle2d.scad>; use <turtle2d.scad>;
function footprints2(cmds, start = [0, 0]) = function footprints2(cmds, start = [0, 0]) =
let( let(t = turtle2d("create", start.x, start.y, 0))
t = turtle2d("create", start.x, start.y, 0), [turtle2d("pt", t), each _footprints2(cmds, t, len(cmds))];
leng = len(cmds)
)
[turtle2d("pt", t), each _footprints2(cmds, t, leng)];

View File

@@ -12,8 +12,5 @@ use <_impl/_footprints3.scad>;
use <turtle3d.scad>; use <turtle3d.scad>;
function footprints3(cmds, start = [0, 0, 0]) = function footprints3(cmds, start = [0, 0, 0]) =
let( let(t = turtle3d("create", start, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
t = turtle3d("create", start, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]), [turtle3d("pt", t), each _footprints3(cmds, t, len(cmds))];
leng = len(cmds)
)
[turtle3d("pt", t), each _footprints3(cmds, t, leng)];

View File

@@ -16,9 +16,9 @@ function swap(lt, i, j) =
b = max([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], 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], 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]]
]; ];

View File

@@ -4,7 +4,6 @@ use <_convex_centroid.scad>;
function _convex_ct_clk_order(points) = function _convex_ct_clk_order(points) =
let( let(
cpt = _convex_centroid(points), cpt = _convex_centroid(points),
pts_as = [for(p = points) [p, atan2(p.y - cpt.y, p.x - cpt.x)]], pts_as = [for(p = points) [p, atan2(p.y - cpt.y, p.x - cpt.x)]]
sorted = sort(pts_as, by = "idx", idx = 1)
) )
[for(v = sorted) v[0]]; [for(v = sort(pts_as, by = "idx", idx = 1)) v[0]];

View File

@@ -6,8 +6,8 @@ function _lookup_noise_table(i) = _noise_table[i % 256];
function cell_pt(fcord, grid_w, seed, x, y, gw, gh) = function cell_pt(fcord, grid_w, seed, x, y, gw, gh) =
let( let(
nx = fcord[0] + x, nx = fcord.x + x,
ny = fcord[1] + y, ny = fcord.y + y,
sd_x = nx < 0 ? nx + gw : sd_x = nx < 0 ? nx + gw :
nx >= gw ? nx % gw : nx, nx >= gw ? nx % gw : nx,
sd_y = ny < 0 ? ny + gh : sd_y = ny < 0 ? ny + gh :

View File

@@ -18,10 +18,10 @@ function vrn2_cells_space(size, grid_w, seed) =
region_size = grid_w * 3, region_size = grid_w * 3,
half_region_size = region_size * 0.5, half_region_size = region_size * 0.5,
shape = shape_square(grid_w * 3), shape = shape_square(grid_w * 3),
gw = size[0] / grid_w, gw = size.x / grid_w,
gh = size[1] / grid_w, gh = size.y / grid_w,
cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size.y])
for(cx = [-grid_w:grid_w:size[0]]) for(cx = [-grid_w:grid_w:size.x])
let( let(
nbrs = _neighbors( nbrs = _neighbors(
[floor(cx / grid_w), floor(cy / grid_w)], [floor(cx / grid_w), floor(cy / grid_w)],

View File

@@ -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) = function cell_pt(fcord, seed, x, y, gw, gh) =
let( let(
nx = fcord[0] + x, nx = fcord.x + x,
ny = fcord[1] + y, ny = fcord.y + y,
sd_x = nx < 0 ? nx + gw : sd_x = nx < 0 ? nx + gw :
nx >= gw ? nx % gw : nx, nx >= gw ? nx % gw : nx,
sd_y = ny < 0 ? ny + gh : 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; gw = size[0] / grid_w;
gh = size[1] / grid_w; gh = size[1] / grid_w;
cell_nbrs_lt = [for(cy = [-grid_w:grid_w:size[1]]) cell_nbrs_lt = [
for(cx = [-grid_w:grid_w:size[0]]) for(cy = [-grid_w:grid_w:size.y], cx = [-grid_w:grid_w:size.x])
let( let(
nbrs = _neighbors( nbrs = _neighbors(
[floor(cx / grid_w), floor(cy / grid_w)], [floor(cx / grid_w), floor(cy / grid_w)],

View File

@@ -17,13 +17,12 @@ module vrn3_space(size, grid_w, seed, spacing = 1) {
function _lookup_noise_table(i) = _noise_table[i % 256]; function _lookup_noise_table(i) = _noise_table[i % 256];
function _neighbors(fcord, seed, grid_w) = [ function _neighbors(fcord, seed, grid_w) = [
for(z = [-1:1]) let(range = [-1:1])
for(y = [-1:1]) for(z = range, y = range, x = range)
for(x = [-1:1])
let( let(
nx = fcord[0] + x, nx = fcord.x + x,
ny = fcord[1] + y, ny = fcord.y + y,
nz = fcord[2] + z, nz = fcord.z + z,
sd_base = abs(nx + ny * grid_w + nz * grid_w * grid_w), sd_base = abs(nx + ny * grid_w + nz * grid_w * grid_w),
sd1 = _lookup_noise_table(seed + sd_base), sd1 = _lookup_noise_table(seed + sd_base),
sd2 = _lookup_noise_table(sd1 * 255 + sd_base), sd2 = _lookup_noise_table(sd1 * 255 + sd_base),
@@ -55,9 +54,7 @@ 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. It can be avoided by taking 177-nearest-neighbor cells but the time taken would be unacceptable.
*/ */
cell_nbrs_lt = [ cell_nbrs_lt = [
for(cz = [0:grid_w:size[2]]) for(cz = [0:grid_w:size.z], cy = [0:grid_w:size.y], cx = [0:grid_w:size.x])
for(cy = [0:grid_w:size[1]])
for(cx = [0:grid_w:size[0]])
let( let(
nbrs = _neighbors( nbrs = _neighbors(
[floor(cx / grid_w), floor(cy / grid_w), floor(cz / grid_w)], [floor(cx / grid_w), floor(cy / grid_w), floor(cz / grid_w)],

View File

@@ -27,7 +27,7 @@ function _vx_cylinder_diff_r(r, h, filled, thickness) =
let(r = round(r1 + dr * i)) let(r = round(r1 + dr * i))
each [ each [
for(pt = _vx_cylinder_vx_circle(r, filled, thickness)) 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) for(i = 0; i < h; i = i + 1)
each [ each [
for(pt = c) for(pt = c) [each pt, i]
[pt.x, pt.y, i]
] ]
]; ];

View File

@@ -8,7 +8,7 @@ function vx_polygon(points, filled = false) =
!filled ? contour : !filled ? contour :
let( let(
sortedXY = sort(contour, by = "vt"), sortedXY = sort(contour, by = "vt"),
ys = [for(p = sortedXY) p[1]], ys = [for(p = sortedXY) p.y],
rows = [ rows = [
for(y = [min(ys):max(ys)]) for(y = [min(ys):max(ys)])
let( let(
@@ -20,9 +20,9 @@ function vx_polygon(points, filled = false) =
sortedXY, sortedXY,
[ [
for(row = rows) for(row = rows)
let(to = len(row) - 1, y = row[0][1]) let(to = len(row) - 1, row0 = row[0], y = row0[1])
if(to > 0 && (row[0][0] + 1 != row[to][0])) if(to > 0 && (row0[0] + 1 != row[to][0]))
for(i = [row[0][0] + 1:row[to][0] - 1]) for(i = [row0[0] + 1:row[to][0] - 1])
let(p = [i, y]) let(p = [i, y])
if(in_shape(points, p)) p if(in_shape(points, p)) p
] ]