1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 22:28:16 +01:00

add sweep

This commit is contained in:
Justin Lin 2020-02-29 09:17:40 +08:00
parent 3efaf6e14e
commit 5177a5f4d9

248
src/experimental/sweep.scad Normal file
View File

@ -0,0 +1,248 @@
/**
copy from polysections. sweep will replace polysections.
**/
use <util/reverse.scad>;
module sweep(sections, triangles = "SOLID") {
function side_indexes(sects, begin_idx = 0) =
let(
leng_sects = len(sects),
leng_pts_sect = len(sects[0]),
range_j = [begin_idx:leng_pts_sect:begin_idx + (leng_sects - 2) * leng_pts_sect],
range_i = [0:leng_pts_sect - 1]
)
concat(
[
for(j = range_j)
for(i = range_i)
[
j + i,
j + (i + 1) % leng_pts_sect,
j + (i + 1) % leng_pts_sect + leng_pts_sect
]
],
[
for(j = range_j)
for(i = range_i)
[
j + i,
j + (i + 1) % leng_pts_sect + leng_pts_sect ,
j + i + leng_pts_sect
]
]
);
function search_at(f_sect, p, leng_pts_sect, i = 0) =
i < leng_pts_sect ?
(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) =
let(
found_at_i = search_at(f_sect, l_sect[0], leng_pts_sect)
)
found_at_i <= 0 ? false :
l_sect == concat(
[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]]
);
function to_v_pts(sects) =
[
for(sect = sects)
for(pt = sect)
pt
];
module solid_sections(sects) {
leng_sects = len(sects);
leng_pts_sect = len(sects[0]);
first_sect = sects[0];
last_sect = sects[leng_sects - 1];
v_pts = [
for(sect = sects)
for(pt = sect)
pt
];
begin_end_the_same =
first_sect == last_sect ||
the_same_after_twisting(first_sect, last_sect, leng_pts_sect);
if(begin_end_the_same) {
f_idxes = side_indexes(sects);
polyhedron(
v_pts,
f_idxes
);
// hook for testing
test_polysections_solid(v_pts, f_idxes, triangles);
} else {
range_i = [0:leng_pts_sect - 1];
first_idxes = [for(i = range_i) leng_pts_sect - 1 - i];
last_idxes = [
for(i = range_i)
i + leng_pts_sect * (leng_sects - 1)
];
f_idxes = concat([first_idxes], side_indexes(sects), [last_idxes]);
polyhedron(
v_pts,
f_idxes
);
// hook for testing
test_polysections_solid(v_pts, f_idxes, triangles);
}
}
module hollow_sections(sects) {
leng_sects = len(sects);
leng_sect = len(sects[0]);
half_leng_sect = leng_sect / 2;
half_leng_v_pts = leng_sects * half_leng_sect;
function strip_sects(begin_idx, end_idx) =
[
for(i = 0; i < leng_sects; i = i + 1)
[
for(j = begin_idx; j <= end_idx; j = j + 1)
sects[i][j]
]
];
function first_idxes() =
[
for(i = 0; i < half_leng_sect; i = i + 1)
[
i,
i + half_leng_v_pts,
(i + 1) % half_leng_sect + half_leng_v_pts,
(i + 1) % half_leng_sect
]
];
function last_idxes(begin_idx) =
[
for(i = 0; i < half_leng_sect; i = i + 1)
[
begin_idx + i,
begin_idx + (i + 1) % half_leng_sect,
begin_idx + (i + 1) % half_leng_sect + half_leng_v_pts,
begin_idx + i + half_leng_v_pts
]
];
outer_sects = strip_sects(0, half_leng_sect - 1);
inner_sects = strip_sects(half_leng_sect, leng_sect - 1);
outer_v_pts = to_v_pts(outer_sects);
inner_v_pts = to_v_pts(inner_sects);
outer_idxes = side_indexes(outer_sects);
inner_idxes = [
for(idxes = side_indexes(inner_sects, half_leng_v_pts))
reverse(idxes)
];
first_outer_sect = outer_sects[0];
last_outer_sect = outer_sects[leng_sects - 1];
first_inner_sect = inner_sects[0];
last_inner_sect = inner_sects[leng_sects - 1];
leng_pts_sect = len(first_outer_sect);
begin_end_the_same =
(first_outer_sect == last_outer_sect && first_inner_sect == last_inner_sect) ||
(
the_same_after_twisting(first_outer_sect, last_outer_sect, leng_pts_sect) &&
the_same_after_twisting(first_inner_sect, last_inner_sect, leng_pts_sect)
);
v_pts = concat(outer_v_pts, inner_v_pts);
if(begin_end_the_same) {
f_idxes = concat(outer_idxes, inner_idxes);
polyhedron(
v_pts,
f_idxes
);
// hook for testing
test_polysections_solid(v_pts, f_idxes, triangles);
} else {
first_idxes = first_idxes();
last_idxes = last_idxes(half_leng_v_pts - half_leng_sect);
f_idxes = concat(first_idxes, outer_idxes, inner_idxes, last_idxes);
polyhedron(
v_pts,
f_idxes
);
// hook for testing
test_polysections_solid(v_pts, f_idxes, triangles);
}
}
module triangles_defined_sections() {
module tri_sections(tri1, tri2) {
hull() polyhedron(
points = concat(tri1, tri2),
faces = [
[0, 1, 2],
[3, 5, 4],
[1, 3, 4], [2, 1, 4], [2, 3, 0],
[0, 3, 1], [2, 4, 5], [2, 5, 3]
]
);
}
module two_sections(section1, section2) {
for(idx = triangles) {
tri_sections(
[
section1[idx[0]],
section1[idx[1]],
section1[idx[2]]
],
[
section2[idx[0]],
section2[idx[1]],
section2[idx[2]]
]
);
}
}
for(i = [0:len(sections) - 2]) {
two_sections(
sections[i],
sections[i + 1]
);
}
}
if(triangles == "SOLID") {
solid_sections(sections);
} else if(triangles == "HOLLOW") {
hollow_sections(sections);
}
else {
triangles_defined_sections();
}
}
// override it to test
module test_polysections_solid(points, faces, triangles) {
}