Skip to main content

lensbench_project/
surface_ops.rs

1//! 面の表で置いた絞り以外の面の操作(ソルブ・ピックアップ)を、ブロックの `surface_ops`([`crate::FaceMods`])に
2//! 足す・外す。同じ面・量の操作は 1 つにまとめ、ブロックの元(.zmx など)にあるものを置き換える・外すときは外す
3//! 操作も置く(置いたものを外したとき、元のものに戻らない)。GUI の組合せの要素が使う
4
5use crate::edit_spec::{BlockRef, PickupOp, Ref, SolveOp, SolveWhat, SurfaceOp};
6
7/// 面にソルブを置く。shown は面の表にそのソルブが出ていたか。要素で置いたものでなければ元(.zmx など)の
8/// ソルブなので、外す操作も置く(置いたソルブを外したとき、元のソルブに戻らない)
9pub fn put_solve(ops: &mut Vec<SurfaceOp>, o: SolveOp<BlockRef>, shown: bool) {
10    let (at, c) = (o.surface.clone(), o.is_curv());
11    let own =
12        |q: &SurfaceOp| matches!(q, SurfaceOp::AddSolve(q) if q.surface == at && q.is_curv() == c);
13    let from_source = shown && !ops.iter().any(own);
14    ops.retain(|q| !own(q));
15    let removed = ops.iter().any(|q| solve_of(q, &at, c));
16    if from_source && !removed {
17        let what = if c { SolveWhat::C } else { SolveWhat::T };
18        ops.push(SurfaceOp::RemoveSolve { surface: at, what });
19    }
20    ops.push(SurfaceOp::AddSolve(o));
21}
22
23/// 面にピックアップを置く([`put_solve`] と同じく、元のピックアップなら外す操作も置く)
24pub fn put_pickup(ops: &mut Vec<SurfaceOp>, o: PickupOp<BlockRef>, shown: bool) {
25    let at = o.dst.clone();
26    let what = o.what.clone();
27    let own = |q: &SurfaceOp| matches!(q, SurfaceOp::AddPickup(q) if q.dst == at && q.what == what);
28    let from_source = shown && !ops.iter().any(own);
29    ops.retain(|q| !own(q));
30    let removed = ops.iter().any(|q| pickup_of(q, &at, &what));
31    if from_source && !removed {
32        ops.push(SurfaceOp::RemovePickup { dst: at, what });
33    }
34    ops.push(SurfaceOp::AddPickup(o));
35}
36
37/// 面 i のソルブを外す: 要素で置いたソルブなら外し、無ければ要素の元(.zmx など)のソルブを外す操作を置く
38pub fn drop_solve(ops: &mut Vec<SurfaceOp>, i: usize, what: SolveWhat) {
39    drop_solve_at(ops, BlockRef::Sys(Ref::Index(i)), what)
40}
41
42/// [`drop_solve`] と同じ。面は here(復路の面を指すブロックの面など)
43pub fn drop_solve_at(ops: &mut Vec<SurfaceOp>, here: BlockRef, what: SolveWhat) {
44    let c = what == SolveWhat::C;
45    let own = |q: &SurfaceOp| matches!(q, SurfaceOp::AddSolve(q) if q.surface == here && q.is_curv() == c);
46    if ops.iter().any(own) {
47        ops.retain(|q| !own(q));
48    } else {
49        ops.retain(|q| !solve_of(q, &here, c));
50        ops.push(SurfaceOp::RemoveSolve {
51            surface: here,
52            what,
53        });
54    }
55}
56
57/// 面 i のピックアップ what を外す(ソルブと同じく、要素で置いたものが無ければ要素の元のものを外す)
58pub fn drop_pickup(ops: &mut Vec<SurfaceOp>, i: usize, what: String) {
59    drop_pickup_at(ops, BlockRef::Sys(Ref::Index(i)), what)
60}
61
62/// [`drop_pickup`] と同じ。面は here
63pub fn drop_pickup_at(ops: &mut Vec<SurfaceOp>, here: BlockRef, what: String) {
64    let own =
65        |q: &SurfaceOp| matches!(q, SurfaceOp::AddPickup(q) if q.dst == here && q.what == what);
66    if ops.iter().any(own) {
67        ops.retain(|q| !own(q));
68    } else {
69        ops.retain(|q| !pickup_of(q, &here, &what));
70        ops.push(SurfaceOp::RemovePickup { dst: here, what });
71    }
72}
73
74/// 面 at の量(曲率か厚さ)のソルブを置く・外す操作か
75fn solve_of(q: &SurfaceOp, at: &BlockRef, curv: bool) -> bool {
76    match q {
77        SurfaceOp::AddSolve(q) => q.surface == *at && q.is_curv() == curv,
78        SurfaceOp::RemoveSolve { surface, what } => {
79            surface == at && (*what == SolveWhat::C) == curv
80        }
81        _ => false,
82    }
83}
84
85/// 面 at の量 what のピックアップを置く・外す操作か
86fn pickup_of(q: &SurfaceOp, at: &BlockRef, what: &str) -> bool {
87    match q {
88        SurfaceOp::AddPickup(q) => q.dst == *at && q.what == what,
89        SurfaceOp::RemovePickup { dst, what: w } => dst == at && w == what,
90        _ => false,
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    fn solve(i: usize) -> SolveOp<BlockRef> {
99        SolveOp {
100            surface: BlockRef::Sys(Ref::Index(i)),
101            other: None,
102            kind: Some("marginal_height".into()),
103            value: None,
104            param: None,
105            macro_ref: None,
106        }
107    }
108
109    fn pickup(i: usize) -> PickupOp<BlockRef> {
110        PickupOp {
111            dst: BlockRef::Sys(Ref::Index(i)),
112            src: BlockRef::Sys(Ref::Index(1)),
113            what: "t".into(),
114            scale: None,
115            offset: None,
116            from: None,
117        }
118    }
119
120    /// 同じ面・量のソルブは 1 つ。元のソルブを置き換えると外す操作も置き、置いたものを外しても元に戻らない
121    #[test]
122    fn solves_replace_and_remove_the_source() {
123        let mut ops = Vec::new();
124        put_solve(&mut ops, solve(2), false);
125        put_solve(&mut ops, solve(2), true);
126        assert_eq!(ops.len(), 1);
127        drop_solve(&mut ops, 2, SolveWhat::T);
128        assert!(ops.is_empty());
129        // 元のソルブ(表に出ている)を置き換える
130        put_solve(&mut ops, solve(3), true);
131        assert!(matches!(ops[0], SurfaceOp::RemoveSolve { .. }));
132        assert_eq!(ops.len(), 2);
133        drop_solve(&mut ops, 3, SolveWhat::T);
134        assert_eq!(ops.len(), 1);
135        assert!(matches!(ops[0], SurfaceOp::RemoveSolve { .. }));
136        // 元のソルブを外すのは 1 回だけ(曲率のソルブは別)
137        drop_solve(&mut ops, 3, SolveWhat::T);
138        drop_solve(&mut ops, 3, SolveWhat::C);
139        assert_eq!(ops.len(), 2);
140    }
141
142    /// ピックアップもソルブと同じ
143    #[test]
144    fn pickups_replace_and_remove_the_source() {
145        let mut ops = Vec::new();
146        put_pickup(&mut ops, pickup(2), true);
147        put_pickup(&mut ops, pickup(2), true);
148        assert_eq!(ops.len(), 2);
149        drop_pickup(&mut ops, 2, "t".into());
150        assert_eq!(ops.len(), 1);
151        assert!(matches!(ops[0], SurfaceOp::RemovePickup { .. }));
152    }
153}