Skip to main content

gltf/accessor/
sparse.rs

1use crate::{buffer, Document};
2
3/// The index data type.
4#[derive(Clone, Debug)]
5pub enum IndexType {
6    /// Corresponds to `GL_UNSIGNED_BYTE`.
7    U8 = 5121,
8
9    /// Corresponds to `GL_UNSIGNED_SHORT`.
10    U16 = 5123,
11
12    /// Corresponds to `GL_UNSIGNED_INT`.
13    U32 = 5125,
14}
15
16/// Indices of those attributes that deviate from their initialization value.
17pub struct Indices<'a> {
18    /// The parent `Document` struct.
19    document: &'a Document,
20
21    /// The corresponding JSON struct.
22    json: &'a json::accessor::sparse::Indices,
23}
24
25impl<'a> Indices<'a> {
26    /// Constructs `sparse::Indices`.
27    pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Indices) -> Self {
28        Self { document, json }
29    }
30
31    /// Returns the buffer view containing the sparse indices.
32    pub fn view(&self) -> buffer::View<'a> {
33        self.document
34            .views()
35            .nth(self.json.buffer_view.value())
36            .unwrap()
37    }
38
39    /// The offset relative to the start of the parent buffer view in bytes.
40    pub fn offset(&self) -> usize {
41        self.json.byte_offset.0 as usize
42    }
43
44    /// The data type of each index.
45    pub fn index_type(&self) -> IndexType {
46        match self.json.component_type.unwrap().0 {
47            json::accessor::ComponentType::U8 => IndexType::U8,
48            json::accessor::ComponentType::U16 => IndexType::U16,
49            json::accessor::ComponentType::U32 => IndexType::U32,
50            _ => unreachable!(),
51        }
52    }
53
54    /// Optional application specific data.
55    pub fn extras(&self) -> &'a json::Extras {
56        &self.json.extras
57    }
58}
59
60/// Sparse storage of attributes that deviate from their initialization value.
61pub struct Sparse<'a> {
62    /// The parent `Document` struct.
63    document: &'a Document,
64
65    /// The corresponding JSON struct.
66    json: &'a json::accessor::sparse::Sparse,
67}
68
69impl<'a> Sparse<'a> {
70    /// Constructs `Sparse`.
71    pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Sparse) -> Self {
72        Self { document, json }
73    }
74
75    /// Returns the number of attributes encoded in this sparse accessor.
76    pub fn count(&self) -> usize {
77        self.json.count.0 as usize
78    }
79
80    /// Returns an index array of size `count` that points to those accessor
81    /// attributes that deviate from their initialization value.
82    pub fn indices(&self) -> Indices<'a> {
83        Indices::new(self.document, &self.json.indices)
84    }
85
86    /// Returns an array of size `count * number_of_components`, storing the
87    /// displaced accessor attributes pointed by `indices`.
88    pub fn values(&self) -> Values<'a> {
89        Values::new(self.document, &self.json.values)
90    }
91
92    /// Optional application specific data.
93    pub fn extras(&self) -> &'a json::Extras {
94        &self.json.extras
95    }
96}
97
98/// Array of size `count * number_of_components` storing the displaced accessor
99/// attributes pointed by `accessor::sparse::Indices`.
100pub struct Values<'a> {
101    /// The parent `Document` struct.
102    document: &'a Document,
103
104    /// The corresponding JSON struct.
105    json: &'a json::accessor::sparse::Values,
106}
107
108impl<'a> Values<'a> {
109    /// Constructs `sparse::Values`.
110    pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Values) -> Self {
111        Self { document, json }
112    }
113
114    /// Returns the buffer view containing the sparse values.
115    pub fn view(&self) -> buffer::View<'a> {
116        self.document
117            .views()
118            .nth(self.json.buffer_view.value())
119            .unwrap()
120    }
121
122    /// The offset relative to the start of the parent buffer view in bytes.
123    pub fn offset(&self) -> usize {
124        self.json.byte_offset.0 as usize
125    }
126
127    /// Optional application specific data.
128    pub fn extras(&self) -> &'a json::Extras {
129        &self.json.extras
130    }
131}
132
133impl IndexType {
134    /// Returns the number of bytes this value represents.
135    pub fn size(&self) -> usize {
136        use self::IndexType::*;
137        match *self {
138            U8 => 1,
139            U16 => 2,
140            U32 => 4,
141        }
142    }
143}