use super::{
request::{PlanJob, panel_kind_from_index},
types::CameraPose,
};
use crate::diagram2d::PanelKind;
use indicatrix::{geometry::meet_solver::SolvedTier, optics::materials::GemMaterial};
use indicatrix_cut_core::Design;
use std::{collections::BTreeSet, sync::Arc};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RasterLimits {
pub max_device_pixel_ratio: f32,
pub min_edge: u32,
pub max_edge: u32,
}
#[must_use]
pub fn raster_size_for_view(
logical_width: f32,
logical_height: f32,
scale_factor: f32,
limits: RasterLimits,
) -> (u32, u32) {
let ratio = scale_factor.clamp(1.0, limits.max_device_pixel_ratio.max(1.0));
let width = logical_width.max(0.0) * ratio;
let height = logical_height.max(0.0) * ratio;
let longest = width.max(height);
let max_edge = limits.max_edge.max(limits.min_edge).max(1);
let shrink = if longest > max_edge as f32 {
max_edge as f32 / longest
} else {
1.0
};
let clamp = |edge: f32| ((edge * shrink).round() as u32).clamp(limits.min_edge, max_edge);
(clamp(width), clamp(height))
}
#[must_use]
pub fn with_min_aspect(size: (u32, u32), min_aspect: f32, min_edge: u32) -> (u32, u32) {
let (width, height) = size;
if min_aspect <= 0.0 || (width as f32) >= (height as f32) * min_aspect {
return size;
}
let reduced = ((width as f32 / min_aspect).round() as u32).max(min_edge);
(width, reduced.min(height))
}
#[must_use]
pub fn diagram_raster_size(
logical_width: f32,
logical_height: f32,
scale_factor: f32,
limits: RasterLimits,
min_width: u32,
) -> (u32, u32) {
let (width, height) = raster_size_for_view(logical_width, logical_height, scale_factor, limits);
if width >= min_width {
return (width, height);
}
let grow = min_width as f32 / width.max(1) as f32;
let max_edge = limits.max_edge.max(limits.min_edge).max(1);
let longest = width.max(height) as f32 * grow;
let shrink = if longest > max_edge as f32 {
max_edge as f32 / longest
} else {
1.0
};
let fit =
|edge: u32| ((edge as f32 * grow * shrink).round() as u32).clamp(limits.min_edge, max_edge);
(fit(width), fit(height))
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ContainFit {
pub offset_x: f32,
pub offset_y: f32,
pub scale: f32,
}
impl ContainFit {
#[must_use]
pub fn to_image(self, x: f32, y: f32) -> (f32, f32) {
(
(x - self.offset_x) / self.scale,
(y - self.offset_y) / self.scale,
)
}
#[must_use]
pub const fn to_view(self, px: f32, py: f32) -> (f32, f32) {
(
px.mul_add(self.scale, self.offset_x),
py.mul_add(self.scale, self.offset_y),
)
}
#[must_use]
pub fn length_to_image(self, logical: f32) -> f32 {
logical / self.scale
}
}
#[must_use]
pub fn contain_fit(
view_width: f32,
view_height: f32,
image_width: u32,
image_height: u32,
) -> Option<ContainFit> {
if view_width <= 0.0 || view_height <= 0.0 || image_width == 0 || image_height == 0 {
return None;
}
let (image_w, image_h) = (image_width as f32, image_height as f32);
let scale = (view_width / image_w).min(view_height / image_h);
Some(ContainFit {
offset_x: image_w.mul_add(-scale, view_width) * 0.5,
offset_y: image_h.mul_add(-scale, view_height) * 0.5,
scale,
})
}
#[must_use]
pub fn contain_pixel(
x: f32,
y: f32,
view_width: f32,
view_height: f32,
image_width: u32,
image_height: u32,
) -> Option<(u32, u32)> {
let fit = contain_fit(view_width, view_height, image_width, image_height)?;
let (image_w, image_h) = (image_width as f32, image_height as f32);
let (px, py) = fit.to_image(x, y);
let (px, py) = (px.floor(), py.floor());
if !(px >= 0.0 && py >= 0.0 && px < image_w && py < image_h) {
return None;
}
Some((px as u32, py as u32))
}
#[must_use]
pub const fn panel_index(panel: PanelKind) -> i32 {
match panel {
PanelKind::Crown => 0,
PanelKind::Pavilion => 1,
PanelKind::Profile => 2,
}
}
#[must_use]
pub const fn toggle_enlarged_panel(current: i32, clicked: Option<PanelKind>) -> i32 {
if panel_kind_from_index(current).is_some() {
return -1;
}
match clicked {
Some(panel) => panel_index(panel),
None => -1,
}
}
#[must_use]
pub fn step_selection(current: Option<usize>, delta: i32, tier_count: usize) -> Option<usize> {
if tier_count == 0 {
return current;
}
let last = tier_count - 1;
let Some(current) = current else {
return Some(if delta >= 0 { 0 } else { last });
};
let moved = i64::try_from(current.min(last)).unwrap_or(i64::MAX) + i64::from(delta);
Some(usize::try_from(moved.max(0)).unwrap_or(0).min(last))
}
#[must_use]
pub fn facets_of_tiers(facet_tier: &[Option<usize>], tiers: &BTreeSet<usize>) -> Vec<u32> {
if tiers.is_empty() {
return Vec::new();
}
facet_tier
.iter()
.enumerate()
.filter(|(_, tier)| tier.is_some_and(|tier| tiers.contains(&tier)))
.map(|(id, _)| id as u32)
.collect()
}
#[must_use]
pub fn dirty_tiers(previous: &Design, current: &Design) -> Option<BTreeSet<usize>> {
if previous.tiers.len() != current.tiers.len()
|| previous.meta.gear_teeth != current.meta.gear_teeth
|| previous.meta.gear_reference_angle != current.meta.gear_reference_angle
|| previous.meta.symmetry_order != current.meta.symmetry_order
|| previous.meta.mirror != current.meta.mirror
{
return None;
}
Some(
previous
.tiers
.iter()
.zip(¤t.tiers)
.enumerate()
.filter(|(_, (before, after))| before != after)
.map(|(index, _)| index)
.collect(),
)
}
#[must_use]
pub fn plans_without_solver(design: &Design) -> bool {
design.tiers.iter().all(|tier| {
matches!(
tier.constraint,
indicatrix::geometry::meet_solver::MeetConstraint::ScaleReference(_)
)
})
}
#[derive(Debug, Clone)]
pub enum ReplanBasis {
Chain {
last_solved: Vec<SolvedTier>,
dirty: BTreeSet<usize>,
},
FullSolve,
}
#[must_use]
pub fn replan_basis(
design: &Design,
current_solve: Option<&[SolvedTier]>,
cached: Option<(&Design, &[SolvedTier])>,
) -> ReplanBasis {
if let Some(solved) = current_solve.filter(|solved| solved.len() == design.tiers.len()) {
return ReplanBasis::Chain {
last_solved: solved.to_vec(),
dirty: BTreeSet::new(),
};
}
if let Some((previous, masts)) = cached
&& masts.len() == design.tiers.len()
&& let Some(dirty) = dirty_tiers(previous, design)
{
return ReplanBasis::Chain {
last_solved: masts.to_vec(),
dirty,
};
}
ReplanBasis::FullSolve
}
pub struct ReplanInputs<'a> {
pub design: Arc<Design>,
pub generation: u64,
pub basis: ReplanBasis,
pub camera: CameraPose,
pub size: (u32, u32),
pub selected_tier: Option<usize>,
pub custom_materials: &'a [GemMaterial],
pub view_mode: u8,
pub show_preform: bool,
pub enlarged_panel: i32,
pub tier_cutoff: i32,
}
#[must_use]
pub fn plan_job(inputs: ReplanInputs<'_>) -> PlanJob {
let (last_solved, dirty) = match inputs.basis {
ReplanBasis::Chain { last_solved, dirty } => (Some(last_solved), dirty),
ReplanBasis::FullSolve => (None, BTreeSet::new()),
};
let n_d = inputs
.design
.effective_refractive_index_with(inputs.custom_materials);
PlanJob {
design: inputs.design,
dirty,
last_solved,
camera: inputs.camera,
size: inputs.size,
selected_tier: inputs.selected_tier,
n_d,
view_mode: inputs.view_mode,
generation: inputs.generation,
show_preform: inputs.show_preform,
enlarged_panel: inputs.enlarged_panel,
tier_cutoff: usize::try_from(inputs.tier_cutoff).ok(),
cut_steps: None,
}
}