Rewrote extrude_2d_shapes_along_3dpath() to use quaternions, fixing extrusion rotation issues.

This commit is contained in:
Revar Desmera 2019-02-02 02:19:05 -08:00
parent 851dec95ac
commit c72b9d0459

View File

@ -230,45 +230,43 @@ module extrude_2dpath_along_3dpath(polyline, path, convexity=10) {
// clipsize = increase if artifacts are left. Default: 1000 // clipsize = increase if artifacts are left. Default: 1000
// Example: // Example:
// path = [ [0, 0, 0], [33, 33, 33], [66, 33, 40], [100, 0, 0] ]; // path = [ [0, 0, 0], [33, 33, 33], [66, 33, 40], [100, 0, 0] ];
// extrude_2d_shapes_along_3dpath(path) circle(r=10, center=true); // extrude_2d_shapes_along_3dpath(path) circle(r=10, center=true, $fn=5);
module extrude_2d_shapes_along_3dpath(path, convexity=10, clipsize=1000) { module extrude_2d_shapes_along_3dpath(path, convexity=10, clipsize=100) {
function polyquats(path, q=Q_Ident(), v=[0,0,1], i=0) = let(
v2 = path[i+1] - path[i],
ang = vector3d_angle(v,v2),
axis = ang>0.001? normalize(cross(v,v2)) : [0,0,1],
newq = Q_Mul(Quat(axis, ang), q),
dist = norm(v2)
) i < (len(path)-2)?
concat([[dist, newq, ang]], polyquats(path, newq, v2, i+1)) :
[[dist, newq, ang]];
epsilon = 0.0001; // Make segments ever so slightly too long so they overlap.
ptcount = len(path); ptcount = len(path);
pquats = polyquats(path);
for (i = [0 : ptcount-2]) { for (i = [0 : ptcount-2]) {
pt1 = path[i]; pt1 = path[i];
pt2 = path[i+1]; pt2 = path[i+1];
pt0 = i==0? pt1 : path[i-1]; dist = pquats[i][0];
pt3 = (i>=ptcount-2)? pt2 : path[i+2]; q = pquats[i][1];
dist = distance(pt1,pt2); difference() {
v1 = pt2-pt1; translate(pt1) {
v0 = (i==0)? v1 : (pt1-pt0); Qrot(q) {
v2 = (i==ptcount-2)? v1 : (pt3-pt2); down(clipsize/2/2) {
az1 = atan2(v1[1], v1[0]); linear_extrude(height=dist+clipsize/2, convexity=convexity) {
alt1 = (len(pt1)<3)? 0 : atan2(v1[2], hypot(v1[1], v1[0]));
az0 = atan2(v0[1], v0[0]);
alt0 = (len(pt0)<3)? 0 : atan2(v0[2], hypot(v0[1], v0[0]));
az2 = atan2(v2[1], v2[0]);
alt2 = (len(pt2)<3)? 0 : atan2(v2[2], hypot(v2[1], v2[0]));
translate(pt1) {
difference() {
rotate([0, 90-alt1, az1]) {
down(dist) {
linear_extrude(height=dist*3, convexity=10) {
children(); children();
} }
} }
} }
rotate([0, 90-(alt0+alt1)/2, (az0+az1)/2]) { }
down(dist+0.05) { translate(pt1) {
cube(size=[clipsize,clipsize,dist*2], center=true); hq = (i > 0)? Q_Slerp(q, pquats[i-1][1], 0.5) : q;
} Qrot(hq) down(clipsize/2+epsilon) cube(clipsize, center=true);
} }
translate(v1) { translate(pt2) {
rotate([0, 90-(alt1+alt2)/2, (az1+az2)/2]) { hq = (i < ptcount-2)? Q_Slerp(q, pquats[i+1][1], 0.5) : q;
up(dist) { Qrot(hq) up(clipsize/2+epsilon) cube(clipsize, center=true);
cube(size=[clipsize,clipsize,dist*2], center=true);
}
}
}
} }
} }
} }