truck_polymesh/
attributes.rs

1use crate::*;
2
3impl<T: Copy> Attributes<usize> for Vec<T> {
4    type Output = T;
5    fn get(&self, idx: usize) -> Option<T> { self.as_slice().get(idx).copied() }
6}
7
8impl Attributes<StandardVertex> for StandardAttributes {
9    type Output = StandardAttribute;
10    fn get(&self, v: StandardVertex) -> Option<Self::Output> {
11        Some(StandardAttribute {
12            position: self.positions.get(v.pos)?,
13            uv_coord: match v.uv {
14                Some(i) => Some(self.uv_coords.get(i)?),
15                None => None,
16            },
17            normal: match v.nor {
18                Some(i) => Some(self.normals.get(i)?),
19                None => None,
20            },
21        })
22    }
23}
24
25impl StandardAttributes {
26    /// Returns the vector of all positions.
27    #[inline(always)]
28    pub const fn positions(&self) -> &Vec<Point3> { &self.positions }
29
30    /// Returns the mutable slice of all positions.
31    #[inline(always)]
32    pub fn positions_mut(&mut self) -> &mut [Point3] { &mut self.positions }
33
34    /// Adds a position.
35    #[inline(always)]
36    pub fn push_position(&mut self, position: Point3) { self.positions.push(position) }
37
38    /// Extend positions by iterator.
39    #[inline(always)]
40    pub fn extend_positions<I: IntoIterator<Item = Point3>>(&mut self, iter: I) {
41        self.positions.extend(iter)
42    }
43
44    /// Returns the vector of all uv (texture) coordinates.
45    #[inline(always)]
46    pub const fn uv_coords(&self) -> &Vec<Vector2> { &self.uv_coords }
47
48    /// Returns the mutable slice of all uv (texture) coordinates.
49    #[inline(always)]
50    pub fn uv_coords_mut(&mut self) -> &mut [Vector2] { &mut self.uv_coords }
51
52    /// Adds a uv (texture) coordinate.
53    #[inline(always)]
54    pub fn push_uv_coord(&mut self, uv_coord: Vector2) { self.uv_coords.push(uv_coord) }
55
56    /// Extend uv (texture) coordinates by iterator.
57    #[inline(always)]
58    pub fn extend_uv_coords<I: IntoIterator<Item = Vector2>>(&mut self, iter: I) {
59        self.uv_coords.extend(iter)
60    }
61
62    /// Returns the vector of all normals.
63    #[inline(always)]
64    pub const fn normals(&self) -> &Vec<Vector3> { &self.normals }
65
66    /// Returns the mutable slice of all normals.
67    #[inline(always)]
68    pub fn normals_mut(&mut self) -> &mut [Vector3] { &mut self.normals }
69
70    /// Extend normals by iterator
71    #[inline(always)]
72    pub fn extend_normals<I: IntoIterator<Item = Vector3>>(&mut self, iter: I) {
73        self.normals.extend(iter)
74    }
75}