use super::{CuttingStep, progress::Progress};
use std::fmt::Write as _;
pub const BOX: f64 = 200.0;
const CENTRE: f64 = BOX / 2.0;
const RING: f64 = 76.0;
const MINOR_INNER: f64 = 70.0;
const MAJOR_INNER: f64 = 63.0;
const SCALE_RADIUS: f64 = 50.0;
const LABEL_RADIUS: f64 = 91.0;
const MAX_LABELLED_MARKS: usize = 16;
const MAX_MINOR_TICKS: u32 = 192;
#[derive(Debug, Clone, PartialEq)]
pub struct WheelMark {
pub x: f32,
pub y: f32,
pub label_x: f32,
pub label_y: f32,
pub label: String,
pub ticked: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct WheelScale {
pub x: f32,
pub y: f32,
pub label: String,
}
#[derive(Debug, Clone, PartialEq)]
pub struct IndexWheel {
pub minor_ticks: String,
pub major_ticks: String,
pub spokes: String,
pub marks: Vec<WheelMark>,
pub scale: Vec<WheelScale>,
}
impl IndexWheel {
#[must_use]
pub fn new(gear_teeth: u32, step: &CuttingStep, progress: &Progress) -> Self {
let mut wheel = Self {
minor_ticks: String::new(),
major_ticks: String::new(),
spokes: String::new(),
marks: Vec::new(),
scale: Vec::new(),
};
if gear_teeth == 0 {
return wheel;
}
let major = major_step(gear_teeth);
let minor = gear_teeth.div_ceil(MAX_MINOR_TICKS).max(1);
for tooth in (0..gear_teeth).step_by(minor as usize) {
if !tooth.is_multiple_of(major) {
push_segment(&mut wheel.minor_ticks, tooth, gear_teeth, MINOR_INNER);
}
}
for tooth in (0..gear_teeth).step_by(major as usize) {
push_segment(&mut wheel.major_ticks, tooth, gear_teeth, MAJOR_INNER);
let (x, y) = place(SCALE_RADIUS, f64::from(tooth), gear_teeth);
wheel.scale.push(WheelScale {
x: fraction(x),
y: fraction(y),
label: tooth.to_string(),
});
}
let labelled = step.indices.len() <= MAX_LABELLED_MARKS;
for (chip, index) in step.indices.iter().enumerate() {
let (x, y) = place(RING, index.value, gear_teeth);
let (label_x, label_y) = place(LABEL_RADIUS, index.value, gear_teeth);
let _ = write!(wheel.spokes, "M {CENTRE:.2} {CENTRE:.2} L {x:.2} {y:.2} ");
wheel.marks.push(WheelMark {
x: fraction(x),
y: fraction(y),
label_x: fraction(label_x),
label_y: fraction(label_y),
label: if labelled {
index.text.clone()
} else {
String::new()
},
ticked: progress.chip_ticked(step, chip),
});
}
wheel
}
}
#[must_use]
pub fn major_step(gear_teeth: u32) -> u32 {
[16, 12, 10, 8, 6, 4, 2]
.into_iter()
.find(|arcs| gear_teeth.is_multiple_of(*arcs) && gear_teeth / arcs >= 1)
.map_or_else(|| (gear_teeth / 12).max(1), |arcs| gear_teeth / arcs)
}
fn place(radius: f64, position: f64, gear_teeth: u32) -> (f64, f64) {
let turn = position.rem_euclid(f64::from(gear_teeth)) / f64::from(gear_teeth);
let angle = turn * std::f64::consts::TAU;
(
radius.mul_add(angle.sin(), CENTRE),
(-radius).mul_add(angle.cos(), CENTRE),
)
}
fn push_segment(commands: &mut String, tooth: u32, gear_teeth: u32, inner: f64) {
let (x0, y0) = place(inner, f64::from(tooth), gear_teeth);
let (x1, y1) = place(RING, f64::from(tooth), gear_teeth);
let _ = write!(commands, "M {x0:.2} {y0:.2} L {x1:.2} {y1:.2} ");
}
fn fraction(coordinate: f64) -> f32 {
(coordinate / BOX) as f32
}