fastsim_core/utils/
interp.rs

1use crate::imports::*;
2
3/// Methods for proportionally scaling interpolator function data
4pub trait InterpolatorMethods {
5    fn set_min(&mut self, min: f64) -> anyhow::Result<()>;
6    fn set_max(&mut self, max: f64) -> anyhow::Result<()>;
7    fn set_range(&mut self, range: f64) -> anyhow::Result<()>;
8}
9
10impl InterpolatorMethods for Interpolator {
11    #[allow(unused)]
12    fn set_min(&mut self, min: f64) -> anyhow::Result<()> {
13        let old_min = self.min()?;
14        match self {
15            Interpolator::Interp0D(value) => {
16                *value = min;
17                Ok(())
18            }
19            Interpolator::Interp1D(..) => {
20                todo!()
21            }
22            Interpolator::Interp2D(..) => {
23                todo!()
24            }
25            Interpolator::Interp3D(..) => {
26                todo!()
27            }
28            Interpolator::InterpND(..) => {
29                todo!()
30            }
31        }
32    }
33
34    fn set_max(&mut self, max: f64) -> anyhow::Result<()> {
35        let old_max = self.max()?;
36        match self {
37            Interpolator::Interp0D(value) => {
38                *value = max;
39                Ok(())
40            }
41            Interpolator::Interp1D(..) => {
42                Ok(self.set_f_x(self.f_x()?.iter().map(|x| x * max / old_max).collect())?)
43            }
44            Interpolator::Interp2D(..) => Ok(self.set_f_xy(
45                self.f_xy()?
46                    .iter()
47                    .map(|v| v.iter().map(|x| x * max / old_max).collect())
48                    .collect(),
49            )?),
50            Interpolator::Interp3D(..) => Ok(self.set_f_xyz(
51                self.f_xyz()?
52                    .iter()
53                    .map(|v0| {
54                        v0.iter()
55                            .map(|v1| v1.iter().map(|x| x * max / old_max).collect())
56                            .collect()
57                    })
58                    .collect(),
59            )?),
60            Interpolator::InterpND(..) => {
61                Ok(self.set_values(self.values()?.map(|x| x * max / old_max))?)
62            }
63        }
64    }
65
66    fn set_range(&mut self, range: f64) -> anyhow::Result<()> {
67        let old_max = self.max()?;
68        let old_range = old_max - self.min()?;
69        ensure!(old_range != 0., "Cannot modify range when min == max");
70        match self {
71            Interpolator::Interp0D(..) => unreachable!("The above `ensure` should trigger"),
72            Interpolator::Interp1D(..) => Ok(self.set_f_x(
73                self.f_x()?
74                    .iter()
75                    .map(|x| old_max + (x - old_max) * range / old_range)
76                    .collect(),
77            )?),
78            Interpolator::Interp2D(..) => Ok(self.set_f_xy(
79                self.f_xy()?
80                    .iter()
81                    .map(|v| {
82                        v.iter()
83                            .map(|x| old_max + (x - old_max) * range / old_range)
84                            .collect()
85                    })
86                    .collect(),
87            )?),
88            Interpolator::Interp3D(..) => Ok(self.set_f_xyz(
89                self.f_xyz()?
90                    .iter()
91                    .map(|v0| {
92                        v0.iter()
93                            .map(|v1| {
94                                v1.iter()
95                                    .map(|x| old_max + (x - old_max) * range / old_range)
96                                    .collect()
97                            })
98                            .collect()
99                    })
100                    .collect(),
101            )?),
102            Interpolator::InterpND(..) => Ok(self.set_values(
103                self.values()?
104                    .map(|x| old_max + (x - old_max) * range / old_range),
105            )?),
106        }
107    }
108}
109
110impl Init for Interpolator {
111    fn init(&mut self) -> Result<(), Error> {
112        Ok(self.validate().map_err(ninterp::error::Error::from)?)
113    }
114}
115impl SerdeAPI for Interpolator {
116    #[cfg(feature = "resources")]
117    const RESOURCE_PREFIX: &'static str = "interpolators";
118}