#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Ending {
ZeroCurvature,
ZeroSlope,
WrapAround,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct InterpolantSettings {
pub ending_start: Ending,
pub ending_end: Ending,
}
impl Default for InterpolantSettings {
fn default() -> Self {
Self {
ending_start: Ending::ZeroCurvature,
ending_end: Ending::ZeroCurvature,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct InterpolantData {
pub parameter_positions: Vec<f64>,
pub result_buffer: Vec<f64>,
pub sample_values: Vec<f64>,
pub value_size: usize,
pub settings: Option<InterpolantSettings>,
}
impl InterpolantData {
pub fn copy_sample_value(&mut self, index: usize) -> &[f64] {
let stride = self.value_size;
let offset = index * stride;
for i in 0..stride {
self.result_buffer[i] = self.sample_values[offset + i];
}
&self.result_buffer
}
fn position(&self, index: isize) -> Option<f64> {
if index < 0 {
return None;
}
self.parameter_positions.get(index as usize).copied()
}
}
pub trait Interpolation {
fn default_settings(&self) -> InterpolantSettings {
InterpolantSettings::default()
}
fn interval_changed(&mut self, _data: &InterpolantData, _i1: usize, _t0: f64, _t1: f64) {}
fn interpolate(&mut self, data: &mut InterpolantData, i1: usize, t0: f64, t: f64, t1: f64);
fn copy_sample_value(&mut self, data: &mut InterpolantData, index: usize) {
data.copy_sample_value(index);
}
}
#[derive(Clone, Debug)]
pub struct Interpolant<I> {
pub data: InterpolantData,
pub interpolation: I,
cached_index: usize,
}
impl<I: Interpolation> Interpolant<I> {
pub fn new(
parameter_positions: Vec<f64>,
sample_values: Vec<f64>,
sample_size: usize,
result_buffer: Option<Vec<f64>>,
interpolation: I,
) -> Self {
let mut result_buffer = result_buffer.unwrap_or_else(|| vec![0.0; sample_size]);
if result_buffer.len() < sample_size {
result_buffer.resize(sample_size, 0.0);
}
Self {
data: InterpolantData {
parameter_positions,
result_buffer,
sample_values,
value_size: sample_size,
settings: None,
},
interpolation,
cached_index: 0,
}
}
pub fn settings(&self) -> InterpolantSettings {
self.data
.settings
.unwrap_or_else(|| self.interpolation.default_settings())
}
pub fn result_buffer(&self) -> &[f64] {
&self.data.result_buffer
}
#[doc(hidden)]
pub fn copy_sample_value(&mut self, index: usize) -> &[f64] {
self.interpolation.copy_sample_value(&mut self.data, index);
&self.data.result_buffer
}
pub fn evaluate(&mut self, t: f64) -> &[f64] {
let mut i1 = self.cached_index as isize;
let mut t1 = self.data.position(i1);
let mut t0 = self.data.position(i1 - 1);
'validate_interval: {
'seek: {
let mut right: isize;
'linear_scan: {
'forward_scan: {
if !t1.is_some_and(|t1| t < t1) {
let give_up_at = i1 + 2;
loop {
if t1.is_none() {
if t0.is_some_and(|t0| t < t0) {
break 'forward_scan;
}
i1 = self.data.parameter_positions.len() as isize;
self.cached_index = i1 as usize;
self.interpolation
.copy_sample_value(&mut self.data, (i1 - 1) as usize);
return &self.data.result_buffer;
}
if i1 == give_up_at {
break; }
t0 = t1;
i1 += 1;
t1 = self.data.position(i1);
if t1.is_some_and(|t1| t < t1) {
break 'seek;
}
}
right = self.data.parameter_positions.len() as isize;
break 'linear_scan;
}
}
if !t0.is_some_and(|t0| t >= t0) {
let t1global = self.data.position(1);
if t1global.is_some_and(|t1global| t < t1global) {
i1 = 2; t0 = t1global;
}
let give_up_at = i1 - 2;
loop {
if t0.is_none() {
self.cached_index = 0;
self.interpolation.copy_sample_value(&mut self.data, 0);
return &self.data.result_buffer;
}
if i1 == give_up_at {
break; }
t1 = t0;
i1 -= 1;
t0 = self.data.position(i1 - 1);
if t0.is_some_and(|t0| t >= t0) {
break 'seek;
}
}
right = i1;
i1 = 0;
break 'linear_scan;
}
break 'validate_interval;
}
while i1 < right {
let mid = ((i1 + right) as usize) >> 1;
if t < self.data.parameter_positions[mid] {
right = mid as isize;
} else {
i1 = mid as isize + 1;
}
}
t1 = self.data.position(i1);
t0 = self.data.position(i1 - 1);
if t0.is_none() {
self.cached_index = 0;
self.interpolation.copy_sample_value(&mut self.data, 0);
return &self.data.result_buffer;
}
if t1.is_none() {
i1 = self.data.parameter_positions.len() as isize;
self.cached_index = i1 as usize;
self.interpolation
.copy_sample_value(&mut self.data, (i1 - 1) as usize);
return &self.data.result_buffer;
}
}
self.cached_index = i1 as usize;
let (t0, t1) = (
t0.expect("three-rs: the interval search leaves t0 set"),
t1.expect("three-rs: the interval search leaves t1 set"),
);
self.interpolation
.interval_changed(&self.data, i1 as usize, t0, t1);
}
let (t0, t1) = (
t0.expect("three-rs: the interval search leaves t0 set"),
t1.expect("three-rs: the interval search leaves t1 set"),
);
self.interpolation
.interpolate(&mut self.data, i1 as usize, t0, t, t1);
&self.data.result_buffer
}
}