|
| 1 | +use bevy_reflect::Reflect; |
| 2 | +use core::iter::FusedIterator; |
| 3 | +use thiserror::Error; |
| 4 | +use wgpu::IndexFormat; |
| 5 | + |
| 6 | +/// A disjunction of four iterators. This is necessary to have a well-formed type for the output |
| 7 | +/// of [`Mesh::triangles`](super::Mesh::triangles), which produces iterators of four different types depending on the |
| 8 | +/// branch taken. |
| 9 | +pub(crate) enum FourIterators<A, B, C, D> { |
| 10 | + First(A), |
| 11 | + Second(B), |
| 12 | + Third(C), |
| 13 | + Fourth(D), |
| 14 | +} |
| 15 | + |
| 16 | +impl<A, B, C, D, I> Iterator for FourIterators<A, B, C, D> |
| 17 | +where |
| 18 | + A: Iterator<Item = I>, |
| 19 | + B: Iterator<Item = I>, |
| 20 | + C: Iterator<Item = I>, |
| 21 | + D: Iterator<Item = I>, |
| 22 | +{ |
| 23 | + type Item = I; |
| 24 | + |
| 25 | + fn next(&mut self) -> Option<Self::Item> { |
| 26 | + match self { |
| 27 | + FourIterators::First(iter) => iter.next(), |
| 28 | + FourIterators::Second(iter) => iter.next(), |
| 29 | + FourIterators::Third(iter) => iter.next(), |
| 30 | + FourIterators::Fourth(iter) => iter.next(), |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// An error that occurred while trying to invert the winding of a [`Mesh`](super::Mesh). |
| 36 | +#[derive(Debug, Error)] |
| 37 | +pub enum MeshWindingInvertError { |
| 38 | + /// This error occurs when you try to invert the winding for a mesh with [`PrimitiveTopology::PointList`](super::PrimitiveTopology::PointList). |
| 39 | + #[error("Mesh winding invertation does not work for primitive topology `PointList`")] |
| 40 | + WrongTopology, |
| 41 | + |
| 42 | + /// This error occurs when you try to invert the winding for a mesh with |
| 43 | + /// * [`PrimitiveTopology::TriangleList`](super::PrimitiveTopology::TriangleList), but the indices are not in chunks of 3. |
| 44 | + /// * [`PrimitiveTopology::LineList`](super::PrimitiveTopology::LineList), but the indices are not in chunks of 2. |
| 45 | + #[error("Indices weren't in chunks according to topology")] |
| 46 | + AbruptIndicesEnd, |
| 47 | +} |
| 48 | + |
| 49 | +/// An error that occurred while trying to extract a collection of triangles from a [`Mesh`](super::Mesh). |
| 50 | +#[derive(Debug, Error)] |
| 51 | +pub enum MeshTrianglesError { |
| 52 | + #[error("Source mesh does not have primitive topology TriangleList or TriangleStrip")] |
| 53 | + WrongTopology, |
| 54 | + |
| 55 | + #[error("Source mesh lacks position data")] |
| 56 | + MissingPositions, |
| 57 | + |
| 58 | + #[error("Source mesh position data is not Float32x3")] |
| 59 | + PositionsFormat, |
| 60 | + |
| 61 | + #[error("Source mesh lacks face index data")] |
| 62 | + MissingIndices, |
| 63 | + |
| 64 | + #[error("Face index data references vertices that do not exist")] |
| 65 | + BadIndices, |
| 66 | +} |
| 67 | + |
| 68 | +/// An array of indices into the [`VertexAttributeValues`](super::VertexAttributeValues) for a mesh. |
| 69 | +/// |
| 70 | +/// It describes the order in which the vertex attributes should be joined into faces. |
| 71 | +#[derive(Debug, Clone, Reflect)] |
| 72 | +pub enum Indices { |
| 73 | + U16(Vec<u16>), |
| 74 | + U32(Vec<u32>), |
| 75 | +} |
| 76 | + |
| 77 | +impl Indices { |
| 78 | + /// Returns an iterator over the indices. |
| 79 | + pub fn iter(&self) -> impl Iterator<Item = usize> + '_ { |
| 80 | + match self { |
| 81 | + Indices::U16(vec) => IndicesIter::U16(vec.iter()), |
| 82 | + Indices::U32(vec) => IndicesIter::U32(vec.iter()), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Returns the number of indices. |
| 87 | + pub fn len(&self) -> usize { |
| 88 | + match self { |
| 89 | + Indices::U16(vec) => vec.len(), |
| 90 | + Indices::U32(vec) => vec.len(), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// Returns `true` if there are no indices. |
| 95 | + pub fn is_empty(&self) -> bool { |
| 96 | + match self { |
| 97 | + Indices::U16(vec) => vec.is_empty(), |
| 98 | + Indices::U32(vec) => vec.is_empty(), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// An Iterator for the [`Indices`]. |
| 104 | +enum IndicesIter<'a> { |
| 105 | + U16(core::slice::Iter<'a, u16>), |
| 106 | + U32(core::slice::Iter<'a, u32>), |
| 107 | +} |
| 108 | + |
| 109 | +impl Iterator for IndicesIter<'_> { |
| 110 | + type Item = usize; |
| 111 | + |
| 112 | + fn next(&mut self) -> Option<Self::Item> { |
| 113 | + match self { |
| 114 | + IndicesIter::U16(iter) => iter.next().map(|val| *val as usize), |
| 115 | + IndicesIter::U32(iter) => iter.next().map(|val| *val as usize), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 120 | + match self { |
| 121 | + IndicesIter::U16(iter) => iter.size_hint(), |
| 122 | + IndicesIter::U32(iter) => iter.size_hint(), |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<'a> ExactSizeIterator for IndicesIter<'a> {} |
| 128 | +impl<'a> FusedIterator for IndicesIter<'a> {} |
| 129 | + |
| 130 | +impl From<&Indices> for IndexFormat { |
| 131 | + fn from(indices: &Indices) -> Self { |
| 132 | + match indices { |
| 133 | + Indices::U16(_) => IndexFormat::Uint16, |
| 134 | + Indices::U32(_) => IndexFormat::Uint32, |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments