use super::{MAX_SWEEP_ANGLE_DEG, SweepError, SweepPlan, SweepRange};
use crate::{loading::eval_number, solve_policy::SolveCostEstimate};
use indicatrix_cut_core::design::snap_noise;
pub const DEFAULT_SWEEP_HALF_RANGE_DEG: f64 = 5.0;
pub const DEFAULT_SWEEP_STEP_DEG: f64 = 0.5;
const DEFAULT_RANGE_FLOOR_DEG: f64 = 0.5;
const ROW_BASE_SECONDS: f64 = 0.03;
const SOLVE_SECONDS_PER_PLANE: f64 = 0.0004;
const TARGET_SOLVE_FACTOR: f64 = 4.0;
const TILT_ROW_SECONDS: f64 = 1.5;
const PARALLEL_EFFICIENCY: f64 = 0.8;
#[must_use]
pub fn default_range(current_deg: f64) -> (f64, f64) {
let magnitude = current_deg.abs();
let top = MAX_SWEEP_ANGLE_DEG - 0.4;
let low = snap_noise((magnitude - DEFAULT_SWEEP_HALF_RANGE_DEG).max(DEFAULT_RANGE_FLOOR_DEG));
let high = snap_noise((magnitude + DEFAULT_SWEEP_HALF_RANGE_DEG).min(top));
(low, high)
}
#[must_use]
pub fn format_angle_input(angle_deg: f64) -> String {
snap_noise(angle_deg.abs()).to_string()
}
pub fn parse_sweep_range(from: &str, to: &str, step: &str) -> Result<SweepRange, SweepError> {
let read = |label: &str, text: &str| {
eval_number(text, None).map_err(|error| SweepError::BadNumber(error.message(label, text)))
};
Ok(SweepRange {
from_deg: read("From", from)?,
to_deg: read("To", to)?,
step_deg: read("Step", step)?,
})
}
#[must_use]
pub fn estimate_seconds(
rows: usize,
tilt_average: bool,
workers: usize,
cost: SolveCostEstimate,
) -> f64 {
let mut solve = SOLVE_SECONDS_PER_PLANE * cost.planes as f64;
if cost.has_tier_targets {
solve *= TARGET_SOLVE_FACTOR;
}
let tilt = if tilt_average { TILT_ROW_SECONDS } else { 0.0 };
let per_row = ROW_BASE_SECONDS + solve + tilt;
let speedup = (workers.max(1) as f64 * PARALLEL_EFFICIENCY).max(1.0);
rows as f64 * per_row / speedup
}
#[must_use]
pub fn format_duration_estimate(seconds: f64) -> String {
if seconds < 1.0 {
return "under a second".to_owned();
}
if seconds < 90.0 {
let rounded = if seconds < 10.0 {
seconds.round()
} else {
(seconds / 5.0).round() * 5.0
};
let count = rounded as u64;
return format!("about {count} second{}", if count == 1 { "" } else { "s" });
}
let minutes = (seconds / 60.0).round().max(2.0) as u64;
format!("about {minutes} minutes")
}
#[must_use]
pub fn plan_summary(plan: &SweepPlan, duration: &str) -> String {
let (Some(first), Some(last)) = (plan.angles.first(), plan.angles.last()) else {
return String::new();
};
format!(
"{} angles from {:.2} to {:.2} degrees. This takes {duration}.",
plan.angles.len(),
first.abs(),
last.abs()
)
}