use glam::DVec3;
use indicatrix::{
geometry::GpuFacetPlane,
optics::{LightingPreset, materials::GemMaterial},
};
use indicatrix_cut_core::{ObjectiveFidelity, evaluate_objective_under};
pub const NOT_AVAILABLE: &str = "n/a";
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MetricColumn {
pub windowing_pct: f32,
pub brilliance_pct: f32,
pub extinction_pct: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct RetargetMetrics {
pub current_in_current: Option<MetricColumn>,
pub current_in_target: Option<MetricColumn>,
pub retargeted_in_target: Option<MetricColumn>,
}
#[must_use]
pub fn measure_column(
planes: &[(DVec3, f64)],
gem: &GemMaterial,
lighting: LightingPreset,
) -> MetricColumn {
let gpu: Vec<GpuFacetPlane> = planes
.iter()
.map(|&(normal, offset)| GpuFacetPlane::new(normal.as_vec3(), -offset as f32))
.collect();
let components = evaluate_objective_under(&gpu, gem, ObjectiveFidelity::Fast, lighting);
MetricColumn {
windowing_pct: components.windowing_pct,
brilliance_pct: components.tilt_brilliance_pct,
extinction_pct: components.extinction_pct,
}
}
fn cell(column: Option<MetricColumn>, pick: fn(MetricColumn) -> f32) -> String {
column.map_or_else(
|| NOT_AVAILABLE.to_string(),
|column| format!("{:.1} %", pick(column)),
)
}
impl RetargetMetrics {
#[must_use]
pub fn cells(&self) -> Vec<String> {
let columns = [
self.current_in_current,
self.current_in_target,
self.retargeted_in_target,
];
let rows: [fn(MetricColumn) -> f32; 3] = [
|column| column.windowing_pct,
|column| column.brilliance_pct,
|column| column.extinction_pct,
];
rows.iter()
.flat_map(|&pick| columns.iter().map(move |&column| cell(column, pick)))
.collect()
}
#[must_use]
pub const fn any(&self) -> bool {
self.current_in_current.is_some()
|| self.current_in_target.is_some()
|| self.retargeted_in_target.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn column(w: f32, b: f32, e: f32) -> MetricColumn {
MetricColumn {
windowing_pct: w,
brilliance_pct: b,
extinction_pct: e,
}
}
#[test]
fn cells_run_row_by_row_in_column_order() {
let metrics = RetargetMetrics {
current_in_current: Some(column(1.0, 2.0, 3.0)),
current_in_target: None,
retargeted_in_target: Some(column(4.0, 5.0, 6.0)),
};
assert_eq!(
metrics.cells(),
vec![
"1.0 %", "n/a", "4.0 %", "2.0 %", "n/a", "5.0 %", "3.0 %", "n/a", "6.0 %", ]
);
assert!(metrics.any());
assert!(!RetargetMetrics::default().any());
}
#[test]
fn the_standard_brilliant_measures_in_range_in_two_materials() {
use indicatrix_cut_core::{ConstraintTier, Design, PreformSpec, ScheduleMeta};
let design = Design::new(
PreformSpec::block(2.0, 1.0, 4.0),
ScheduleMeta::standard_round_brilliant(),
ConstraintTier::standard_round_brilliant(),
);
let solved = design.solve().expect("every tier is pinned");
let planes = design.planes_from_solved(&solved);
let lighting = LightingPreset::RingLights;
for gem in [GemMaterial::diamond(), GemMaterial::sapphire()] {
let column = measure_column(&planes, &gem, lighting);
for value in [
column.windowing_pct,
column.brilliance_pct,
column.extinction_pct,
] {
assert!((0.0..=100.0).contains(&value), "{}: {value}", gem.name);
}
}
}
}