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
//! FBX format export stub (ASCII FBX 7.4 compatible header + geometry).

#[allow(dead_code)]
pub struct FbxNode {
    pub name: String,
    pub id: u64,
    pub parent_id: Option<u64>,
    pub transform: [[f32; 4]; 4],
}

#[allow(dead_code)]
pub struct FbxMesh {
    pub node_id: u64,
    pub positions: Vec<[f32; 3]>,
    pub normals: Vec<[f32; 3]>,
    pub uvs: Vec<[f32; 2]>,
    pub indices: Vec<u32>,
}

#[allow(dead_code)]
pub struct FbxScene {
    pub name: String,
    pub nodes: Vec<FbxNode>,
    pub meshes: Vec<FbxMesh>,
    pub up_axis: u8,
    pub units: f32,
}

#[allow(dead_code)]
pub struct FbxExport {
    pub content: String,
    pub version: u32,
}

#[allow(dead_code)]
pub fn new_fbx_scene(name: &str) -> FbxScene {
    FbxScene {
        name: name.to_string(),
        nodes: Vec::new(),
        meshes: Vec::new(),
        up_axis: 1,
        units: 1.0,
    }
}

#[allow(dead_code)]
pub fn add_fbx_node(scene: &mut FbxScene, name: &str, parent: Option<u64>) -> u64 {
    let id = scene.nodes.len() as u64 + 1;
    scene.nodes.push(FbxNode {
        name: name.to_string(),
        id,
        parent_id: parent,
        transform: fbx_identity_matrix(),
    });
    id
}

#[allow(dead_code)]
pub fn add_fbx_mesh(scene: &mut FbxScene, mesh: FbxMesh) {
    scene.meshes.push(mesh);
}

#[allow(dead_code)]
pub fn fbx_identity_matrix() -> [[f32; 4]; 4] {
    [
        [1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 1.0],
    ]
}

#[allow(dead_code)]
pub fn fbx_header(version: u32) -> String {
    let major = version / 1000;
    let minor = (version % 1000) / 100;
    let patch = version % 100;
    format!(
        "; FBX {major}.{minor}.{patch} project file\n\
         ; Copyright (C) 1997-2023 Autodesk Inc. and/or its licensors.\n\
         ; All Rights Reserved.\n\
         \n\
         FBXHeaderExtension: {{\n\
         \tFBXHeaderVersion: 1003\n\
         \tFBXVersion: {version}\n\
         }}\n"
    )
}

#[allow(dead_code)]
pub fn fbx_node_to_string(node: &FbxNode) -> String {
    let parent_str = match node.parent_id {
        Some(pid) => format!("\tParentId: {pid}\n"),
        None => String::new(),
    };
    let t = &node.transform;
    format!(
        "Model: {id}, \"{name}\", \"Mesh\" {{\n\
         {parent_str}\
         \tTransform: {r0:?}, {r1:?}, {r2:?}, {r3:?}\n\
         }}\n",
        id = node.id,
        name = node.name,
        r0 = t[0],
        r1 = t[1],
        r2 = t[2],
        r3 = t[3],
    )
}

#[allow(dead_code)]
pub fn fbx_mesh_to_string(mesh: &FbxMesh) -> String {
    let vert_count = mesh.positions.len();
    let mut s = format!(
        "Geometry: {id}, \"Geometry::\", \"Mesh\" {{\n\
         \tVertices: *{vert_count} {{\n",
        id = mesh.node_id,
    );
    s.push_str("\t\ta: ");
    let coords: Vec<String> = mesh
        .positions
        .iter()
        .map(|p| format!("{},{},{}", p[0], p[1], p[2]))
        .collect();
    s.push_str(&coords.join(","));
    s.push('\n');
    s.push_str("\t}\n");

    let idx_count = mesh.indices.len();
    s.push_str(&format!("\tPolygonVertexIndex: *{idx_count} {{\n\t\ta: "));
    let idx_strs: Vec<String> = mesh
        .indices
        .chunks(3)
        .flat_map(|tri| {
            if tri.len() == 3 {
                vec![
                    tri[0].to_string(),
                    tri[1].to_string(),
                    format!("-{}", tri[2] + 1),
                ]
            } else {
                tri.iter().map(|v| v.to_string()).collect()
            }
        })
        .collect();
    s.push_str(&idx_strs.join(","));
    s.push_str("\n\t}\n}\n");
    s
}

#[allow(dead_code)]
pub fn fbx_connections(scene: &FbxScene) -> String {
    let mut s = "Connections: {\n".to_string();
    for node in &scene.nodes {
        let parent = node.parent_id.unwrap_or(0);
        s.push_str(&format!("\tC: \"OO\", {}, {}\n", node.id, parent));
    }
    for mesh in &scene.meshes {
        s.push_str(&format!(
            "\tC: \"OO\", Geo_{}, {}\n",
            mesh.node_id, mesh.node_id
        ));
    }
    s.push_str("}\n");
    s
}

#[allow(dead_code)]
#[deprecated(
    since = "0.1.1",
    note = "ASCII FBX export is superseded by the binary writer; \
            use fbx_binary::export_mesh_fbx_binary instead"
)]
pub fn export_fbx_ascii(scene: &FbxScene) -> FbxExport {
    let version = 7400u32;
    let mut content = fbx_header(version);

    content.push_str("\nObjects: {\n");
    for node in &scene.nodes {
        content.push_str(&fbx_node_to_string(node));
    }
    for mesh in &scene.meshes {
        content.push_str(&fbx_mesh_to_string(mesh));
    }
    content.push_str("}\n\n");
    content.push_str(&fbx_connections(scene));

    FbxExport { content, version }
}

#[allow(dead_code)]
pub fn node_count_fbx(scene: &FbxScene) -> usize {
    scene.nodes.len()
}

#[allow(dead_code)]
pub fn mesh_count_fbx(scene: &FbxScene) -> usize {
    scene.meshes.len()
}

#[allow(dead_code)]
pub fn validate_fbx_scene(scene: &FbxScene) -> Vec<String> {
    let mut issues = Vec::new();
    if scene.name.is_empty() {
        issues.push("Scene name is empty".to_string());
    }
    let node_ids: Vec<u64> = scene.nodes.iter().map(|n| n.id).collect();
    for node in &scene.nodes {
        if let Some(pid) = node.parent_id {
            if !node_ids.contains(&pid) {
                issues.push(format!(
                    "Node '{}' references non-existent parent id {pid}",
                    node.name
                ));
            }
        }
    }
    for mesh in &scene.meshes {
        if mesh.positions.is_empty() {
            issues.push(format!("Mesh for node {} has no positions", mesh.node_id));
        }
        if mesh.indices.len() % 3 != 0 {
            issues.push(format!(
                "Mesh for node {} has non-triangulated index count {}",
                mesh.node_id,
                mesh.indices.len()
            ));
        }
    }
    if scene.units <= 0.0 {
        issues.push("Scene units must be positive".to_string());
    }
    issues
}

#[allow(dead_code)]
pub fn fbx_export_size_estimate(export: &FbxExport) -> usize {
    export.content.len()
}

#[cfg(test)]
#[allow(deprecated)]
mod tests {
    use super::*;

    #[test]
    fn test_new_fbx_scene() {
        let scene = new_fbx_scene("TestScene");
        assert_eq!(scene.name, "TestScene");
        assert!(scene.nodes.is_empty());
        assert!(scene.meshes.is_empty());
        assert_eq!(scene.up_axis, 1);
        assert!((scene.units - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_add_fbx_node_no_parent() {
        let mut scene = new_fbx_scene("S");
        let id = add_fbx_node(&mut scene, "Root", None);
        assert_eq!(id, 1);
        assert_eq!(scene.nodes.len(), 1);
        assert_eq!(scene.nodes[0].name, "Root");
        assert!(scene.nodes[0].parent_id.is_none());
    }

    #[test]
    fn test_add_fbx_node_with_parent() {
        let mut scene = new_fbx_scene("S");
        let root = add_fbx_node(&mut scene, "Root", None);
        let child = add_fbx_node(&mut scene, "Child", Some(root));
        assert_eq!(child, 2);
        assert_eq!(scene.nodes[1].parent_id, Some(root));
    }

    #[test]
    fn test_add_fbx_mesh() {
        let mut scene = new_fbx_scene("S");
        let mesh = FbxMesh {
            node_id: 1,
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![[0.0, 0.0, 1.0]; 3],
            uvs: vec![[0.0, 0.0]; 3],
            indices: vec![0, 1, 2],
        };
        add_fbx_mesh(&mut scene, mesh);
        assert_eq!(scene.meshes.len(), 1);
    }

    #[test]
    fn test_fbx_header_contains_fbx() {
        let header = fbx_header(7400);
        assert!(header.contains("FBX"));
        assert!(header.contains("7400"));
    }

    #[test]
    fn test_fbx_identity_matrix() {
        let m = fbx_identity_matrix();
        for (i, row) in m.iter().enumerate() {
            for (j, val) in row.iter().enumerate() {
                let expected = if i == j { 1.0f32 } else { 0.0f32 };
                assert!((val - expected).abs() < 1e-6, "m[{i}][{j}] = {val}");
            }
        }
    }

    #[test]
    fn test_export_fbx_ascii_non_empty() {
        let mut scene = new_fbx_scene("Test");
        add_fbx_node(&mut scene, "Root", None);
        let export = export_fbx_ascii(&scene);
        assert!(!export.content.is_empty());
        assert_eq!(export.version, 7400);
    }

    #[test]
    fn test_node_count_fbx() {
        let mut scene = new_fbx_scene("S");
        assert_eq!(node_count_fbx(&scene), 0);
        add_fbx_node(&mut scene, "A", None);
        add_fbx_node(&mut scene, "B", None);
        assert_eq!(node_count_fbx(&scene), 2);
    }

    #[test]
    fn test_mesh_count_fbx() {
        let mut scene = new_fbx_scene("S");
        assert_eq!(mesh_count_fbx(&scene), 0);
        let mesh = FbxMesh {
            node_id: 1,
            positions: vec![[0.0; 3]],
            normals: vec![],
            uvs: vec![],
            indices: vec![],
        };
        add_fbx_mesh(&mut scene, mesh);
        assert_eq!(mesh_count_fbx(&scene), 1);
    }

    #[test]
    fn test_validate_fbx_scene_passes() {
        let mut scene = new_fbx_scene("Valid");
        add_fbx_node(&mut scene, "Root", None);
        let issues = validate_fbx_scene(&scene);
        assert!(issues.is_empty(), "Expected no issues, got: {issues:?}");
    }

    #[test]
    fn test_validate_fbx_scene_empty_name() {
        let scene = new_fbx_scene("");
        let issues = validate_fbx_scene(&scene);
        assert!(!issues.is_empty());
    }

    #[test]
    fn test_fbx_mesh_to_string_contains_vertex_count() {
        let mesh = FbxMesh {
            node_id: 42,
            positions: vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
            normals: vec![],
            uvs: vec![],
            indices: vec![0, 1, 2],
        };
        let s = fbx_mesh_to_string(&mesh);
        assert!(s.contains("*3"), "Expected vertex count 3 in: {s}");
    }

    #[test]
    fn test_fbx_connections_non_empty() {
        let mut scene = new_fbx_scene("S");
        add_fbx_node(&mut scene, "Root", None);
        let conn = fbx_connections(&scene);
        assert!(conn.contains("Connections:"));
        assert!(conn.contains("OO"));
    }

    #[test]
    fn test_fbx_export_size_estimate() {
        let mut scene = new_fbx_scene("S");
        add_fbx_node(&mut scene, "Root", None);
        let export = export_fbx_ascii(&scene);
        let size = fbx_export_size_estimate(&export);
        assert_eq!(size, export.content.len());
        assert!(size > 0);
    }

    #[test]
    fn test_fbx_node_to_string() {
        let node = FbxNode {
            name: "TestNode".to_string(),
            id: 100,
            parent_id: None,
            transform: fbx_identity_matrix(),
        };
        let s = fbx_node_to_string(&node);
        assert!(s.contains("TestNode"));
        assert!(s.contains("100"));
    }

    #[test]
    fn test_validate_fbx_bad_parent() {
        let mut scene = new_fbx_scene("S");
        scene.nodes.push(FbxNode {
            name: "Orphan".to_string(),
            id: 1,
            parent_id: Some(999),
            transform: fbx_identity_matrix(),
        });
        let issues = validate_fbx_scene(&scene);
        assert!(!issues.is_empty());
    }
}