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
use crate::BoneKeyframe;
pub fn get_surrounding_keyframes(
keyframes: &Vec<BoneKeyframe>,
current_frame: f32,
) -> (BoneKeyframe, BoneKeyframe) {
let mut closest_lower = None;
let mut closest_upper = None;
for (idx, frame) in keyframes.iter().enumerate() {
if (frame.frame() as f32) <= current_frame {
closest_lower = Some(idx)
}
if (frame.frame() as f32) >= current_frame {
closest_upper = Some(idx);
break;
}
}
if closest_upper.is_none() {
closest_upper = closest_lower;
} else if closest_lower.is_none() {
closest_lower = closest_upper;
}
(
keyframes[closest_lower.unwrap()],
keyframes[closest_upper.unwrap()],
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::bone_dual_quat_identity;
#[test]
fn surrounding_keyframes() {
let keyframes = vec![
BoneKeyframe::new(2, bone_dual_quat_identity()),
BoneKeyframe::new(5, bone_dual_quat_identity()),
BoneKeyframe::new(8, bone_dual_quat_identity()),
];
let tests = vec![
(0.0, [0, 0]),
(4.0, [0, 1]),
(5.0, [1, 1]),
(7.0, [1, 2]),
(8.0, [2, 2]),
(9.0, [2, 2]),
];
for (idx, (elapsed_frames, [expected_lower, expected_upper])) in
tests.into_iter().enumerate()
{
let (lower, upper) = get_surrounding_keyframes(&keyframes, elapsed_frames);
assert_eq!(lower, keyframes[expected_lower], "Test idx {}", idx);
assert_eq!(upper, keyframes[expected_upper], "Test idx {}", idx);
}
}
}