1use crate::freeform::{GridSag, ZernSag};
11use crate::grin::Grin;
12use crate::surface::{Binary2, CylFresnel, SurfType, Surface};
13use crate::usershape::UserShape;
14use crate::usersurf::UserSurf;
15use std::sync::Arc;
16
17pub const JONES_IDENTITY: [f64; 8] = [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0];
19
20#[derive(Debug, Clone, PartialEq, Default, serde::Serialize, serde::Deserialize)]
25#[serde(into = "FormFlat", try_from = "FormFlat")]
26#[non_exhaustive]
27pub enum Form {
28 #[default]
29 Standard,
30 EvenAsph { asph: Vec<f64> },
32 CoordBrk { cb: [f64; 5], order: u8 },
34 Jones { jones: [f64; 8] },
36 Paraxial { focal: f64, opd_mode: i32 },
38 Toroidal { cx: f64, asph: Vec<f64> },
40 ZernikeStd {
42 asph: Vec<f64>,
43 zern: Option<Arc<ZernSag>>,
44 },
45 ZernikeFringe {
47 asph: Vec<f64>,
48 zern: Option<Arc<ZernSag>>,
49 },
50 GridSag {
52 asph: Vec<f64>,
53 grid: Option<Arc<GridSag>>,
54 grid_file: String,
55 zern: Option<Arc<ZernSag>>,
56 },
57 Fresnel { asph: Vec<f64> },
59 CylFresnel {
61 asph: Vec<f64>,
62 cylf: Option<Arc<CylFresnel>>,
63 },
64 Binary2 {
66 asph: Vec<f64>,
67 bin2: Option<Arc<Binary2>>,
68 },
69 Gradium { grin: Option<Arc<Grin>> },
71 Gradient9 { grin: Option<Arc<Grin>> },
73 OddAsph { asph: Vec<f64> },
75 TiltSurf { tan: [f64; 2] },
77 Irregular { p: [f64; 8] },
79 User { user: Option<UserSurf> },
81 UserShape { shape: Option<UserShape> },
83}
84
85impl Form {
86 pub fn of(t: SurfType) -> Form {
88 match t {
89 SurfType::Standard => Form::Standard,
90 SurfType::EvenAsph => Form::EvenAsph { asph: Vec::new() },
91 SurfType::CoordBrk => Form::CoordBrk {
92 cb: [0.0; 5],
93 order: 0,
94 },
95 SurfType::Jones => Form::Jones {
96 jones: JONES_IDENTITY,
97 },
98 SurfType::Paraxial => Form::Paraxial {
99 focal: 0.0,
100 opd_mode: 0,
101 },
102 SurfType::Toroidal => Form::Toroidal {
103 cx: 0.0,
104 asph: Vec::new(),
105 },
106 SurfType::ZernikeStd => Form::ZernikeStd {
107 asph: Vec::new(),
108 zern: None,
109 },
110 SurfType::ZernikeFringe => Form::ZernikeFringe {
111 asph: Vec::new(),
112 zern: None,
113 },
114 SurfType::GridSag => Form::GridSag {
115 asph: Vec::new(),
116 grid: None,
117 grid_file: String::new(),
118 zern: None,
119 },
120 SurfType::Fresnel => Form::Fresnel { asph: Vec::new() },
121 SurfType::CylFresnel => Form::CylFresnel {
122 asph: Vec::new(),
123 cylf: None,
124 },
125 SurfType::Binary2 => Form::Binary2 {
126 asph: Vec::new(),
127 bin2: None,
128 },
129 SurfType::Gradium => Form::Gradium { grin: None },
130 SurfType::Gradient9 => Form::Gradient9 { grin: None },
131 SurfType::OddAsph => Form::OddAsph { asph: Vec::new() },
132 SurfType::TiltSurf => Form::TiltSurf { tan: [0.0; 2] },
133 SurfType::Irregular => Form::Irregular { p: [0.0; 8] },
134 SurfType::User => Form::User { user: None },
135 SurfType::UserShape => Form::UserShape { shape: None },
136 }
137 }
138
139 pub fn typ(&self) -> SurfType {
141 match self {
142 Form::Standard => SurfType::Standard,
143 Form::EvenAsph { .. } => SurfType::EvenAsph,
144 Form::CoordBrk { .. } => SurfType::CoordBrk,
145 Form::Jones { .. } => SurfType::Jones,
146 Form::Paraxial { .. } => SurfType::Paraxial,
147 Form::Toroidal { .. } => SurfType::Toroidal,
148 Form::ZernikeStd { .. } => SurfType::ZernikeStd,
149 Form::ZernikeFringe { .. } => SurfType::ZernikeFringe,
150 Form::GridSag { .. } => SurfType::GridSag,
151 Form::Fresnel { .. } => SurfType::Fresnel,
152 Form::CylFresnel { .. } => SurfType::CylFresnel,
153 Form::Binary2 { .. } => SurfType::Binary2,
154 Form::Gradium { .. } => SurfType::Gradium,
155 Form::Gradient9 { .. } => SurfType::Gradient9,
156 Form::OddAsph { .. } => SurfType::OddAsph,
157 Form::TiltSurf { .. } => SurfType::TiltSurf,
158 Form::Irregular { .. } => SurfType::Irregular,
159 Form::User { .. } => SurfType::User,
160 Form::UserShape { .. } => SurfType::UserShape,
161 }
162 }
163}
164
165impl Surface {
166 pub fn typ(&self) -> SurfType {
168 self.form.typ()
169 }
170
171 pub fn set_type(&mut self, t: SurfType) {
174 if self.typ() == t {
175 return;
176 }
177 let zern = match &self.form {
178 Form::ZernikeStd { zern, .. }
179 | Form::ZernikeFringe { zern, .. }
180 | Form::GridSag { zern, .. } => zern.clone(),
181 _ => None,
182 };
183 let grin = match &self.form {
184 Form::Gradium { grin } | Form::Gradient9 { grin } => grin.clone(),
185 _ => None,
186 };
187 let asph = self.asph().to_vec();
188 self.form = Form::of(t);
189 self.set_asph(asph);
190 match &mut self.form {
191 Form::ZernikeStd { zern: z, .. }
192 | Form::ZernikeFringe { zern: z, .. }
193 | Form::GridSag { zern: z, .. } => *z = zern,
194 Form::Gradium { grin: g } | Form::Gradient9 { grin: g } => *g = grin,
195 _ => {}
196 }
197 }
198
199 pub fn cx(&self) -> f64 {
201 match self.form {
202 Form::Toroidal { cx, .. } => cx,
203 _ => 0.0,
204 }
205 }
206
207 pub fn zern(&self) -> Option<&ZernSag> {
209 match &self.form {
210 Form::ZernikeStd { zern, .. }
211 | Form::ZernikeFringe { zern, .. }
212 | Form::GridSag { zern, .. } => zern.as_deref(),
213 _ => None,
214 }
215 }
216
217 pub fn zern_mut(&mut self) -> Option<&mut Option<Arc<ZernSag>>> {
219 match &mut self.form {
220 Form::ZernikeStd { zern, .. }
221 | Form::ZernikeFringe { zern, .. }
222 | Form::GridSag { zern, .. } => Some(zern),
223 _ => None,
224 }
225 }
226
227 pub fn grid(&self) -> Option<&GridSag> {
229 match &self.form {
230 Form::GridSag { grid, .. } => grid.as_deref(),
231 _ => None,
232 }
233 }
234
235 pub fn grid_file(&self) -> &str {
237 match &self.form {
238 Form::GridSag { grid_file, .. } => grid_file,
239 _ => "",
240 }
241 }
242
243 pub fn bin2(&self) -> Option<&Binary2> {
245 match &self.form {
246 Form::Binary2 { bin2, .. } => bin2.as_deref(),
247 _ => None,
248 }
249 }
250
251 pub fn cylf(&self) -> Option<&CylFresnel> {
253 match &self.form {
254 Form::CylFresnel { cylf, .. } => cylf.as_deref(),
255 _ => None,
256 }
257 }
258
259 pub fn grin(&self) -> Option<&Grin> {
261 match &self.form {
262 Form::Gradium { grin } | Form::Gradient9 { grin } => grin.as_deref(),
263 _ => None,
264 }
265 }
266
267 pub fn cb(&self) -> [f64; 5] {
269 match self.form {
270 Form::CoordBrk { cb, .. } => cb,
271 _ => [0.0; 5],
272 }
273 }
274
275 pub fn cb_order(&self) -> u8 {
277 match self.form {
278 Form::CoordBrk { order, .. } => order,
279 _ => 0,
280 }
281 }
282
283 pub fn focal(&self) -> f64 {
285 match self.form {
286 Form::Paraxial { focal, .. } => focal,
287 _ => 0.0,
288 }
289 }
290
291 pub fn opd_mode(&self) -> i32 {
293 match self.form {
294 Form::Paraxial { opd_mode, .. } => opd_mode,
295 _ => 0,
296 }
297 }
298
299 pub fn jones(&self) -> [f64; 8] {
301 match self.form {
302 Form::Jones { jones } => jones,
303 _ => JONES_IDENTITY,
304 }
305 }
306
307 pub fn user(&self) -> Option<&UserSurf> {
309 match &self.form {
310 Form::User { user } => user.as_ref(),
311 _ => None,
312 }
313 }
314
315 pub fn user_shape(&self) -> Option<&UserShape> {
317 match &self.form {
318 Form::UserShape { shape } => shape.as_ref(),
319 _ => None,
320 }
321 }
322
323 pub fn cb_mut(&mut self) -> Option<(&mut [f64; 5], &mut u8)> {
325 match &mut self.form {
326 Form::CoordBrk { cb, order } => Some((cb, order)),
327 _ => None,
328 }
329 }
330
331 pub fn paraxial_mut(&mut self) -> Option<(&mut f64, &mut i32)> {
333 match &mut self.form {
334 Form::Paraxial { focal, opd_mode } => Some((focal, opd_mode)),
335 _ => None,
336 }
337 }
338
339 pub fn jones_mut(&mut self) -> Option<&mut [f64; 8]> {
341 match &mut self.form {
342 Form::Jones { jones } => Some(jones),
343 _ => None,
344 }
345 }
346
347 pub fn cx_mut(&mut self) -> Option<&mut f64> {
349 match &mut self.form {
350 Form::Toroidal { cx, .. } => Some(cx),
351 _ => None,
352 }
353 }
354
355 pub fn grid_file_mut(&mut self) -> Option<&mut String> {
357 match &mut self.form {
358 Form::GridSag { grid_file, .. } => Some(grid_file),
359 _ => None,
360 }
361 }
362
363 pub fn grid_mut(&mut self) -> Option<&mut Option<Arc<GridSag>>> {
365 match &mut self.form {
366 Form::GridSag { grid, .. } => Some(grid),
367 _ => None,
368 }
369 }
370
371 pub fn bin2_mut(&mut self) -> Option<&mut Option<Arc<Binary2>>> {
373 match &mut self.form {
374 Form::Binary2 { bin2, .. } => Some(bin2),
375 _ => None,
376 }
377 }
378
379 pub fn cylf_mut(&mut self) -> Option<&mut Option<Arc<CylFresnel>>> {
381 match &mut self.form {
382 Form::CylFresnel { cylf, .. } => Some(cylf),
383 _ => None,
384 }
385 }
386
387 pub fn user_mut(&mut self) -> Option<&mut Option<UserSurf>> {
389 match &mut self.form {
390 Form::User { user } => Some(user),
391 _ => None,
392 }
393 }
394
395 pub fn user_shape_mut(&mut self) -> Option<&mut Option<UserShape>> {
397 match &mut self.form {
398 Form::UserShape { shape } => Some(shape),
399 _ => None,
400 }
401 }
402
403 pub fn grin_mut(&mut self) -> Option<&mut Option<Arc<Grin>>> {
405 match &mut self.form {
406 Form::Gradium { grin } | Form::Gradient9 { grin } => Some(grin),
407 _ => None,
408 }
409 }
410
411 pub fn asph(&self) -> &[f64] {
414 match &self.form {
415 Form::EvenAsph { asph }
416 | Form::Toroidal { asph, .. }
417 | Form::ZernikeStd { asph, .. }
418 | Form::ZernikeFringe { asph, .. }
419 | Form::GridSag { asph, .. }
420 | Form::Fresnel { asph }
421 | Form::CylFresnel { asph, .. }
422 | Form::Binary2 { asph, .. }
423 | Form::OddAsph { asph } => asph,
424 Form::TiltSurf { tan } => tan,
425 Form::Irregular { p } => p,
426 _ => &[],
427 }
428 }
429
430 pub fn asph_mut(&mut self) -> Option<&mut Vec<f64>> {
432 match &mut self.form {
433 Form::EvenAsph { asph }
434 | Form::Toroidal { asph, .. }
435 | Form::ZernikeStd { asph, .. }
436 | Form::ZernikeFringe { asph, .. }
437 | Form::GridSag { asph, .. }
438 | Form::Fresnel { asph }
439 | Form::CylFresnel { asph, .. }
440 | Form::Binary2 { asph, .. }
441 | Form::OddAsph { asph } => Some(asph),
442 _ => None,
443 }
444 }
445
446 pub fn flip_z(&mut self) -> crate::Result<()> {
453 if let Form::User { user: Some(u) } = &self.form {
454 let f = u.flipped(crate::usersurf::Flip::Z)?;
455 self.form = Form::User { user: Some(f) };
456 }
457 self.c = -self.c;
458 if let Some(crate::surface::Bire::In { axis, .. }) = &mut self.bire {
459 axis[2] = -axis[2];
460 }
461 match &mut self.form {
462 Form::Gradium { grin } | Form::Gradient9 { grin } => {
463 if let Some(g) = grin {
464 Arc::make_mut(g).flip_z();
465 }
466 }
467 Form::Toroidal { cx, asph } => {
468 *cx = -*cx;
469 asph.iter_mut().for_each(|a| *a = -*a);
470 }
471 Form::ZernikeStd { asph, zern } | Form::ZernikeFringe { asph, zern } => {
472 asph.iter_mut().for_each(|a| *a = -*a);
473 if let Some(z) = zern {
474 Arc::make_mut(z).negate();
475 }
476 }
477 Form::GridSag {
478 asph, grid, zern, ..
479 } => {
480 asph.iter_mut().for_each(|a| *a = -*a);
481 if let Some(g) = grid {
482 Arc::make_mut(g).negate();
483 }
484 if let Some(z) = zern {
485 Arc::make_mut(z).negate();
486 }
487 }
488 Form::CylFresnel { asph, cylf } => {
489 asph.iter_mut().for_each(|a| *a = -*a);
490 if let Some(f) = cylf {
491 let f = Arc::make_mut(f);
492 f.c = -f.c;
493 f.coef.iter_mut().for_each(|a| *a = -*a);
494 }
495 }
496 Form::TiltSurf { tan } => tan.iter_mut().for_each(|a| *a = -*a),
497 Form::UserShape { shape: Some(u) } => u.flip_z = !u.flip_z,
498 Form::Irregular { p } => {
499 for i in 2..7 {
500 p[i] = -p[i];
501 }
502 }
503 _ => {
504 if let Some(a) = self.asph_mut() {
505 a.iter_mut().for_each(|a| *a = -*a);
506 }
507 }
508 }
509 Ok(())
510 }
511
512 pub fn mirror_x(&mut self) -> crate::Result<()> {
518 if let Form::User { user: Some(u) } = &self.form {
519 let f = u.flipped(crate::usersurf::Flip::X)?;
520 self.form = Form::User { user: Some(f) };
521 }
522 if let Some(crate::surface::Bire::In { axis, .. }) = &mut self.bire {
523 axis[0] = -axis[0];
524 }
525 match &mut self.form {
526 Form::Gradium { grin } | Form::Gradient9 { grin } => {
527 if let Some(g) = grin {
528 Arc::make_mut(g).mirror_x();
529 }
530 }
531 Form::ZernikeStd { zern, .. } | Form::ZernikeFringe { zern, .. } => {
532 if let Some(z) = zern {
533 Arc::make_mut(z).mirror_x();
534 }
535 }
536 Form::GridSag { grid, zern, .. } => {
537 if let Some(g) = grid {
538 Arc::make_mut(g).mirror_x();
539 }
540 if let Some(z) = zern {
541 Arc::make_mut(z).mirror_x();
542 }
543 }
544 Form::TiltSurf { tan } => tan[0] = -tan[0],
545 Form::UserShape { shape: Some(u) } => u.mirror_x = !u.mirror_x,
546 Form::Irregular { p } => {
547 p[0] = -p[0];
548 p[3] = -p[3];
549 p[7] = -p[7];
550 }
551 _ => {}
552 }
553 Ok(())
554 }
555
556 pub fn parm(&self, i: u8) -> Option<f64> {
558 let d = self;
559 let i = i as usize;
560 let at = |v: &[f64], j: usize| -> Option<f64> {
561 (j < 8).then(|| v.get(j).copied().unwrap_or(0.0))
562 };
563 match d.typ() {
564 SurfType::Toroidal if i == 1 => Some(if d.cx() == 0.0 { 0.0 } else { 1.0 / d.cx() }),
565 t if asph_parm_base(t).is_some_and(|b| (b..=8).contains(&i)) => {
566 at(d.asph(), i - asph_parm_base(t).unwrap_or(1))
567 }
568 SurfType::CoordBrk if (1..=5).contains(&i) => Some(d.cb()[i - 1]),
569 SurfType::CoordBrk if i == 6 => Some(d.cb_order() as f64),
570 SurfType::Paraxial if i == 1 => Some(d.focal()),
571 SurfType::Paraxial if i == 2 => Some(d.opd_mode() as f64),
572 SurfType::Jones if (1..=8).contains(&i) => Some(d.jones()[i - 1]),
573 SurfType::Gradium | SurfType::Gradient9 => {
574 use crate::grin::GrinKind;
575 let g = d.grin()?;
576 match (&g.kind, i) {
577 (_, 1) => Some(g.dt),
578 (GrinKind::Gradium { prof, .. }, 0) => Some(prof.zmax),
579 (GrinKind::Gradium { dz, .. }, 2) => Some(*dz),
580 (GrinKind::Gradium { .. }, 3) => g.ref_index(),
581 (GrinKind::Gradium { dec, .. }, 4..=7) => Some(dec[i - 4]),
582 (GrinKind::Gradium { cap, .. }, 8) => Some(*cap as f64),
583 (GrinKind::Grad9 { tan, .. }, 2 | 3) => Some(tan[i - 2]),
584 _ => None,
585 }
586 }
587 _ => None,
588 }
589 }
590
591 pub fn set_parm(&mut self, i: u8, v: f64) -> bool {
594 let d = self;
595 let i = i as usize;
596 let t = d.typ();
597 if let Some(b) = asph_parm_base(t).filter(|b| (*b..=8).contains(&i)) {
598 if let Some(a) = d.asph_mut().filter(|a| a.len() < 9 - b) {
599 a.resize(9 - b, 0.0);
600 }
601 return d.set_asph_at(i - b, v);
602 }
603 match (&mut d.form, i) {
604 (Form::Toroidal { cx, .. }, 1) => *cx = if v == 0.0 { 0.0 } else { 1.0 / v },
605 (Form::CoordBrk { cb, .. }, 1..=5) => cb[i - 1] = v,
606 (Form::CoordBrk { order, .. }, 6) => *order = v as u8,
607 (Form::Paraxial { focal, .. }, 1) => *focal = v,
608 (Form::Paraxial { opd_mode, .. }, 2) => *opd_mode = v as i32,
609 (Form::Jones { jones }, 1..=8) => {
610 jones[i - 1] = v;
611 d.retarder = None;
612 }
613 (Form::Gradium { grin } | Form::Gradient9 { grin }, _) => {
615 use crate::grin::GrinKind;
616 let Some(g) = grin.as_mut() else {
617 return false;
618 };
619 let g = std::sync::Arc::make_mut(g);
620 match (&mut g.kind, i) {
621 (_, 1) => g.dt = v,
622 (GrinKind::Grad9 { tan, .. }, 2 | 3) => tan[i - 2] = v,
623 _ => return false,
624 }
625 }
626 _ => return false,
627 }
628 true
629 }
630
631 pub fn asph_fixed_mut(&mut self) -> Option<&mut [f64]> {
633 match &mut self.form {
634 Form::TiltSurf { tan } => Some(tan),
635 Form::Irregular { p } => Some(p),
636 _ => None,
637 }
638 }
639
640 pub fn set_asph(&mut self, v: Vec<f64>) -> bool {
643 if let Some(a) = self.asph_mut() {
644 *a = v;
645 return true;
646 }
647 let fixed = self.asph_fixed_mut();
648 let n = fixed.as_ref().map_or(0, |f| f.len());
649 if let Some(f) = fixed {
650 for (i, x) in f.iter_mut().enumerate() {
651 *x = v.get(i).copied().unwrap_or(0.0);
652 }
653 }
654 v.iter().skip(n).all(|&x| x == 0.0)
655 }
656
657 pub fn set_asph_at(&mut self, i: usize, x: f64) -> bool {
659 if let Some(a) = self.asph_mut() {
660 if a.len() <= i {
661 a.resize(i + 1, 0.0);
662 }
663 a[i] = x;
664 return true;
665 }
666 match self.asph_fixed_mut().and_then(|f| f.get_mut(i)) {
667 Some(v) => {
668 *v = x;
669 true
670 }
671 None => false,
672 }
673 }
674}
675
676#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
678struct FormFlat {
679 typ: SurfType,
680 asph: Vec<f64>,
681 cx: f64,
682 zern: Option<Arc<ZernSag>>,
683 grid: Option<Arc<GridSag>>,
684 grid_file: String,
685 bin2: Option<Arc<Binary2>>,
686 cylf: Option<Arc<CylFresnel>>,
687 grin: Option<Arc<Grin>>,
688 cb: [f64; 5],
689 cb_order: u8,
690 focal: f64,
691 opd_mode: i32,
692 jones: [f64; 8],
693 #[serde(default, skip_serializing_if = "Option::is_none")]
694 user: Option<UserSurf>,
695 #[serde(default, skip_serializing_if = "Option::is_none")]
696 shape: Option<UserShape>,
697}
698
699impl From<Form> for FormFlat {
700 fn from(f: Form) -> FormFlat {
701 let mut s = Surface {
702 form: f,
703 ..Default::default()
704 };
705 FormFlat {
706 typ: s.typ(),
707 asph: s.asph().to_vec(),
708 cx: s.cx(),
709 zern: s.zern_mut().and_then(|z| z.take()),
710 grid: s.grid_mut().and_then(|g| g.take()),
711 grid_file: s.grid_file().to_string(),
712 bin2: s.bin2_mut().and_then(|b| b.take()),
713 cylf: s.cylf_mut().and_then(|c| c.take()),
714 grin: s.grin_mut().and_then(|g| g.take()),
715 cb: s.cb(),
716 cb_order: s.cb_order(),
717 focal: s.focal(),
718 opd_mode: s.opd_mode(),
719 jones: s.jones(),
720 user: s.user_mut().and_then(|u| u.take()),
721 shape: s.user_shape_mut().and_then(|u| u.take()),
722 }
723 }
724}
725
726impl TryFrom<FormFlat> for Form {
727 type Error = String;
728
729 fn try_from(v: FormFlat) -> Result<Form, String> {
731 let typ = v.typ;
732 let mut s = Surface {
733 form: Form::of(typ),
734 ..Default::default()
735 };
736 let mut unused = Vec::new();
737 if !s.set_asph(v.asph) {
738 unused.push("asph");
739 }
740 macro_rules! put {
741 ($name:literal, $slot:expr, $val:expr, $default:expr) => {
742 match $slot {
743 Some(x) => *x = $val,
744 None if $val != $default => unused.push($name),
745 None => {}
746 }
747 };
748 }
749 put!("cx", s.cx_mut(), v.cx, 0.0);
750 put!("zern", s.zern_mut(), v.zern, None);
751 put!("grid", s.grid_mut(), v.grid, None);
752 put!("bin2", s.bin2_mut(), v.bin2, None);
753 put!("cylf", s.cylf_mut(), v.cylf, None);
754 put!("grin", s.grin_mut(), v.grin, None);
755 put!("jones", s.jones_mut(), v.jones, JONES_IDENTITY);
756 put!("user", s.user_mut(), v.user, None);
757 put!("shape", s.user_shape_mut(), v.shape, None);
758 match &mut s.form {
759 Form::GridSag { grid_file, .. } => *grid_file = v.grid_file,
760 _ if !v.grid_file.is_empty() => unused.push("grid_file"),
761 _ => {}
762 }
763 match s.cb_mut() {
764 Some((cb, order)) => (*cb, *order) = (v.cb, v.cb_order),
765 None => {
766 if v.cb != [0.0; 5] {
767 unused.push("cb");
768 }
769 if v.cb_order != 0 {
770 unused.push("cb_order");
771 }
772 }
773 }
774 match s.paraxial_mut() {
775 Some((focal, mode)) => (*focal, *mode) = (v.focal, v.opd_mode),
776 None => {
777 if v.focal != 0.0 {
778 unused.push("focal");
779 }
780 if v.opd_mode != 0 {
781 unused.push("opd_mode");
782 }
783 }
784 }
785 if !unused.is_empty() {
786 let names = unused.join(", ");
787 return Err(format!(
788 "{names} not used by a {typ:?} surface (remove the values or change typ)"
789 ));
790 }
791 Ok(s.form)
792 }
793}
794
795impl schemars::JsonSchema for Form {
796 fn schema_name() -> std::borrow::Cow<'static, str> {
797 "Form".into()
798 }
799 fn json_schema(g: &mut schemars::SchemaGenerator) -> schemars::Schema {
800 FormFlat::json_schema(g)
801 }
802}
803
804pub fn asph_parm_base(t: SurfType) -> Option<usize> {
808 match t {
809 SurfType::EvenAsph | SurfType::Fresnel | SurfType::CylFresnel | SurfType::Binary2 => {
810 Some(1)
811 }
812 SurfType::Toroidal => Some(2),
813 t if crate::freeform::is_freeform(t) => Some(1),
814 _ => None,
815 }
816}