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
use crate::errors::Error;
use crate::*;

impl Vertex {
    #[inline(always)]
    fn tuple(x: usize, uv: bool, nor: bool) -> Vertex {
        let pos = x;
        let uv = if uv { Some(x) } else { None };
        let nor = if nor { Some(x) } else { None };
        Vertex { pos, uv, nor }
    }
}

impl StructuredMesh {
    /// Creates a structured polygon without `uv_division` and `normal`.
    #[inline(always)]
    pub fn from_positions(positions: Vec<Vec<Point3>>) -> StructuredMesh {
        StructuredMesh::try_from_positions(positions).unwrap_or_else(|e| panic!("{:?}", e))
    }
    /// Creates a structured polygon without `uv_division` and `normal`.
    #[inline(always)]
    pub fn try_from_positions(positions: Vec<Vec<Point3>>) -> Result<StructuredMesh> {
        check_matrix_regularity(&positions)?;
        Ok(StructuredMesh::from_positions_unchecked(positions))
    }

    /// Creates a structured polygon without `uv_division` and `normal`.
    #[inline(always)]
    pub fn from_positions_unchecked(positions: Vec<Vec<Point3>>) -> StructuredMesh {
        StructuredMesh {
            positions,
            uv_division: None,
            normals: None,
        }
    }
    /// Creates a structured polygon without normals.
    #[inline(always)]
    pub fn from_positions_and_uvs(
        positions: Vec<Vec<Point3>>,
        (u_div, v_div): (Vec<f64>, Vec<f64>),
    ) -> StructuredMesh {
        StructuredMesh::try_from_positions_and_uvs(positions, (u_div, v_div))
            .unwrap_or_else(|e| panic!("{:?}", e))
    }

    /// Creates a structured polygon without normals.
    #[inline(always)]
    pub fn try_from_positions_and_uvs(
        positions: Vec<Vec<Point3>>,
        (u_div, v_div): (Vec<f64>, Vec<f64>),
    ) -> Result<StructuredMesh> {
        check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
        Ok(StructuredMesh::from_positions_and_uvs_unchecked(
            positions,
            (u_div, v_div),
        ))
    }
    /// Creates a structured polygon without normals.
    #[inline(always)]
    pub fn from_positions_and_uvs_unchecked(
        positions: Vec<Vec<Point3>>,
        uv_divisions: (Vec<f64>, Vec<f64>),
    ) -> StructuredMesh {
        StructuredMesh {
            positions,
            uv_division: Some(uv_divisions),
            normals: None,
        }
    }
    /// Creates a structured polygon without uv divisions.
    #[inline(always)]
    pub fn from_positions_and_normals(
        positions: Vec<Vec<Point3>>,
        normals: Vec<Vec<Vector3>>,
    ) -> StructuredMesh {
        StructuredMesh::try_from_positions_and_normals(positions, normals)
            .unwrap_or_else(|e| panic!("{:?}", e))
    }
    /// Creates a structured polygon without uv divisions.
    #[inline(always)]
    pub fn try_from_positions_and_normals(
        positions: Vec<Vec<Point3>>,
        normals: Vec<Vec<Vector3>>,
    ) -> Result<StructuredMesh> {
        check_matrix_regularity(&positions)?;
        check_matrices_compatibility(&positions, &normals)?;
        Ok(StructuredMesh::from_positions_and_normals_unchecked(
            positions, normals,
        ))
    }
    /// Creates a structured polygon without uv divisions.
    #[inline(always)]
    pub fn from_positions_and_normals_unchecked(
        positions: Vec<Vec<Point3>>,
        normals: Vec<Vec<Vector3>>,
    ) -> StructuredMesh {
        StructuredMesh {
            positions,
            uv_division: None,
            normals: Some(normals),
        }
    }

    /// Creates new structured mesh.
    /// Checks whether the size of vectors are compatible before creation.
    #[inline(always)]
    pub fn new(
        positions: Vec<Vec<Point3>>,
        uv_division: (Vec<f64>, Vec<f64>),
        normals: Vec<Vec<Vector3>>,
    ) -> StructuredMesh {
        StructuredMesh::try_new(positions, uv_division, normals)
            .unwrap_or_else(|e| panic!("{:?}", e))
    }
    /// Creates new structured mesh.
    /// Checks whether the size of vectors are compatible before creation.
    #[inline(always)]
    pub fn try_new(
        positions: Vec<Vec<Point3>>,
        (u_div, v_div): (Vec<f64>, Vec<f64>),
        normals: Vec<Vec<Vector3>>,
    ) -> Result<StructuredMesh> {
        check_matrix_vectors_compatibility(&positions, &u_div, &v_div)?;
        check_matrix_vectors_compatibility(&normals, &u_div, &v_div)?;
        check_vectors_regularity(&u_div, &v_div)?;
        Ok(StructuredMesh::new_unchecked(
            positions,
            (u_div, v_div),
            normals,
        ))
    }

    /// Creates new structured mesh.
    /// Does not check whether the size of vectors are compatible before creation.
    #[inline(always)]
    pub fn new_unchecked(
        positions: Vec<Vec<Point3>>,
        uv_division: (Vec<f64>, Vec<f64>),
        normals: Vec<Vec<Vector3>>,
    ) -> StructuredMesh {
        StructuredMesh {
            positions,
            uv_division: Some(uv_division),
            normals: Some(normals),
        }
    }

    /// Returns the matrix of all positions.
    #[inline(always)]
    pub fn positions(&self) -> &Vec<Vec<Point3>> { &self.positions }

    /// Returns the vector of the mutable references to the rows of the positions matrix.
    #[inline(always)]
    pub fn positions_mut(&mut self) -> Vec<&mut [Point3]> {
        self.positions.iter_mut().map(|arr| arr.as_mut()).collect()
    }

    /// Returns the divisions of uv coordinates.
    #[inline(always)]
    pub fn uv_division(&self) -> Option<(&Vec<f64>, &Vec<f64>)> {
        self.uv_division.as_ref().map(|tuple| (&tuple.0, &tuple.1))
    }

    /// Returns the mutable slice of uv coordinates division.
    #[inline(always)]
    pub fn uv_division_mut(&mut self) -> Option<(&mut [f64], &mut [f64])> {
        self.uv_division
            .as_mut()
            .map(|tuple| (tuple.0.as_mut(), tuple.1.as_mut()))
    }

    /// Returns the matrix of all normals.
    #[inline(always)]
    pub fn normals(&self) -> Option<&Vec<Vec<Vector3>>> { self.normals.as_ref() }

    /// Returns the vector of the mutable references to the rows of the normals matrix.
    #[inline(always)]
    pub fn normals_mut(&mut self) -> Option<Vec<&mut [Vector3]>> {
        self.normals
            .as_mut()
            .map(|normals| normals.iter_mut().map(|arr| arr.as_mut()).collect())
    }

    /// Creates new polygon mesh by destructing `self`.
    #[inline(always)]
    pub fn destruct(self) -> PolygonMesh {
        let StructuredMesh {
            positions,
            uv_division,
            normals,
        } = self;
        let m = positions.len();
        let n = positions[0].len();
        let positions = positions.into_iter().flatten().collect();
        let uv_coords = uv_division
            .map(move |(udiv, vdiv)| {
                udiv.into_iter()
                    .flat_map(|u| vdiv.iter().map(move |v| Vector2::new(u, *v)))
                    .collect()
            })
            .unwrap_or(Vec::new());
        let normals = normals
            .map(|n| n.into_iter().flatten().collect())
            .unwrap_or(Vec::new());
        let uv = !uv_coords.is_empty();
        let nor = !normals.is_empty();
        let quad_faces: Vec<_> = (1..m)
            .flat_map(|i| (1..n).map(move |j| (i, j)))
            .map(move |(i, j)| {
                [
                    Vertex::tuple((i - 1) * n + j - 1, uv, nor),
                    Vertex::tuple(i * n + j - 1, uv, nor),
                    Vertex::tuple(i * n + j, uv, nor),
                    Vertex::tuple((i - 1) * n + j, uv, nor),
                ]
            })
            .collect();
        let faces = Faces {
            quad_faces,
            ..Default::default()
        };
        PolygonMesh {
            positions,
            uv_coords,
            normals,
            faces,
        }
    }
}

#[inline(always)]
fn check_matrix_regularity<T>(matrix: &Vec<Vec<T>>) -> Result<()> {
    for arr in matrix {
        if arr.len() != matrix[0].len() {
            return Err(Error::IrregularArray);
        }
    }
    Ok(())
}

#[inline(always)]
fn check_matrices_compatibility<S, T>(matrix0: &Vec<Vec<S>>, matrix1: &Vec<Vec<T>>) -> Result<()> {
    if matrix0.len() != matrix1.len() {
        return Err(Error::DifferentLengthArrays);
    }
    for arr in matrix0 {
        if arr.len() != matrix1[0].len() {
            return Err(Error::DifferentLengthArrays);
        }
    }
    Ok(())
}

#[inline(always)]
fn check_vectors_regularity(vec0: &Vec<f64>, vec1: &Vec<f64>) -> Result<()> {
    for i in 1..vec0.len() {
        if vec0[i - 1] > vec0[i] {
            panic!("{}", Error::UnsortedDivision);
        }
    }
    for i in 1..vec1.len() {
        if vec1[i - 1] > vec1[i] {
            panic!("{}", Error::UnsortedDivision);
        }
    }
    Ok(())
}

#[inline(always)]
fn check_matrix_vectors_compatibility<T>(
    matrix: &Vec<Vec<T>>,
    vec0: &Vec<f64>,
    vec1: &Vec<f64>,
) -> Result<()> {
    if matrix.len() != vec0.len() {
        return Err(Error::DifferentLengthArrays);
    }
    for arr in matrix {
        if arr.len() != vec1.len() {
            return Err(Error::DifferentLengthArrays);
        }
    }
    Ok(())
}