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
use super::{IndexT, PrimitiveType, Mesh};
use std::slice;
use std::ops::Index;
use std::borrow::Borrow;

#[derive(Debug, Clone, Copy)]
pub struct MeshSlice<'a, T>{
    vertices: &'a [T],
    indices: &'a [IndexT],
    primitive_type: PrimitiveType,
}

/// Mesh slice with no indices from vertices and primitive type
pub fn mesh_slice<T>(vertices: &[T], ty: PrimitiveType) -> MeshSlice<T>{
    MeshSlice::from_vertices_and_type(vertices, ty)
}

impl<'a, T> MeshSlice<'a, T>{
    /// MeshSlice from vertices, indices and primitive type
    pub fn new(vertices: &'a [T], indices: &'a [IndexT], primitive_type: PrimitiveType) -> MeshSlice<'a, T>{
        MeshSlice{vertices, indices, primitive_type}
    }

    /// Triangles mesh from vertices with no indices
    pub fn from_vertices(vertices: &[T]) -> MeshSlice<T>{
        MeshSlice{vertices: vertices, indices: &[], primitive_type: PrimitiveType::Triangles}
    }

    /// MeshSlice with no indices from vertices and primitive type
    pub fn from_vertices_and_type(vertices: &[T], ty: PrimitiveType) -> MeshSlice<T>{
        MeshSlice{vertices: vertices, indices: &[], primitive_type: ty}
    }

    /// Triangles mesh from vertices and indices
    pub fn from_vertices_indices(vertices: &'a [T], indices: &'a [IndexT]) -> MeshSlice<'a, T>{
        MeshSlice{vertices: vertices, indices: indices, primitive_type: PrimitiveType::Triangles}
    }

    /// Empty mesh with the specified primitive type
    pub fn with_type(ty: PrimitiveType) -> MeshSlice<'a, T>{
         MeshSlice{vertices: &[], indices: &[], primitive_type: ty}
    }

    pub fn indices(&self) -> &&[IndexT]{
        &self.indices
    }

    pub fn vertices(&self) -> &[T]{
        &self.vertices
    }

    /// Iterator over the mesh vertices in the order they where inserted
    pub fn iter(&self) -> slice::Iter<T>{
        self.vertices.iter()
    }

    /// Consume the mesh into an iterator over it's vertices in the order they where inserted
    pub fn into_iter(self) -> slice::Iter<'a, T>{
        self.vertices.into_iter()
    }

    /// Last vertex if there's any
    pub fn last(&self) -> Option<&T>{
        self.vertices.last()
    }

    /// Primitive type
    pub fn primitive_type(&self) -> PrimitiveType{
        self.primitive_type
    }

    pub fn set_primitive_type(&mut self, ty: PrimitiveType){
        self.primitive_type = ty;
    }

    /// Number of vertices
    pub fn len(&self) -> usize{
        self.vertices.len()
    }

    /// Contains any vertices
    pub fn is_empty(&self) -> bool{
        self.vertices.is_empty()
    }

    pub fn faces(&self) -> Box<dyn Iterator<Item = [&T; 3]> + '_>{
        if self.primitive_type() == PrimitiveType::Triangles{
            if self.indices.is_empty(){
                Box::new(self.vertices.chunks_exact(3).map(|tri| [
                    &tri[0],
                    &tri[1],
                    &tri[2],
                ]))
            }else{
                Box::new(self.indices.chunks_exact(3).map(move |tri| [
                    &self.vertices[tri[0] as usize],
                    &self.vertices[tri[1] as usize],
                    &self.vertices[tri[2] as usize],
                ]))
            }
        }else{
            unimplemented!("Only PrimitiveType::Triangles supported")
        }
    }
}

impl<'a, T: Clone> MeshSlice<'a, T>{
    pub fn into_faces(self) -> Box<dyn Iterator<Item = [T; 3]> + 'a>
    where T: 'static
    {
        if self.primitive_type() == PrimitiveType::Triangles{
            if self.indices.is_empty(){
                let vertices = self.vertices;
                Box::new((0..vertices.len()).step_by(3).map(move |i| [
                    vertices[i+0].clone(),
                    vertices[i+1].clone(),
                    vertices[i+2].clone(),
                ]))
            }else{
                let indices = self.indices;
                let vertices = self.vertices;
                Box::new((0..indices.len()).step_by(3).map(move |i| [
                    vertices[indices[i+0] as usize].clone(),
                    vertices[indices[i+1] as usize].clone(),
                    vertices[indices[i+2] as usize].clone(),
                ]))
            }
        }else{
            unimplemented!("Only PrimitiveType::Triangles supported")
        }
    }

    pub fn to_owned(&self) -> Mesh<T>{
        Mesh::new(
            self.vertices.to_vec(),
            self.indices.to_vec(),
            self.primitive_type
        )
    }
}

impl<'a, T> Index<usize> for MeshSlice<'a, T>{
	type Output = T;
    fn index(&self, index: usize) -> &T{
        self.vertices.index(index)
    }
}

impl<'a, T> AsRef<[T]> for MeshSlice<'a, T>{
    fn as_ref(&self) -> &[T]{
        self.vertices.as_ref()
    }
}

impl<'a, T> Borrow<[T]> for MeshSlice<'a, T>{
    fn borrow(&self) -> &[T]{
        self.vertices.borrow()
    }
}