Fixed facet sampling bugs in spiral_sweep

This commit is contained in:
Adrian Mariano 2021-08-24 22:14:54 -04:00
parent 3c3e911db9
commit 8b23eb4951
2 changed files with 31 additions and 34 deletions

View file

@ -1054,6 +1054,8 @@ module extrude_from_to(pt1, pt2, convexity, twist, scale, slices) {
// poly = [[-10,0], [-3,-5], [3,-5], [10,0], [0,-30]]; // poly = [[-10,0], [-3,-5], [3,-5], [10,0], [0,-30]];
// spiral_sweep(poly, h=200, r=50, twist=1080, $fn=36); // spiral_sweep(poly, h=200, r=50, twist=1080, $fn=36);
module spiral_sweep(poly, h, r, twist=360, higbee, center, r1, r2, d, d1, d2, higbee1, higbee2, internal=false, anchor, spin=0, orient=UP) { module spiral_sweep(poly, h, r, twist=360, higbee, center, r1, r2, d, d1, d2, higbee1, higbee2, internal=false, anchor, spin=0, orient=UP) {
higsample = 10; // Oversample factor for higbee tapering
dummy1=assert(twist>0);
bounds = pointlist_bounds(poly); bounds = pointlist_bounds(poly);
yctr = (bounds[0].y+bounds[1].y)/2; yctr = (bounds[0].y+bounds[1].y)/2;
xmin = bounds[0].x; xmin = bounds[0].x;
@ -1063,17 +1065,19 @@ module spiral_sweep(poly, h, r, twist=360, higbee, center, r1, r2, d, d1, d2, hi
r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=50); r1 = get_radius(r1=r1, r=r, d1=d1, d=d, dflt=50);
r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=50); r2 = get_radius(r1=r2, r=r, d1=d2, d=d, dflt=50);
sides = segs(max(r1,r2)); sides = segs(max(r1,r2));
steps = ceil(sides*(twist/360)); ang_step = 360/sides;
anglist = [for(ang = [0:ang_step:twist-EPSILON]) ang,
twist];
higbee1 = first_defined([higbee1, higbee, 0]); higbee1 = first_defined([higbee1, higbee, 0]);
higbee2 = first_defined([higbee2, higbee, 0]); higbee2 = first_defined([higbee2, higbee, 0]);
assert(higbee1>=0 && higbee2>=0);
higang1 = 360 * higbee1 / (2 * r1 * PI); higang1 = 360 * higbee1 / (2 * r1 * PI);
higang2 = 360 * higbee2 / (2 * r2 * PI); higang2 = 360 * higbee2 / (2 * r2 * PI);
higsteps1 = ceil(higang1/360*sides); dummy2=assert(higbee1>=0 && higbee2>=0)
higsteps2 = ceil(higang2/360*sides); assert(higang1 < twist/2)
assert(twist>0); assert(higang2 < twist/2);
assert(higang1 < twist/2); function polygon_r(N,theta) =
assert(higang2 < twist/2); let( alpha = 360/N )
cos(alpha/2)/(cos(posmod(theta,alpha)-alpha/2));
function higsize(a) = lookup(a,[ function higsize(a) = lookup(a,[
[-0.001, higang1>0?0:1], [-0.001, higang1>0?0:1],
if (higang1>0) for (x=[0.125:0.125:1]) [ x*higang1, pow(x,1/2)], if (higang1>0) for (x=[0.125:0.125:1]) [ x*higang1, pow(x,1/2)],
@ -1081,26 +1085,25 @@ module spiral_sweep(poly, h, r, twist=360, higbee, center, r1, r2, d, d1, d2, hi
[twist+0.001, higang2>0?0:1] [twist+0.001, higang2>0?0:1]
]); ]);
us = [ interp_ang = [
0, for(i=idx(anglist,e=-2))
for (i=[higsteps1/10:higsteps1/10:higsteps1]) i, each lerpn(anglist[i],anglist[i+1],
for (i=[higsteps1+1:1:steps-higsteps2-1]) i, (higang1<anglist[i+1] || (twist-higang2>anglist[i])) ? ceil((anglist[i+1]-anglist[i])/ang_step*higsample)
for (i=[steps-higsteps2:higsteps2/10:steps-higsteps2/10]) i, : 1,
steps endpoint=false),
]; last(anglist)
zang = atan2(r2-r1,h); ];
echo(interp_ang=interp_ang,twist=twist);
skewmat = affine3d_skew_xz(xa=atan2(r2-r1,h));
points = [ points = [
for (p = us) let ( for (a = interp_ang) let (
u = p / steps,
a = twist * u,
hsc = higsize(a), hsc = higsize(a),
u = a/twist,
r = lerp(r1,r2,u), r = lerp(r1,r2,u),
mat = affine3d_zrot(a) * mat = affine3d_zrot(a) *
affine3d_translate([r, 0, h * (u-0.5)]) * affine3d_translate([polygon_r(sides,a)*r, 0, h * (u-0.5)]) *
affine3d_xrot(90) * affine3d_xrot(90) *
affine3d_skew_xz(xa=zang) * skewmat *
//affine3d_scale([hsc,lerp(hsc,1,0.25),1]), //affine3d_scale([hsc,lerp(hsc,1,0.25),1]),
scale([hsc,lerp(hsc,1,0.25),1], cp=[internal ? xmax : xmin, yctr, 0]), scale([hsc,lerp(hsc,1,0.25),1], cp=[internal ? xmax : xmin, yctr, 0]),
pts = apply(mat, poly) pts = apply(mat, poly)

View file

@ -1087,7 +1087,6 @@ module generic_threaded_nut(
} }
// Module: thread_helix() // Module: thread_helix()
// Usage: // Usage:
// thread_helix(d, pitch, [thread_depth], [flank_angle], [twist], [profile=], [left_handed=], [higbee=], [internal=]); // thread_helix(d, pitch, [thread_depth], [flank_angle], [twist], [profile=], [left_handed=], [higbee=], [internal=]);
@ -1196,16 +1195,11 @@ module thread_helix(
// //
// Fixed higbee in spiral_sweep for properly centered scaling and for staying on the internal/external base of threads // Fixed higbee in spiral_sweep for properly centered scaling and for staying on the internal/external base of threads
// Fixed bug in spiral_sweep where two segments were missing if higbee is zero // Fixed bug in spiral_sweep where two segments were missing if higbee is zero
// // Fixed faceting bugs in spiral_sweep where segments weren't aligned with requested number of segments, and higbee
// would pull away from the cylinder by using a higher count and following a true circle
//
// Questions: // Questions:
// Existing code intersection with cylinder, so any profile over 0 gets removed. Also---exact match intersection? Better
// to intersect with a cube that is larger than the screw? Cheaper....
// higbee can look bad due to edge issues in the VNF. Possibly "convex" style is better. "quincunx" may be best, but costly.
// buttress_threaded_rod(d=25, l=20, pitch=2, $fa=1, $fs=1,higbee=15, $fn=32);
// Use quincunx when higbee is requested but min_edge otherwise?
// Should nut modules take d1/d2 for tapered nuts? // Should nut modules take d1/d2 for tapered nuts?
//
// Run time checks. No difference for preview, but switching intersection to cube reduced render in the test from 11s to 8s. // Need explanation of what exactly the diff is between threaded_rod and helix_threads.
// Switching to doing the intersection with vnf_halfspace made preview slower, 3.8s instead of 1.6s but render faster (4s instead of 8s) // Higbee is different, angle in one and length in another. Need to reconcile
// Using custom masks for beveling reduces render to 8s.