[WIP] add polyhedron-based catears

This commit is contained in:
djerun 2025-06-27 09:00:23 +02:00
commit 58fe834489
3 changed files with 187 additions and 96 deletions

View file

@ -33,23 +33,91 @@ module bezier_curve_debug(control_points, $fn=$fn) {
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);
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)
];
rendered_curve_segment_vertices = function (p, v, n, width, height, chamfer) [];
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`
*/
module render_curve(curve_vertices, width, height, chamfer) {
start = let(v=curve_vertices[0], n=curve_vertices[1]) let(p=v-n)
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_vertives[i-1],
p = curve_vertices[i-1],
v = curve_vertices[i],
n = curve_vertives[i+1]
) rendered_curve_segment_vertices(p, v, n, width, height, chamfer)
n = curve_vertices[i+1]
) each 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);
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);
// TODO
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([