oxihuman-export 0.1.2

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming formats
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Binary point cache export for vertex animation sequences.
//!
//! Format: `.opc` (OxiHuman Point Cache)
//! Header: 32 bytes, frame data follows as packed f32 LE triples.

#![allow(dead_code)]

use std::io::{Read, Write};
use std::path::Path;

use anyhow::{anyhow, bail, Context};
use oxihuman_mesh::MeshBuffers;

/// Magic bytes identifying an OPC file.
pub const OPC_MAGIC: &[u8; 4] = b"OPC1";

/// Header for a point cache file.
#[derive(Debug, Clone, PartialEq)]
pub struct PointCacheHeader {
    pub vertex_count: u32,
    pub frame_count: u32,
    pub fps: f32,
}

/// In-memory point cache: per-frame vertex positions.
#[derive(Debug, Clone)]
pub struct PointCache {
    pub header: PointCacheHeader,
    /// All frames: outer = frame index, inner = per-vertex [x, y, z].
    pub frames: Vec<Vec<[f32; 3]>>,
}

impl PointCache {
    /// Create a new empty cache for the given vertex count and frame rate.
    pub fn new(vertex_count: usize, fps: f32) -> Self {
        Self {
            header: PointCacheHeader {
                vertex_count: vertex_count as u32,
                frame_count: 0,
                fps,
            },
            frames: Vec::new(),
        }
    }

    /// Append a frame. Returns an error if the position count does not match.
    pub fn add_frame(&mut self, positions: Vec<[f32; 3]>) -> anyhow::Result<()> {
        if positions.len() != self.header.vertex_count as usize {
            bail!(
                "frame vertex count {} does not match cache vertex count {}",
                positions.len(),
                self.header.vertex_count
            );
        }
        self.frames.push(positions);
        self.header.frame_count = self.frames.len() as u32;
        Ok(())
    }

    /// Number of frames currently stored.
    pub fn frame_count(&self) -> usize {
        self.frames.len()
    }

    /// Number of vertices per frame.
    pub fn vertex_count(&self) -> usize {
        self.header.vertex_count as usize
    }

    /// Total duration in seconds.
    pub fn duration(&self) -> f32 {
        if self.header.fps == 0.0 {
            0.0
        } else {
            self.header.frame_count as f32 / self.header.fps
        }
    }

    /// Get a reference to a frame by index.
    pub fn get_frame(&self, index: usize) -> Option<&Vec<[f32; 3]>> {
        self.frames.get(index)
    }

    /// Interpolate vertex positions at fractional frame time `t` (range: 0 .. frame_count-1).
    ///
    /// Returns `None` if the cache has fewer than two frames or `t` is out of range.
    pub fn sample(&self, t: f32) -> Option<Vec<[f32; 3]>> {
        let n = self.frames.len();
        if n < 2 {
            return None;
        }
        if t < 0.0 || t > (n - 1) as f32 {
            return None;
        }
        let f = t.floor() as usize;
        let frac = t - f as f32;
        let f_next = (f + 1).min(n - 1);

        let a = &self.frames[f];
        let b = &self.frames[f_next];

        let result = a
            .iter()
            .zip(b.iter())
            .map(|(pa, pb)| {
                [
                    pa[0] + frac * (pb[0] - pa[0]),
                    pa[1] + frac * (pb[1] - pa[1]),
                    pa[2] + frac * (pb[2] - pa[2]),
                ]
            })
            .collect();
        Some(result)
    }
}

/// Export a `PointCache` to a `.opc` binary file.
pub fn export_point_cache(cache: &PointCache, path: &Path) -> anyhow::Result<()> {
    let mut file =
        std::fs::File::create(path).with_context(|| format!("cannot create {}", path.display()))?;

    // --- Header (32 bytes) ---
    file.write_all(OPC_MAGIC)?;
    file.write_all(&cache.header.vertex_count.to_le_bytes())?;
    file.write_all(&cache.header.frame_count.to_le_bytes())?;
    file.write_all(&cache.header.fps.to_le_bytes())?;
    file.write_all(&[0u8; 16])?; // reserved

    // --- Frame data ---
    for frame in &cache.frames {
        for &[x, y, z] in frame {
            file.write_all(&x.to_le_bytes())?;
            file.write_all(&y.to_le_bytes())?;
            file.write_all(&z.to_le_bytes())?;
        }
    }

    Ok(())
}

/// Load a `PointCache` from a `.opc` binary file.
pub fn load_point_cache(path: &Path) -> anyhow::Result<PointCache> {
    let mut file =
        std::fs::File::open(path).with_context(|| format!("cannot open {}", path.display()))?;

    let header = read_header(&mut file)?;

    let vertex_count = header.vertex_count as usize;
    let frame_count = header.frame_count as usize;

    let mut frames = Vec::with_capacity(frame_count);
    let mut buf4 = [0u8; 4];

    for _ in 0..frame_count {
        let mut verts = Vec::with_capacity(vertex_count);
        for _ in 0..vertex_count {
            file.read_exact(&mut buf4)?;
            let x = f32::from_le_bytes(buf4);
            file.read_exact(&mut buf4)?;
            let y = f32::from_le_bytes(buf4);
            file.read_exact(&mut buf4)?;
            let z = f32::from_le_bytes(buf4);
            verts.push([x, y, z]);
        }
        frames.push(verts);
    }

    Ok(PointCache { header, frames })
}

/// Build a `PointCache` from a slice of `MeshBuffers`. All meshes must share the same vertex count.
pub fn mesh_sequence_to_cache(frames: &[MeshBuffers], fps: f32) -> anyhow::Result<PointCache> {
    if frames.is_empty() {
        bail!("frame sequence is empty");
    }
    let vertex_count = frames[0].positions.len();
    let mut cache = PointCache::new(vertex_count, fps);
    for (i, mesh) in frames.iter().enumerate() {
        if mesh.positions.len() != vertex_count {
            bail!(
                "frame {} has {} vertices; expected {}",
                i,
                mesh.positions.len(),
                vertex_count
            );
        }
        cache.add_frame(mesh.positions.clone())?;
    }
    Ok(cache)
}

/// Extract the vertex positions for a single frame from a cache.
pub fn cache_frame_to_positions(cache: &PointCache, frame: usize) -> anyhow::Result<Vec<[f32; 3]>> {
    cache.get_frame(frame).cloned().ok_or_else(|| {
        anyhow!(
            "frame index {} out of range (cache has {} frames)",
            frame,
            cache.frame_count()
        )
    })
}

/// Validate a `.opc` file and return its header without loading frame data.
pub fn validate_point_cache_file(path: &Path) -> anyhow::Result<PointCacheHeader> {
    let mut file =
        std::fs::File::open(path).with_context(|| format!("cannot open {}", path.display()))?;
    read_header(&mut file)
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn read_header<R: Read>(reader: &mut R) -> anyhow::Result<PointCacheHeader> {
    let mut magic = [0u8; 4];
    reader.read_exact(&mut magic)?;
    if &magic != OPC_MAGIC {
        bail!("invalid magic bytes: expected OPC1, got {:?}", magic);
    }

    let mut buf4 = [0u8; 4];

    reader.read_exact(&mut buf4)?;
    let vertex_count = u32::from_le_bytes(buf4);

    reader.read_exact(&mut buf4)?;
    let frame_count = u32::from_le_bytes(buf4);

    reader.read_exact(&mut buf4)?;
    let fps = f32::from_le_bytes(buf4);

    // Skip 16 reserved bytes
    let mut reserved = [0u8; 16];
    reader.read_exact(&mut reserved)?;

    Ok(PointCacheHeader {
        vertex_count,
        frame_count,
        fps,
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use oxihuman_mesh::MeshBuffers;

    fn make_mesh(positions: Vec<[f32; 3]>) -> MeshBuffers {
        let n = positions.len();
        MeshBuffers {
            positions,
            normals: vec![[0.0, 1.0, 0.0]; n],
            tangents: vec![[1.0, 0.0, 0.0, 1.0]; n],
            uvs: vec![[0.0, 0.0]; n],
            indices: vec![],
            colors: None,
            has_suit: false,
        }
    }

    #[test]
    fn test_point_cache_new() {
        let cache = PointCache::new(4, 24.0);
        assert_eq!(cache.vertex_count(), 4);
        assert_eq!(cache.frame_count(), 0);
        assert_eq!(cache.header.fps, 24.0);
    }

    #[test]
    fn test_add_frame() {
        let mut cache = PointCache::new(2, 30.0);
        let frame = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        cache.add_frame(frame.clone()).expect("should succeed");
        assert_eq!(cache.frame_count(), 1);
        assert_eq!(cache.header.frame_count, 1);
        assert_eq!(cache.get_frame(0).expect("should succeed"), &frame);
    }

    #[test]
    fn test_add_frame_wrong_vertex_count() {
        let mut cache = PointCache::new(2, 30.0);
        let bad = vec![[1.0, 2.0, 3.0]]; // only 1 vertex
        let result = cache.add_frame(bad);
        assert!(result.is_err());
    }

    #[test]
    fn test_duration() {
        let mut cache = PointCache::new(1, 25.0);
        cache.add_frame(vec![[0.0, 0.0, 0.0]]).expect("should succeed");
        cache.add_frame(vec![[1.0, 1.0, 1.0]]).expect("should succeed");
        // 2 frames / 25 fps = 0.08 s
        let expected = 2.0 / 25.0;
        assert!((cache.duration() - expected).abs() < 1e-6);
    }

    #[test]
    fn test_get_frame() {
        let mut cache = PointCache::new(2, 24.0);
        let f0 = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]];
        let f1 = vec![[0.0, 1.0, 0.0], [1.0, 1.0, 0.0]];
        cache.add_frame(f0.clone()).expect("should succeed");
        cache.add_frame(f1.clone()).expect("should succeed");
        assert_eq!(cache.get_frame(0).expect("should succeed"), &f0);
        assert_eq!(cache.get_frame(1).expect("should succeed"), &f1);
        assert!(cache.get_frame(2).is_none());
    }

    #[test]
    fn test_sample_exact_frame() {
        let mut cache = PointCache::new(1, 24.0);
        cache.add_frame(vec![[0.0, 0.0, 0.0]]).expect("should succeed");
        cache.add_frame(vec![[2.0, 4.0, 6.0]]).expect("should succeed");
        let s0 = cache.sample(0.0).expect("should succeed");
        assert_eq!(s0[0], [0.0, 0.0, 0.0]);
        let s1 = cache.sample(1.0).expect("should succeed");
        assert_eq!(s1[0], [2.0, 4.0, 6.0]);
    }

    #[test]
    fn test_sample_between_frames() {
        let mut cache = PointCache::new(1, 24.0);
        cache.add_frame(vec![[0.0, 0.0, 0.0]]).expect("should succeed");
        cache.add_frame(vec![[2.0, 4.0, 6.0]]).expect("should succeed");
        let s = cache.sample(0.5).expect("should succeed");
        let eps = 1e-5;
        assert!((s[0][0] - 1.0).abs() < eps);
        assert!((s[0][1] - 2.0).abs() < eps);
        assert!((s[0][2] - 3.0).abs() < eps);
    }

    #[test]
    fn test_sample_out_of_range() {
        let mut cache = PointCache::new(1, 24.0);
        cache.add_frame(vec![[0.0, 0.0, 0.0]]).expect("should succeed");
        cache.add_frame(vec![[1.0, 1.0, 1.0]]).expect("should succeed");
        assert!(cache.sample(-0.1).is_none());
        assert!(cache.sample(1.1).is_none());
    }

    #[test]
    fn test_export_and_load() {
        let path = std::path::Path::new("/tmp/test_export_and_load.opc");
        let mut cache = PointCache::new(2, 30.0);
        cache
            .add_frame(vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
            .expect("should succeed");
        cache
            .add_frame(vec![[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]])
            .expect("should succeed");

        export_point_cache(&cache, path).expect("should succeed");
        let loaded = load_point_cache(path).expect("should succeed");

        assert_eq!(loaded.header.vertex_count, 2);
        assert_eq!(loaded.header.frame_count, 2);
        assert_eq!(loaded.header.fps, 30.0);
        assert_eq!(loaded.frames[0], vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]);
        assert_eq!(loaded.frames[1], vec![[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]);
    }

    #[test]
    fn test_validate_header() {
        let path = std::path::Path::new("/tmp/test_validate_header.opc");
        let mut cache = PointCache::new(3, 60.0);
        cache
            .add_frame(vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
            .expect("should succeed");
        export_point_cache(&cache, path).expect("should succeed");

        let hdr = validate_point_cache_file(path).expect("should succeed");
        assert_eq!(hdr.vertex_count, 3);
        assert_eq!(hdr.frame_count, 1);
        assert_eq!(hdr.fps, 60.0);
    }

    #[test]
    fn test_validate_bad_magic() {
        let path = std::path::Path::new("/tmp/test_validate_bad_magic.opc");
        // Write a file with wrong magic
        std::fs::write(path, b"BAAD\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00").expect("should succeed");
        let result = validate_point_cache_file(path);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("invalid magic bytes") || msg.contains("OPC1"));
    }

    #[test]
    fn test_mesh_sequence_to_cache() {
        let m0 = make_mesh(vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]);
        let m1 = make_mesh(vec![[0.0, 1.0, 0.0], [1.0, 1.0, 0.0]]);
        let cache = mesh_sequence_to_cache(&[m0, m1], 24.0).expect("should succeed");
        assert_eq!(cache.vertex_count(), 2);
        assert_eq!(cache.frame_count(), 2);
        assert_eq!(cache.header.fps, 24.0);
    }

    #[test]
    fn test_mesh_sequence_mismatched_vertex_count() {
        let m0 = make_mesh(vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]);
        let m1 = make_mesh(vec![[0.0, 1.0, 0.0]]); // 1 vertex
        let result = mesh_sequence_to_cache(&[m0, m1], 24.0);
        assert!(result.is_err());
    }

    #[test]
    fn test_cache_frame_to_positions() {
        let mut cache = PointCache::new(2, 24.0);
        let f0 = vec![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        cache.add_frame(f0.clone()).expect("should succeed");
        let positions = cache_frame_to_positions(&cache, 0).expect("should succeed");
        assert_eq!(positions, f0);
        assert!(cache_frame_to_positions(&cache, 99).is_err());
    }
}