use indicatrix::{
geometry::meet_solver::{Block, MeetConstraint, classify_blocks},
optics::materials::GemMaterial,
};
use indicatrix_cut_core::{
AngleChange, Design, DesignSolveError, Edit, OptimizeConfig, ResolvedMaterial, Risk,
SearchHooks, critical_angle_deg, optics_hints::is_horizontal_angle_deg, optimize_design,
};
pub mod anchors;
pub mod check;
pub mod metrics;
pub mod plan;
mod refit;
pub mod search;
#[cfg(test)]
mod tests;
pub mod validity;
pub mod view;
pub use anchors::{AnchorChange, apply_with_anchors};
pub use check::GirdleAllowance;
pub use plan::{PlanRow, RetargetPlan, build_plan};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CrownShift {
pub fraction: f64,
pub scale_by_ratio: bool,
pub follow_pavilion: bool,
}
impl CrownShift {
#[must_use]
pub const fn fixed() -> Self {
Self {
fraction: 0.0,
scale_by_ratio: false,
follow_pavilion: false,
}
}
}
impl Default for CrownShift {
fn default() -> Self {
Self {
fraction: 0.0,
scale_by_ratio: false,
follow_pavilion: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RetargetMode {
Shift,
Optimize(OptimizeConfig),
}
#[derive(Debug, Clone, PartialEq)]
pub struct RetargetRow {
pub tier_index: usize,
pub block: Block,
pub name: String,
pub old_angle: f64,
pub new_angle: f64,
pub margin_deg: f64,
pub risk: Risk,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RetargetProposal {
pub rows: Vec<RetargetRow>,
pub target: ResolvedMaterial,
pub notes: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RetargetError {
Solve(DesignSolveError),
AnchoredTiers(Vec<(usize, String)>),
}
impl std::fmt::Display for RetargetError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Solve(err) => write!(f, "design does not solve: {err}"),
Self::AnchoredTiers(tiers) => {
write!(f, "adopt these tiers' meet constraint before optimizing: ")?;
for (i, (index, name)) in tiers.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "#{index} \"{name}\"")?;
}
Ok(())
}
}
}
}
impl std::error::Error for RetargetError {}
const fn is_scale_reference(constraint: &MeetConstraint) -> bool {
matches!(constraint, MeetConstraint::ScaleReference(_))
}
fn shifted_angle(
design: &Design,
index: usize,
block: Block,
n_from: f64,
n_to: f64,
crown: CrownShift,
stretch: f64,
) -> f64 {
plan::shift_angle(
design.tiers[index].angle_deg,
block,
n_from,
n_to,
crown,
stretch,
)
.angle_deg
}
#[must_use]
pub fn retarget_scope(design: &Design) -> (Vec<usize>, Vec<Block>) {
let blocks = classify_blocks(&design.meet_tier_inputs());
let scope = (0..design.tiers.len())
.filter(|&i| {
blocks[i] != Block::Girdle && !is_horizontal_angle_deg(design.tiers[i].angle_deg)
})
.collect();
(scope, blocks)
}
#[must_use]
pub fn anchored_tiers_in(design: &Design, scope: &[usize]) -> Vec<(usize, String)> {
scope
.iter()
.filter(|&&i| is_scale_reference(&design.tiers[i].constraint))
.map(|&i| (i, design.tiers[i].name.clone()))
.collect()
}
#[must_use]
pub fn seed_shift_design(
design: &Design,
scope: &[usize],
blocks: &[Block],
n_from: f64,
n_to: f64,
crown: CrownShift,
) -> Design {
let mut seeded = design.clone();
let stretch = plan::pavilion_stretch(design, n_from, n_to);
for &i in scope {
seeded.tiers[i].angle_deg =
shifted_angle(design, i, blocks[i], n_from, n_to, crown, stretch);
}
seeded
}
#[must_use]
pub fn rows_from_outcome(
original: &Design,
seeded: &Design,
scope: &[usize],
blocks: &[Block],
n_to: f64,
changes: &[AngleChange],
) -> Vec<RetargetRow> {
let mut final_angles: Vec<f64> = scope.iter().map(|&i| seeded.tiers[i].angle_deg).collect();
for change in changes {
if let Some(slot) = scope.iter().position(|&i| i == change.index) {
final_angles[slot] = change.to_deg;
}
}
let entries: Vec<(usize, Block, f64)> = scope
.iter()
.zip(&final_angles)
.map(|(&index, &angle)| (index, blocks[index], angle))
.collect();
plan::legacy_rows(original, &entries, n_to)
}
#[must_use]
pub fn build_notes(
mode: RetargetMode,
n_from: f64,
n_to: f64,
crown: CrownShift,
stretch: f64,
) -> Vec<String> {
let delta = critical_angle_deg(n_to) - critical_angle_deg(n_from);
let mut notes = vec![format!(
"Retargeting from n_D {n_from:.4} to n_D {n_to:.4}: critical angle moves by {delta:+.3} deg."
)];
if crown.follow_pavilion && !crown.scale_by_ratio && stretch.to_bits() != 1.0_f64.to_bits() {
notes.push(format!(
"Crown tiers follow the pavilion's stretch: every crown angle's tangent is scaled by {stretch:.3}, so the stone keeps its silhouette and its table size."
));
}
if matches!(mode, RetargetMode::Optimize(_)) {
notes.push(
"Optimize mode seeds from the critical-angle shift, then searches free tiers only; anchored tiers keep their seeded angle."
.to_string(),
);
}
notes
}
#[must_use = "a proposal must be shown to the user before it is applied"]
pub fn build_proposal(
design: &Design,
target: &ResolvedMaterial,
crown: CrownShift,
mode: RetargetMode,
custom_materials: &[GemMaterial],
) -> Result<RetargetProposal, RetargetError> {
build_proposal_impl(
design,
design.effective_refractive_index_with(custom_materials),
target,
crown,
mode,
)
}
fn build_proposal_impl(
design: &Design,
n_from: f64,
target: &ResolvedMaterial,
crown: CrownShift,
mode: RetargetMode,
) -> Result<RetargetProposal, RetargetError> {
match mode {
RetargetMode::Shift => Ok(plan::build_plan_from(design, n_from, target, crown).proposal()),
RetargetMode::Optimize(config) => {
let (scope, blocks) = retarget_scope(design);
let final_angles =
build_optimized_angles(design, n_from, target, &scope, crown, &config)?;
let entries: Vec<(usize, Block, f64)> = scope
.iter()
.zip(&final_angles)
.map(|(&index, &angle)| (index, blocks[index], angle))
.collect();
Ok(RetargetProposal {
rows: plan::legacy_rows(design, &entries, target.n_d),
target: target.clone(),
notes: build_notes(
mode,
n_from,
target.n_d,
crown,
plan::pavilion_stretch(design, n_from, target.n_d),
),
})
}
}
}
fn build_optimized_angles(
design: &Design,
n_from: f64,
target: &ResolvedMaterial,
scope: &[usize],
crown: CrownShift,
config: &OptimizeConfig,
) -> Result<Vec<f64>, RetargetError> {
let anchored = anchored_tiers_in(design, scope);
if !anchored.is_empty() {
return Err(RetargetError::AnchoredTiers(anchored));
}
let n_to = target.n_d;
let blocks = classify_blocks(&design.meet_tier_inputs());
let seeded = seed_shift_design(design, scope, &blocks, n_from, n_to, crown);
let outcome = optimize_design(&seeded, &target.gem, config, &SearchHooks::default())
.map_err(RetargetError::Solve)?;
let mut final_angles: Vec<f64> = scope.iter().map(|&i| seeded.tiers[i].angle_deg).collect();
for change in &outcome.changes {
if let Some(slot) = scope.iter().position(|&i| i == change.index) {
final_angles[slot] = change.to_deg;
}
}
Ok(final_angles)
}
#[must_use]
pub fn apply(design: &Design, proposal: &RetargetProposal) -> Edit {
let changes = proposal
.rows
.iter()
.filter(|row| row.tier_index < design.tiers.len())
.map(|row| {
let current = design.tiers[row.tier_index].angle_deg;
(row.tier_index, current, row.new_angle)
})
.collect();
Edit::RetargetAngles { changes }
}