use super::details::SheetDetails;
use indicatrix::{
geometry::meet_solver::{Block, SolvedTier, classify_blocks},
optics::materials::GemMaterial,
};
use indicatrix_cut_core::{CutSheetRow, CuttingSheet, Design, design::TierRef};
use std::fmt::Write as _;
pub const UNTITLED_HEADING: &str = "Cutting instructions";
const TABLE_CODE: &str = "T";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct BlockCount {
pub tiers: usize,
pub facets: usize,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FacetData {
pub pavilion: BlockCount,
pub girdle: BlockCount,
pub crown: BlockCount,
pub table: BlockCount,
}
impl FacetData {
#[must_use]
pub fn count(rows: &[CutSheetRow], blocks: &[Block]) -> Self {
let mut data = Self::default();
for (row, &block) in rows.iter().zip(blocks) {
let facets = row.indices.len().max(1);
let is_table = block == Block::Crown && row.concave.is_none() && row.code == TABLE_CODE;
let slot = if is_table {
&mut data.table
} else {
match block {
Block::Pavilion => &mut data.pavilion,
Block::Girdle => &mut data.girdle,
Block::Crown => &mut data.crown,
}
};
slot.tiers += 1;
slot.facets += facets;
}
data
}
#[must_use]
pub const fn total_tiers(&self) -> usize {
self.pavilion.tiers + self.girdle.tiers + self.crown.tiers + self.table.tiers
}
#[must_use]
pub const fn total_facets(&self) -> usize {
self.pavilion.facets + self.girdle.facets + self.crown.facets + self.table.facets
}
#[must_use]
pub fn crown_tiers_text(&self) -> String {
with_table(self.crown.tiers, self.table.tiers)
}
#[must_use]
pub fn crown_facets_text(&self) -> String {
with_table(self.crown.facets, self.table.facets)
}
}
fn with_table(count: usize, table: usize) -> String {
if table == 0 {
count.to_string()
} else {
format!("{count}+{table}")
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct SizeData {
pub length_to_width: Option<f64>,
pub height_to_width: Option<f64>,
pub volume_to_width_cubed: Option<f64>,
pub pavilion_to_width: Option<f64>,
pub crown_to_width: Option<f64>,
pub pavilion_to_crown: Option<f64>,
}
impl SizeData {
#[must_use]
pub fn measure(design: &Design, solved: &[SolvedTier]) -> Self {
if design.tiers.is_empty() {
return Self::default();
}
let Some(metrics) = design.measure_from_solved_geom(solved).ok().flatten() else {
return Self::default();
};
let width = metrics.width_axis;
if width <= 1e-9 {
return Self::default();
}
let pavilion = metrics.pavilion_depth.map(|depth| depth / width);
let crown = metrics.crown_height.map(|height| height / width);
Self {
length_to_width: Some(metrics.length_axis / width),
height_to_width: Some(metrics.total_height / width),
volume_to_width_cubed: Some(metrics.volume / (width * width * width)),
pavilion_to_width: pavilion,
crown_to_width: crown,
pavilion_to_crown: Option::zip(pavilion, crown)
.filter(|&(_, crown)| crown > 1e-9)
.map(|(pavilion, crown)| pavilion / crown),
}
}
#[must_use]
pub fn cells(&self) -> [(&'static str, String); 6] {
let text =
|value: Option<f64>| value.map_or_else(|| "-".to_string(), |v| format!("{v:.3}"));
[
("L/W", text(self.length_to_width)),
("H/W", text(self.height_to_width)),
("V/W\u{b3}", text(self.volume_to_width_cubed)),
("P/W", text(self.pavilion_to_width)),
("C/W", text(self.crown_to_width)),
("P/C", text(self.pavilion_to_crown)),
]
}
}
fn trimmed(value: f64) -> String {
let text = format!("{value:.2}");
text.trim_end_matches('0').trim_end_matches('.').to_string()
}
#[must_use]
pub(super) fn material_label(design: &Design, n_d: f64) -> Option<String> {
if let Some(name) = design.material.name.as_deref() {
return Some(name.to_string());
}
crate::material_lookup::material_for_refractive_index(n_d).map(|(name, _)| format!("{name}?"))
}
fn design_data(design: &Design, n_d: f64, details: &SheetDetails) -> Vec<(String, String)> {
let mut rows = Vec::new();
if let Some((low, high)) = details.ri_range {
rows.push(("RI range".to_string(), format!("{low:.2} - {high:.2}")));
} else {
let value = material_label(design, n_d).map_or_else(
|| format!("{n_d:.3}"),
|material| format!("{n_d:.3} ({material})"),
);
rows.push(("RI".to_string(), value));
}
if let Some((small, large)) = details.size_range_mm {
rows.push((
"Size range".to_string(),
format!("{} - {} mm", trimmed(small), trimmed(large)),
));
}
rows.push((
"Symmetry".to_string(),
format!(
"{}-fold{}",
design.meta.symmetry_order,
if design.meta.mirror { ", mirror" } else { "" }
),
));
rows.push((
"Index gear".to_string(),
format!("{} index", design.meta.gear_teeth_abs()),
));
if !details.shape.trim().is_empty() {
rows.push(("Shape".to_string(), details.shape.trim().to_string()));
}
rows
}
#[must_use]
pub fn row_blocks(design: &Design) -> Vec<Block> {
let blocks = classify_blocks(&design.meet_tier_inputs());
design
.cutting_order()
.into_iter()
.map(|tier_ref| match tier_ref {
TierRef::Flat(i) => blocks.get(i).copied().unwrap_or(Block::Girdle),
TierRef::Concave(i) => {
if design.concave_tiers[i].is_crown_side() {
Block::Crown
} else {
Block::Pavilion
}
}
})
.collect()
}
#[derive(Debug, Clone)]
pub struct SheetLayout {
pub details: SheetDetails,
pub facets: FacetData,
pub sizes: SizeData,
pub design_data: Vec<(String, String)>,
pub extras: Vec<(String, String)>,
pub sheet: CuttingSheet,
pub blocks: Vec<Block>,
pub mm_per_unit: Option<f64>,
pub girdle_diameter_mm: Option<f64>,
}
impl SheetLayout {
#[must_use]
pub fn build(
design: &Design,
solved: &[SolvedTier],
custom: &[GemMaterial],
details: &SheetDetails,
) -> Self {
let sheet = design.cutting_sheet_with(solved, custom);
let blocks = row_blocks(design);
let facets = FacetData::count(&sheet.rows, &blocks);
let yield_report = design.yield_report(solved);
let n_d = design.effective_refractive_index_with(custom);
let mut extras = Vec::new();
if let Some(mm) = design.girdle_diameter_mm {
extras.push(("Girdle diameter".to_string(), format!("{mm:.3} mm")));
}
if let Some(carat) = yield_report.carat_weight {
extras.push((
"Carat weight (estimate)".to_string(),
format!(
"{carat:.3} ct -- assumes the authored facets reach the preform's own walls"
),
));
}
Self {
details: details.clone(),
facets,
sizes: SizeData::measure(design, solved),
design_data: design_data(design, n_d, details),
extras,
sheet,
blocks,
mm_per_unit: yield_report.mm_per_unit,
girdle_diameter_mm: design.girdle_diameter_mm,
}
}
#[must_use]
pub fn heading(&self) -> &str {
let title = self.details.title.trim();
if title.is_empty() {
UNTITLED_HEADING
} else {
title
}
}
#[must_use]
pub fn pavilion_rows(&self) -> Vec<(&CutSheetRow, Block)> {
self.section_rows(|block| block != Block::Crown)
}
#[must_use]
pub fn crown_rows(&self) -> Vec<(&CutSheetRow, Block)> {
self.section_rows(|block| block == Block::Crown)
}
fn section_rows(&self, keep: impl Fn(Block) -> bool) -> Vec<(&CutSheetRow, Block)> {
self.sheet
.rows
.iter()
.zip(self.blocks.iter().copied())
.filter(|&(_, block)| keep(block))
.collect()
}
#[must_use]
pub fn has_cheater_column(&self) -> bool {
self.sheet
.rows
.iter()
.any(|row| row.cheater_offset_deg.is_some())
}
#[must_use]
pub fn to_text(&self) -> String {
let mut out = String::new();
let _ = writeln!(out, "{}", self.heading());
let details = &self.details;
if !details.subtitle.trim().is_empty() {
let _ = writeln!(out, "{}", details.subtitle.trim());
}
if !details.author.trim().is_empty() {
let _ = writeln!(out, "by {}", details.author.trim());
}
if !details.date_text.trim().is_empty() {
let _ = writeln!(out, "{}", details.date_text.trim());
}
out.push_str("\nFacet Data\n");
for (name, tiers, facets) in [
(
"Pavilion",
self.facets.pavilion.tiers.to_string(),
self.facets.pavilion.facets.to_string(),
),
(
"Girdle",
self.facets.girdle.tiers.to_string(),
self.facets.girdle.facets.to_string(),
),
(
"Crown",
self.facets.crown_tiers_text(),
self.facets.crown_facets_text(),
),
(
"Total",
self.facets.total_tiers().to_string(),
self.facets.total_facets().to_string(),
),
] {
let _ = writeln!(out, " {name:<9} tiers {tiers:<5} facets {facets}");
}
out.push_str("\nSize Data\n ");
let cells: Vec<String> = self
.sizes
.cells()
.into_iter()
.map(|(label, value)| format!("{label} {value}"))
.collect();
out.push_str(&cells.join(" "));
out.push('\n');
out.push_str("\nDesign Data\n");
for (label, value) in self.design_data.iter().chain(&self.extras) {
let _ = writeln!(out, " {label}: {value}");
}
if !details.comments.is_empty() {
out.push_str("\nComments\n");
for comment in &details.comments {
let _ = writeln!(out, " {comment}");
}
}
for (heading, rows) in [
("Pavilion", self.pavilion_rows()),
("Crown", self.crown_rows()),
] {
if rows.is_empty() {
continue;
}
let _ = writeln!(out, "\n{heading}");
for (row, _) in rows {
row.write_text(&mut out);
}
}
out
}
}