use super::{RetargetProposal, apply, plan::RetargetPlan, validity::StoneAnalysis};
use glam::DVec3;
use indicatrix::geometry::meet_solver::MeetConstraint;
use indicatrix_cut_core::{
Design, Edit,
design::{
RelationError,
hinge::{FacetSide, TierHinge, mast_through},
},
};
use std::collections::BTreeMap;
const MIN_ANCHORED_MAST: f64 = 1e-6;
const NEIGHBOUR_TOLERANCE: f64 = 1e-6;
const MIN_VERTICAL_COMPONENT: f64 = 1e-9;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AnchorChange {
pub tier_index: usize,
pub old_mast: f64,
pub new_mast: f64,
}
#[must_use]
pub fn scale_reference_mast(design: &Design, index: usize) -> Option<f64> {
match design.tiers.get(index)?.constraint {
MeetConstraint::ScaleReference(mast) => Some(mast),
_ => None,
}
}
#[derive(Debug, Clone, Copy)]
struct MovedPlane {
angle_deg: f64,
side: FacetSide,
mast: f64,
}
fn anchor_order(a: &TierHinge, b: &TierHinge) -> std::cmp::Ordering {
let distance = |hinge: &TierHinge| match hinge.side {
FacetSide::Crown => hinge.point.y,
FacetSide::Pavilion => -hinge.point.y,
};
(!a.wall_edge)
.cmp(&!b.wall_edge)
.then_with(|| distance(a).total_cmp(&distance(b)))
}
fn follow_neighbours(
hinge: &TierHinge,
own_tier: usize,
original: &StoneAnalysis,
moved: &BTreeMap<usize, MovedPlane>,
) -> DVec3 {
let mut heights = Vec::new();
for (&tier, plane) in moved {
if tier == own_tier {
continue;
}
let Some(range) = original.ranges.get(tier) else {
continue;
};
let (sin_t, cos_t) = plane.angle_deg.abs().to_radians().sin_cos();
let vertical = match plane.side {
FacetSide::Crown => cos_t,
FacetSide::Pavilion => -cos_t,
};
if vertical.abs() < MIN_VERTICAL_COMPONENT {
continue;
}
for index in range.clone() {
let Some(&(normal, offset)) = original.planes.get(index) else {
continue;
};
if (normal.dot(hinge.point) - offset).abs() > NEIGHBOUR_TOLERANCE {
continue;
}
let azimuth = normal.z.atan2(normal.x);
let radial = azimuth
.cos()
.mul_add(hinge.point.x, azimuth.sin() * hinge.point.z);
heights.push(sin_t.mul_add(-radial, plane.mast) / vertical);
}
}
let height = match hinge.side {
FacetSide::Crown => heights.into_iter().reduce(f64::min),
FacetSide::Pavilion => heights.into_iter().reduce(f64::max),
};
height.map_or(hinge.point, |y| DVec3::new(hinge.point.x, y, hinge.point.z))
}
pub fn fold_relations(design: &mut Design) -> Result<Vec<usize>, RelationError> {
let updates = design.evaluate_relations()?;
let mut changed = Vec::new();
for (index, angle) in updates {
if let Some(tier) = design.tiers.get_mut(index)
&& tier.angle_deg.to_bits() != angle.to_bits()
{
tier.angle_deg = angle;
changed.push(index);
}
}
Ok(changed)
}
pub fn anchored_candidate_for(
design: &Design,
moves: &[(usize, f64)],
original: &StoneAnalysis,
) -> Result<(Design, Vec<AnchorChange>), RelationError> {
anchored_candidate_with(design, moves, original, false)
}
pub fn anchored_candidate_split(
design: &Design,
moves: &[(usize, f64)],
split: &StoneAnalysis,
flat_shift: f64,
) -> Result<(Design, Vec<AnchorChange>), RelationError> {
let (mut candidate, mut anchors) = anchored_candidate_with(design, moves, split, true)?;
for flat in &split.flats {
let Some(old_mast) = scale_reference_mast(design, flat.tier_index) else {
continue;
};
let new_mast = old_mast + flat_shift;
if !new_mast.is_finite() || new_mast < MIN_ANCHORED_MAST {
continue;
}
candidate.tiers[flat.tier_index].constraint = MeetConstraint::ScaleReference(new_mast);
anchors.retain(|anchor| anchor.tier_index != flat.tier_index);
anchors.push(AnchorChange {
tier_index: flat.tier_index,
old_mast,
new_mast,
});
}
anchors.sort_by_key(|anchor| anchor.tier_index);
Ok((candidate, anchors))
}
fn anchored_candidate_with(
design: &Design,
moves: &[(usize, f64)],
original: &StoneAnalysis,
reanchor_all: bool,
) -> Result<(Design, Vec<AnchorChange>), RelationError> {
let mut candidate = design.clone();
for &(index, angle) in moves {
if let Some(tier) = candidate.tiers.get_mut(index) {
tier.angle_deg = angle;
}
}
let followers = fold_relations(&mut candidate)?;
let mut moving: Vec<(usize, f64)> = moves.to_vec();
moving.extend(
followers
.into_iter()
.map(|index| (index, candidate.tiers[index].angle_deg)),
);
if reanchor_all {
for &index in original.hinges.keys() {
if !moving.iter().any(|&(moved, _)| moved == index) {
moving.push((index, candidate.tiers[index].angle_deg));
}
}
}
let mut order: Vec<((usize, f64), &TierHinge)> = moving
.iter()
.filter_map(|&(index, angle)| {
original
.hinges
.get(&index)
.map(|hinge| ((index, angle), hinge))
})
.collect();
order.sort_by(|a, b| anchor_order(a.1, b.1));
let mut moved = BTreeMap::new();
let mut anchors = Vec::new();
for ((tier_index, new_angle), hinge) in order {
let Some(old_mast) = scale_reference_mast(design, tier_index) else {
continue;
};
let pivot = follow_neighbours(hinge, tier_index, original, &moved);
let new_mast = mast_through(new_angle, hinge.azimuth_rad, hinge.side, pivot);
if !new_mast.is_finite() || new_mast < MIN_ANCHORED_MAST {
continue;
}
candidate.tiers[tier_index].constraint = MeetConstraint::ScaleReference(new_mast);
moved.insert(
tier_index,
MovedPlane {
angle_deg: new_angle,
side: hinge.side,
mast: new_mast,
},
);
anchors.push(AnchorChange {
tier_index,
old_mast,
new_mast,
});
}
anchors.sort_by_key(|anchor| anchor.tier_index);
Ok((candidate, anchors))
}
#[must_use]
pub fn anchored_candidate(
design: &Design,
plan: &RetargetPlan,
original: &StoneAnalysis,
) -> (Design, Vec<AnchorChange>) {
let moves = plan.moving_angles();
anchored_candidate_for(design, &moves, original).unwrap_or_else(|_| {
let mut candidate = design.clone();
for &(index, angle) in &moves {
if let Some(tier) = candidate.tiers.get_mut(index) {
tier.angle_deg = angle;
}
}
(candidate, Vec::new())
})
}
#[must_use]
pub fn mast_differences(before: &Design, after: &Design) -> Vec<AnchorChange> {
before
.tiers
.iter()
.zip(&after.tiers)
.enumerate()
.filter_map(
|(tier_index, (was, now))| match (&was.constraint, &now.constraint) {
(
MeetConstraint::ScaleReference(old_mast),
MeetConstraint::ScaleReference(new_mast),
) if old_mast.to_bits() != new_mast.to_bits() => Some(AnchorChange {
tier_index,
old_mast: *old_mast,
new_mast: *new_mast,
}),
_ => None,
},
)
.collect()
}
#[must_use]
pub fn angle_differences(before: &Design, after: &Design) -> Vec<(usize, f64)> {
before
.tiers
.iter()
.zip(&after.tiers)
.enumerate()
.filter(|&(index, (was, now))| {
was.angle_deg.to_bits() != now.angle_deg.to_bits() && !before.is_tier_driven(index)
})
.map(|(index, (_, now))| (index, now.angle_deg))
.collect()
}
#[must_use]
pub fn apply_with_anchors(
design: &Design,
proposal: &RetargetProposal,
anchors: &[AnchorChange],
) -> Edit {
let angles = apply(design, proposal);
let masts: Vec<Edit> = anchors
.iter()
.filter(|anchor| anchor.tier_index < design.tiers.len())
.map(|anchor| Edit::SetConstraint {
index: anchor.tier_index,
constraint: MeetConstraint::ScaleReference(anchor.new_mast),
})
.collect();
if masts.is_empty() {
return angles;
}
let mut edits = Vec::with_capacity(masts.len() + 1);
edits.push(angles);
edits.extend(masts);
Edit::Batch(edits)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::retarget::{CrownShift, PlanRow, build_plan, validity::analyze};
use indicatrix_cut_core::{
BuiltinMaterials, ConstraintTier, History, MaterialSelection, PreformSpec, ScheduleMeta,
};
fn selection(name: &str) -> MaterialSelection {
MaterialSelection {
name: Some(name.to_string()),
specific_gravity_override: None,
refractive_index_override: None,
body_color_override: None,
body_color_bands_override: None,
absorption_path_scale_override: None,
}
}
fn brilliant() -> Design {
let mut design = Design::new(
PreformSpec::block(2.0, 1.0, 4.0),
ScheduleMeta::standard_round_brilliant(),
ConstraintTier::standard_round_brilliant(),
);
design.material = selection("Diamond");
design
}
fn quartz() -> indicatrix_cut_core::ResolvedMaterial {
selection("Quartz").resolve(&BuiltinMaterials)
}
fn tier_named(design: &Design, name: &str) -> usize {
design.tiers.iter().position(|t| t.name == name).unwrap()
}
#[test]
fn anchored_candidate_moves_angles_and_masts_of_scale_reference_tiers_only() {
let design = brilliant();
let original = analyze(&design, true).expect("every tier is pinned");
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let (candidate, anchors) = anchored_candidate(&design, &plan, &original);
let moving: Vec<&PlanRow> = plan.rows.iter().filter(|r| r.moves).collect();
assert_ne!(moving, Vec::<&PlanRow>::new());
for row in &moving {
assert!(
(candidate.tiers[row.tier_index].angle_deg - row.new_angle).abs() < 1e-12,
"{}",
row.name
);
}
for name in ["Table", "Culet", "Girdle"] {
let index = tier_named(&design, name);
assert_eq!(candidate.tiers[index], design.tiers[index], "{name}");
assert!(anchors.iter().all(|a| a.tier_index != index), "{name}");
}
for anchor in &anchors {
assert_eq!(
scale_reference_mast(&candidate, anchor.tier_index),
Some(anchor.new_mast)
);
assert_eq!(
scale_reference_mast(&design, anchor.tier_index),
Some(anchor.old_mast)
);
}
}
#[test]
fn a_plan_that_changes_nothing_anchors_every_mast_where_it_was() {
let design = brilliant();
let original = analyze(&design, true).expect("every tier is pinned");
let same = selection("Diamond").resolve(&BuiltinMaterials);
let plan = build_plan(&design, &same, CrownShift::default(), &[]);
let (_, anchors) = anchored_candidate(&design, &plan, &original);
assert_ne!(anchors, Vec::new());
for anchor in anchors {
assert!(
(anchor.new_mast - anchor.old_mast).abs() < 1e-5,
"tier {}: {} -> {}",
anchor.tier_index,
anchor.old_mast,
anchor.new_mast
);
}
}
#[test]
fn a_pavilion_that_gets_steeper_keeps_the_girdle_edge_of_its_break_facet() {
let design = brilliant();
let original = analyze(&design, true).expect("every tier is pinned");
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let (candidate, _) = anchored_candidate(&design, &plan, &original);
let lower = tier_named(&design, "Lower Girdle");
let hinge = original.hinges[&lower];
let new_angle = candidate.tiers[lower].angle_deg;
assert!(
new_angle < design.tiers[lower].angle_deg,
"steeper pavilion"
);
let mast = scale_reference_mast(&candidate, lower).unwrap();
let through = mast_through(new_angle, hinge.azimuth_rad, hinge.side, hinge.point);
assert!((mast - through).abs() < 1e-9, "{mast} vs {through}");
}
#[test]
fn a_main_facet_follows_the_new_plane_of_the_break_facet_it_meets() {
let design = brilliant();
let original = analyze(&design, true).expect("every tier is pinned");
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let (candidate, _) = anchored_candidate(&design, &plan, &original);
let main = tier_named(&design, "Pavilion Main");
let lower = tier_named(&design, "Lower Girdle");
let hinge = original.hinges[&main];
let on_original = original.ranges[lower].clone().any(|index| {
let (normal, offset) = original.planes[index];
(normal.dot(hinge.point) - offset).abs() < 1e-6
});
assert!(on_original, "the pivot should lie on the break facet");
let (normal, _) = original.ranges[lower]
.clone()
.map(|index| original.planes[index])
.find(|(normal, offset)| (normal.dot(hinge.point) - offset).abs() < 1e-6)
.expect("the pivot lies on a break facet");
let azimuth = normal.z.atan2(normal.x);
let (sin_t, cos_t) = candidate.tiers[lower]
.angle_deg
.abs()
.to_radians()
.sin_cos();
let lower_mast = scale_reference_mast(&candidate, lower).unwrap();
let radial = azimuth
.cos()
.mul_add(hinge.point.x, azimuth.sin() * hinge.point.z);
let followed_height = sin_t.mul_add(-radial, lower_mast) / -cos_t;
let followed = DVec3::new(hinge.point.x, followed_height, hinge.point.z);
let main_mast = scale_reference_mast(&candidate, main).unwrap();
let through = mast_through(
candidate.tiers[main].angle_deg,
hinge.azimuth_rad,
hinge.side,
followed,
);
assert!(
(main_mast - through).abs() < 1e-9,
"main mast {main_mast} vs {through}"
);
}
#[test]
fn apply_with_anchors_is_one_undo_step_and_undoes_exactly() {
let mut design = brilliant();
let original_design = design.clone();
let original = analyze(&design, true).expect("every tier is pinned");
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let (_, anchors) = anchored_candidate(&design, &plan, &original);
assert_ne!(anchors, Vec::new());
let proposal = plan.proposal();
let edit = apply_with_anchors(&design, &proposal, &anchors);
assert!(matches!(edit, Edit::Batch(_)));
assert_eq!(edit.describe(&design), "Retarget angles");
let mut history = History::new();
history.apply(&mut design, edit).expect("the batch applies");
assert_ne!(design, original_design);
for anchor in &anchors {
assert_eq!(
scale_reference_mast(&design, anchor.tier_index),
Some(anchor.new_mast)
);
}
assert!(history.undo(&mut design).unwrap());
assert_eq!(
design, original_design,
"one undo restores angles and masts"
);
assert!(!history.can_undo(), "the retarget was one undo step");
}
#[test]
fn apply_with_no_anchors_is_the_plain_angle_edit() {
let design = brilliant();
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let proposal = plan.proposal();
assert_eq!(
apply_with_anchors(&design, &proposal, &[]),
apply(&design, &proposal)
);
}
#[test]
fn an_anchor_for_a_missing_tier_is_dropped() {
let design = brilliant();
let plan = build_plan(&design, &quartz(), CrownShift::default(), &[]);
let proposal = plan.proposal();
let stale = [AnchorChange {
tier_index: 99,
old_mast: 1.0,
new_mast: 1.1,
}];
assert_eq!(
apply_with_anchors(&design, &proposal, &stale),
apply(&design, &proposal)
);
}
}