gausplat_loader/source/polygon/object/
access.rs

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
//! Polygon object access implementation.

pub use super::*;
pub use bytemuck::Pod;

use bytemuck::{try_cast_slice, try_cast_slice_mut};

/// Element entry.
#[derive(AsRef, Clone, Constructor, Debug, Eq, From, PartialEq)]
pub struct ElementEntry<'e> {
    /// Element metadata.
    pub meta: &'e Element,
    /// Element data.
    pub data: &'e Vec<Vec<u8>>,
}

/// Element entry (mutable).
#[derive(AsRef, Debug, Eq, From, PartialEq)]
pub struct ElementEntryMut<'e> {
    /// Element metadata (mutable).
    pub meta: &'e mut Element,
    /// Element data (mutable).
    pub data: &'e mut Vec<Vec<u8>>,
}

/// Property entry.
#[derive(AsRef, Clone, Constructor, Debug, Eq, From, Hash, PartialEq)]
pub struct PropertyEntry<'p> {
    /// Property metadata.
    pub meta: &'p Property,
    /// Property data.
    pub data: &'p Vec<u8>,
}

/// Property entry (mutable).
#[derive(AsRef, Debug, Eq, From, Hash, PartialEq)]
pub struct PropertyEntryMut<'p> {
    /// Property metadata (mutable).
    pub meta: &'p mut Property,
    /// Property data (mutable).
    pub data: &'p mut Vec<u8>,
}

/// Short named accessors and mutators
impl Object {
    /// Get an element by name.
    #[doc(alias = "get_element")]
    #[inline]
    pub fn elem<Q: AsRef<str>>(
        &self,
        name: Q,
    ) -> Option<ElementEntry<'_>> {
        self.get_element(name)
    }

    /// Get a mutable element by name.
    #[doc(alias = "get_mut_element")]
    #[inline]
    pub fn elem_mut<Q: AsRef<str>>(
        &mut self,
        name: Q,
    ) -> Option<ElementEntryMut<'_>> {
        self.get_mut_element(name)
    }

    /// Return an iterator over elements.
    #[doc(alias = "iter_elements")]
    #[inline]
    pub fn elems(&self) -> impl Iterator<Item = ElementEntry<'_>> {
        self.iter_elements()
    }

    /// Return an iterator over mutable elements.
    #[doc(alias = "iter_mut_elements")]
    #[inline]
    pub fn elems_mut(&mut self) -> impl Iterator<Item = ElementEntryMut<'_>> {
        self.iter_mut_elements()
    }

    /// Get a property of an element.
    #[doc(alias = "get_property_of_element")]
    #[inline]
    pub fn elem_prop<'e, 'p: 'e, Q: AsRef<str>>(
        &'p self,
        element_name: Q,
        property_name: Q,
    ) -> Option<PropertyEntry<'p>> {
        self.get_property_of_element(element_name, property_name)
    }

    /// Get a mutable property of an element.
    #[doc(alias = "get_mut_property_of_element")]
    #[inline]
    pub fn elem_prop_mut<'e, 'p: 'e, Q: AsRef<str>>(
        &'p mut self,
        element_name: Q,
        property_name: Q,
    ) -> Option<PropertyEntryMut<'p>> {
        self.get_mut_property_of_element(element_name, property_name)
    }
}

/// Short named accessors
impl<'e, 'p: 'e> ElementEntry<'e> {
    /// Get a property by name.
    #[doc(alias = "get_property")]
    #[inline]
    pub fn prop<Q: AsRef<str>>(
        &'p self,
        name: Q,
    ) -> Option<PropertyEntry<'p>> {
        self.get_property(name)
    }

    /// Return an iterator over properties.
    #[doc(alias = "iter_properties")]
    #[inline]
    pub fn props(&'p self) -> impl Iterator<Item = PropertyEntry<'p>> {
        self.iter_properties()
    }
}

/// Short named mutators
impl<'e, 'p: 'e> ElementEntryMut<'e> {
    /// Get a mutable property by name.
    #[doc(alias = "get_mut_property")]
    #[inline]
    pub fn prop_mut<Q: AsRef<str>>(
        &'p mut self,
        name: Q,
    ) -> Option<PropertyEntryMut<'p>> {
        self.get_mut_property(name)
    }

    /// Return an iterator over mutable properties.
    #[doc(alias = "iter_mut_properties")]
    #[inline]
    pub fn props_mut(&'p mut self) -> impl Iterator<Item = PropertyEntryMut<'p>> {
        self.iter_mut_properties()
    }
}

/// Short named accessors
impl<'p> PropertyEntry<'p> {
    /// Cast the property data to a slice of the kind.
    #[doc(alias = "as_kind")]
    #[inline]
    pub fn cast<T: Pod>(&'p self) -> Result<&'p [T], Error> {
        self.as_kind()
    }
}

/// Short named mutators
impl<'p> PropertyEntryMut<'p> {
    /// Cast the property data to a mutable slice of the kind.
    #[doc(alias = "as_mut_kind")]
    #[inline]
    pub fn cast_mut<T: Pod>(&'p mut self) -> Result<&'p mut [T], Error> {
        self.as_mut_kind()
    }
}

/// Long named accessors and mutators
impl Object {
    /// Get an element by name.
    #[doc(alias = "elem")]
    pub fn get_element<Q: AsRef<str>>(
        &self,
        name: Q,
    ) -> Option<ElementEntry<'_>> {
        let (index, _, meta) = self.header.get_full(name.as_ref())?;
        // NOTE: Currently, there is only scalar payload implemented.
        let data = self
            .payload
            .try_unwrap_scalar_ref()
            .unwrap()
            .data
            .get(index)?;
        Some(ElementEntry { meta, data })
    }

    /// Get a mutable element by name.
    #[doc(alias = "elem_mut")]
    pub fn get_mut_element<Q: AsRef<str>>(
        &mut self,
        name: Q,
    ) -> Option<ElementEntryMut<'_>> {
        let (index, _, meta) = self.header.get_full_mut(name.as_ref())?;
        // NOTE: Currently, there is only scalar payload implemented.
        let data = self
            .payload
            .try_unwrap_scalar_mut()
            .unwrap()
            .data
            .get_mut(index)?;
        Some(ElementEntryMut { meta, data })
    }

    /// Get a property of an element.
    #[doc(alias = "elem_prop")]
    pub fn get_property_of_element<'e, 'p: 'e, Q: AsRef<str>>(
        &'p self,
        element_name: Q,
        property_name: Q,
    ) -> Option<PropertyEntry<'p>> {
        let (index, _, meta) = self.header.get_full(element_name.as_ref())?;
        // NOTE: Currently, there is only scalar payload implemented.
        let data = self
            .payload
            .try_unwrap_scalar_ref()
            .unwrap()
            .data
            .get(index)?;
        let (index, _, meta) = meta.get_full(property_name.as_ref())?;
        let data = data.get(index)?;
        Some(PropertyEntry { meta, data })
    }

    /// Get a mutable property of an element.
    #[doc(alias = "elem_prop_mut")]
    pub fn get_mut_property_of_element<'e, 'p: 'e, Q: AsRef<str>>(
        &'p mut self,
        element_name: Q,
        property_name: Q,
    ) -> Option<PropertyEntryMut<'p>> {
        let (index, _, meta) = self.header.get_full_mut(element_name.as_ref())?;
        // NOTE: Currently, there is only scalar payload implemented.
        let data = self
            .payload
            .try_unwrap_scalar_mut()
            .unwrap()
            .data
            .get_mut(index)?;
        let (index, _, meta) = meta.get_full_mut(property_name.as_ref())?;
        let data = data.get_mut(index)?;
        Some(PropertyEntryMut { meta, data })
    }

    /// Return an iterator over elements.
    #[doc(alias = "elems")]
    #[inline]
    pub fn iter_elements(&self) -> impl Iterator<Item = ElementEntry<'_>> {
        // NOTE: Currently, there is only scalar payload implemented.
        self.header
            .elements
            .values()
            .zip(self.payload.try_unwrap_scalar_ref().unwrap().data.iter())
            .map(Into::into)
    }

    /// Return an iterator over mutable elements.
    #[doc(alias = "elems_mut")]
    #[inline]
    pub fn iter_mut_elements(&mut self) -> impl Iterator<Item = ElementEntryMut<'_>> {
        // NOTE: Currently, there is only scalar payload implemented.
        self.header
            .elements
            .values_mut()
            .zip(
                self.payload
                    .try_unwrap_scalar_mut()
                    .unwrap()
                    .data
                    .iter_mut(),
            )
            .map(Into::into)
    }
}

/// Long named accessors
impl<'e, 'p: 'e> ElementEntry<'e> {
    /// Get a property by name.
    #[doc(alias = "prop")]
    #[inline]
    pub fn get_property<Q: AsRef<str>>(
        &'p self,
        name: Q,
    ) -> Option<PropertyEntry<'p>> {
        let (index, _, meta) = self.meta.get_full(name.as_ref())?;
        let data = self.data.get(index)?;
        Some(PropertyEntry { meta, data })
    }

    /// Return an iterator over properties.
    #[doc(alias = "props")]
    #[inline]
    pub fn iter_properties(&'p self) -> impl Iterator<Item = PropertyEntry<'p>> {
        self.meta
            .properties
            .values()
            .zip(self.data.iter())
            .map(Into::into)
    }
}

/// Long named mutators
impl<'e, 'p: 'e> ElementEntryMut<'e> {
    /// Get a mutable property by name.
    #[doc(alias = "prop_mut")]
    #[inline]
    pub fn get_mut_property<Q: AsRef<str>>(
        &'p mut self,
        name: Q,
    ) -> Option<PropertyEntryMut<'p>> {
        let (index, _, meta) = self.meta.get_full_mut(name.as_ref())?;
        let data = self.data.get_mut(index)?;
        Some(PropertyEntryMut { meta, data })
    }

    /// Return an iterator over mutable properties.
    #[doc(alias = "props_mut")]
    #[inline]
    pub fn iter_mut_properties(
        &'p mut self
    ) -> impl Iterator<Item = PropertyEntryMut<'p>> {
        self.meta
            .properties
            .values_mut()
            .zip(self.data.iter_mut())
            .map(Into::into)
    }
}

/// Long named accessors
impl<'p> PropertyEntry<'p> {
    /// Cast the property data to a slice of the kind.
    #[doc(alias = "cast")]
    #[inline]
    pub fn as_kind<T: Pod>(&'p self) -> Result<&'p [T], Error> {
        Ok(try_cast_slice(self.data)?)
    }
}

/// Long named mutators
impl<'p> PropertyEntryMut<'p> {
    /// Cast the property data to a mutable slice of the kind.
    #[doc(alias = "cast_mut")]
    #[inline]
    pub fn as_mut_kind<T: Pod>(&'p mut self) -> Result<&'p mut [T], Error> {
        Ok(try_cast_slice_mut(self.data)?)
    }
}