fix bevel miscutting due to y≠f(x)

the new algorithm is slow for sure but it works fine
This commit is contained in:
numzero 2026-10-08 23:36:18 +03:00
parent 160052a23a
commit 1c0dfa51c9

View File

@ -157,27 +157,16 @@ fn main() {
let rack_y_at_x = |x, profile| rack_shape(x - rack_offset * PI, profile);
let inv_cutter_y_at_x = |x| rack_y_at_x(x, &cutter_profile);
let bevel_cutter_y_at_x = |x| {
if x < crown_cutter.first().unwrap().x || x > crown_cutter.last().unwrap().x {
return -cutter_profile.dedendum;
}
let mut i1 = 0;
let mut i2 = crown_cutter.len() - 1;
while i2 - i1 > 1 {
let i3 = (i1 + i2) / 2;
if x < crown_cutter[i3].x {
i2 = i3;
} else {
i1 = i3;
}
}
let a = crown_cutter[i1];
let b = crown_cutter[i2];
assert!(a.x <= x);
assert!(x <= b.x);
let delta = (x - a.x) / (b.x - a.x);
a.y + delta * (b.y - a.y)
};
let bevel_cutter: Vec<_> = crown_cutter
.iter()
.map(|pt| {
let rel = vec2(r_pitch - pt.y, pt.x);
let r = rel.length();
let phi = rel.to_angle();
(r, phi)
})
.collect();
let kr_to_xy = |k, r: f32| {
let u = k as f32 / n_points_per_tooth as f32;
@ -198,13 +187,22 @@ fn main() {
}
for (k, r) in bevel_shape.iter_mut().enumerate() {
loop {
let xy = kr_to_xy(k, *r);
let cut_y = bevel_cutter_y_at_x(xy.x);
if cut_y < xy.y {
break;
let u = k as f32 / n_points_per_tooth as f32;
let angle =
2.0 * PI * (((gear_offset + u) / n_teeth as f32 + 0.5).fract_gl() - 0.5);
for (&(mut r1, mut phi1), &(mut r2, mut phi2)) in
bevel_cutter.iter().zip(&bevel_cutter[1..])
{
if phi2 < phi1 {
(r1, phi1, r2, phi2) = (r2, phi2, r1, phi1);
}
*r -= 0.01;
if angle < phi1 || angle > phi2 {
continue;
}
if *r < r1 && *r < r2 {
continue;
}
*r = f32::min(r1, r2);
}
}