188 lines
6.8 KiB
OpenSCAD
188 lines
6.8 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]
|
|
]
|
|
];
|
|
|
|
rotation_matrix = function(a) [
|
|
[cos(a), -sin(a), 0],
|
|
[sin(a), cos(a), 0],
|
|
[ 0, 0, 1]
|
|
];
|
|
|
|
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);
|
|
}
|
|
|
|
// c^2=a^2+b^2 // a == b
|
|
// c^2=2a^2 // /2
|
|
// c^2/2=a^2 // sqrt
|
|
// sqrt(c^2/2)=a
|
|
//chamfer_offset = function (c) sqrt((c*c)/2);
|
|
chamfer_offset = function (c) c/2;
|
|
|
|
rendered_curve_segment_vertices = function (p, v, n, width, height, chamfer)
|
|
let (d=normalize(n-p)) let (up=[0, 0, 1]) let (right=normalize(cross(d, up))) [
|
|
//p, v, n, width, height, chamfer_offset(chamfer), d, up, right,
|
|
v+up*(height/2)+right*(-width/2+chamfer_offset(chamfer)),
|
|
v+up*(height/2)+right*(width/2-chamfer_offset(chamfer)),
|
|
v+up*(height/2-chamfer_offset(chamfer))+right*(width/2),
|
|
v+up*(-height/2+chamfer_offset(chamfer))+right*(width/2),
|
|
v+up*(-height/2)+right*(width/2-chamfer_offset(chamfer)),
|
|
v+up*(-height/2)+right*(-width/2+chamfer_offset(chamfer)),
|
|
v+up*(-height/2+chamfer_offset(chamfer))+right*(-width/2),
|
|
v+up*(height/2-chamfer_offset(chamfer))+right*(-width/2)
|
|
];
|
|
|
|
module render_curve(curve_vertices, width, height, chamfer, debug=false) {
|
|
/*
|
|
* vertex generation order:
|
|
* - we generate vertices segment by segment
|
|
* - one segement for every curve vertex provided
|
|
* - each segment has eight vertices that form the outer ring
|
|
* - the corners are chamfered so the vertices are pairs at the corners
|
|
* - we start with the top segment and move clockwise
|
|
* - top-down axis is determined globally by z-axis unless direction at start vertex is z
|
|
* - if direction at start vertes is z, top-down is y-adis instead
|
|
* - the left vertex of the top edge is 0, the other is 1
|
|
* - left-right axis is determined by normal between travel direction and up-down axis
|
|
* - the last vertex is part of the same corner as 0 but part of the left edge
|
|
* - segment edge n has vertices `n` and `(n+1)%8`
|
|
* - segment joining edge n has vertices local index `n` of both segments
|
|
* - global `i` index of local index `n` for segment `m` is `i=8*m+n`
|
|
* - so we have `count=length(curve_vertices)` segments and `count*8` vertices
|
|
* - we are not generating a thorus so the faces for our polyhedron are:
|
|
* - two eight sided end cap faces
|
|
* - `(count-1)*8` four sided mantle faces
|
|
* - direction at vertex is the vector from the previonus to the next vertex
|
|
* - for the first an last vertex the only neighbor is mirrored around the vertex
|
|
* - the start end cap is face `0`
|
|
* - the face mantle face n has vertices `[n, +(n+1)%8, n+8, n+8+(n+1)%8]`
|
|
* - the stop end cap is face `count*8`
|
|
*/
|
|
|
|
start = let(v=curve_vertices[0], n=curve_vertices[1]) let(p=v+(v-n))
|
|
rendered_curve_segment_vertices(p, v, n, width, height, chamfer);
|
|
echo("len(curve_vertices): ", len(curve_vertices));
|
|
middle = [ for (i=[1:len(curve_vertices)-2]) let(
|
|
p = curve_vertices[i-1],
|
|
v = curve_vertices[i],
|
|
n = curve_vertices[i+1]
|
|
) each rendered_curve_segment_vertices(p, v, n, width, height, chamfer)
|
|
];
|
|
echo("len(middle): ", len(middle));
|
|
stop = let(i=len(curve_vertices))
|
|
let(v=curve_vertices[i-1], p=curve_vertices[i-2]) let(n=v+(v-p))
|
|
rendered_curve_segment_vertices(p, v, n, width, height, chamfer);
|
|
|
|
if (debug) {
|
|
echo(start);
|
|
color("#ff00ff") for (v = start) translate(v+[0, 0, height]) sphere(d=.25, $fn=16);
|
|
echo(middle);
|
|
color("white") for (v = middle) translate(v+[0, 0, height]) sphere(d=.25, $fn=16);
|
|
echo(stop);
|
|
color("cyan") for (v = stop) translate(v+[0, 0, height]) sphere(d=.25, $fn=16);
|
|
}
|
|
|
|
render_vertices = concat(start, middle, stop);
|
|
echo("len(render_vertices)", len(render_vertices));
|
|
echo(render_vertices);
|
|
|
|
start_cap = [ for (i=[0:1:7]) i];
|
|
echo(start_cap);
|
|
mantle = let (M=len(curve_vertices)-1) [ for (m=[0:M-1],n=[0:7]) let (i=m*8+n)
|
|
[i, i+8, n==7 ? i+1 : i+8+1, n==7 ? i-7 : i+1]
|
|
];
|
|
echo(mantle);
|
|
end_cap = let (m=len(curve_vertices)-1) [ for (i=[m*8+7:-1:m*8]) i ];
|
|
echo(end_cap);
|
|
|
|
faces = concat([start_cap], mantle, [end_cap]);
|
|
|
|
translate([0, 0, debug ? height : 0]) polyhedron(points=render_vertices, faces=faces);
|
|
}
|
|
|
|
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);
|
|
}
|