signifcant speed-up and added size modifier along extrusion

This commit is contained in:
tessaK9 2026-03-11 11:16:55 +01:00
commit d2911fafc3

View file

@ -1,41 +1,33 @@
module section(points){
module extrusion(points){
//points[0] must have its points ordered clockwise
assert(len(points) == 2, "section is formed between exactly two polygons");
assert(len(points[0]) == len(points[1]), "polygons need to have the same number of vertices");
assert(len(points[0])>2, "polygons need to have at least 3 sides");
n = len(points[0]);
points_new = concat(points[0], points[1]);
faces = concat([[for(i = [0 : 1 : n-1]) i]], concat([[for(i = [1 : n]) (2*n)-i]], concat(
[for(i = [0 : n-2]) [i+1, i, n+i, n+i+1]], [[0,n-1,(2*n)-1,n]])));
n = len(points);
k = len(points[0]);
points_new = [for(L = points) for(p = L) p];
faces = concat([[for(i = [0 : k-1]) i], [for(i = [1 : k]) (n*k)-i]], concat([for (i = [0 : n-2]) for (j = [0 : k-2]) [(i*k)+j+1, (i*k)+j, ((i+1)*k)+j, ((i+1)*k)+j+1]], [for (i = [0 : n-2]) [i*k,(i+1)*k-1,((i+2)*k)-1,(i+1)*k]]));
polyhedron(points = points_new, faces = faces, convexity = 10);
}
module extrusion(points){
assert(len(points)>1, "extrusion is formed between at least two polygons");
for(i = [0 : len(points)-2]) section([points[i],points[i+1]]);
}
module bezier_extrude(ctrl, shape, steps = 512){
module bezier_extrude(ctrl, shape, mod = function (t) 1, sections = 64){
B_0 = ctrl[0];
B_1 = 3*((-1)*ctrl[0] + ctrl[1]);
B_1 = 3*(-ctrl[0] + ctrl[1]);
B_2 = 3*(ctrl[0] - 2*ctrl[1] + ctrl[2]);
B_3 = (-1)*ctrl[0] + 3*(ctrl[1] - ctrl[2]) + ctrl[3];
B_3 = -ctrl[0] + 3*(ctrl[1] - ctrl[2]) + ctrl[3];
function bezier(t) = B_0 + t*B_1 + (t^2)*B_2 + (t^3)*B_3;
Bd_0 = 3*((-1)*ctrl[0] + ctrl[1]);
Bd_1 = 6*(ctrl[0] - 2*ctrl[1] + ctrl[2]);
Bd_2 = 3*((-1)*ctrl[0] + 3*(ctrl[1] - ctrl[2]) + ctrl[3]);
Bn_0 = [Bd_0.y,-Bd_0.x];
Bn_1 = [Bd_1.y,-Bd_1.x];
Bn_2 = [Bd_2.y,-Bd_2.x];
Bn_0 = [B_1.y,-B_1.x];
Bn_1 = [2*B_2.y,(-2)*B_2.x];
Bn_2 = [3*B_3.y,(-3)*B_3.x];
function normal(t) = Bn_0 + t*Bn_1 + (t^2)*Bn_2;
slices = [for(i = [0 : steps]) let (t = i/steps, p = bezier(t), normal = normal(t), n = normal/norm(normal)) [for (v = shape) [p.x+(n.x * v.x),p.y+(n.y * v.x), v.y]]];
slices = [for (i = [0 : sections])
let (t = i/sections, p = bezier(t), normal = normal(t), n = normal/norm(normal))
[for (v = shape) [p.x+(mod(t) * n.x * v.x),p.y+(mod(t) * n.y * v.x), mod(t) * v.y]]];
extrusion(slices);
}
shape = [[0,0.5],[0.8,0],[-0.5,-1],[-0.8,0]];
ctrl = [[0,0],[0,10],[10,0],[10,10]];
bezier_extrude(ctrl = ctrl, shape = shape);
wave = function (x) 0.75+0.25*cos(3*360*x);
//extrusion([[[0,0,0],[0,1,0],[0,1,1],[0,0,1]],[[1,0,0],[1,1,0],[1,1,1],[1,0,1]],[[2,0,1],[2,1,1],[2,1,2],[2,0,2]]]);
shape = [[0,0.5],[0.8,0],[0.5,-1],[-0.5,-1],[-0.8,0]];
ctrl = [[0,0],[0,10],[10,0],[10,10]];
bezier_extrude(ctrl = ctrl, shape = shape, mod = wave);