mmd-anim-runtime 0.3.1

Renderer-independent MMD animation runtime core
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use std::fmt;

use crate::{
    AppendTransformInit, BoneIndex, BoneInit, IkAngleLimit, IkLinkInit, IkSolverInit, MorphIndex,
    MorphInit, build_morph_init_from_offsets,
};

#[cfg(test)]
use crate::MorphOffsetSpan;

pub struct FlatBoneInput<'a> {
    pub parent_indices: &'a [i32],
    pub rest_positions_xyz: &'a [f32],
    pub inverse_bind_matrices: &'a [f32],
    pub transform_orders: &'a [i32],
}

#[derive(Debug, Clone, Copy)]
pub struct FlatIkLinkInput {
    pub bone_index: u32,
    pub has_angle_limit: bool,
    pub angle_limit_min_xyz: [f32; 3],
    pub angle_limit_max_xyz: [f32; 3],
}

#[derive(Debug, Clone, Copy)]
pub struct FlatIkSolverInput {
    pub ik_bone_index: u32,
    pub target_bone_index: u32,
    pub link_offset: usize,
    pub link_count: usize,
    pub iteration_count: u32,
    pub limit_angle: f32,
}

#[derive(Debug, Clone, Copy)]
pub struct FlatAppendTransformInput {
    pub target_bone_index: u32,
    pub source_bone_index: u32,
    pub ratio: f32,
    pub affect_rotation: bool,
    pub affect_translation: bool,
    pub local: bool,
}

#[derive(Debug, Clone, Copy)]
pub struct FlatBoneMorphInput {
    pub morph_index: u32,
    pub target_bone_index: u32,
    pub position_offset_xyz: [f32; 3],
    pub rotation_offset_xyzw: [f32; 4],
}

#[derive(Debug, Clone, Copy)]
pub struct FlatGroupMorphInput {
    pub morph_index: u32,
    pub child_morph_index: u32,
    pub ratio: f32,
}

pub struct FlatMorphInput<'a> {
    pub morph_count: u32,
    pub bone_morphs: &'a [FlatBoneMorphInput],
    pub group_morphs: &'a [FlatGroupMorphInput],
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlatModelInputError {
    EmptyBoneSet,
    RestPositionsLen,
    InverseBindMatricesLen,
    TransformOrdersLen,
    InvalidParentIndex,
    RangeOverflow,
    RangeOutOfBounds,
    MorphCountZeroWithData,
    BoneMorphIndexOutOfRange,
    GroupMorphIndexOutOfRange,
}

impl fmt::Display for FlatModelInputError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let message = match self {
            Self::EmptyBoneSet => "model must contain at least one bone",
            Self::RestPositionsLen => "rest_positions_xyz must contain bone_count * 3 values",
            Self::InverseBindMatricesLen => {
                "inverse_bind_matrices must contain bone_count * 16 values"
            }
            Self::TransformOrdersLen => "transform_orders must contain bone_count values",
            Self::InvalidParentIndex => "parent index must be -1 or non-negative",
            Self::RangeOverflow => "range overflow",
            Self::RangeOutOfBounds => "track keyframe range is out of bounds",
            Self::MorphCountZeroWithData => {
                "morph_count must be non-zero when morph data is provided"
            }
            Self::BoneMorphIndexOutOfRange => "bone morph index is out of range",
            Self::GroupMorphIndexOutOfRange => "group morph index is out of range",
        };
        f.write_str(message)
    }
}

impl std::error::Error for FlatModelInputError {}

pub fn build_bones_from_flat(
    input: FlatBoneInput<'_>,
) -> Result<Vec<BoneInit>, FlatModelInputError> {
    if input.parent_indices.is_empty() {
        return Err(FlatModelInputError::EmptyBoneSet);
    }
    if input.rest_positions_xyz.len() != input.parent_indices.len() * 3 {
        return Err(FlatModelInputError::RestPositionsLen);
    }
    if !input.inverse_bind_matrices.is_empty()
        && input.inverse_bind_matrices.len() != input.parent_indices.len() * 16
    {
        return Err(FlatModelInputError::InverseBindMatricesLen);
    }
    if !input.transform_orders.is_empty()
        && input.transform_orders.len() != input.parent_indices.len()
    {
        return Err(FlatModelInputError::TransformOrdersLen);
    }

    let mut bones = Vec::with_capacity(input.parent_indices.len());
    for (bone_index, parent_index) in input.parent_indices.iter().enumerate() {
        let parent = match *parent_index {
            -1 => None,
            parent if parent >= 0 => Some(BoneIndex(parent as u32)),
            _ => return Err(FlatModelInputError::InvalidParentIndex),
        };
        let position_offset = bone_index * 3;
        let mut bone = BoneInit::new(
            parent,
            glam::Vec3A::new(
                input.rest_positions_xyz[position_offset],
                input.rest_positions_xyz[position_offset + 1],
                input.rest_positions_xyz[position_offset + 2],
            ),
        );
        if !input.inverse_bind_matrices.is_empty() {
            let inverse_bind_offset = bone_index * 16;
            let inverse_bind_matrix = input.inverse_bind_matrices
                [inverse_bind_offset..inverse_bind_offset + 16]
                .try_into()
                .expect("validated inverse bind matrix slice length");
            bone.inverse_bind_matrix = glam::Mat4::from_cols_array(inverse_bind_matrix);
        }
        if !input.transform_orders.is_empty() {
            bone.transform_order = input.transform_orders[bone_index];
        }
        bones.push(bone);
    }

    Ok(bones)
}

pub fn build_ik_solvers_from_flat(
    solvers: &[FlatIkSolverInput],
    links: &[FlatIkLinkInput],
) -> Result<Vec<IkSolverInit>, FlatModelInputError> {
    build_ik_solvers_from_flat_iter(solvers.iter().copied(), links)
}

pub fn build_ik_solvers_from_flat_iter(
    solvers: impl IntoIterator<Item = FlatIkSolverInput>,
    links: &[FlatIkLinkInput],
) -> Result<Vec<IkSolverInit>, FlatModelInputError> {
    solvers
        .into_iter()
        .map(|solver| {
            let link_end = solver
                .link_offset
                .checked_add(solver.link_count)
                .ok_or(FlatModelInputError::RangeOverflow)?;
            let solver_links = links
                .get(solver.link_offset..link_end)
                .ok_or(FlatModelInputError::RangeOutOfBounds)?
                .iter()
                .map(|link| {
                    let mut init = IkLinkInit::new(BoneIndex(link.bone_index));
                    if link.has_angle_limit {
                        init = init.with_angle_limit(IkAngleLimit::new(
                            glam::Vec3A::new(
                                link.angle_limit_min_xyz[0],
                                link.angle_limit_min_xyz[1],
                                link.angle_limit_min_xyz[2],
                            ),
                            glam::Vec3A::new(
                                link.angle_limit_max_xyz[0],
                                link.angle_limit_max_xyz[1],
                                link.angle_limit_max_xyz[2],
                            ),
                        ));
                    }
                    init
                })
                .collect();

            Ok(IkSolverInit {
                ik_bone: BoneIndex(solver.ik_bone_index),
                target_bone: BoneIndex(solver.target_bone_index),
                links: solver_links,
                iteration_count: solver.iteration_count,
                limit_angle: solver.limit_angle,
            })
        })
        .collect()
}

pub fn build_morph_init_from_flat(
    input: FlatMorphInput<'_>,
) -> Result<MorphInit, FlatModelInputError> {
    build_morph_init_from_flat_iter(
        input.morph_count,
        input.bone_morphs.iter().copied(),
        input.group_morphs.iter().copied(),
    )
}

pub fn build_morph_init_from_flat_iter(
    morph_count: u32,
    bone_morphs: impl IntoIterator<Item = FlatBoneMorphInput>,
    group_morphs: impl IntoIterator<Item = FlatGroupMorphInput>,
) -> Result<MorphInit, FlatModelInputError> {
    let bone_morphs = bone_morphs.into_iter().collect::<Vec<_>>();
    let group_morphs = group_morphs.into_iter().collect::<Vec<_>>();
    if morph_count == 0 {
        if bone_morphs.is_empty() && group_morphs.is_empty() {
            return Ok(MorphInit::default());
        }
        return Err(FlatModelInputError::MorphCountZeroWithData);
    }
    let bone_offsets = bone_morphs
        .iter()
        .map(|entry| {
            (
                MorphIndex(entry.morph_index),
                crate::BoneMorphOffset {
                    target_bone: BoneIndex(entry.target_bone_index),
                    position_offset: glam::Vec3A::from_array(entry.position_offset_xyz),
                    rotation_offset: glam::Quat::from_xyzw(
                        entry.rotation_offset_xyzw[0],
                        entry.rotation_offset_xyzw[1],
                        entry.rotation_offset_xyzw[2],
                        entry.rotation_offset_xyzw[3],
                    ),
                },
            )
        })
        .collect();
    let group_offsets = group_morphs
        .iter()
        .map(|entry| {
            (
                MorphIndex(entry.morph_index),
                crate::GroupMorphOffset {
                    child_morph: MorphIndex(entry.child_morph_index),
                    ratio: entry.ratio,
                },
            )
        })
        .collect();
    build_morph_init_from_offsets(morph_count, bone_offsets, group_offsets).map_err(|error| {
        match error {
            crate::ModelBuildError::MorphCountZeroWithData => {
                FlatModelInputError::MorphCountZeroWithData
            }
            crate::ModelBuildError::InvalidBoneMorphMorph { .. } => {
                FlatModelInputError::BoneMorphIndexOutOfRange
            }
            crate::ModelBuildError::InvalidGroupMorph { .. }
            | crate::ModelBuildError::InvalidGroupMorphChild { .. }
            | crate::ModelBuildError::GroupMorphCycle { .. }
            | crate::ModelBuildError::GroupMorphCycleAt { .. } => {
                FlatModelInputError::GroupMorphIndexOutOfRange
            }
            _ => FlatModelInputError::RangeOverflow,
        }
    })
}

pub fn build_append_transforms_from_flat(
    append_transforms: &[FlatAppendTransformInput],
) -> Vec<AppendTransformInit> {
    build_append_transforms_from_flat_iter(append_transforms.iter().copied())
}

pub fn build_append_transforms_from_flat_iter(
    append_transforms: impl IntoIterator<Item = FlatAppendTransformInput>,
) -> Vec<AppendTransformInit> {
    append_transforms
        .into_iter()
        .map(|append| {
            let mut init = AppendTransformInit::new(
                BoneIndex(append.target_bone_index),
                BoneIndex(append.source_bone_index),
                append.ratio,
            );
            if append.affect_rotation {
                init = init.with_rotation();
            }
            if append.affect_translation {
                init = init.with_translation();
            }
            if append.local {
                init = init.with_local();
            }
            init
        })
        .collect()
}

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

    #[test]
    fn builds_bones_from_flat_arrays() {
        let bones = build_bones_from_flat(FlatBoneInput {
            parent_indices: &[-1, 0],
            rest_positions_xyz: &[0.0, 1.0, 2.0, 3.0, 4.0, 5.0],
            inverse_bind_matrices: &[],
            transform_orders: &[2, 1],
        })
        .unwrap();

        assert_eq!(bones.len(), 2);
        assert_eq!(bones[0].parent, None);
        assert_eq!(bones[1].parent, Some(BoneIndex(0)));
        assert_eq!(bones[1].rest_position.to_array(), [3.0, 4.0, 5.0]);
        assert_eq!(bones[0].transform_order, 2);
        assert_eq!(bones[1].transform_order, 1);
    }

    #[test]
    fn rejects_invalid_flat_bone_arrays() {
        let error = build_bones_from_flat(FlatBoneInput {
            parent_indices: &[0],
            rest_positions_xyz: &[0.0, 1.0],
            inverse_bind_matrices: &[],
            transform_orders: &[],
        })
        .unwrap_err();

        assert_eq!(error, FlatModelInputError::RestPositionsLen);
        assert_eq!(
            error.to_string(),
            "rest_positions_xyz must contain bone_count * 3 values"
        );
    }

    #[test]
    fn builds_ik_solvers_from_flat_arrays() {
        let solvers = build_ik_solvers_from_flat(
            &[FlatIkSolverInput {
                ik_bone_index: 3,
                target_bone_index: 2,
                link_offset: 0,
                link_count: 1,
                iteration_count: 10,
                limit_angle: 0.5,
            }],
            &[FlatIkLinkInput {
                bone_index: 1,
                has_angle_limit: true,
                angle_limit_min_xyz: [-1.0, -2.0, -3.0],
                angle_limit_max_xyz: [1.0, 2.0, 3.0],
            }],
        )
        .unwrap();

        assert_eq!(solvers.len(), 1);
        assert_eq!(solvers[0].ik_bone, BoneIndex(3));
        assert_eq!(solvers[0].target_bone, BoneIndex(2));
        assert_eq!(solvers[0].links.len(), 1);
        assert!(solvers[0].links[0].angle_limit.is_some());
    }

    #[test]
    fn builds_append_transforms_from_flat_arrays() {
        let append_transforms = build_append_transforms_from_flat(&[FlatAppendTransformInput {
            target_bone_index: 2,
            source_bone_index: 1,
            ratio: 0.25,
            affect_rotation: true,
            affect_translation: false,
            local: true,
        }]);

        assert_eq!(append_transforms.len(), 1);
        assert_eq!(append_transforms[0].target_bone, BoneIndex(2));
        assert_eq!(append_transforms[0].source_bone, BoneIndex(1));
        assert_eq!(append_transforms[0].ratio, 0.25);
        assert!(append_transforms[0].affect_rotation);
        assert!(!append_transforms[0].affect_translation);
        assert!(append_transforms[0].local);
    }

    #[test]
    fn builds_morph_init_from_flat_arrays() {
        let morph = build_morph_init_from_flat(FlatMorphInput {
            morph_count: 2,
            bone_morphs: &[FlatBoneMorphInput {
                morph_index: 1,
                target_bone_index: 0,
                position_offset_xyz: [1.0, 2.0, 3.0],
                rotation_offset_xyzw: [0.0, 0.0, 0.0, 1.0],
            }],
            group_morphs: &[],
        })
        .unwrap();

        assert_eq!(morph.morph_count, 2);
        assert_eq!(morph.bone_offsets.len(), 1);
        assert_eq!(morph.bone_spans.len(), 2);
        assert_eq!(morph.bone_spans[0], MorphOffsetSpan::default());
        assert_eq!(morph.bone_spans[1], MorphOffsetSpan { start: 0, count: 1 });
        assert_eq!(morph.bone_offsets[0].target_bone, BoneIndex(0));
    }

    #[test]
    fn rejects_out_of_range_bone_morph_index() {
        let error = build_morph_init_from_flat(FlatMorphInput {
            morph_count: 1,
            bone_morphs: &[FlatBoneMorphInput {
                morph_index: 1,
                target_bone_index: 0,
                position_offset_xyz: [0.0, 0.0, 0.0],
                rotation_offset_xyzw: [0.0, 0.0, 0.0, 1.0],
            }],
            group_morphs: &[],
        })
        .unwrap_err();

        assert_eq!(error, FlatModelInputError::BoneMorphIndexOutOfRange);
    }

    #[test]
    fn rejects_zero_morph_count_with_data() {
        let error = build_morph_init_from_flat(FlatMorphInput {
            morph_count: 0,
            bone_morphs: &[FlatBoneMorphInput {
                morph_index: 0,
                target_bone_index: 0,
                position_offset_xyz: [0.0, 0.0, 0.0],
                rotation_offset_xyzw: [0.0, 0.0, 0.0, 1.0],
            }],
            group_morphs: &[],
        })
        .unwrap_err();

        assert_eq!(error, FlatModelInputError::MorphCountZeroWithData);
        assert_eq!(
            error.to_string(),
            "morph_count must be non-zero when morph data is provided"
        );
    }
}