alloy/model/
obj.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! A module for parsing .obj files.
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use std::str::FromStr;

/// A model that is loaded from a .obj file.
///
/// This is a raw representation of the `obj` file without doing any manipulation
/// of the data. In order to be better consumed by the rendering engine, the data
/// should be converted into a more appropriate format first.
///
/// This type is largely used in tooling for pipeline processing of models.
#[derive(Debug, Default)]
pub struct Model {
  /// The vertices of the model.
  pub vertices: Vec<[f32; 4]>,

  /// The normals of the model.
  pub normals: Vec<[f32; 3]>,

  /// The texture coordinates of the model.
  pub tex_coords: Vec<[f32; 3]>,

  /// The indices of the vertices that make up the faces of the model.
  pub vertex_indices: Vec<[u32; 3]>,

  /// The indices of the texture coordinates that make up the faces of the model.
  pub tex_coords_indices: Vec<[u32; 3]>,

  /// The indices of the normals that make up the faces of the model.
  pub normal_indices: Vec<[u32; 3]>,

  /// The number of components in each vertex.
  pub vertex_components: usize,

  /// The number of components in each texture coordinate.
  pub tex_coord_components: usize,
}

// Constructors

impl Model {
  /// Create a new model.
  pub fn new() -> Self {
    Self::default()
  }

  /// Load a model from a stored .obj file.
  ///
  /// # Parameters
  ///
  /// * `file_path` - The path to the .obj file to load.
  pub fn from_file(file_path: &str) -> Result<Self> {
    let path = Path::new(file_path);
    let file = File::open(path)?;
    let reader = io::BufReader::new(file);

    let mut model = Model::new();

    for line in reader.lines() {
      let line = line?;
      let parts: Vec<_> = line.split_whitespace().collect();

      if parts.is_empty() {
        continue;
      }

      match parts[0] {
        "#" => {} // Ignore comments
        "v" => model.parse_vertex(&parts[1..])?,
        "vn" => model.parse_normal(&parts[1..])?,
        "vt" => model.parse_tex_coords(&parts[1..])?,
        "f" => model.parse_face(&parts[1..])?,
        "s" => {} // Ignore smoothing groups

        _ => {
          return Err(Error::ParseErr(ParseError::UnsupportedKey(
            parts[0].to_string(),
          )))
        }
      }
    }

    Ok(model)
  }
}

// Parsing

impl Model {
  fn parse_vertex(&mut self, parts: &[&str]) -> Result<()> {
    if parts.len() < 3 || parts.len() > 4 {
      return Err(Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::Vertex,
        parts: parts.len(),
      }));
    }

    let float_err = |e| {
      Error::ParseErr(ParseError::FloatParseError {
        kind: Kind::Vertex,
        underlying: e,
      })
    };
    let mut vertex = [0.0f32; 4];
    vertex[0] = parts[0].parse().map_err(float_err)?;
    vertex[1] = parts[1].parse().map_err(float_err)?;
    vertex[2] = parts[2].parse().map_err(float_err)?;
    if parts.len() == 4 {
      vertex[3] = parts[3].parse().map_err(float_err)?;
      self.vertex_components = 4;
    } else {
      vertex[3] = 1.0;
      self.vertex_components = 3;
    }

    self.vertices.push(vertex);
    Ok(())
  }

  fn parse_normal(&mut self, parts: &[&str]) -> Result<()> {
    if parts.len() != 3 {
      return Err(Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::Normal,
        parts: parts.len(),
      }));
    }
    let float_err = |e| {
      Error::ParseErr(ParseError::FloatParseError {
        kind: Kind::Normal,
        underlying: e,
      })
    };
    let normal = [
      parts[0].parse().map_err(float_err)?,
      parts[1].parse().map_err(float_err)?,
      parts[2].parse().map_err(float_err)?,
    ];
    self.normals.push(normal);
    Ok(())
  }

  fn parse_tex_coords(&mut self, parts: &[&str]) -> Result<()> {
    if parts.len() > 3 {
      return Err(Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::TexCoord,
        parts: parts.len(),
      }));
    }
    let float_err = |e| {
      Error::ParseErr(ParseError::FloatParseError {
        kind: Kind::TexCoord,
        underlying: e,
      })
    };
    let mut tex_coord = [0.0f32; 3];

    match parts.len() {
      1 => {
        tex_coord[0] = parts[0].parse().map_err(float_err)?;
        self.tex_coord_components = 1;
      }
      2 => {
        tex_coord[0] = parts[0].parse().map_err(float_err)?;
        tex_coord[1] = parts[1].parse().map_err(float_err)?;
        self.tex_coord_components = 2;
      }
      3 => {
        tex_coord[0] = parts[0].parse().map_err(float_err)?;
        tex_coord[1] = parts[1].parse().map_err(float_err)?;
        tex_coord[2] = parts[2].parse().map_err(float_err)?;
        self.tex_coord_components = 3;
      }
      _ => unreachable!(),
    }

    self.tex_coords.push(tex_coord);
    Ok(())
  }

  fn parse_face(&mut self, parts: &[&str]) -> Result<()> {
    if parts.len() != 3 {
      return Err(Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::Face,
        parts: parts.len(),
      }));
    }

    let mut vertex = [0u32; 3];
    let mut tex_coords = [0u32; 3];
    let mut normals = [0u32; 3];
    let mut has_tex_coords = false;
    let mut has_normals = false;

    let map_err = || {
      Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::Face,
        parts: 0,
      })
    };

    for (i, part) in parts.iter().enumerate() {
      let indices = self.parse_face_vertex(&part.split('/').collect::<Vec<_>>())?;
      vertex[i] = indices[0].ok_or_else(map_err)? - 1;

      if let Some(tex_coord) = indices[1] {
        tex_coords[i] = tex_coord - 1;
        has_tex_coords = true;
      }
      if let Some(normal) = indices[2] {
        normals[i] = normal - 1;
        has_normals = true;
      }
    }

    self.vertex_indices.push(vertex);
    if has_tex_coords {
      self.tex_coords_indices.push(tex_coords);
    }
    if has_normals {
      self.normal_indices.push(normals);
    }

    Ok(())
  }

  fn parse_face_vertex(&mut self, parts: &[&str]) -> Result<[Option<u32>; 3]> {
    let index_err = |e| {
      Error::ParseErr(ParseError::IndexParseError {
        kind: Kind::Face,
        underlying: e,
      })
    };
    match parts.len() {
      1 => {
        let vertex = parts[0].parse().map_err(index_err)?;
        Ok([Some(vertex), None, None])
      }
      2 => {
        let vertex = parts[0].parse().map_err(index_err)?;
        let tex_coords = parts[1].parse().map_err(|e| {
          Error::ParseErr(ParseError::IndexParseError {
            kind: Kind::Face,
            underlying: e,
          })
        })?;
        Ok([Some(vertex), Some(tex_coords), None])
      }
      3 => {
        let vertex = Some(parts[0].parse().map_err(index_err)?);
        let mut tex_coords = None;
        if !parts[1].is_empty() {
          tex_coords = Some(parts[1].parse().map_err(index_err)?);
        }
        let normal = Some(parts[2].parse().map_err(index_err)?);
        Ok([vertex, tex_coords, normal])
      }
      _ => Err(Error::ParseErr(ParseError::BadDefinition {
        kind: Kind::Face,
        parts: parts.len(),
      })),
    }
  }
}

/// The underlying kind of element being referenced in the obj file.
#[derive(Debug)]
pub enum Kind {
  /// A vertex
  Vertex,

  /// A normal
  Normal,

  /// A texture coordinate
  TexCoord,

  /// A face
  Face,
}

impl std::fmt::Display for Kind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(match self {
      Self::Vertex => "vertex",
      Self::Normal => "normal",
      Self::TexCoord => "texcoord",
      Self::Face => "face",
    })
  }
}

/// An error that is emitted when parsing a badly defined .obj file.
#[derive(Debug)]
pub enum ParseError {
  /// The error that is emitted when a definition has too few or too many components.
  BadDefinition {
    /// The kind of element that was being parsed.
    kind: Kind,
    /// The number of components that were parsed.
    parts: usize,
  },
  /// The error that is emitted when a float cannot be parsed.
  FloatParseError {
    /// The kind of element that was being parsed.
    kind: Kind,
    /// The underlying error that was emitted by the float parser.
    underlying: <f32 as FromStr>::Err,
  },
  /// The error that is emitted when an index cannot be parsed.
  IndexParseError {
    /// The kind of element that was being parsed.
    kind: Kind,
    /// The underlying error that was emitted by the index parser.
    underlying: <u32 as FromStr>::Err,
  },
  /// The error that is emitted when an unsupported key is encountered.
  UnsupportedKey(String),
}

impl std::fmt::Display for ParseError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::BadDefinition { kind, parts } => {
        if parts > &3usize {
          write!(f, "too many components for {}", kind)
        } else {
          write!(f, "too few components for {}", kind)
        }
      }
      Self::FloatParseError { kind, underlying } => {
        write!(f, "unable to parse f32 for {}: {}", kind, underlying)
      }
      Self::IndexParseError { kind, underlying } => {
        write!(f, "unable to parse u32 for {}: {}", kind, underlying)
      }
      Self::UnsupportedKey(key) => write!(f, "unsupported key: {}", key),
    }
  }
}

/// An error that is emitted when parsing a .obj file.
#[derive(Debug)]
pub enum Error {
  /// The error that is emitted when a parse error occurs.
  ParseErr(ParseError),
  /// The error that is emitted when an IO error occurs.
  IOErr(io::Error),
}

impl From<ParseError> for Error {
  fn from(value: ParseError) -> Self {
    Self::ParseErr(value)
  }
}

impl From<io::Error> for Error {
  fn from(value: io::Error) -> Self {
    Self::IOErr(value)
  }
}

impl std::fmt::Display for Error {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::ParseErr(err) => err.fmt(f),
      Self::IOErr(err) => err.fmt(f),
    }
  }
}

type Result<T> = std::result::Result<T, Error>;

impl std::error::Error for ParseError {}