use super::row_format::representative_crown_and_pavilion_angles_deg;
use crate::loading::eval_number;
use indicatrix_cut_core::{
Design, Risk, crown_window_margin_deg, crown_windowing_risk,
design::{ExprError, TierId},
tier_margin_deg, windowing_risk,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiveMargin {
pub text: String,
pub level: i32,
pub is_estimate: bool,
}
const fn level_of(risk: Risk) -> i32 {
match risk {
Risk::Safe => 0,
Risk::Marginal => 1,
Risk::Windows => 2,
}
}
fn typed_angle_deg(design: &Design, text: &str) -> Option<f64> {
if text.trim_start().starts_with('=') {
return relation_angle_deg(design, text);
}
eval_number(text, None).ok()
}
#[must_use]
pub fn relation_angle_deg(design: &Design, text: &str) -> Option<f64> {
let relation = design.parse_relation(text).ok()?;
let mut lookup = |id: &TierId| -> Result<f64, ExprError> {
let tier = design
.index_of_tier_id(*id)
.and_then(|position| design.tiers.get(position))
.ok_or_else(|| ExprError::UnknownName("a tier is missing".to_owned()))?;
Ok(tier.angle_deg.abs())
};
let magnitude = relation.angle.eval(&mut lookup).ok()?;
if magnitude <= 0.0 || magnitude > 90.0 {
return None;
}
let first_read = relation
.references()
.iter()
.filter_map(|id| design.index_of_tier_id(*id))
.min()
.and_then(|position| design.tiers.get(position))?;
Some(if first_read.angle_deg.is_sign_negative() {
-magnitude
} else {
magnitude
})
}
#[must_use]
pub fn angle_live_margin(design: &Design, n_d: f64, angle_text: &str) -> Option<LiveMargin> {
let angle_deg = typed_angle_deg(design, angle_text)?;
if angle_deg < 0.0 {
Some(LiveMargin {
text: format!("{:+.1}\u{b0}", tier_margin_deg(angle_deg, n_d)),
level: level_of(windowing_risk(angle_deg, n_d)),
is_estimate: false,
})
} else if angle_deg > 0.0 {
let (_, pavilion_deg) = representative_crown_and_pavilion_angles_deg(design);
let pavilion_deg = pavilion_deg?;
Some(LiveMargin {
text: format!(
"{:+.1}\u{b0}",
crown_window_margin_deg(pavilion_deg, angle_deg, n_d)
),
level: level_of(crown_windowing_risk(pavilion_deg, angle_deg, n_d)),
is_estimate: true,
})
} else {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::EditorSession;
fn design() -> Design {
EditorSession::from_template(
indicatrix_cut_core::FreshDesignSpec {
gear_teeth: 96,
symmetry_order: 8,
mirror: true,
material: indicatrix_cut_core::MaterialSelection::none(),
preform: indicatrix_cut_core::PreformSpec::cylinder(96, 1.5, 1.0, 1.5),
},
1,
)
.design
}
#[test]
fn a_pavilion_angle_reads_the_plain_margin_and_a_crown_angle_an_estimate() {
let design = design();
let n_d = design.effective_refractive_index();
let pavilion = angle_live_margin(&design, n_d, "-43.0").expect("a pavilion bar");
assert!(!pavilion.is_estimate);
assert_eq!(
pavilion.text,
format!("{:+.1}\u{b0}", tier_margin_deg(-43.0, n_d))
);
let crown = angle_live_margin(&design, n_d, " 34.5 ").expect("a crown bar");
assert!(crown.is_estimate);
assert!((0..=2).contains(&crown.level));
}
#[test]
fn nothing_is_shown_for_text_zero_or_a_crown_without_a_pavilion() {
let design = design();
let n_d = design.effective_refractive_index();
assert_eq!(angle_live_margin(&design, n_d, "abc"), None);
assert_eq!(angle_live_margin(&design, n_d, "0"), None);
assert_eq!(angle_live_margin(&design, n_d, ""), None);
let empty = EditorSession::fresh().design;
assert_eq!(angle_live_margin(&empty, 1.54, "34.5"), None);
}
fn two_tiers() -> Design {
use indicatrix::geometry::meet_solver::MeetConstraint;
use indicatrix_cut_core::{ConstraintTier, Edit};
let mut session = EditorSession::fresh();
for (index, (name, angle)) in [("P1", -43.0), ("C1", 34.5)].into_iter().enumerate() {
let tier = ConstraintTier {
angle_deg: angle,
name: name.to_owned(),
indices: vec![0.0, 24.0],
constraint: MeetConstraint::ScaleReference(0.65),
imported_meet: None,
original_notes: None,
detached: Vec::new(),
};
session
.apply(Edit::AddTier { index, tier })
.expect("a tier is added");
}
session.design
}
#[test]
fn arithmetic_reads_the_bar_of_its_result() {
let design = two_tiers();
let n_d = design.effective_refractive_index();
let calculated = angle_live_margin(&design, n_d, "-40 + 0.5").expect("a bar");
assert_eq!(
Some(calculated),
angle_live_margin(&design, n_d, "-39.5"),
"the sum reads like the number it makes"
);
assert_eq!(angle_live_margin(&design, n_d, "-40 +"), None);
}
#[test]
fn a_relation_reads_the_bar_of_the_angle_it_would_give() {
let design = two_tiers();
let n_d = design.effective_refractive_index();
assert_eq!(
angle_live_margin(&design, n_d, "=P1 - 3"),
angle_live_margin(&design, n_d, "-40")
);
let crown = angle_live_margin(&design, n_d, "= C1 + 1.5").expect("a crown bar");
assert!(crown.is_estimate);
assert_eq!(Some(crown), angle_live_margin(&design, n_d, "36"));
for text in ["=", "=Q9 - 3", "=P1 - 60", "=P1 +", "=P1 - 43"] {
assert_eq!(angle_live_margin(&design, n_d, text), None, "{text:?}");
}
}
}