oxihuman-morph 0.1.2

Parametric morphology engine for human body generation — targets, blendshapes, FACS
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
//! Facial rigging with control bones and corrective shapes.

#[allow(dead_code)]
pub struct FacialBone {
    pub name: String,
    pub position: [f32; 3],
    pub rotation: [f32; 4], // quaternion
    pub parent: Option<usize>,
    pub children: Vec<usize>,
    pub weight: f32,
    pub influence_radius: f32,
}

#[allow(dead_code)]
pub struct CorrectiveShape {
    pub name: String,
    pub trigger_bone: String,
    pub trigger_angle: f32, // radians
    pub deltas: Vec<[f32; 3]>,
    pub weight: f32,
}

#[allow(dead_code)]
pub struct FacialRig {
    pub bones: Vec<FacialBone>,
    pub correctives: Vec<CorrectiveShape>,
    pub version: u32,
}

#[allow(dead_code)]
pub struct FacialPose {
    pub bone_rotations: Vec<[f32; 4]>, // per-bone quaternions
    pub blend_weights: Vec<f32>,       // per-corrective weights
}

#[allow(dead_code)]
pub fn new_facial_rig() -> FacialRig {
    FacialRig {
        bones: Vec::new(),
        correctives: Vec::new(),
        version: 1,
    }
}

#[allow(dead_code)]
pub fn add_bone(rig: &mut FacialRig, name: &str, pos: [f32; 3], parent: Option<usize>) -> usize {
    let idx = rig.bones.len();
    if let Some(p) = parent {
        if p < rig.bones.len() {
            rig.bones[p].children.push(idx);
        }
    }
    rig.bones.push(FacialBone {
        name: name.to_string(),
        position: pos,
        rotation: [0.0, 0.0, 0.0, 1.0],
        parent,
        children: Vec::new(),
        weight: 1.0,
        influence_radius: 0.1,
    });
    idx
}

#[allow(dead_code)]
pub fn add_corrective(rig: &mut FacialRig, c: CorrectiveShape) {
    rig.correctives.push(c);
}

#[allow(dead_code)]
pub fn default_facial_rig() -> FacialRig {
    let mut rig = new_facial_rig();
    // Root jaw
    let jaw = add_bone(&mut rig, "jaw", [0.0, -0.05, 0.0], None);
    // Eyes
    let _eye_l = add_bone(&mut rig, "eye_l", [-0.03, 0.02, 0.04], None);
    let _eye_r = add_bone(&mut rig, "eye_r", [0.03, 0.02, 0.04], None);
    // Brows
    let _brow_l = add_bone(&mut rig, "brow_l", [-0.03, 0.05, 0.03], None);
    let _brow_l_inner = add_bone(&mut rig, "brow_l_inner", [-0.015, 0.05, 0.03], None);
    let _brow_r = add_bone(&mut rig, "brow_r", [0.03, 0.05, 0.03], None);
    let _brow_r_inner = add_bone(&mut rig, "brow_r_inner", [0.015, 0.05, 0.03], None);
    // Cheeks
    let _cheek_l = add_bone(&mut rig, "cheek_l", [-0.04, 0.0, 0.03], None);
    let _cheek_r = add_bone(&mut rig, "cheek_r", [0.04, 0.0, 0.03], None);
    // Lips (children of jaw)
    let _lip_upper = add_bone(&mut rig, "lip_upper", [0.0, -0.01, 0.05], Some(jaw));
    let _lip_lower = add_bone(&mut rig, "lip_lower", [0.0, -0.03, 0.05], Some(jaw));
    let _lip_corner_l = add_bone(&mut rig, "lip_corner_l", [-0.02, -0.02, 0.05], Some(jaw));
    let _lip_corner_r = add_bone(&mut rig, "lip_corner_r", [0.02, -0.02, 0.05], Some(jaw));
    // Nose
    let _nose = add_bone(&mut rig, "nose", [0.0, 0.01, 0.06], None);
    rig
}

#[allow(dead_code)]
pub fn get_bone<'a>(rig: &'a FacialRig, name: &str) -> Option<&'a FacialBone> {
    rig.bones.iter().find(|b| b.name == name)
}

#[allow(dead_code)]
pub fn set_bone_rotation(rig: &mut FacialRig, name: &str, rot: [f32; 4]) -> bool {
    if let Some(bone) = rig.bones.iter_mut().find(|b| b.name == name) {
        bone.rotation = rot;
        true
    } else {
        false
    }
}

/// Compute corrective weights from bone angles.
#[allow(dead_code)]
pub fn evaluate_correctives(rig: &FacialRig) -> Vec<f32> {
    rig.correctives
        .iter()
        .map(|c| {
            // Find trigger bone rotation angle
            if let Some(bone) = rig.bones.iter().find(|b| b.name == c.trigger_bone) {
                let angle = quat_angle_from_identity(bone.rotation);
                let diff = (angle - c.trigger_angle).abs();
                let w = (1.0 - diff / std::f32::consts::PI).max(0.0);
                w * c.weight
            } else {
                0.0
            }
        })
        .collect()
}

fn quat_angle_from_identity(q: [f32; 4]) -> f32 {
    // w component of normalized quaternion → angle = 2 * acos(w)
    let w = q[3].clamp(-1.0, 1.0);
    2.0 * w.acos()
}

#[allow(dead_code)]
pub fn apply_facial_pose(
    rig: &FacialRig,
    pose: &FacialPose,
    base_positions: &[[f32; 3]],
) -> Vec<[f32; 3]> {
    let mut result: Vec<[f32; 3]> = base_positions.to_vec();

    // Apply corrective shape deltas
    for (i, &w) in pose.blend_weights.iter().enumerate() {
        if i >= rig.correctives.len() {
            break;
        }
        if w.abs() < 1e-6 {
            continue;
        }
        let corr = &rig.correctives[i];
        for (j, pos) in result.iter_mut().enumerate() {
            if j < corr.deltas.len() {
                pos[0] += corr.deltas[j][0] * w;
                pos[1] += corr.deltas[j][1] * w;
                pos[2] += corr.deltas[j][2] * w;
            }
        }
    }
    result
}

#[allow(dead_code)]
pub fn bone_count(rig: &FacialRig) -> usize {
    rig.bones.len()
}

#[allow(dead_code)]
pub fn corrective_count(rig: &FacialRig) -> usize {
    rig.correctives.len()
}

#[allow(dead_code)]
pub fn facial_rig_to_json(rig: &FacialRig) -> String {
    let mut s = String::from("{");
    s.push_str(&format!(
        r#""version":{},"bone_count":{},"corrective_count":{}"#,
        rig.version,
        rig.bones.len(),
        rig.correctives.len()
    ));
    s.push('}');
    s
}

#[allow(dead_code)]
pub fn identity_pose(rig: &FacialRig) -> FacialPose {
    FacialPose {
        bone_rotations: vec![[0.0, 0.0, 0.0, 1.0]; rig.bones.len()],
        blend_weights: vec![0.0; rig.correctives.len()],
    }
}

#[allow(dead_code)]
pub fn blend_facial_poses(a: &FacialPose, b: &FacialPose, t: f32) -> FacialPose {
    let len = a.bone_rotations.len().min(b.bone_rotations.len());
    let bone_rotations = (0..len)
        .map(|i| {
            let qa = a.bone_rotations[i];
            let qb = b.bone_rotations[i];
            slerp_quat(qa, qb, t)
        })
        .collect();

    let wlen = a.blend_weights.len().min(b.blend_weights.len());
    let blend_weights = (0..wlen)
        .map(|i| a.blend_weights[i] * (1.0 - t) + b.blend_weights[i] * t)
        .collect();

    FacialPose {
        bone_rotations,
        blend_weights,
    }
}

fn slerp_quat(a: [f32; 4], b: [f32; 4], t: f32) -> [f32; 4] {
    let mut dot = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
    let bq = if dot < 0.0 {
        dot = -dot;
        [-b[0], -b[1], -b[2], -b[3]]
    } else {
        b
    };

    if dot > 0.9995 {
        let r = [
            a[0] + t * (bq[0] - a[0]),
            a[1] + t * (bq[1] - a[1]),
            a[2] + t * (bq[2] - a[2]),
            a[3] + t * (bq[3] - a[3]),
        ];
        normalize_q(r)
    } else {
        let theta_0 = dot.acos();
        let theta = theta_0 * t;
        let sin_theta = theta.sin();
        let sin_theta_0 = theta_0.sin();
        let s0 = (theta_0 * (1.0 - t)).sin() / sin_theta_0;
        let s1 = sin_theta / sin_theta_0;
        [
            s0 * a[0] + s1 * bq[0],
            s0 * a[1] + s1 * bq[1],
            s0 * a[2] + s1 * bq[2],
            s0 * a[3] + s1 * bq[3],
        ]
    }
}

fn normalize_q(q: [f32; 4]) -> [f32; 4] {
    let len = (q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]).sqrt();
    if len < 1e-9 {
        [0.0, 0.0, 0.0, 1.0]
    } else {
        [q[0] / len, q[1] / len, q[2] / len, q[3] / len]
    }
}

#[allow(dead_code)]
pub fn quat_angle_between(q1: [f32; 4], q2: [f32; 4]) -> f32 {
    // angle between two quaternions: 2 * acos(|q1 . q2|)
    let dot = (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2] + q1[3] * q2[3])
        .abs()
        .clamp(0.0, 1.0);
    2.0 * dot.acos()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::PI;

    #[test]
    fn test_new_facial_rig_empty() {
        let rig = new_facial_rig();
        assert_eq!(rig.bones.len(), 0);
        assert_eq!(rig.correctives.len(), 0);
        assert_eq!(rig.version, 1);
    }

    #[test]
    fn test_add_bone_returns_index() {
        let mut rig = new_facial_rig();
        let idx = add_bone(&mut rig, "jaw", [0.0, 0.0, 0.0], None);
        assert_eq!(idx, 0);
        assert_eq!(rig.bones.len(), 1);
        assert_eq!(rig.bones[0].name, "jaw");
    }

    #[test]
    fn test_add_bone_parent_child_link() {
        let mut rig = new_facial_rig();
        let p = add_bone(&mut rig, "root", [0.0, 0.0, 0.0], None);
        let c = add_bone(&mut rig, "child", [1.0, 0.0, 0.0], Some(p));
        assert!(rig.bones[p].children.contains(&c));
        assert_eq!(rig.bones[c].parent, Some(p));
    }

    #[test]
    fn test_add_corrective() {
        let mut rig = new_facial_rig();
        add_bone(&mut rig, "jaw", [0.0, 0.0, 0.0], None);
        let cs = CorrectiveShape {
            name: "jaw_open".to_string(),
            trigger_bone: "jaw".to_string(),
            trigger_angle: PI / 4.0,
            deltas: vec![[0.0, -0.01, 0.0]; 3],
            weight: 1.0,
        };
        add_corrective(&mut rig, cs);
        assert_eq!(rig.correctives.len(), 1);
    }

    #[test]
    fn test_default_facial_rig_non_empty() {
        let rig = default_facial_rig();
        assert!(rig.bones.len() >= 14);
    }

    #[test]
    fn test_get_bone_found() {
        let rig = default_facial_rig();
        let bone = get_bone(&rig, "jaw");
        assert!(bone.is_some());
        assert_eq!(bone.expect("should succeed").name, "jaw");
    }

    #[test]
    fn test_get_bone_not_found() {
        let rig = default_facial_rig();
        assert!(get_bone(&rig, "nonexistent").is_none());
    }

    #[test]
    fn test_bone_count() {
        let rig = default_facial_rig();
        assert_eq!(bone_count(&rig), rig.bones.len());
    }

    #[test]
    fn test_corrective_count_zero() {
        let rig = default_facial_rig();
        assert_eq!(corrective_count(&rig), 0);
    }

    #[test]
    fn test_set_bone_rotation_success() {
        let mut rig = default_facial_rig();
        let rot = [0.0, 0.0, 0.707, 0.707];
        let ok = set_bone_rotation(&mut rig, "jaw", rot);
        assert!(ok);
        let bone = get_bone(&rig, "jaw").expect("should succeed");
        assert_eq!(bone.rotation, rot);
    }

    #[test]
    fn test_set_bone_rotation_fail() {
        let mut rig = default_facial_rig();
        let ok = set_bone_rotation(&mut rig, "ghost_bone", [0.0, 0.0, 0.0, 1.0]);
        assert!(!ok);
    }

    #[test]
    fn test_identity_pose() {
        let rig = default_facial_rig();
        let pose = identity_pose(&rig);
        assert_eq!(pose.bone_rotations.len(), rig.bones.len());
        for rot in &pose.bone_rotations {
            assert_eq!(*rot, [0.0, 0.0, 0.0, 1.0]);
        }
        for w in &pose.blend_weights {
            assert_eq!(*w, 0.0);
        }
    }

    #[test]
    fn test_blend_facial_poses() {
        let rig = default_facial_rig();
        let a = identity_pose(&rig);
        let mut b = identity_pose(&rig);
        if !b.bone_rotations.is_empty() {
            b.bone_rotations[0] = [0.0, 0.0, 0.707, 0.707];
        }
        let blended = blend_facial_poses(&a, &b, 0.5);
        assert_eq!(blended.bone_rotations.len(), a.bone_rotations.len());
    }

    #[test]
    fn test_evaluate_correctives_empty() {
        let rig = default_facial_rig();
        let weights = evaluate_correctives(&rig);
        assert!(weights.is_empty());
    }

    #[test]
    fn test_apply_facial_pose_no_correctives() {
        let rig = default_facial_rig();
        let pose = identity_pose(&rig);
        let base = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
        let result = apply_facial_pose(&rig, &pose, &base);
        assert_eq!(result.len(), base.len());
        for (r, b) in result.iter().zip(base.iter()) {
            assert!((r[0] - b[0]).abs() < 1e-6);
        }
    }

    #[test]
    fn test_quat_angle_between_identity() {
        let q = [0.0, 0.0, 0.0, 1.0];
        let angle = quat_angle_between(q, q);
        assert!(angle < 1e-5);
    }

    #[test]
    fn test_facial_rig_to_json_contains_version() {
        let rig = default_facial_rig();
        let json = facial_rig_to_json(&rig);
        assert!(json.contains("version"));
        assert!(json.contains("bone_count"));
    }
}