1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-12 09:44:16 +02:00
This commit is contained in:
Justin Lin
2022-03-01 15:52:17 +08:00
parent 8475049fad
commit 96ec1e0c26
14 changed files with 96 additions and 167 deletions

View File

@@ -49,7 +49,7 @@ module along_with(points, angles, twist = 0, scale = 1.0, method = "AXIS_ANGLE")
let( let(
vt0 = pts[i] - pts[i - 1], vt0 = pts[i] - pts[i - 1],
vt1 = pts[i + 1] - pts[i], 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) v = cross(vt0, vt1)
) )
[a, v] [a, v]

View File

@@ -23,8 +23,8 @@ function _tri_circumcircle(shape_pts) =
) )
det == 0 ? undef : det == 0 ? undef :
let( let(
x = (d1 * v0[1] - d0 * v1[1]) / det, x = (d1 * v0.y - d0 * v1.y) / det,
y = (d0 * v1[0] - d1 * v0[0]) / det, y = (d0 * v1.x - d1 * v0.x) / det,
center = [x, y], center = [x, y],
v = p0 - center v = p0 - center
) )
@@ -179,10 +179,9 @@ function delaunayBadTriangles(d, p, _indices_hash) =
function inCircumcircle(t, p, circles, _indices_hash) = function inCircumcircle(t, p, circles, _indices_hash) =
let( let(
c = hashmap_get(circles, t, hash = _indices_hash), c = hashmap_get(circles, t, hash = _indices_hash),
ct = cc_center(c), v = cc_center(c) - p
rr = (ct[0] - p[0]) ^ 2 + (ct[1] - p[1]) ^ 2
) )
rr <= cc_rr(c); v * v <= cc_rr(c);
function delaunayBoundaries(d, badTriangles, _indices_hash) = function delaunayBoundaries(d, badTriangles, _indices_hash) =
let( let(

View File

@@ -14,13 +14,7 @@ function _triangulate_snipable(shape_pts, u, v, w, n, indices, epsilon = 0.0001)
a = shape_pts[indices[u]], a = shape_pts[indices[u]],
b = shape_pts[indices[v]], b = shape_pts[indices[v]],
c = shape_pts[indices[w]], c = shape_pts[indices[w]],
ax = a[0], determinant = cross([b.x - a.x, b.y - a.y], [c.x - a.x, c.y - a.y])
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])
) )
epsilon > determinant ? epsilon > determinant ?
false : _triangulate_snipable_sub(shape_pts, n, u, v, w, a, b, c, indices); 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 // remove the elem at idx v from indices
function _triangulate_remove_v(indices, v, num_of_vertices) = function _triangulate_remove_v(indices, v, num_of_vertices) =
let( let(nv_minuns_one = num_of_vertices - 1)
nv_minuns_one = num_of_vertices - 1
)
v == 0 ? [for(i = 1; i <= nv_minuns_one; i = i + 1) indices[i]] : ( 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( 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]], [for(i = 0; i < v; i = i + 1) indices[i]],

View File

@@ -20,6 +20,6 @@ function tri_circumcenter(shape_pts) =
det = -cross(v0 , v1) det = -cross(v0 , v1)
) )
det == 0 ? undef : [ det == 0 ? undef : [
(d1 * v0[1] - d0 * v1[1]) / det, (d1 * v0.y - d0 * v1.y) / det,
(d0 * v1[0] - d1 * v0[0]) / det (d0 * v1.x - d1 * v0.x) / det
]; ];

View File

@@ -24,8 +24,8 @@ function tri_delaunay(points, ret = "TRI_INDICES") =
max_y = max(ys), max_y = max(ys),
min_y = min(ys), min_y = min(ys),
center = [max_x + min_x, max_y + min_y] / 2, center = [max_x + min_x, max_y + min_y] / 2,
width = abs(max_x - center[0]) * 4, width = abs(max_x - center.x) * 4,
height = abs(max_y - center[1]) * 4, height = abs(max_y - center.y) * 4,
leng_pts = len(points), leng_pts = len(points),
d = _tri_delaunay( d = _tri_delaunay(
delaunay_init(center, width, height, leng_pts, _indices_hash), delaunay_init(center, width, height, leng_pts, _indices_hash),

View File

@@ -10,4 +10,4 @@
use <../__comm__/__angy_angz.scad>; use <../__comm__/__angy_angz.scad>;
function polar_coordinate(point) = [norm(point), atan2(point[1], point[0])]; // r, theta function polar_coordinate(point) = [norm(point), atan2(point.y, point.x)]; // r, theta

View File

@@ -12,6 +12,6 @@ function spherical_coordinate(point) =
// mathematics [r, theta, phi] // mathematics [r, theta, phi]
[ [
norm(point), norm(point),
atan2(point[1], point[0]), atan2(point.y, point.x),
atan2(sqrt(point[0]^2 + point[1]^2), point[2]) atan2(sqrt(point.x ^ 2 + point.y ^ 2), point.z)
]; ];

View File

@@ -8,4 +8,6 @@
* *
**/ **/
function sum(lt) = [for(i = [0:len(lt) - 1]) 1] * lt; function sum(lt) =
let(leng = len(lt))
[for(i = 0; i < leng; i = i + 1) 1] * lt;

View File

@@ -26,7 +26,7 @@ _vx_contour_nxt_offset = [
]; ];
function _vx_contour_travel(pts, p, fst) = function _vx_contour_travel(pts, p, fst) =
let( 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 = p + _vx_contour_nxt_offset[dir_i]
) )
nxt_p == fst ? [p] : [p, each _vx_contour_travel(pts, nxt_p, fst)]; nxt_p == fst ? [p] : [p, each _vx_contour_travel(pts, nxt_p, fst)];

View File

@@ -1,20 +1,18 @@
function _vx_cylinder_vx_circle(radius, filled, thickness) = function _vx_cylinder_vx_circle(radius, filled, thickness) =
let(range = [-radius: radius - 1]) let(range = [-radius: radius - 1])
filled ? [ filled ? [
for(y = range) for(y = range, x = range)
for(x = range) let(v = [x, y])
let(v = [x, y]) if(norm(v) < radius) v
if(norm(v) < radius) v
] : ] :
let(ishell = radius * radius - 2 * thickness * radius) let(ishell = radius ^ 2 - 2 * thickness * radius)
[ [
for(y = range) for(y = range, x = range)
for(x = range) let(
let( v = [x, y],
v = [x, y], leng = norm(v)
leng = norm(v) )
) if(leng < radius && (leng ^ 2) > ishell) v
if(leng < radius && (leng * leng) > ishell) v
]; ];
function _vx_cylinder_diff_r(r, h, filled, thickness) = 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)) 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[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) for(i = 0; i < h; i = i + 1)
each [ each [
for(pt = c) for(pt = c)
[pt[0], pt[1], i] [pt.x, pt.y, i]
] ]
]; ];

View File

@@ -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_zd(zd, ax, az) = (zd >= 0 ? zd - ax : zd) + az;
function _vx_line_xdominant(start, end, a, s) = function _vx_line_xdominant(start, end, a, s) =
let( let(
x = start[0], shrx = floor(a.x / 2),
y = start[1], yd = a.y - shrx,
z = start[2], zd = a.z - shrx
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]
) )
[ [
start, start,
each _vx_line_xdominant_sub( each _vx_line_xdominant_sub(
x + sx, start.x + s.x,
_vx_line_xdominant_y(y, yd, sy), _vx_line_xdominant_y(start.y, yd, s.y),
_vx_line_xdominant_z(z, zd, sz), _vx_line_xdominant_z(start.z, zd, s.z),
endx, end.x,
a, a,
s, s,
_vx_line_xdominant_yd(yd, ax, ay), _vx_line_xdominant_yd(yd, a.x, a.y),
_vx_line_xdominant_zd(zd, ax, az) _vx_line_xdominant_zd(zd, a.x, a.z)
) )
]; ];
function _vx_line_xdominant_sub(x, y, z, endx, a, s, yd, zd) = 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 == endx ? [] : [
[x, y, z], [x, y, z],
each _vx_line_xdominant_sub( each _vx_line_xdominant_sub(
x + sx, x + s.x,
_vx_line_xdominant_y(y, yd, sy), _vx_line_xdominant_y(y, yd, s.y),
_vx_line_xdominant_z(z, zd, sz), _vx_line_xdominant_z(z, zd, s.z),
endx, endx,
a, a,
s, s,
_vx_line_xdominant_yd(yd, ax, ay), _vx_line_xdominant_yd(yd, a.x, a.y),
_vx_line_xdominant_zd(zd, ax, az) _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_zd(zd, ay, az) = (zd >= 0 ? zd - ay : zd) + az;
function _vx_line_ydominant(start, end, a, s) = function _vx_line_ydominant(start, end, a, s) =
let( let(
x = start[0], shry = floor(a.y / 2),
y = start[1], xd = a.x - shry,
z = start[2], zd = a.z - shry
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]
) )
[ [
start, start,
each _vx_line_ydominant_sub( each _vx_line_ydominant_sub(
_vx_line_ydominant_x(x, xd, sx), _vx_line_ydominant_x(start.x, xd, s.x),
y + sy, start.y + s.y,
_vx_line_ydominant_z(z, zd, sz), _vx_line_ydominant_z(start.z, zd, s.z),
endy, end.y,
a, a,
s, s,
_vx_line_ydominant_xd(xd, ax, ay), _vx_line_ydominant_xd(xd, a.x, a.y),
_vx_line_ydominant_zd(zd, ay, az) _vx_line_ydominant_zd(zd, a.y, a.z)
) )
]; ];
function _vx_line_ydominant_sub(x, y, z, endy, a, s, xd, zd) = 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 ? [] : [ y == endy ? [] : [
[x, y, z], [x, y, z],
each _vx_line_ydominant_sub( each _vx_line_ydominant_sub(
_vx_line_ydominant_x(x, xd, sx), _vx_line_ydominant_x(x, xd, s.x),
y + sy, y + s.y,
_vx_line_ydominant_z(z, zd, sz), _vx_line_ydominant_z(z, zd, s.z),
endy, endy,
a, a,
s, s,
_vx_line_ydominant_xd(xd, ax, ay), _vx_line_ydominant_xd(xd, a.x, a.y),
_vx_line_ydominant_zd(zd, ay, az) _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_yd(yd, ay, az) = (yd >= 0 ? yd - az : yd) + ay;
function _vx_line_zdominant(start, end, a, s) = function _vx_line_zdominant(start, end, a, s) =
let( let(
x = start[0], shrz = floor(a.z / 2),
y = start[1], xd = a.x - shrz,
z = start[2], yd = a.y - shrz
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]
) )
[ [
start, start,
each _vx_line_zdominant_sub( each _vx_line_zdominant_sub(
_vx_line_zdominant_x(x, xd, sx), _vx_line_zdominant_x(start.x, xd, s.x),
_vx_line_zdominant_y(y, yd, sy), _vx_line_zdominant_y(start.y, yd, s.y),
z + sz, start.z + s.z,
endz, end.z,
a, a,
s, s,
_vx_line_zdominant_xd(xd, ax, az), _vx_line_zdominant_xd(xd, a.x, a.z),
_vx_line_zdominant_yd(yd, ay, az) _vx_line_zdominant_yd(yd, a.y, a.z)
) )
]; ];
function _vx_line_zdominant_sub(x, y, z, endz, a, s, xd, yd) = 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 ? [] : [ z == endz ? [] : [
[x, y, z], [x, y, z],
each _vx_line_zdominant_sub( each _vx_line_zdominant_sub(
_vx_line_zdominant_x(x, xd, sx), _vx_line_zdominant_x(x, xd, s.x),
_vx_line_zdominant_y(y, yd, sy), _vx_line_zdominant_y(y, yd, s.y),
z + sz, z + s.z,
endz, endz,
a, a,
s, s,
_vx_line_zdominant_xd(xd, ax, az), _vx_line_zdominant_xd(xd, a.x, a.z),
_vx_line_zdominant_yd(yd, ay, az) _vx_line_zdominant_yd(yd, a.y, a.z)
) )
]; ];

View File

@@ -12,5 +12,4 @@ use <_impl/_vx_circle_impl.scad>;
use <../util/dedup.scad>; use <../util/dedup.scad>;
function vx_circle(radius, filled = false) = function vx_circle(radius, filled = false) =
let(all = _vx_circle_impl(radius, filled)) dedup(_vx_circle_impl(radius, filled));
dedup(all);

View File

@@ -12,17 +12,14 @@ use <_impl/_vx_curve_impl.scad>;
use <../util/dedup.scad>; use <../util/dedup.scad>;
function vx_curve(points, tightness = 0) = function vx_curve(points, tightness = 0) =
let( let(leng = len(points))
leng = len(points), dedup([
pts = [ each [
each [ for(i = [0:leng - 4])
for(i = [0:leng - 4]) let(
let( pts = _vx_catmull_rom_spline_4pts([for(j = [i:i + 3]) points[j]], tightness)
pts = _vx_catmull_rom_spline_4pts([for(j = [i:i + 3]) points[j]], tightness) )
) for(i = [0:len(pts) - 2]) pts[i]
for(i = [0:len(pts) - 2]) pts[i] ],
], points[leng - 2]
points[leng - 2] ]);
]
)
dedup(pts);

View File

@@ -11,21 +11,17 @@
function vx_sphere(radius, filled = false, thickness = 1) = function vx_sphere(radius, filled = false, thickness = 1) =
let(range = [-radius: radius - 1]) let(range = [-radius: radius - 1])
filled ? [ filled ? [
for(z = range) for(z = range, y = range, x = range)
for(y = range) let(v = [x, y, z])
for(x = range) if(norm(v) < radius) v
let(v = [x, y, z])
if(norm(v) < radius) v
] : ] :
let(ishell = radius * radius - 2 * thickness * radius) let(ishell = radius * radius - 2 * thickness * radius)
[ [
for(z = range) for(z = range, y = range, x = range)
for(y = range) let(
for(x = range) v = [x, y, z],
let( leng = norm(v)
v = [x, y, z], )
leng = norm(v) if(leng < radius && (leng * leng) > ishell) v
)
if(leng < radius && (leng * leng) > ishell) v
]; ];