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::document::{Document, Keep};
use crate::num::f32_of;
use crate::quat;
use crate::source::{Axis, Fact, meters_in};
const DEFAULT_RATE: f64 = super::keys::ASSUMED_FRAME_RATE;
const BLOCK: usize = 512;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Processor {
Intel,
Dec,
Mips,
}
impl Processor {
fn name(self) -> &'static str {
match self {
Self::Intel => "Intel",
Self::Dec => "DEC",
Self::Mips => "MIPS",
}
}
}
#[derive(Clone, Copy)]
struct Reader<'a> {
b: &'a [u8],
p: Processor,
}
impl<'a> Reader<'a> {
fn slice(&self, at: usize, n: usize) -> Option<&'a [u8]> {
self.b.get(at..at.checked_add(n)?)
}
fn arr<const N: usize>(&self, at: usize) -> Option<[u8; N]> {
self.slice(at, N)?.try_into().ok()
}
fn u16(&self, at: usize) -> Option<u16> {
let a = self.arr::<2>(at)?;
Some(if self.p == Processor::Mips {
u16::from_be_bytes(a)
} else {
u16::from_le_bytes(a)
})
}
fn i16(&self, at: usize) -> Option<i16> {
self.u16(at).map(|v| i16::from_ne_bytes(v.to_ne_bytes()))
}
fn f32(&self, at: usize) -> Option<f32> {
let a = self.arr::<4>(at)?;
Some(match self.p {
Processor::Intel => f32::from_le_bytes(a),
Processor::Mips => f32::from_be_bytes(a),
Processor::Dec => f32::from_le_bytes([a[2], a[3], a[0], a[1]]) / 4.0,
})
}
}
struct Param<'a> {
kind: i8,
dims: Vec<usize>,
data: &'a [u8],
}
struct Params<'a> {
rd: Reader<'a>,
map: HashMap<String, Param<'a>>,
cut_short: bool,
}
impl<'a> Params<'a> {
fn read(rd: Reader<'a>, mut at: usize, end: usize) -> Self {
let mut groups: HashMap<u8, String> = HashMap::new();
let mut found: Vec<(u8, String, Param<'a>)> = Vec::new();
let mut cut_short = false;
while at < end {
let Some(&[n, id]) = rd.slice(at, 2) else {
cut_short = at < rd.b.len();
break;
};
let len = usize::from(i8::from_ne_bytes([n]).unsigned_abs());
if len == 0 {
break;
}
let id = i8::from_ne_bytes([id]);
let (Some(name), Some(offset)) = (rd.slice(at + 2, len), rd.i16(at + 2 + len)) else {
cut_short = true;
break;
};
let name = crate::bytes::text(name).trim().to_ascii_uppercase();
let body = at + 4 + len;
if id < 0 {
groups.entry(id.unsigned_abs()).or_insert(name);
} else if id > 0 {
let Some(p) = Self::param(rd, body) else {
cut_short = true;
break;
};
found.push((id.unsigned_abs(), name, p));
}
let Ok(step) = usize::try_from(offset) else { break };
if step == 0 {
break;
}
at = at + 2 + len + step;
}
let mut map = HashMap::new();
for (g, name, p) in found {
if let Some(group) = groups.get(&g) {
map.entry(format!("{group}:{name}")).or_insert(p);
}
}
Self { rd, map, cut_short }
}
fn param(rd: Reader<'a>, at: usize) -> Option<Param<'a>> {
let kind = i8::from_ne_bytes([*rd.b.get(at)?]);
let ndims = usize::from(*rd.b.get(at + 1)?);
let dims: Vec<usize> = rd.slice(at + 2, ndims)?.iter().map(|&d| usize::from(d)).collect();
let count = dims.iter().try_fold(1usize, |a, &d| a.checked_mul(d))?;
let size = count.checked_mul(usize::from(kind.unsigned_abs()))?;
let data = rd.slice(at + 2 + ndims, size)?;
Some(Param { kind, dims, data })
}
fn get(&self, key: &str) -> Option<&Param<'a>> {
self.map.get(key)
}
fn numbers(&self, key: &str) -> Vec<f64> {
let Some(p) = self.get(key) else { return Vec::new() };
let rd = Reader {
b: p.data,
p: self.rd.p,
};
match p.kind {
1 => p.data.iter().map(|&b| f64::from(b)).collect(),
2 => (0..p.data.len() / 2)
.filter_map(|i| rd.i16(i * 2))
.map(f64::from)
.collect(),
4 => (0..p.data.len() / 4)
.filter_map(|i| rd.f32(i * 4))
.map(f64::from)
.collect(),
_ => Vec::new(),
}
}
fn number(&self, key: &str) -> Option<f64> {
self.numbers(key).first().copied().filter(|v| v.is_finite())
}
fn words(&self, key: &str) -> Vec<u16> {
let Some(p) = self.get(key).filter(|p| p.kind == 2) else {
return Vec::new();
};
let rd = Reader {
b: p.data,
p: self.rd.p,
};
(0..p.data.len() / 2).filter_map(|i| rd.u16(i * 2)).collect()
}
fn count(&self, key: &str) -> Option<usize> {
match self.get(key)?.kind {
2 => self.words(key).first().map(|&w| usize::from(w)),
_ => whole(self.number(key)?),
}
}
fn strings(&self, key: &str) -> Vec<String> {
let Some(p) = self.get(key).filter(|p| p.kind == -1) else {
return Vec::new();
};
let tidy = |b: &[u8]| {
scene::text(b)
.trim_matches(|c: char| c == '\0' || c.is_whitespace())
.to_owned()
};
match p.dims.first() {
None => vec![tidy(p.data)],
Some(0) => Vec::new(),
Some(&n) => p.data.chunks(n).map(tidy).collect(),
}
}
fn string(&self, key: &str) -> Option<String> {
self.strings(key).into_iter().next().filter(|s| !s.is_empty())
}
}
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "the range is checked first"
)]
fn whole(v: f64) -> Option<usize> {
(0.0..4e9).contains(&v).then(|| v.round() as usize)
}
#[allow(clippy::cast_precision_loss, reason = "frame numbers are far below 2^52")]
fn seconds(f: usize, rate: f64) -> f32 {
f32_of(f as f64 / rate)
}
fn axis(s: &str) -> Option<[f64; 3]> {
let s = s.trim().to_ascii_uppercase();
let (sign, a) = match s.strip_prefix('-') {
Some(rest) => (-1.0, rest),
None => (1.0, s.trim_start_matches('+')),
};
let k = match a {
"X" => 0,
"Y" => 1,
"Z" => 2,
_ => return None,
};
let mut v = [0.0; 3];
v[k] = sign;
Some(v)
}
fn screen_rotation(x: [f64; 3], y: [f64; 3]) -> Option<[f64; 4]> {
let z = [
x[1] * y[2] - x[2] * y[1],
x[2] * y[0] - x[0] * y[2],
x[0] * y[1] - x[1] * y[0],
];
if z.iter().all(|c| *c == 0.0) {
return None;
}
let rows = [x, y, z];
let mut m = [0.0; 9];
for (r, row) in rows.iter().enumerate() {
for (c, v) in row.iter().enumerate() {
m[c * 3 + r] = *v;
}
}
Some(quat::from_matrix(&m))
}
pub(crate) fn load(input: Input<'_>, keep: Keep) -> Result<Document> {
let bytes = input.read()?;
let b: &[u8] = &bytes;
ensure!(
b.len() >= 2 && b[1] == 0x50,
UnknownFormat,
"not a C3D file: its second byte isn't 0x50"
);
ensure!(
b[0] >= 2,
Corrupt,
"C3D header points at parameter block {}, not one past the header",
b[0]
);
let param_start = (usize::from(b[0]) - 1) * BLOCK;
let Some(&proc_byte) = b.get(param_start + 3) else {
bail!(Truncated, "C3D file is cut short: its parameters are missing");
};
let p = match proc_byte {
84 => Some(Processor::Intel),
85 => Some(Processor::Dec),
86 => Some(Processor::Mips),
_ => None,
};
let rd = Reader {
b,
p: p.unwrap_or(Processor::Intel),
};
let blocks = usize::from(b[param_start + 2]);
let end = if blocks == 0 {
b.len()
} else {
param_start + blocks * BLOCK
};
let params = Params::read(rd, param_start + 4, end);
let word = |w: usize| rd.u16((w - 1) * 2).unwrap_or(0);
let float = |w: usize| rd.f32((w - 1) * 2).map(f64::from).filter(|v| v.is_finite());
let points = match word(2) {
0 => params.count("POINT:USED").unwrap_or(0).min(usize::from(u16::MAX)),
n => usize::from(n),
};
let analog_words = usize::from(word(3));
let header_scale = float(7).filter(|s| *s != 0.0);
let scale = header_scale.or_else(|| params.number("POINT:SCALE")).unwrap_or(1.0);
let floats = scale < 0.0;
let point_scale = if scale == 0.0 { 1.0 } else { scale.abs() };
let start_block = match word(9) {
0 => params.count("POINT:DATA_START").unwrap_or(0),
n => usize::from(n),
};
ensure!(start_block > 0, Corrupt, "C3D file doesn't say where its data starts");
let data_start = (start_block - 1).saturating_mul(BLOCK);
let rate_given = float(11)
.filter(|r| *r > 0.0)
.or_else(|| params.number("POINT:RATE").filter(|r| *r > 0.0));
let rate = rate_given.unwrap_or(DEFAULT_RATE);
let (first, last) = (usize::from(word(4)), usize::from(word(5)));
let claimed = if last == usize::from(u16::MAX) || last < first {
let field = |key: &str| match params.words(key)[..] {
[lo, hi, ..] => Some(usize::from(lo) | usize::from(hi) << 16),
_ => None,
};
match (field("TRIAL:ACTUAL_START_FIELD"), field("TRIAL:ACTUAL_END_FIELD")) {
(Some(s), Some(e)) if e >= s => Some(e - s + 1),
_ => params.count("POINT:FRAMES"),
}
} else {
Some(last - first + 1)
};
let width = if floats { 4 } else { 2 };
let frame_size = (points * 4 + analog_words) * width;
let fit = b.len().saturating_sub(data_start).checked_div(frame_size).unwrap_or(0);
let frames = match claimed {
Some(c) => fit.min(c),
None => fit,
};
let missing_frames = claimed.map_or(0, |c| c.saturating_sub(frames));
let mut labels = params.strings("POINT:LABELS");
for k in 2.. {
if labels.len() >= points {
break;
}
let more = params.strings(&format!("POINT:LABELS{k}"));
if more.is_empty() {
break;
}
labels.extend(more);
}
let descriptions = params.strings("POINT:DESCRIPTIONS");
let mut tracks: Vec<(Vec<f32>, Vec<f32>)> = vec![(Vec::new(), Vec::new()); points];
let mut invalid = 0usize;
for f in 0..frames {
let at = data_start + f * frame_size;
let time = seconds(f, rate);
for (m, track) in tracks.iter_mut().enumerate() {
let at = at + m * 4 * width;
let sample = if floats {
let v = [0, 1, 2, 3].map(|i| rd.f32(at + i * 4).map_or(f64::NAN, f64::from));
(v[3] >= 0.0).then_some([v[0], v[1], v[2]])
} else {
let v = [0, 1, 2, 3].map(|i| f64::from(rd.i16(at + i * 2).unwrap_or(-1)));
(v[3] >= 0.0).then(|| [v[0], v[1], v[2]].map(|c| c * point_scale))
};
match sample.filter(|s| s.iter().all(|c| c.is_finite())) {
Some(s) => {
track.0.push(time);
track.1.extend(s.map(f32_of));
}
None => invalid += 1,
}
}
}
let mut scene = Scene::default();
let root = scene.add(Node::named("markers", None));
let mut channels = Vec::new();
let mut empty = 0usize;
for (m, (times, values)) in tracks.into_iter().enumerate() {
let name = match labels.get(m) {
Some(l) if !l.is_empty() => l.clone(),
_ => format!("marker{}", m + 1),
};
let mut node = Node::named(name, Some(root));
if let Some(d) = descriptions.get(m).filter(|d| !d.is_empty()) {
node.extras = Some(json!({ "description": d }));
}
match values.get(..3) {
Some(v) => node.translation = [v[0], v[1], v[2]],
None => empty += 1,
}
let i = scene.add(node);
if !times.is_empty() {
channels.push(Channel {
node: i,
path: Path::Translation,
times,
values,
interp: Interp::Linear,
});
}
}
if keep.animations && !channels.is_empty() {
scene.animations.push(Animation {
name: super::keys::clip_name(input.path).unwrap_or_default(),
channels,
extras: None,
});
}
let units = params.string("POINT:UNITS");
let meters = units.as_deref().and_then(meters_in);
let x_screen = params.string("POINT:X_SCREEN");
let y_screen = params.string("POINT:Y_SCREEN");
let screen = match (x_screen.as_deref().and_then(axis), y_screen.as_deref().and_then(axis)) {
(Some(x), Some(y)) => screen_rotation(x, y),
_ => None,
};
let turn = screen.unwrap_or_else(|| scene::Z_UP_TO_Y_UP.map(f64::from));
let info = &mut scene.info;
info.format = Some("C3D".into());
info.meters_per_unit = meters.map_or(Fact::Unknown, Fact::Declared);
info.unit.clone_from(&units);
info.up_axis = match y_screen.as_deref().and_then(|s| s.parse().ok()) {
Some(up) if screen.is_some() => Fact::Declared(up),
_ => Fact::Assumed(Axis::PosZ),
};
info.frame_rate = match rate_given {
Some(r) => Fact::Declared(r),
None => Fact::Assumed(rate),
};
info.tool = params.string("MANUFACTURER:SOFTWARE");
let mut source = Map::new();
source.insert("processor".into(), json!(rd.p.name()));
if missing_frames > 0 {
source.insert("truncated".into(), json!(true));
}
source.insert("storage".into(), json!(if floats { "float" } else { "integer" }));
if let Some(x) = &x_screen {
source.insert("xScreen".into(), json!(x));
}
source.insert("firstFrame".into(), json!(first));
source.insert("scale".into(), json!(scale));
for (key, name) in [
("MANUFACTURER:COMPANY", "company"),
("MANUFACTURER:VERSION_LABEL", "softwareVersion"),
("SUBJECTS:NAMES", "subject"),
] {
if let Some(v) = params.string(key) {
source.insert(name.into(), Value::String(v));
}
}
let analog_channels = params.count("ANALOG:USED").unwrap_or(0);
let facts = &mut scene.facts;
facts.insert("markers".into(), json!(points));
facts.insert("frames".into(), json!(frames));
if invalid > 0 {
facts.insert("missing_samples".into(), json!(invalid));
}
if empty > 0 {
facts.insert("markers_never_seen".into(), json!(empty));
}
if missing_frames > 0 {
facts.insert("frames_cut_off".into(), json!(missing_frames));
}
if analog_words > 0 || analog_channels > 0 {
facts.insert("analog_channels_dropped".into(), json!(analog_channels));
if let Some(r) = params.number("ANALOG:RATE") {
facts.insert("analog_rate".into(), json!(r));
}
}
let events = usize::from(word(151)).min(18);
if events > 0 {
facts.insert("events_dropped".into(), json!(events));
}
if p.is_none() {
facts.insert("processor_unknown".into(), json!(proc_byte));
}
if params.cut_short {
facts.insert("parameters_cut_short".into(), json!(true));
}
scene.source = source;
scene.unit(meters.unwrap_or(1.0));
scene.turn(quat::normalize(turn));
scene.finish(keep)
}
#[cfg(test)]
mod tests {
use super::*;
fn build(
p: Processor,
names: &[&str],
frames: &[Vec<Option<[f32; 3]>>],
scale: f32,
extra: &[(&str, &str)],
) -> Vec<u8> {
let u16b = |v: u16| {
if p == Processor::Mips {
v.to_be_bytes()
} else {
v.to_le_bytes()
}
};
let f32b = |v: f32| match p {
Processor::Intel => v.to_le_bytes(),
Processor::Mips => v.to_be_bytes(),
Processor::Dec => {
let a = (v * 4.0).to_le_bytes();
[a[2], a[3], a[0], a[1]]
}
};
let mut params: Vec<u8> = vec![1, 0x50, 0, 83 + p as u8 + 1];
let record = |params: &mut Vec<u8>, id: i8, name: &str, body: Vec<u8>, last: bool| {
params.push(u8::try_from(name.len()).unwrap());
params.extend(id.to_ne_bytes());
params.extend(name.as_bytes());
let offset = if last {
0
} else {
u16::try_from(body.len() + 2).unwrap()
};
params.extend(u16b(offset));
params.extend(body);
};
let chars = |s: &[&str]| {
let n = s.iter().map(|s| s.len()).max().unwrap_or(0).max(1);
let mut body = vec![0xff, 2, u8::try_from(n).unwrap(), u8::try_from(s.len()).unwrap()];
for s in s {
body.extend(format!("{s:<n$}").as_bytes());
}
body.push(0);
body
};
record(&mut params, -1, "POINT", vec![0], false);
let mut used = vec![2, 0];
used.extend(u16b(u16::try_from(names.len()).unwrap()));
used.push(0);
record(&mut params, 1, "USED", used, false);
record(&mut params, 1, "LABELS", chars(names), false);
for (k, v) in extra {
record(&mut params, 1, k, chars(&[v]), false);
}
record(&mut params, -2, "ANALOG", vec![0], true);
let blocks = params.len().div_ceil(BLOCK);
params[2] = u8::try_from(blocks).unwrap();
params.resize(blocks * BLOCK, 0);
let mut header = vec![0u8; BLOCK];
header[0] = 2;
header[1] = 0x50;
let mut put = |w: usize, v: [u8; 2]| header[(w - 1) * 2..w * 2].copy_from_slice(&v);
put(2, u16b(u16::try_from(names.len()).unwrap()));
put(4, u16b(1));
put(5, u16b(u16::try_from(frames.len()).unwrap()));
let s = f32b(scale);
put(7, [s[0], s[1]]);
put(8, [s[2], s[3]]);
put(9, u16b(u16::try_from(2 + blocks).unwrap()));
let r = f32b(10.0);
put(11, [r[0], r[1]]);
put(12, [r[2], r[3]]);
let mut data = Vec::new();
for frame in frames {
for s in frame {
if scale < 0.0 {
let v = s.map_or([0.0, 0.0, 0.0, -1.0], |v| [v[0], v[1], v[2], 0.0]);
for c in v {
data.extend(f32b(c));
}
} else {
#[allow(clippy::cast_possible_truncation, reason = "test values fit")]
let q = |c: f32| (c / scale).round() as i16;
let v = s.map_or([0, 0, 0, -1], |v| [q(v[0]), q(v[1]), q(v[2]), 0]);
for c in v {
data.extend(u16b(u16::from_ne_bytes(c.to_ne_bytes())));
}
}
}
}
data.resize(data.len().div_ceil(BLOCK).max(1) * BLOCK, 0);
[header, params, data].concat()
}
fn sample() -> Vec<Vec<Option<[f32; 3]>>> {
vec![
vec![Some([100.0, 200.0, 300.0]), None],
vec![Some([110.0, 200.0, 300.0]), Some([0.0, 0.0, 1000.0])],
vec![Some([120.0, 210.0, 290.0]), Some([0.0, 10.0, 1000.0])],
]
}
fn read(b: &[u8]) -> Result<Document> {
load(Input::memory(b, "t.c3d"), Keep::ALL)
}
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 every_processor_and_storage_reads_the_same() {
for p in [Processor::Intel, Processor::Dec, Processor::Mips] {
for scale in [0.1, -1.0] {
let b = build(p, &["LASI", ""], &sample(), scale, &[]);
let doc = read(&b).unwrap();
assert_eq!(doc.facts["markers"], json!(2), "{p:?}");
assert_eq!(doc.facts["frames"], json!(3));
assert_eq!(doc.facts["missing_samples"], json!(1));
let info = doc.source_info();
assert_eq!(info.up_axis, Fact::Assumed(Axis::PosZ), "{p:?}");
assert_eq!(info.meters_per_unit, Fact::Unknown);
assert_eq!(info.unit, None);
assert_eq!(info.frame_rate, Fact::Declared(10.0));
assert!(!info.converted.units && info.converted.up_axis);
let rig = doc.rig().unwrap();
assert_eq!(rig.nodes[1].name.as_deref(), Some("LASI"));
assert_eq!(rig.nodes[2].name.as_deref(), Some("marker2"));
assert_eq!(rig.nodes[3].name.as_deref(), Some("RootNode"));
assert_eq!(rig.nodes[0].parent, Some(3));
assert!(near(&rig.nodes[0].rest.rotation, &[0.0, 0.0, 0.0, 1.0]));
let pose = rig.pose(Some(0), 0.0);
assert!(near(&pose[1].translation, &[100.0, 200.0, 300.0]));
let world = |pose: &[crate::rig::Trs], i: usize| {
let m = rig.world(pose)[i];
[m[12], m[13], m[14]]
};
assert!(
near(&world(&pose, 1), &[100.0, 300.0, -200.0]),
"{p:?} {scale}: {:?}",
world(&pose, 1)
);
let pose = rig.pose(Some(0), 0.2);
assert!(near(&world(&pose, 2), &[0.0, 1000.0, -10.0]), "{p:?} {scale}");
assert_eq!(rig.animations[0].channels[1].times, [0.1, 0.2]);
}
}
}
#[test]
fn screen_axes_and_units() {
let b = build(
Processor::Intel,
&["a"],
&sample()[..1].iter().map(|f| f[..1].to_vec()).collect::<Vec<_>>(),
-1.0,
&[("X_SCREEN", "+X"), ("Y_SCREEN", "+Y"), ("UNITS", "m")],
);
let doc = read(&b).unwrap();
let info = doc.source_info();
assert_eq!(info.format.as_deref(), Some("C3D"));
assert_eq!(info.up_axis, Fact::Declared(Axis::PosY));
assert_eq!(info.meters_per_unit, Fact::Declared(1.0));
assert_eq!(info.unit.as_deref(), Some("m"));
assert!(!info.converted.units);
assert!(!info.converted.up_axis, "a file that is Y up already isn't turned");
let rig = doc.rig().unwrap();
assert!(near(&rig.nodes[1].rest.translation, &[100.0, 200.0, 300.0]));
assert_eq!(doc.json()["nodes"][2], json!({ "name": "RootNode", "children": [0] }));
let q = screen_rotation(axis("-Y").unwrap(), axis("+Z").unwrap()).unwrap();
assert!(near(&quat::rotate(q, [0.0, -1.0, 0.0]).map(f32_of), &[1.0, 0.0, 0.0]));
assert!(near(&quat::rotate(q, [0.0, 0.0, 1.0]).map(f32_of), &[0.0, 1.0, 0.0]));
assert!(near(&quat::rotate(q, [-1.0, 0.0, 0.0]).map(f32_of), &[0.0, 0.0, 1.0]));
assert!(screen_rotation(axis("+Z").unwrap(), axis("-Z").unwrap()).is_none());
}
#[test]
fn not_c3d_is_an_error() {
for b in [&b""[..], b"hello", &[2, 0x50], &[0, 0x50, 0, 0]] {
assert!(read(b).is_err(), "{b:?}");
}
}
#[test]
fn truncated_and_corrupted_files_never_panic() {
let b = build(Processor::Mips, &["a", "b"], &sample(), 0.1, &[("UNITS", "mm")]);
for cut in 0..b.len() {
let _ = read(&b[..cut]);
}
let doc = read(&b[..b.len() - BLOCK + 20]).unwrap();
assert_eq!(doc.facts["frames"], json!(1));
assert_eq!(doc.facts["frames_cut_off"], json!(2));
let doc = read(&b[..b.len() - BLOCK + 10]).unwrap();
assert_eq!(doc.facts["frames"], json!(0));
assert_eq!(doc.facts["frames_cut_off"], json!(3));
let mut x: u32 = 1;
for _ in 0..3000 {
let mut c = b.clone();
for _ in 0..4 {
x = x.wrapping_mul(1_103_515_245).wrapping_add(12345);
let i = (x >> 8) as usize % c.len();
c[i] = (x >> 24) as u8;
}
let _ = read(&c);
}
}
}