1use super::{Kind, Param};
6use crate::error::Result;
7use crate::invalid;
8use crate::pickup::Pickup;
9use crate::solves::{Solve, SolveKind};
10use crate::system::{Prescription, System};
11
12enum Target {
14 Solve(usize),
15 Pick(usize),
16}
17
18fn what(p: Param) -> (Kind, usize) {
20 match p.kind {
21 Kind::CurvSolve(i) => (Kind::Curv, i as usize),
22 Kind::ThickSolve(i) => (Kind::Thick, i as usize),
23 Kind::ParmSolve(n, i) => (Kind::Parm(n), i as usize),
24 _ => unreachable!("not a solve parameter"),
25 }
26}
27
28fn find(solves: &[Solve], pickups: &[Pickup], p: Param) -> Result<Target> {
29 let j = p.index();
30 let (w, _) = what(p);
31 let solve = (!matches!(w, Kind::Parm(_)))
33 .then(|| {
34 solves
35 .iter()
36 .position(|q| q.surf == j && q.kind.param_kind() == w)
37 })
38 .flatten();
39 if let Some(i) = solve {
40 return Ok(Target::Solve(i));
41 }
42 if let Some(i) = pickups.iter().position(|q| q.dst == j && q.what == w) {
43 return Ok(Target::Pick(i));
44 }
45 invalid!(
46 "{}: surface has no solve or pickup for this value",
47 p.name()
48 )
49}
50
51fn solve_values(k: &SolveKind) -> Vec<f64> {
53 use SolveKind as S;
54 match *k {
55 S::MarginalHeight { height } => vec![height, 0.0],
56 S::MarginalHeightZone { height, zone } => vec![height, zone],
57 S::ChiefHeight { height } => vec![height],
58 S::Opd { opd, zone } => vec![opd, zone],
59 S::Edge { thickness, radius } => vec![thickness, radius],
60 S::Position { other, length } => vec![other as f64, length],
61 S::Compensator { other, sum } => vec![other as f64, sum],
62 S::CenterOfCurvature { other }
63 | S::ConcentricSurface { other }
64 | S::ConcentricRadius { other } => vec![other as f64],
65 S::MarginalAngle { angle } | S::ChiefAngle { angle } => vec![angle],
66 S::ElementPower { power } => vec![power],
67 S::FNumber { fnum } => vec![fnum],
68 _ => Vec::new(),
69 }
70}
71
72fn is_surface(k: &SolveKind, i: usize) -> bool {
74 i == 0 && k.other_surface().is_some()
75}
76
77fn with_value(k: &SolveKind, i: usize, v: f64) -> SolveKind {
79 use SolveKind as S;
80 let mut a = solve_values(k);
81 a[i] = v;
82 let o = a[0] as usize;
83 match *k {
84 S::MarginalHeight { .. } | S::MarginalHeightZone { .. } if a[1] == 0.0 => {
85 S::MarginalHeight { height: a[0] }
86 }
87 S::MarginalHeight { .. } | S::MarginalHeightZone { .. } => S::MarginalHeightZone {
88 height: a[0],
89 zone: a[1],
90 },
91 S::ChiefHeight { .. } => S::ChiefHeight { height: a[0] },
92 S::Opd { .. } => S::Opd {
93 opd: a[0],
94 zone: a[1],
95 },
96 S::Edge { .. } => S::Edge {
97 thickness: a[0],
98 radius: a[1],
99 },
100 S::Position { .. } => S::Position {
101 other: o,
102 length: a[1],
103 },
104 S::Compensator { .. } => S::Compensator {
105 other: o,
106 sum: a[1],
107 },
108 S::CenterOfCurvature { .. } => S::CenterOfCurvature { other: o },
109 S::ConcentricSurface { .. } => S::ConcentricSurface { other: o },
110 S::ConcentricRadius { .. } => S::ConcentricRadius { other: o },
111 S::MarginalAngle { .. } => S::MarginalAngle { angle: a[0] },
112 S::ChiefAngle { .. } => S::ChiefAngle { angle: a[0] },
113 S::ElementPower { .. } => S::ElementPower { power: a[0] },
114 S::FNumber { .. } => S::FNumber { fnum: a[0] },
115 k => k,
116 }
117}
118
119fn pick_values(q: &Pickup) -> Vec<f64> {
121 if q.what == Kind::Curv {
122 vec![q.src as f64, q.scale]
123 } else {
124 vec![q.src as f64, q.scale, q.offset]
125 }
126}
127
128pub(super) fn get(s: &System, p: Param) -> Result<f64> {
130 solve_value(&s.solves, &s.pickups, p)
131}
132
133pub fn solve_value(solves: &[Solve], pickups: &[Pickup], p: Param) -> Result<f64> {
135 let (_, i) = what(p);
136 let v = match find(solves, pickups, p)? {
137 Target::Solve(k) => solve_values(&solves[k].kind),
138 Target::Pick(k) => pick_values(&pickups[k]),
139 };
140 match v.get(i) {
141 Some(&x) => Ok(x),
142 None => invalid!("{}: the solve or pickup has no value {}", p.name(), i + 1),
143 }
144}
145
146pub(super) fn check(s: &System, p: Param, v: f64) -> Result<()> {
148 get(s, p)?;
149 let (_, i) = what(p);
150 let surf = match find(&s.solves, &s.pickups, p)? {
151 Target::Solve(k) => is_surface(&s.solves[k].kind, i),
152 Target::Pick(_) => i == 0,
153 };
154 let ns = s.surfaces.len() as f64;
155 if surf && (v.fract() != 0.0 || v < 0.0 || v >= ns || v == p.index() as f64) {
156 invalid!(
157 "{}: surface {v} (another surface, 0 to {})",
158 p.name(),
159 ns - 1.0
160 );
161 }
162 if !v.is_finite() {
163 invalid!("{} = {v}", p.name());
164 }
165 Ok(())
166}
167
168pub fn set_solve_value(rx: &mut Prescription, p: Param, v: f64) -> bool {
170 let (_, i) = what(p);
171 match find(&rx.solves, &rx.pickups, p) {
172 Ok(Target::Solve(k)) if i < solve_values(&rx.solves[k].kind).len() => {
173 rx.solves[k].kind = with_value(&rx.solves[k].kind, i, v);
174 }
175 Ok(Target::Pick(k)) if i < pick_values(&rx.pickups[k]).len() => {
176 let q = &mut rx.pickups[k];
177 match i {
178 0 => q.src = v as usize,
179 1 => q.scale = v,
180 _ => q.offset = v,
181 }
182 }
183 _ => return false,
184 }
185 true
186}