use std::collections::HashMap;
use serde_json::{Map, Value, json};
use crate::document::{self, Accessor, Builder, Limits, Part, Sampler};
use crate::num::f32_of;
use crate::quat::normalize;
pub(crate) const TIME_OFFSET: &str = "timeOffset";
pub(crate) const ASSUMED_FRAME_RATE: f64 = 30.0;
pub(crate) fn clip_name(path: &std::path::Path) -> Option<String> {
path.file_stem()
.map(|s| s.to_string_lossy().into_owned())
.filter(|s| !s.is_empty())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Path {
Translation,
Rotation,
Scale,
Weights,
}
impl Path {
pub fn name(self) -> &'static str {
match self {
Path::Translation => "translation",
Path::Rotation => "rotation",
Path::Scale => "scale",
Path::Weights => "weights",
}
}
pub fn of(name: &str) -> Option<Path> {
Some(match name {
"translation" => Path::Translation,
"rotation" => Path::Rotation,
"scale" => Path::Scale,
"weights" => Path::Weights,
_ => return None,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Interp {
Step,
Linear,
Cubic,
}
#[derive(Debug, Clone)]
pub(crate) enum Times {
Single(Vec<f32>),
Double(Vec<f64>),
}
impl Times {
fn len(&self) -> usize {
match self {
Times::Single(v) => v.len(),
Times::Double(v) => v.len(),
}
}
fn first(&self) -> Option<f64> {
let least = |a: Option<f64>, t: f64| match a {
_ if !t.is_finite() => a,
Some(a) if a <= t => Some(a),
_ => Some(t),
};
match self {
Times::Single(v) => v.iter().map(|&t| f64::from(t)).fold(None, least),
Times::Double(v) => v.iter().copied().fold(None, least),
}
}
fn from(self, offset: f64) -> Vec<f32> {
#[allow(clippy::float_cmp, reason = "exactly zero: nothing to take off")]
match self {
Times::Single(v) if offset == 0.0 => v,
Times::Single(v) => v.into_iter().map(|t| f32_of(f64::from(t) - offset)).collect(),
Times::Double(v) => v.into_iter().map(|t| f32_of(t - offset)).collect(),
}
}
}
impl From<Vec<f32>> for Times {
fn from(v: Vec<f32>) -> Self {
Times::Single(v)
}
}
impl From<Vec<f64>> for Times {
fn from(v: Vec<f64>) -> Self {
Times::Double(v)
}
}
#[derive(Debug, Clone)]
pub(crate) struct Track {
pub node: usize,
pub also: Vec<usize>,
pub path: Path,
pub interp: Interp,
pub times: Times,
pub values: Vec<f32>,
pub width: usize,
}
impl Track {
pub fn linear(node: usize, path: Path, times: impl Into<Times>, values: Vec<f32>) -> Track {
Track {
node,
also: Vec::new(),
path,
interp: Interp::Linear,
times: times.into(),
values,
width: if path == Path::Rotation { 4 } else { 3 },
}
}
}
pub(crate) struct Writer {
extras: bool,
inputs: HashMap<u64, Vec<(Vec<f32>, usize)>>,
data: Vec<f32>,
placed: Vec<usize>,
}
impl Writer {
pub fn new(extras: bool) -> Writer {
Writer {
extras,
inputs: HashMap::new(),
data: Vec::new(),
placed: Vec::new(),
}
}
fn place(&mut self, b: &mut Builder, v: &[f32], count: usize, kind: &'static str) -> usize {
b.accessors.push(Accessor {
byte_offset: Some(self.data.len() as u64 * 4),
component_type: 5126,
count,
kind,
..Accessor::default()
});
self.data.extend_from_slice(v);
self.placed.push(b.accessors.len() - 1);
b.accessors.len() - 1
}
pub fn animation(
&mut self,
b: &mut Builder,
name: Option<String>,
extras: Option<Value>,
tracks: Vec<Track>,
) -> Option<f64> {
let (offset, single) = start(tracks.iter().map(|t| &t.times));
let mut channels = Vec::new();
let mut samplers = Vec::new();
for mut t in tracks {
let (node, path, interp) = (t.node, t.path, t.interp);
let also = std::mem::take(&mut t.also);
let width = t.width.max(1);
let Some((times, values)) = clean(t, offset) else {
continue;
};
let count = times.len();
let hash = times.iter().fold(times.len() as u64, |h, t| {
(h.rotate_left(5) ^ u64::from(t.to_bits())).wrapping_mul(0x9e37_79b9_7f4a_7c15)
});
let found = self.inputs.get(&hash).and_then(|list| {
list.iter()
.find(|(t, _)| t.iter().map(|x| x.to_bits()).eq(times.iter().map(|x| x.to_bits())))
.map(|&(_, i)| i)
});
let input = if let Some(i) = found {
i
} else {
let i = self.place(b, ×, count, "SCALAR");
b.accessors[i].min = Some(Limits::floats(×[..1]));
b.accessors[i].max = Some(Limits::floats(×[count - 1..]));
self.inputs.entry(hash).or_default().push((times, i));
i
};
let per = if interp == Interp::Cubic { 3 } else { 1 };
let output = match path {
Path::Weights => self.place(b, &values, count * per * width, "SCALAR"),
Path::Rotation => self.place(b, &values, count * per, "VEC4"),
Path::Translation | Path::Scale => self.place(b, &values, count * per, "VEC3"),
};
for node in std::iter::once(node).chain(also) {
channels.push(document::Channel {
sampler: samplers.len(),
node,
path: path.name(),
});
}
samplers.push(Sampler {
input,
output,
interpolation: match interp {
Interp::Step => "STEP",
Interp::Linear => "LINEAR",
Interp::Cubic => "CUBICSPLINE",
},
});
}
if channels.is_empty() {
return None;
}
let mut extras = extras.filter(|_| self.extras);
if self.extras {
note_offset(&mut extras, offset, single);
}
b.animations.push(document::Animation {
channels,
samplers,
name: name.filter(|n| !n.is_empty()),
extras,
});
Some(offset)
}
pub fn finish(self, b: &mut Builder) {
if !self.data.is_empty() {
let view = b.view(Part::f32(self.data), None);
for a in self.placed {
b.accessors[a].buffer_view = Some(view);
}
}
}
}
fn start<'t>(times: impl IntoIterator<Item = &'t Times>) -> (f64, bool) {
times
.into_iter()
.filter_map(|t| Some((t.first()?, matches!(t, Times::Single(_)))))
.fold(None, |a: Option<(f64, bool)>, t| match a {
Some(a) if a.0 <= t.0 => Some(a),
_ => Some(t),
})
.unwrap_or((0.0, false))
}
fn note_offset(extras: &mut Option<Value>, offset: f64, single: bool) {
#[allow(clippy::float_cmp, reason = "exactly zero: nothing was taken off")]
if offset == 0.0 {
return;
}
let e = extras.get_or_insert_with(|| Value::Object(Map::new()));
if let Some(o) = e.as_object_mut() {
let noted = if single {
f32_of(offset).to_string().parse().unwrap_or(offset)
} else {
offset
};
o.insert(TIME_OFFSET.into(), json!(noted));
}
}
#[cfg(feature = "gltf")]
enum Kept {
Same,
New(Vec<f32>, Vec<f32>, usize),
Gone,
}
#[cfg(feature = "gltf")]
type Rewritten = Option<Option<(usize, usize)>>;
#[cfg(feature = "gltf")]
pub(crate) fn conform(doc: &mut document::Document, extras: bool) {
let animations = doc
.json()
.get("animations")
.and_then(Value::as_array)
.map_or(0, Vec::len);
let mut plans: Vec<Option<(Vec<Kept>, f64)>> = Vec::with_capacity(animations);
for a in 0..animations {
plans.push(plan(doc, a));
}
if plans.iter().flatten().all(|(kept, offset)| {
#[allow(clippy::float_cmp, reason = "exactly zero: nothing to take off")]
let at_zero = *offset == 0.0;
at_zero && kept.iter().all(|k| matches!(k, Kept::Same))
}) {
return;
}
let mut added = document::Added::new(doc);
let mut inputs: HashMap<Vec<u32>, usize> = HashMap::new();
let mut changed: Vec<(usize, Vec<Rewritten>, f64)> = Vec::new();
for (a, plan) in plans.into_iter().enumerate() {
let Some((kept, offset)) = plan else { continue };
let mut at = Vec::with_capacity(kept.len());
for k in kept {
at.push(match k {
Kept::Same => Some(None),
Kept::Gone => None,
Kept::New(times, values, n) => {
let key: Vec<u32> = times.iter().map(|t| t.to_bits()).collect();
let input = *inputs.entry(key).or_insert_with(|| {
let bounds = (vec![times[0]], vec![times[times.len() - 1]]);
added.floats(×, "SCALAR", Some(bounds))
});
let kind = match n {
3 => "VEC3",
4 => "VEC4",
_ => "SCALAR",
};
Some(Some((input, added.floats(&values, kind, None))))
}
});
}
changed.push((a, at, offset));
}
added.finish(doc);
let json = doc.json_mut();
let Some(Value::Array(list)) = json.get_mut("animations") else {
return;
};
let mut emptied = Vec::new();
for (a, at, offset) in changed {
let Some(anim) = list.get_mut(a) else { continue };
let mut renumber: Vec<Option<usize>> = Vec::with_capacity(at.len());
let mut samplers = Vec::new();
let old = anim
.get("samplers")
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
for (mut sampler, at) in old.into_iter().zip(at) {
let Some(new) = at else {
renumber.push(None);
continue;
};
if let Some((input, output)) = new {
sampler["input"] = json!(input);
sampler["output"] = json!(output);
}
renumber.push(Some(samplers.len()));
samplers.push(sampler);
}
let channels: Vec<Value> = anim
.get("channels")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|c| {
let s = c.get("sampler")?.as_u64().and_then(|s| usize::try_from(s).ok())?;
let to = (*renumber.get(s)?)?;
let mut c = c.clone();
c["sampler"] = json!(to);
Some(c)
})
.collect();
if channels.is_empty() {
emptied.push(a);
continue;
}
anim["samplers"] = Value::Array(samplers);
anim["channels"] = Value::Array(channels);
if extras {
let mut e = anim.get("extras").filter(|e| e.is_object()).cloned();
note_offset(&mut e, offset, true);
if let Some(e) = e {
anim["extras"] = e;
}
}
}
for a in emptied.into_iter().rev() {
list.remove(a);
}
if list.is_empty() {
json.remove("animations");
}
}
#[cfg(feature = "gltf")]
fn plan(doc: &document::Document, a: usize) -> Option<(Vec<Kept>, f64)> {
let anim = doc.json().get("animations")?.get(a)?;
let samplers = anim
.get("samplers")
.and_then(Value::as_array)
.map_or(&[][..], Vec::as_slice);
let path_of = |s: usize| {
anim.get("channels")
.and_then(Value::as_array)
.into_iter()
.flatten()
.find(|c| c.get("sampler").and_then(Value::as_u64) == Some(s as u64))
.and_then(|c| c.pointer("/target/path")?.as_str())
.and_then(Path::of)
};
let index = |s: &Value, k: &str| s.get(k)?.as_u64().and_then(|i| usize::try_from(i).ok());
let mut tracks = Vec::with_capacity(samplers.len());
for (i, s) in samplers.iter().enumerate() {
let (_, times) = doc.read_floats(index(s, "input")?).ok()?;
let (n, values) = doc.read_floats(index(s, "output")?).ok()?;
let interp = match s.get("interpolation").and_then(Value::as_str) {
Some("STEP") => Interp::Step,
Some("CUBICSPLINE") => Interp::Cubic,
_ => Interp::Linear,
};
let per = if interp == Interp::Cubic { 3 } else { 1 };
let path = path_of(i).unwrap_or(Path::Weights);
let width = match path {
Path::Rotation => 4,
Path::Translation | Path::Scale => 3,
Path::Weights => (values.len() / (times.len() * per).max(1)).max(1),
};
let track = Track {
node: 0,
also: Vec::new(),
path,
interp,
times: Times::Single(times),
values,
width,
};
tracks.push((track, n));
}
let (offset, _) = start(tracks.iter().map(|(t, _)| &t.times));
let kept = tracks
.into_iter()
.map(|(t, n)| {
let Times::Single(was) = &t.times else {
unreachable!("read as single precision")
};
let (was_t, was_v) = (was.clone(), t.values.clone());
match clean(t, offset) {
None => Kept::Gone,
#[allow(clippy::float_cmp, reason = "exactly zero: nothing was taken off")]
Some((times, values))
if offset == 0.0
&& times.iter().map(|x| x.to_bits()).eq(was_t.iter().map(|x| x.to_bits()))
&& values.iter().map(|x| x.to_bits()).eq(was_v.iter().map(|x| x.to_bits())) =>
{
Kept::Same
}
Some((times, values)) => Kept::New(times, values, n),
}
})
.collect();
Some((kept, offset))
}
fn clean(t: Track, offset: f64) -> Option<(Vec<f32>, Vec<f32>)> {
let Track {
times,
mut values,
path,
interp,
width,
..
} = t;
let per = if interp == Interp::Cubic { 3 } else { 1 };
let stride = width.max(1) * per;
let keys = times.len().min(values.len() / stride);
let mut times = times.from(offset);
times.truncate(keys);
values.truncate(keys * stride);
let tidy = times.iter().all(|t| t.is_finite())
&& times.windows(2).all(|w| w[0] < w[1])
&& values.iter().all(|x| x.is_finite());
if !tidy {
let mut kept: Vec<f32> = Vec::with_capacity(keys);
let mut out = Vec::with_capacity(keys * stride);
for (&t, v) in times.iter().zip(values.chunks_exact(stride)) {
if !t.is_finite() || v.iter().any(|x| !x.is_finite()) {
continue;
}
kept.push(match kept.last() {
Some(&p) if t <= p => p.next_up(),
_ => t,
});
out.extend_from_slice(v);
}
times = kept;
values = out;
}
if path == Path::Rotation && width == 4 {
let mut last = [0.0f64, 0.0, 0.0, 1.0];
for key in values.chunks_exact_mut(stride) {
let at = if per == 3 { 4 } else { 0 };
let mut q: [f64; 4] = std::array::from_fn(|i| f64::from(key[at + i]));
let unit = (q.iter().map(|c| c * c).sum::<f64>() - 1.0).abs() <= 1e-7;
if !unit {
q = normalize(q).map(f64::from);
}
let dot: f64 = (0..4).map(|i| q[i] * last[i]).sum();
if dot < 0.0 {
q = q.map(|x| -x);
}
last = q;
if !unit || dot < 0.0 {
for (o, x) in key[at..at + 4].iter_mut().zip(q) {
*o = f32_of(x);
}
}
if dot < 0.0 && per == 3 {
for k in (0..4).chain(8..12) {
key[k] = -key[k];
}
}
}
}
(!times.is_empty()).then_some((times, values))
}
#[cfg(test)]
mod tests {
use super::*;
fn track(node: usize, path: Path, times: Times, values: Vec<f32>) -> Track {
Track::linear(node, path, times, values)
}
fn times(b: &Builder, a: usize) -> (f64, f64) {
let (Some(Limits::Float(lo)), Some(Limits::Float(hi))) = (&b.accessors[a].min, &b.accessors[a].max) else {
panic!("a time accessor has bounds");
};
(lo[0], hi[0])
}
#[test]
fn an_animation_starts_at_zero_as_a_whole() {
let mut b = Builder::plain();
let mut w = Writer::new(true);
let tracks = vec![
track(0, Path::Translation, vec![1.0f64, 2.0].into(), vec![0.0; 6]),
track(1, Path::Translation, vec![1.5f64, 3.0].into(), vec![0.0; 6]),
];
assert_eq!(w.animation(&mut b, Some("a".into()), None, tracks), Some(1.0));
w.finish(&mut b);
let a = &b.animations[0];
assert_eq!(times(&b, a.samplers[0].input), (0.0, 1.0));
assert_eq!(times(&b, a.samplers[1].input), (0.5, 2.0));
assert_eq!(a.extras, Some(json!({ "timeOffset": 1.0 })));
}
#[test]
fn a_negative_start_moves_forward_and_zero_leaves_no_note() {
let mut b = Builder::plain();
let mut w = Writer::new(true);
let early = vec![track(0, Path::Scale, vec![-0.5f32, 0.5].into(), vec![1.0; 6])];
assert_eq!(w.animation(&mut b, None, Some(json!({ "own": 1 })), early), Some(-0.5));
let at_zero = vec![track(0, Path::Scale, vec![0.0f32, 0.5].into(), vec![1.0; 6])];
assert_eq!(w.animation(&mut b, None, None, at_zero), Some(0.0));
assert_eq!(times(&b, b.animations[0].samplers[0].input), (0.0, 1.0));
assert_eq!(b.animations[0].extras, Some(json!({ "own": 1, "timeOffset": -0.5 })));
assert_eq!(b.animations[1].extras, None);
assert_eq!(b.animations[1].name, None);
}
#[test]
fn a_single_precision_start_is_noted_as_written() {
let mut b = Builder::plain();
let mut w = Writer::new(true);
let t = vec![track(0, Path::Scale, vec![1.2f32, 2.2].into(), vec![1.0; 6])];
w.animation(&mut b, None, None, t);
assert_eq!(b.animations[0].extras, Some(json!({ "timeOffset": 1.2 })));
assert_eq!(times(&b, b.animations[0].samplers[0].input).0, 0.0);
}
#[test]
fn extras_are_left_out_when_not_kept() {
let mut b = Builder::plain();
let mut w = Writer::new(false);
let t = vec![track(0, Path::Scale, vec![2.0f32, 3.0].into(), vec![1.0; 6])];
assert_eq!(w.animation(&mut b, None, Some(json!({ "own": 1 })), t), Some(2.0));
assert_eq!(b.animations[0].extras, None);
}
#[test]
fn keys_are_finite_rising_and_rotations_take_the_short_way() {
let t = Track::linear(
0,
Path::Rotation,
vec![0.0f32, 1.0, 1.0, f32::NAN, 2.0],
vec![
0.0,
0.0,
0.0,
2.0, 0.0,
0.0,
0.0,
-1.0, 0.0,
0.0,
1.0,
0.0, 0.0,
0.0,
0.0,
1.0, f32::NAN,
0.0,
0.0,
1.0, ],
);
let (times, values) = clean(t, 0.0).unwrap();
assert_eq!(times, [0.0, 1.0, 1.0f32.next_up()]);
assert_eq!(values, [0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0]);
let none = Track::linear(0, Path::Scale, vec![f32::NAN], vec![1.0; 3]);
assert!(clean(none, 0.0).is_none());
}
#[test]
fn a_cubic_rotation_turns_with_its_tangents() {
let t = Track {
interp: Interp::Cubic,
..Track::linear(
0,
Path::Rotation,
vec![0.0f32, 1.0],
vec![
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.5, 0.0, 0.0, 0.0, 0.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.5, 0.0, 0.0,
],
)
};
let (_, v) = clean(t, 0.0).unwrap();
assert_eq!(
&v[12..],
[-0.25, -0.0, -0.0, -0.0, 0.0, 0.0, 0.0, 1.0, -0.0, -0.5, -0.0, -0.0]
);
}
#[test]
fn tracks_with_the_same_times_share_them_and_instances_share_a_sampler() {
let mut b = Builder::plain();
let mut w = Writer::new(true);
let mut first = track(0, Path::Translation, vec![0.0f32, 1.0].into(), vec![0.0; 6]);
first.also = vec![5];
let second = track(1, Path::Scale, vec![0.0f64, 1.0].into(), vec![1.0; 6]);
w.animation(&mut b, None, None, vec![first, second]);
w.finish(&mut b);
let a = &b.animations[0];
assert_eq!(a.samplers[0].input, a.samplers[1].input);
let nodes: Vec<_> = a.channels.iter().map(|c| (c.node, c.sampler)).collect();
assert_eq!(nodes, [(0, 0), (5, 0), (1, 1)]);
assert_eq!(b.views.len(), 1);
}
}