use super::*;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Sampled<'a> {
No,
Unset,
Yes {
samples: Vec<(f64, &'a OwnedArgument)>,
last_defined: &'a OwnedArgument,
},
}
impl<'a> Sampled<'a> {
pub(super) fn samples(
self,
) -> impl Iterator<Item = (f64, &'a OwnedArgument)> {
match self {
Self::Yes { samples, .. } => samples.into_iter(),
Self::No | Self::Unset => Vec::new().into_iter(),
}
}
}
pub(super) fn sampled_attr<'a>(
node: &'a Node,
name: &str,
readable: impl Fn(&OwnedArgument) -> bool,
) -> Sampled<'a> {
let Some(calls) = node.sample_calls(name) else {
return Sampled::No;
};
let mut keep_from = 0;
for (index, (_, arg)) in calls.iter().enumerate() {
if !readable(arg) {
keep_from = index + 1;
}
}
let Some((_, last_defined)) = calls[keep_from..].last() else {
return Sampled::Unset;
};
Sampled::Yes {
samples: crate::scene::latest_per_time(&calls[keep_from..]),
last_defined,
}
}
pub(super) enum Located<'a, T> {
At(&'a T),
Between(&'a T, &'a T, f64),
}
pub(super) fn locate_sample<T>(
samples: &[(f64, T)],
time: f64,
) -> Option<Located<'_, T>> {
let time = time + 0.0;
if time.is_nan() {
return None;
}
let first = samples.first()?;
let last = samples.last()?;
let at =
samples.partition_point(|(t, _)| t.total_cmp(&time) == Ordering::Less);
if let Some((t, value)) = samples.get(at)
&& t.total_cmp(&time) == Ordering::Equal
{
return Some(Located::At(value));
}
if at == 0 {
return Some(Located::At(&first.1));
}
if at == samples.len() {
return Some(Located::At(&last.1));
}
let (from_time, from) = &samples[at - 1];
let (to_time, to) = &samples[at];
let alpha = (time - from_time) / (to_time - from_time);
Some(Located::Between(from, to, alpha))
}
pub(super) enum Local {
Constant(Option<[f64; 16]>),
Sampled(Vec<(f64, [f64; 16])>),
}
pub(super) fn local_at(
handle: &str,
local: &Local,
time: f64,
) -> Result<Option<[f64; 16]>, ResolveError> {
let samples = match local {
Local::Constant(matrix) => return Ok(*matrix),
Local::Sampled(samples) => samples,
};
match locate_sample(samples, time) {
Some(Located::At(matrix)) => Ok(Some(*matrix)),
Some(Located::Between(from, to, alpha)) => {
let mut out = [0.0f64; 16];
for (index, slot) in out.iter_mut().enumerate() {
*slot = from[index] * (1.0 - alpha) + to[index] * alpha;
}
Ok(Some(out))
}
None => Err(ResolveError::MissingSampleAtTime {
handle: handle.to_string(),
time,
available: samples.iter().map(|(t, _)| *t).collect(),
}),
}
}
pub(super) fn interpolate_resolved(
resolved: &[(&str, Local)],
time: f64,
) -> Result<[f64; 16], ResolveError> {
resolved
.iter()
.try_fold(IDENTITY, |matrix, (handle, local)| {
Ok(match local_at(handle, local, time)? {
Some(local) => mul(matrix, local),
None => matrix,
})
})
}
pub(super) fn matrices_of(arg: &OwnedArgument) -> Option<&[f64]> {
arg.as_matrices()
}
pub(super) fn matrix_of(arg: &OwnedArgument) -> Option<[f64; 16]> {
arg.as_matrix()
}
impl Scene {
pub fn motion_times(&self, handle: &str) -> Result<Vec<f64>, ResolveError> {
let chain = self.transform_chain(handle)?;
Ok(self.motion_times_along(&chain))
}
pub fn motion_times_along(&self, path: &[String]) -> Vec<f64> {
let mut times = path
.iter()
.filter_map(|node| self.node(node))
.flat_map(|node| {
sampled_attr(node, TRANSFORMATION_MATRIX, |arg| {
matrix_of(arg).is_some()
})
.samples()
})
.map(|(time, _)| time)
.collect::<Vec<_>>();
times.sort_by(f64::total_cmp);
times.dedup_by(|a, b| a.total_cmp(b) == Ordering::Equal);
times
}
pub fn attribute_times(
&self,
handle: &str,
name: &str,
) -> Result<Vec<f64>, ResolveError> {
let Some(node) = self.existing_node(handle)? else {
return Ok(Vec::new());
};
Ok(node
.sample_calls(name)
.map(|calls| {
crate::scene::latest_per_time(calls)
.into_iter()
.map(|(time, _)| time)
.collect()
})
.unwrap_or_default())
}
pub fn attribute_samples(
&self,
handle: &str,
name: &str,
) -> Result<Vec<(f64, &OwnedArgument)>, ResolveError> {
let Some(node) = self.existing_node(handle)? else {
return Ok(Vec::new());
};
Ok(node
.sample_calls(name)
.map(crate::scene::latest_per_time)
.unwrap_or_default())
}
pub fn sampled_attribute(
&self,
handle: &str,
name: &str,
readable: impl Fn(&OwnedArgument) -> bool,
) -> Result<Sampled<'_>, ResolveError> {
let Some(node) = self.existing_node(handle)? else {
return Ok(Sampled::No);
};
Ok(sampled_attr(node, name, readable))
}
pub fn world_transform_interpolated_at(
&self,
handle: &str,
time: f64,
) -> Result<[f64; 16], ResolveError> {
let chain = self.transform_chain(handle)?;
self.interpolate_along(&chain, time)
}
pub(super) fn interpolate_along(
&self,
path: &[String],
time: f64,
) -> Result<[f64; 16], ResolveError> {
let resolved: Vec<(&str, Local)> = path
.iter()
.map(|node| (node.as_str(), self.local_resolved(node)))
.collect();
interpolate_resolved(&resolved, time)
}
pub fn world_transform_at(
&self,
handle: &str,
time: f64,
) -> Result<[f64; 16], ResolveError> {
let chain = self.transform_chain(handle)?;
chain.iter().try_fold(IDENTITY, |matrix, node| {
Ok(match self.local_transform_at(node, time)? {
Some(local) => mul(matrix, local),
None => matrix,
})
})
}
pub fn world_transform_samples(
&self,
handle: &str,
) -> Result<Vec<(f64, [f64; 16])>, ResolveError> {
let chain = self.transform_chain(handle)?;
let resolved: Vec<(&str, Local)> = chain
.iter()
.map(|node| (node.as_str(), self.local_resolved(node)))
.collect();
self.motion_times_along(&chain)
.into_iter()
.map(|time| Ok((time, interpolate_resolved(&resolved, time)?)))
.collect()
}
pub(super) fn has_motion_transform(&self, handle: &str) -> bool {
self.node(handle).is_some_and(|node| {
sampled_attr(node, TRANSFORMATION_MATRIX, |arg| {
matrix_of(arg).is_some()
})
.samples()
.next()
.is_some()
})
}
pub(super) fn local_transform(&self, handle: &str) -> Option<[f64; 16]> {
let node = self.node(handle)?;
matrix_of(node.attribute(TRANSFORMATION_MATRIX)?)
}
pub(super) fn local_resolved(&self, handle: &str) -> Local {
let Some(node) = self.node(handle) else {
return Local::Constant(None);
};
let found = sampled_attr(node, TRANSFORMATION_MATRIX, |arg| {
matrix_of(arg).is_some()
});
if matches!(found, Sampled::No | Sampled::Unset) {
return Local::Constant(self.local_transform(handle));
}
Local::Sampled(
found
.samples()
.filter_map(|(t, arg)| matrix_of(arg).map(|m| (t, m)))
.collect(),
)
}
pub(super) fn local_transform_at(
&self,
handle: &str,
time: f64,
) -> Result<Option<[f64; 16]>, ResolveError> {
let time = time + 0.0;
let Some(node) = self.node(handle) else {
return Ok(None);
};
let found = sampled_attr(node, TRANSFORMATION_MATRIX, |arg| {
matrix_of(arg).is_some()
});
if matches!(found, Sampled::No | Sampled::Unset) {
return Ok(self.local_transform(handle));
}
match found
.clone()
.samples()
.find(|(t, _)| t.total_cmp(&time) == Ordering::Equal)
{
Some((_, arg)) => Ok(matrix_of(arg)),
None => Err(ResolveError::MissingSampleAtTime {
handle: handle.to_string(),
time,
available: found.samples().map(|(t, _)| t).collect(),
}),
}
}
}