gltf/animation/util/
morph_target_weights.rs

1use super::MorphTargetWeights;
2use crate::Normalize;
3use std::marker::PhantomData;
4
5/// Casting iterator for `MorphTargetWeights`.
6#[derive(Clone, Debug)]
7pub struct CastingIter<'a, T>(MorphTargetWeights<'a>, PhantomData<T>);
8
9/// Type which describes how to cast any weight into i8.
10#[derive(Clone, Debug)]
11pub struct I8;
12
13/// Type which describes how to cast any weight into u8.
14#[derive(Clone, Debug)]
15pub struct U8;
16
17/// Type which describes how to cast any weight into i16.
18#[derive(Clone, Debug)]
19pub struct I16;
20
21/// Type which describes how to cast any weight into u16.
22#[derive(Clone, Debug)]
23pub struct U16;
24
25/// Type which describes how to cast any weight into f32.
26#[derive(Clone, Debug)]
27pub struct F32;
28
29/// Trait for types which describe casting behaviour.
30pub trait Cast {
31    /// Output type.
32    type Output;
33
34    /// Cast from i8.
35    fn cast_i8(x: i8) -> Self::Output;
36
37    /// Cast from u8.
38    fn cast_u8(x: u8) -> Self::Output;
39
40    /// Cast from i16.
41    fn cast_i16(x: i16) -> Self::Output;
42
43    /// Cast from u16.
44    fn cast_u16(x: u16) -> Self::Output;
45
46    /// Cast from f32.
47    fn cast_f32(x: f32) -> Self::Output;
48}
49
50impl<'a, A> CastingIter<'a, A> {
51    pub(crate) fn new(iter: MorphTargetWeights<'a>) -> Self {
52        CastingIter(iter, PhantomData)
53    }
54
55    /// Unwrap underlying `MorphTargetWeights` object.
56    pub fn unwrap(self) -> MorphTargetWeights<'a> {
57        self.0
58    }
59}
60
61impl<'a, A: Cast> ExactSizeIterator for CastingIter<'a, A> {}
62impl<'a, A: Cast> Iterator for CastingIter<'a, A> {
63    type Item = A::Output;
64
65    #[inline]
66    fn next(&mut self) -> Option<Self::Item> {
67        match self.0 {
68            MorphTargetWeights::I8(ref mut i) => i.next().map(A::cast_i8),
69            MorphTargetWeights::U8(ref mut i) => i.next().map(A::cast_u8),
70            MorphTargetWeights::I16(ref mut i) => i.next().map(A::cast_i16),
71            MorphTargetWeights::U16(ref mut i) => i.next().map(A::cast_u16),
72            MorphTargetWeights::F32(ref mut i) => i.next().map(A::cast_f32),
73        }
74    }
75
76    #[inline]
77    fn nth(&mut self, x: usize) -> Option<Self::Item> {
78        match self.0 {
79            MorphTargetWeights::I8(ref mut i) => i.nth(x).map(A::cast_i8),
80            MorphTargetWeights::U8(ref mut i) => i.nth(x).map(A::cast_u8),
81            MorphTargetWeights::I16(ref mut i) => i.nth(x).map(A::cast_i16),
82            MorphTargetWeights::U16(ref mut i) => i.nth(x).map(A::cast_u16),
83            MorphTargetWeights::F32(ref mut i) => i.nth(x).map(A::cast_f32),
84        }
85    }
86
87    fn last(self) -> Option<Self::Item> {
88        match self.0 {
89            MorphTargetWeights::I8(i) => i.last().map(A::cast_i8),
90            MorphTargetWeights::U8(i) => i.last().map(A::cast_u8),
91            MorphTargetWeights::I16(i) => i.last().map(A::cast_i16),
92            MorphTargetWeights::U16(i) => i.last().map(A::cast_u16),
93            MorphTargetWeights::F32(i) => i.last().map(A::cast_f32),
94        }
95    }
96
97    fn count(self) -> usize {
98        self.size_hint().0
99    }
100
101    #[inline]
102    fn size_hint(&self) -> (usize, Option<usize>) {
103        match self.0 {
104            MorphTargetWeights::I8(ref i) => i.size_hint(),
105            MorphTargetWeights::U8(ref i) => i.size_hint(),
106            MorphTargetWeights::I16(ref i) => i.size_hint(),
107            MorphTargetWeights::U16(ref i) => i.size_hint(),
108            MorphTargetWeights::F32(ref i) => i.size_hint(),
109        }
110    }
111}
112
113impl Cast for I8 {
114    type Output = i8;
115
116    fn cast_i8(x: i8) -> Self::Output {
117        x.normalize()
118    }
119
120    fn cast_u8(x: u8) -> Self::Output {
121        x.normalize()
122    }
123
124    fn cast_i16(x: i16) -> Self::Output {
125        x.normalize()
126    }
127
128    fn cast_u16(x: u16) -> Self::Output {
129        x.normalize()
130    }
131
132    fn cast_f32(x: f32) -> Self::Output {
133        x.normalize()
134    }
135}
136
137impl Cast for U8 {
138    type Output = u8;
139
140    fn cast_i8(x: i8) -> Self::Output {
141        x.normalize()
142    }
143
144    fn cast_u8(x: u8) -> Self::Output {
145        x.normalize()
146    }
147
148    fn cast_i16(x: i16) -> Self::Output {
149        x.normalize()
150    }
151
152    fn cast_u16(x: u16) -> Self::Output {
153        x.normalize()
154    }
155
156    fn cast_f32(x: f32) -> Self::Output {
157        x.normalize()
158    }
159}
160
161impl Cast for I16 {
162    type Output = i16;
163
164    fn cast_i8(x: i8) -> Self::Output {
165        x.normalize()
166    }
167
168    fn cast_u8(x: u8) -> Self::Output {
169        x.normalize()
170    }
171
172    fn cast_i16(x: i16) -> Self::Output {
173        x.normalize()
174    }
175
176    fn cast_u16(x: u16) -> Self::Output {
177        x.normalize()
178    }
179
180    fn cast_f32(x: f32) -> Self::Output {
181        x.normalize()
182    }
183}
184
185impl Cast for U16 {
186    type Output = u16;
187
188    fn cast_i8(x: i8) -> Self::Output {
189        x.normalize()
190    }
191
192    fn cast_u8(x: u8) -> Self::Output {
193        x.normalize()
194    }
195
196    fn cast_i16(x: i16) -> Self::Output {
197        x.normalize()
198    }
199
200    fn cast_u16(x: u16) -> Self::Output {
201        x.normalize()
202    }
203
204    fn cast_f32(x: f32) -> Self::Output {
205        x.normalize()
206    }
207}
208
209impl Cast for F32 {
210    type Output = f32;
211
212    fn cast_i8(x: i8) -> Self::Output {
213        x.normalize()
214    }
215
216    fn cast_u8(x: u8) -> Self::Output {
217        x.normalize()
218    }
219
220    fn cast_i16(x: i16) -> Self::Output {
221        x.normalize()
222    }
223
224    fn cast_u16(x: u16) -> Self::Output {
225        x.normalize()
226    }
227
228    fn cast_f32(x: f32) -> Self::Output {
229        x.normalize()
230    }
231}