scad/primitives.scad
2025-06-15 14:58:41 +02:00

113 lines
3.5 KiB
OpenSCAD

include <globals.scad>
id = function (x) x;
lerp = function (v, old_min, old_max, new_min, new_max) (v-old_min)/(old_max-old_min)*(new_max-new_min)+new_min;
scalc = function (f, v, min, max) lerp(f(lerp(v, min, max, 0, 1)), 0, 1, min, max);
normalize = function (vector) vector / norm(vector);
mat_T = function (mat) [
for (x_y=[0:len(mat[0])-1]) [
for (y_x=[0:len(mat)-1]) mat[y_x][x_y]
]
];
bezier_matrix = [[1, -3, 3, -1],
[0, 3, -6, 3],
[0, 0, 3, -3],
[0, 0, 0, 1]];
spline = function (control_points, spline_matrix, t) mat_T(control_points) * spline_matrix * [1, t, t*t, t*t*t];
bezier_spline = function (control_points, t) spline(control_points, bezier_matrix, t);
bezier_curve_vertices = function (control_points, $fn=$fn) [
for (t=[0:1/($fn-1):1]) bezier_spline(control_points, t)
];
module bezier_curve_debug(control_points, $fn=$fn) {
#color("red") for (c = control_points) translate(c) sphere(d=4, $fn=32);
color("yellow") for (v = bezier_curve_vertices(control_points, $fn)) translate(v) sphere(d=.5, $fn=16);
color("green") translate(bezier_spline(control_points, 0.5)) sphere(d=1, $fn=24);
}
rendered_curve_segment_vertices = function (p, v, n, width, height, chamfer) [];
module render_curve(curve_vertices, width, height, chamfer) {
start = let(v=curve_vertices[0], n=curve_vertices[1]) let(p=v-n)
rendered_curve_segment_vertices(p, v, n, width, height, chamfer);
middle = [ for (i=[1:len(curve_vertices)-2]) let(
p = curve_vertives[i-1],
v = curve_vertices[i],
n = curve_vertives[i+1]
) rendered_curve_segment_vertices(p, v, n, width, height, chamfer)
];
end = let(i=len(curve_vertices)) let(v=curve_vertices[i], p=curve_vertices[i-1]) let(n=v-p)
rendered_curve_segment_vertices(p, v, n, width, height, chamfer);
// TODO
}
arc_vertex = function (a, r, t, translate=[0,0,0], remap=id) remap([
translate.x+cos(a*t)*r,
translate.y+sin(a*t)*r,
translate.z
]);
arc_vertices = function (a, r, t, x=0, y=0, z=0, remap=id, arc_vertex=arc_vertex) [
for (i=[0:1/(t-1):1]) arc_vertex(a, r, i, translate=[x,y,z], remap=remap)
];
module arc(part, radius, thickness, height, $fn=$fn, outer_remap=id, inner_remap=id, arc_vertices=arc_vertices) {
a = 360 * part;
r = radius+thickness/2;
w = thickness;
h = height;
t = $fn;
vertices_lower_outer = arc_vertices(a, r , t, z=-h/2, remap=outer_remap);
vertices_lower_inner = arc_vertices(a, r-w, t, z=-h/2, remap=inner_remap);
vertices_upper_outer = arc_vertices(a, r , t, z= h/2, remap=outer_remap);
vertices_upper_inner = arc_vertices(a, r-w, t, z= h/2, remap=inner_remap);
vertices = concat(
vertices_lower_outer,
vertices_lower_inner,
vertices_upper_outer,
vertices_upper_inner
);
faces_end1 = [ 0, t, 3*t, 2*t];
faces_end2 = [ 2*t-1, t-1, 3*t-1, 4*t-1];
faces_lower = concat(
[ for (i=[t-1:-1:0]) i ],
[ for (i=[t:1:2*t-1]) i ]
);
faces_upper = concat(
[ for (i=[2*t:1:3*t-1]) i ],
[ for (i=[4*t-1:-1:3*t]) i ]
);
faces_outer = [ for (i=[0:1:t-2]) [i, 2*t+i, 2*t+i+1, i+1] ];
faces_inner = [ for (i=[0:1:t-2]) [t+i, t+i+1, 3*t+i+1, 3*t+i] ];
faces = concat(
[faces_end1],
[faces_end2],
[faces_lower],
[faces_upper],
faces_outer,
faces_inner
);
rotate(a/2) polyhedron(vertices, faces);
}
module partial_ring(part, radius, thickness, height) {
rotate(180-180*part, [0, 0, 1])
rotate_extrude(angle=360*part)
translate([radius, 0])
square([thickness, height], center=true);
}