use std::collections::HashMap;
use serde_json::{Map, Value, json};
use super::Input;
use super::scene::{self, Animation, Channel, Interp, Node, Path, Scene};
use crate::Result;
use crate::assets::Assets;
use crate::document::{Document, Keep};
use crate::num::f32_of;
use crate::source::{Axis, Fact};
const MAX_BONES: usize = 32_768;
const MAX_DOFS: usize = 64;
const DEFAULT_RATE: f64 = 120.0;
const INCH: f64 = 0.0254;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Dof {
Translate(usize),
Rotate(usize),
Length,
Other,
}
impl Dof {
fn parse(name: &str) -> Self {
let lower = name.to_ascii_lowercase();
let axis = |c: u8| match c {
b'x' => Some(0),
b'y' => Some(1),
b'z' => Some(2),
_ => None,
};
match lower.as_bytes() {
[b't', c] => axis(*c).map_or(Self::Other, Self::Translate),
[b'r', c] => axis(*c).map_or(Self::Other, Self::Rotate),
[b'l'] => Self::Length,
_ => Self::Other,
}
}
}
#[derive(Debug, Clone)]
struct Bone {
name: String,
id: Option<String>,
direction: [f64; 3],
length: f64,
axis: [f64; 3],
order: Vec<usize>,
dof_names: Vec<String>,
dofs: Vec<Dof>,
limits: Vec<[Option<f64>; 2]>,
}
impl Bone {
fn new(name: String) -> Self {
Self {
name,
id: None,
direction: [0.0; 3],
length: 0.0,
axis: [0.0; 3],
order: vec![0, 1, 2],
dof_names: Vec::new(),
dofs: Vec::new(),
limits: Vec::new(),
}
}
fn frame(&self, radians: f64) -> [f64; 4] {
let mut q = [0.0, 0.0, 0.0, 1.0];
for &a in &self.order {
q = scene::quat_mul(scene::about(a, self.axis[a] * radians), q);
}
q
}
}
#[derive(Debug)]
struct Skeleton {
name: Option<String>,
version: Option<String>,
length: f64,
length_given: bool,
mass: Option<f64>,
radians: bool,
root: Bone,
position: [f64; 3],
bones: Vec<Bone>,
hierarchy: Vec<(String, Vec<String>)>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Section {
None,
Units,
Root,
Bones,
Hierarchy,
Other,
}
fn show(s: &str) -> String {
s.chars().take(40).collect()
}
fn number(tok: Option<&str>, what: &str, no: usize) -> Result<f64> {
match tok.and_then(|t| t.parse::<f64>().ok()).filter(|v| v.is_finite()) {
Some(v) => Ok(v),
None => bail!(
Corrupt,
"ASF line {no}: {what} needs a number, not `{}`",
show(tok.unwrap_or("the end of the line"))
),
}
}
fn three<'a>(tokens: &mut impl Iterator<Item = &'a str>, what: &str, no: usize) -> Result<[f64; 3]> {
Ok([
number(tokens.next(), what, no)?,
number(tokens.next(), what, no)?,
number(tokens.next(), what, no)?,
])
}
fn axis_order(s: &str) -> Option<Vec<usize>> {
s.bytes()
.map(|c| match c.to_ascii_lowercase() {
b'x' => Some(0),
b'y' => Some(1),
b'z' => Some(2),
_ => None,
})
.collect()
}
fn uncomment(line: &str) -> &str {
line.split('#').next().unwrap_or("")
}
fn parse_asf(text: &str) -> Result<Skeleton> {
let mut sk = Skeleton {
name: None,
version: None,
length: 1.0,
length_given: false,
mass: None,
radians: false,
root: Bone::new("root".into()),
position: [0.0; 3],
bones: Vec::new(),
hierarchy: Vec::new(),
};
sk.root.dofs.clear();
let mut section = Section::None;
let mut seen = false;
let mut bone: Option<Bone> = None;
let mut in_limits = false;
let mut names: HashMap<String, usize> = HashMap::new();
for (k, raw) in text.lines().enumerate() {
let no = k + 1;
let line = uncomment(raw).trim();
let mut tokens = line.split_whitespace();
let Some(first) = tokens.next() else { continue };
if let Some(key) = first.strip_prefix(':') {
in_limits = false;
ensure!(
bone.is_none(),
Corrupt,
"ASF line {no}: `{}` inside a bone's begin … end",
show(first)
);
let rest = line[first.len()..].trim();
section = match key.to_ascii_lowercase().as_str() {
"version" => {
sk.version = Some(rest.to_owned());
Section::Other
}
"name" => {
sk.name = Some(rest.to_owned());
Section::Other
}
"units" => Section::Units,
"root" => Section::Root,
"bonedata" => Section::Bones,
"hierarchy" => Section::Hierarchy,
_ => Section::Other,
};
seen = true;
continue;
}
let key = first.to_ascii_lowercase();
match section {
Section::None => bail!(
UnknownFormat,
"not an ASF file: it starts with `{}`, not a `:` section",
show(first)
),
Section::Other => {}
Section::Units => match key.as_str() {
"length" => {
let v = number(tokens.next(), "length", no)?;
ensure!(
v.is_finite() && v > 0.0,
Corrupt,
"ASF line {no}: the length unit must be above 0, not {v}"
);
sk.length = v;
sk.length_given = true;
}
"mass" => sk.mass = Some(number(tokens.next(), "mass", no)?),
"angle" => {
let unit = tokens.next().unwrap_or("").to_ascii_lowercase();
sk.radians = match unit.as_str() {
"deg" | "degree" | "degrees" => false,
"rad" | "radian" | "radians" => true,
_ => bail!(
Corrupt,
"ASF line {no}: the angle unit `{}` isn't deg or rad",
show(&unit)
),
};
}
_ => {}
},
Section::Root => match key.as_str() {
"order" => {
sk.root.dof_names = tokens.map(str::to_owned).collect();
ensure!(
sk.root.dof_names.len() <= MAX_DOFS,
LimitExceeded,
"ASF line {no}: the root's order lists more than {MAX_DOFS} columns"
);
sk.root.dofs = sk.root.dof_names.iter().map(|d| Dof::parse(d)).collect();
}
"axis" => {
let order = tokens.next().unwrap_or("");
let Some(order) = axis_order(order).filter(|o| !o.is_empty()) else {
bail!(
Corrupt,
"ASF line {no}: the root's axis `{}` isn't an order of X, Y and Z",
show(order)
);
};
sk.root.order = order;
}
"position" => sk.position = three(&mut tokens, "position", no)?,
"orientation" => sk.root.axis = three(&mut tokens, "orientation", no)?,
_ => {}
},
Section::Bones => {
if key == "begin" {
ensure!(
bone.is_none(),
Corrupt,
"ASF line {no}: `begin` inside a bone's begin … end"
);
ensure!(
sk.bones.len() < MAX_BONES,
LimitExceeded,
"ASF has more than {MAX_BONES} bones"
);
bone = Some(Bone::new(String::new()));
in_limits = false;
continue;
}
let Some(b) = bone.as_mut() else {
bail!(Corrupt, "ASF line {no}: `{}` outside a bone's begin … end", show(first));
};
if key == "end" {
let mut b = bone.take().unwrap_or_else(|| Bone::new(String::new()));
if b.name.is_empty() {
b.name = format!("bone{}", sk.bones.len() + 1);
}
ensure!(
!b.name.eq_ignore_ascii_case("root") && !names.contains_key(&b.name),
Corrupt,
"ASF line {no}: bone `{}` is defined twice",
show(&b.name)
);
names.insert(b.name.clone(), sk.bones.len());
sk.bones.push(b);
in_limits = false;
continue;
}
let continues = in_limits && first.starts_with('(');
in_limits = false;
if continues || key == "limits" || key.starts_with("limits(") {
let body = if continues { line } else { &line[6..] };
let cleaned = body.replace(['(', ')'], " ");
for pair in cleaned.split_whitespace().collect::<Vec<_>>().chunks(2) {
let bound = |t: &str| match t.parse::<f64>() {
Ok(v) if v.is_finite() => Ok(Some(v)),
Ok(v) if v.is_infinite() => Ok(None),
_ => Err(err!(
Corrupt,
"ASF line {no}: a limit needs a number, not `{}`",
show(t)
)),
};
let [lo, hi] = [pair[0], pair.get(1).copied().unwrap_or("")];
ensure!(
b.limits.len() < MAX_DOFS,
LimitExceeded,
"ASF line {no}: bone `{}` has more than {MAX_DOFS} limits",
show(&b.name)
);
b.limits.push([bound(lo)?, bound(hi)?]);
}
in_limits = true;
continue;
}
match key.as_str() {
"id" => b.id = tokens.next().map(str::to_owned),
"name" => {
let name = line[first.len()..].trim();
ensure!(!name.is_empty(), Corrupt, "ASF line {no}: a bone's name is empty");
name.clone_into(&mut b.name);
}
"direction" => {
let d = three(&mut tokens, "direction", no)?;
let len = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
b.direction = if len > 0.0 { d.map(|c| c / len) } else { [0.0; 3] };
}
"length" => b.length = number(tokens.next(), "length", no)?,
"axis" => {
b.axis = three(&mut tokens, "axis", no)?;
if let Some(o) = tokens.next() {
let Some(order) = axis_order(o).filter(|o| !o.is_empty()) else {
bail!(
Corrupt,
"ASF line {no}: axis order `{}` isn't an order of X, Y and Z",
show(o)
);
};
b.order = order;
}
}
"dof" => {
b.dof_names = tokens.map(str::to_owned).collect();
ensure!(
b.dof_names.len() <= MAX_DOFS,
LimitExceeded,
"ASF line {no}: bone `{}` lists more than {MAX_DOFS} dofs",
show(&b.name)
);
b.dofs = b.dof_names.iter().map(|d| Dof::parse(d)).collect();
}
_ => {}
}
}
Section::Hierarchy => {
if key == "begin" || key == "end" {
continue;
}
sk.hierarchy
.push((first.to_owned(), tokens.map(str::to_owned).collect()));
}
}
}
ensure!(seen, UnknownFormat, "not an ASF file: it has no `:` sections");
if let Some(b) = bone {
bail!(
Truncated,
"ASF file is cut short inside bone `{}`, or its `end` is missing",
show(&b.name)
);
}
Ok(sk)
}
fn order_name(order: &[usize]) -> String {
order.iter().map(|&a| ["X", "Y", "Z"][a]).collect()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Parent {
Root,
Bone(usize),
}
struct Rig {
nodes: Vec<usize>,
root: usize,
unattached: usize,
}
fn build(sk: &Skeleton, scene: &mut Scene) -> Rig {
let index: HashMap<&str, usize> = sk.bones.iter().enumerate().map(|(i, b)| (b.name.as_str(), i)).collect();
let n = sk.bones.len();
let mut parent: Vec<Option<Parent>> = vec![None; n];
let mut children: Vec<Vec<usize>> = vec![Vec::new(); n];
let mut root_children = Vec::new();
for (p, kids) in &sk.hierarchy {
let from = if p.eq_ignore_ascii_case("root") {
Some(Parent::Root)
} else {
index.get(p.as_str()).map(|&i| Parent::Bone(i))
};
let Some(from) = from else { continue };
for kid in kids {
let Some(&c) = index.get(kid.as_str()) else { continue };
if parent[c].is_some() || from == Parent::Bone(c) {
continue;
}
parent[c] = Some(from);
match from {
Parent::Root => root_children.push(c),
Parent::Bone(b) => children[b].push(c),
}
}
}
let mut root_node = Node::named("root", None);
root_node.translation = sk.position.map(f32_of);
root_node.extras = Some(json!({
"order": sk.root.dof_names,
"axis": order_name(&sk.root.order),
"orientation": sk.root.axis,
}));
let root = scene.add(root_node);
let mut nodes = vec![usize::MAX; n];
let mut unattached = 0;
let mut stack: Vec<(usize, usize, [f64; 3])> = root_children.iter().rev().map(|&c| (c, root, [0.0; 3])).collect();
let mut loose = 0;
loop {
while let Some((b, under, at)) = stack.pop() {
if nodes[b] != usize::MAX {
continue;
}
let bone = &sk.bones[b];
let mut node = Node::named(bone.name.clone(), Some(under));
node.translation = at.map(f32_of);
let mut extras = Map::new();
if let Some(id) = &bone.id {
extras.insert("id".into(), json!(id));
}
extras.insert("dof".into(), json!(bone.dof_names));
extras.insert("axis".into(), json!(bone.axis));
extras.insert("axisOrder".into(), json!(order_name(&bone.order)));
if !bone.limits.is_empty() {
extras.insert("limits".into(), json!(bone.limits));
}
node.extras = Some(Value::Object(extras));
let me = scene.add(node);
nodes[b] = me;
let tip = bone.direction.map(|c| c * bone.length);
if children[b].is_empty() {
let mut end = Node::named(format!("{}_end", bone.name), Some(me));
end.translation = tip.map(f32_of);
scene.add(end);
}
for &c in children[b].iter().rev() {
stack.push((c, me, tip));
}
}
while loose < n && nodes[loose] != usize::MAX {
loose += 1;
}
if loose == n {
break;
}
unattached += 1;
stack.push((loose, root, [0.0; 3]));
}
Rig {
nodes,
root,
unattached,
}
}
#[derive(Default)]
struct Track {
frames: Vec<usize>,
rotations: Vec<f32>,
translations: Vec<f32>,
last: Option<usize>,
}
#[derive(Default)]
struct Motion {
frames: usize,
frame_time: f64,
rate_given: bool,
dropped: usize,
unknown: usize,
}
fn rotation(dofs: &[Dof], values: &[f64], radians: f64) -> [f64; 4] {
let mut q = [0.0, 0.0, 0.0, 1.0];
for (d, v) in dofs.iter().zip(values) {
if let Dof::Rotate(a) = *d {
q = scene::quat_mul(scene::about(a, v * radians), q);
}
}
q
}
fn conjugate(q: [f64; 4]) -> [f64; 4] {
[-q[0], -q[1], -q[2], q[3]]
}
fn parse_amc(text: &str, sk: &Skeleton, rig: &Rig, scene: &Scene) -> (Option<Animation>, Motion) {
let mut motion = Motion {
frame_time: 1.0 / DEFAULT_RATE,
..Motion::default()
};
let mut by_name: HashMap<&str, usize> = sk.bones.iter().enumerate().map(|(i, b)| (b.name.as_str(), i)).collect();
let root = sk.bones.len();
by_name.insert("root", root);
let lower: HashMap<String, usize> = by_name.iter().map(|(k, &v)| (k.to_ascii_lowercase(), v)).collect();
let mut tracks: Vec<Track> = std::iter::repeat_with(Track::default).take(root + 1).collect();
let mut angle = if sk.radians { 1.0 } else { std::f64::consts::PI / 180.0 };
let skeleton_angle = if sk.radians { 1.0 } else { std::f64::consts::PI / 180.0 };
let mut values: Vec<f64> = Vec::new();
let mut frame: Option<usize> = None;
for raw in text.lines() {
let line = raw.trim().trim_start_matches('\u{feff}');
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Some(key) = line.strip_prefix(':') {
let mut parts = key.split_whitespace();
match parts.next().unwrap_or("").to_ascii_uppercase().as_str() {
"DEGREES" => angle = std::f64::consts::PI / 180.0,
"RADIANS" => angle = 1.0,
"SAMPLES-PER-SECOND" => {
if let Some(rate) = parts
.next()
.and_then(|t| t.parse::<f64>().ok())
.filter(|r| r.is_finite() && *r > 0.0)
{
motion.frame_time = 1.0 / rate;
motion.rate_given = true;
}
}
_ => {}
}
continue;
}
let mut tokens = line.split_whitespace();
let Some(name) = tokens.next() else { continue };
if tokens.clone().next().is_none() && name.bytes().all(|c| c.is_ascii_digit()) {
frame = Some(motion.frames);
motion.frames += 1;
continue;
}
let Some(f) = frame else {
motion.dropped += 1;
continue;
};
let Some(&b) = by_name.get(name).or_else(|| lower.get(&name.to_ascii_lowercase())) else {
motion.unknown += 1;
continue;
};
let bone = if b == root { &sk.root } else { &sk.bones[b] };
values.clear();
for t in tokens.by_ref().take(bone.dofs.len()) {
match t.parse::<f64>() {
Ok(v) if v.is_finite() => values.push(v),
_ => break,
}
}
if values.len() < bone.dofs.len() {
motion.dropped += 1;
continue;
}
let track = &mut tracks[b];
if track.last == Some(f) {
track.frames.pop();
let r = track.rotations.len().saturating_sub(4);
track.rotations.truncate(r);
let t = track.translations.len().saturating_sub(3);
track.translations.truncate(t);
}
track.last = Some(f);
track.frames.push(f);
let c = bone.frame(skeleton_angle);
let m = rotation(&bone.dofs, &values, angle);
let q = scene::quat_mul(scene::quat_mul(c, m), conjugate(c));
if bone.dofs.iter().any(|d| matches!(d, Dof::Rotate(_))) {
track.rotations.extend(scene::normalize(q));
}
if bone.dofs.iter().any(|d| matches!(d, Dof::Translate(_))) {
let node = if b == root { rig.root } else { rig.nodes[b] };
let rest = scene.nodes[node].translation.map(f64::from);
let mut t = if b == root { rest } else { [0.0; 3] };
for (d, v) in bone.dofs.iter().zip(&values) {
if let Dof::Translate(a) = *d {
t[a] = *v;
}
}
let at = if b == root {
t
} else {
let along = scene::rotate(c, t);
[rest[0] + along[0], rest[1] + along[1], rest[2] + along[2]]
};
track.translations.extend(at.map(f32_of));
}
}
let mut channels = Vec::new();
for (b, track) in tracks.into_iter().enumerate() {
if track.frames.is_empty() {
continue;
}
#[allow(clippy::cast_precision_loss, reason = "frame counts are far below 2^52")]
let times: Vec<f32> = track
.frames
.iter()
.map(|&f| f32_of(f as f64 * motion.frame_time))
.collect();
let node = if b == root { rig.root } else { rig.nodes[b] };
if !track.rotations.is_empty() {
channels.push(Channel {
node,
path: Path::Rotation,
times: times.clone(),
values: track.rotations,
interp: Interp::Linear,
});
}
if !track.translations.is_empty() {
channels.push(Channel {
node,
path: Path::Translation,
times,
values: track.translations,
interp: Interp::Linear,
});
}
}
let animation = (!channels.is_empty()).then(|| Animation {
name: "motion".into(),
channels,
extras: None,
});
(animation, motion)
}
fn decode(bytes: &[u8]) -> String {
let mut s = scene::text(bytes);
if s.starts_with('\u{feff}') {
s.drain(..'\u{feff}'.len_utf8());
}
s
}
fn skeleton_names(amc: &str, path: &std::path::Path) -> Vec<String> {
let mut out = Vec::new();
for line in amc.lines().take(64) {
let line = line.trim();
if let Some(rest) = line.strip_prefix("#!OML:ASF") {
let r = rest.trim();
if !r.is_empty() {
out.push(r.to_owned());
}
}
if line.starts_with(':') || line.bytes().next().is_some_and(|c| c.is_ascii_digit()) {
break;
}
}
if let Some(stem) = path.file_stem().map(|s| s.to_string_lossy().into_owned()) {
out.push(format!("{stem}.asf"));
if let Some((subject, _)) = stem.rsplit_once('_')
&& !subject.is_empty()
{
out.push(format!("{subject}.asf"));
}
}
out
}
fn file_name(reference: &str) -> &str {
reference.rsplit(['/', '\\']).next().unwrap_or(reference)
}
pub(crate) fn load(input: Input<'_>, ext: &str, assets: &dyn Assets, keep: Keep) -> Result<Document> {
let bytes = input.read()?;
let text = decode(&bytes);
let amc = ext.eq_ignore_ascii_case("amc");
let (sk, skeleton_file) = if amc {
let names = skeleton_names(&text, input.path);
let mut found = None;
for name in &names {
if let Some(path) = assets.find(name, input.dir())
&& let Some(b) = assets.read(&path)
{
found = Some((decode(&b), path));
break;
}
}
let Some((asf, path)) = found else {
bail!(
MissingAsset,
"AMC motion needs its ASF skeleton beside it, and none was found (looked for {})",
names.iter().map(|n| file_name(n)).collect::<Vec<_>>().join(", ")
);
};
let name = path.file_name().map(|n| n.to_string_lossy().into_owned());
(parse_asf(&asf)?, name)
} else {
(parse_asf(&text)?, None)
};
let mut scene = Scene::default();
let rig = build(&sk, &mut scene);
let mut source = Map::new();
let info = &mut scene.info;
info.format = Some(if amc { "AMC" } else { "ASF" }.into());
info.version.clone_from(&sk.version);
info.title.clone_from(&sk.name);
let meters = INCH / sk.length;
info.meters_per_unit = if sk.length_given {
Fact::Declared(meters)
} else {
Fact::Assumed(meters)
};
if (sk.length - 1.0).abs() < 1e-12 {
info.unit = Some("inch".into());
}
info.up_axis = Fact::Assumed(Axis::PosY);
source.insert("length".into(), json!(sk.length));
source.insert("angle".into(), json!(if sk.radians { "rad" } else { "deg" }));
if let Some(m) = sk.mass {
source.insert("mass".into(), json!(m));
}
if let Some(f) = &skeleton_file {
source.insert("skeleton".into(), json!(f));
}
let facts = &mut scene.facts;
facts.insert("bones".into(), json!(sk.bones.len()));
let dofs: usize = sk.root.dofs.len() + sk.bones.iter().map(|b| b.dofs.len()).sum::<usize>();
facts.insert("channels".into(), json!(dofs));
let lengths = sk.bones.iter().filter(|b| b.dofs.contains(&Dof::Length)).count();
if lengths > 0 {
facts.insert("length_dofs_dropped".into(), json!(lengths));
}
if rig.unattached > 0 {
facts.insert("unattached_bones".into(), json!(rig.unattached));
}
if amc {
let (animation, motion) = parse_amc(&text, &sk, &rig, &scene);
let facts = &mut scene.facts;
facts.insert("frames".into(), json!(motion.frames));
if motion.dropped > 0 {
facts.insert("dropped_lines".into(), json!(motion.dropped));
}
if motion.unknown > 0 {
facts.insert("unknown_bone_lines".into(), json!(motion.unknown));
}
let rate = 1.0 / motion.frame_time;
scene.info.frame_rate = if motion.rate_given {
Fact::Declared(rate)
} else {
Fact::Assumed(rate)
};
source.insert("frames".into(), json!(motion.frames));
if keep.animations
&& let Some(a) = animation
{
scene.animations.push(a);
}
}
scene.source = source;
scene.scale(f32_of(INCH / sk.length));
scene.finish(keep)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::assets::Memory;
const ASF: &str = "# a two-bone arm
:version 1.10
:name test
:units
mass 1.0
length 0.0254
angle deg
:documentation
begin end
:root
order TX TY TZ RX RY RZ
axis XYZ
position 0 0 0
orientation 0 0 0
:bonedata
begin
id 1
name upper
direction 1 0 0
length 2
axis 0 0 90 XYZ
dof rx ry rz
limits (-180 180)
(-inf inf)
(-90.0 90.0)
end
begin
id 2
name lower
direction 0 1 0
length 3
axis 0 0 0 XYZ
dof rz l
limits (-10 10) (0 1)
end
:hierarchy
begin
root upper
upper lower
end
";
fn asf(text: &str) -> Result<Document> {
load(
Input::memory(text.as_bytes(), "t.asf"),
"asf",
&Memory::new(),
Keep::ALL,
)
}
fn amc(asf_text: &str, motion: &str) -> Result<Document> {
let assets = Memory::new().with("t.asf", asf_text.to_owned());
load(Input::memory(motion.as_bytes(), "t.amc"), "amc", &assets, Keep::ALL)
}
fn error(r: Result<Document>) -> String {
match r {
Ok(_) => panic!("read without error"),
Err(e) => e.to_string(),
}
}
fn near(a: &[f32], b: &[f32]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-5)
}
#[test]
fn skeleton() {
let doc = asf(ASF).unwrap();
let nodes = doc.json()["nodes"].as_array().unwrap();
let names: Vec<_> = nodes.iter().map(|n| n["name"].as_str().unwrap()).collect();
assert_eq!(names, ["root", "upper", "lower", "lower_end"]);
assert_eq!(nodes[2]["translation"], json!([2.0, 0.0, 0.0]));
assert_eq!(nodes[3]["translation"], json!([0.0, 3.0, 0.0]));
assert!(nodes[1].get("translation").is_none());
assert_eq!(doc.facts["bones"], json!(2));
assert_eq!(doc.facts["channels"], json!(11));
assert_eq!(doc.facts["length_dofs_dropped"], json!(1));
assert_eq!(
nodes[1]["extras"]["limits"],
json!([[-180.0, 180.0], [null, null], [-90.0, 90.0]])
);
assert_eq!(nodes[2]["extras"]["dof"], json!(["rz", "l"]));
assert_eq!(doc.json()["asset"]["extras"]["source"]["format"], "ASF");
assert!(doc.json().get("animations").is_none());
let info = doc.source_info();
assert_eq!(info.meters_per_unit, Fact::Declared(1.0));
assert_eq!(info.up_axis, Fact::Assumed(Axis::PosY));
assert_eq!(info.frame_rate, Fact::Unknown);
assert_eq!(info.title.as_deref(), Some("test"));
assert_eq!(info.version.as_deref(), Some("1.10"));
assert!(!info.converted.units && !info.converted.up_axis);
}
#[test]
fn motion() {
let text = "#!OML:ASF elsewhere/t.asf\n:FULLY-SPECIFIED\n:DEGREES\n1\nroot 1 2 3 0 0 0\nupper 0 0 0\nlower 0 0\n2\nroot 1 2 3 0 0 0\nupper 90 0 0\nlower 45 0.5\n";
let doc = amc(ASF, text).unwrap();
assert_eq!(doc.facts["frames"], json!(2));
assert!(doc.facts.get("frame_time_guessed").is_none());
assert_eq!(doc.source_info().frame_rate, Fact::Assumed(120.0));
assert_eq!(doc.json()["asset"]["extras"]["source"]["skeleton"], "t.asf");
let rig = doc.rig().unwrap();
let a = &rig.animations[0];
assert_eq!(a.channels.len(), 4);
let pose = rig.pose(Some(0), 1.0 / 120.0);
assert!(near(&pose[0].translation, &[1.0, 2.0, 3.0]));
let q = pose[1].rotation;
let s = std::f32::consts::FRAC_1_SQRT_2;
assert!(near(&q, &[0.0, s, 0.0, s]) || near(&q, &[0.0, -s, 0.0, -s]), "{q:?}");
let h = (std::f32::consts::PI / 8.0).sin();
let c = (std::f32::consts::PI / 8.0).cos();
assert!(near(&pose[2].rotation, &[0.0, 0.0, h, c]));
}
#[test]
fn skeleton_by_name() {
let motion = ":DEGREES\n1\nroot 0 0 0 0 0 0\n";
let assets = Memory::new().with("02.asf", ASF.as_bytes());
let doc = load(Input::memory(motion.as_bytes(), "02_01.amc"), "amc", &assets, Keep::ALL).unwrap();
assert_eq!(doc.json()["asset"]["extras"]["source"]["skeleton"], "02.asf");
let e = error(load(
Input::memory(motion.as_bytes(), "x.amc"),
"amc",
&assets,
Keep::ALL,
));
assert!(e.contains("x.asf"), "{e}");
}
#[test]
fn radians_and_rate() {
let text = ":RADIANS\n:SAMPLES-PER-SECOND 60\n1\nlower 1.5707963267948966 0\n2\nlower 0 0\n";
let doc = amc(ASF, text).unwrap();
let info = doc.source_info();
assert_eq!(info.frame_rate, Fact::Declared(60.0));
assert_eq!(info.format.as_deref(), Some("AMC"));
let rig = doc.rig().unwrap();
let s = std::f32::consts::FRAC_1_SQRT_2;
assert!(near(&rig.pose(Some(0), 0.0)[2].rotation, &[0.0, 0.0, s, s]));
assert_eq!(rig.animations[0].channels[0].times, [0.0, 1.0 / 60.0]);
}
#[test]
fn bad_lines_are_dropped() {
let text = "upper 1 2 3\n1\nupper 1 2\nupper 1 nan 3\nnobody 1\nlower 1 2\n2\n3\nlower 4 5\n";
let doc = amc(ASF, text).unwrap();
assert_eq!(doc.facts["frames"], json!(3));
assert_eq!(doc.facts["dropped_lines"], json!(3));
assert_eq!(doc.facts["unknown_bone_lines"], json!(1));
let rig = doc.rig().unwrap();
assert_eq!(rig.animations[0].channels.len(), 1);
assert_eq!(rig.animations[0].channels[0].times, [0.0, 2.0 / 120.0]);
}
#[test]
fn loops_and_loose_bones_hang_from_the_root() {
let text = ASF.replace("root upper\n", "lower upper\n");
let doc = asf(&text).unwrap();
assert_eq!(doc.facts["unattached_bones"], json!(1));
let rig = doc.rig().unwrap();
let upper = rig.find("upper").unwrap();
let lower = rig.find("lower").unwrap();
assert_eq!(rig.nodes[upper].parent, Some(0));
assert_eq!(rig.nodes[lower].parent, Some(upper));
}
#[test]
fn bad_skeletons_are_errors() {
let cases = [
("", "no `:` sections"),
("hello\n", "not an ASF file"),
(":bonedata\nbegin\nname a\n", "is cut short inside bone"),
(":bonedata\nname a\n", "outside a bone"),
(":bonedata\nbegin\nname a\nlength x\nend\n", "length needs a number"),
(":bonedata\nbegin\nname a\ndirection 1 0\nend\n", "direction needs"),
(":bonedata\nbegin\nname a\nend\nbegin\nname a\nend\n", "defined twice"),
(":bonedata\nbegin\nname root\nend\n", "defined twice"),
(":units\nangle grad\n", "angle unit"),
(":units\nlength 0\n", "above 0"),
(":bonedata\nbegin\nname a\naxis 0 0 0 XQ\nend\n", "axis order"),
(":bonedata\nbegin\nname a\nlimits (0 x)\nend\n", "limit"),
(":bonedata\nbegin\n:hierarchy\n", "inside a bone"),
];
for (text, want) in cases {
let e = error(asf(text));
assert!(e.contains(want), "{text:?}: {e}");
}
let many = format!(":bonedata\n{}", "begin\nend\n".repeat(MAX_BONES + 1));
assert!(error(asf(&many)).contains("more than"));
}
#[test]
fn truncated_files_never_panic() {
let motion = "1\nroot 1 2 3 4 5 6\nupper 1 2 3\nlower 4 5\n2\nroot 1 2 3 4 5 6\n";
for cut in 0..ASF.len() {
let _ = asf(&ASF[..cut]);
}
for cut in 0..motion.len() {
let _ = amc(ASF, &motion[..cut]).unwrap();
}
}
#[test]
fn keeping_no_animations_keeps_the_facts() {
let text = "1\nroot 1 2 3 0 0 0\n";
let assets = Memory::new().with("t.asf", ASF.as_bytes());
let keep = Keep::ALL.with_animations(false);
let none = load(Input::memory(text.as_bytes(), "t.amc"), "amc", &assets, keep).unwrap();
let all = amc(ASF, text).unwrap();
assert!(none.json().get("animations").is_none());
assert_eq!(none.facts, all.facts);
}
}