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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! A parser for Wavefront's `.mtl` file format, for storing information about
//! the material of which a 3D mesh is composed.
use std::borrow::ToOwned;
use std::cmp::Ordering;
use std::cmp::Ordering::{Equal, Greater, Less};

pub use lex::ParseError;
use lex::{Lexer, PeekableLexer};
use util::OrderingExt;

/// A set of materials in one `.mtl` file.
#[derive(Clone, Debug, PartialEq)]
#[allow(missing_docs)]
pub struct MtlSet {
  pub materials: Vec<Material>,
}

/// A single material that can be applied to any face. They are generally
/// applied by using the Phong shading model.
#[derive(Clone, Debug)]
#[allow(missing_docs)]
pub struct Material {
  pub name: String,
  pub specular_coefficient: f64,
  pub color_ambient: Color,
  pub color_diffuse: Color,
  pub color_specular: Color,
  pub color_emissive: Option<Color>,
  pub optical_density: Option<f64>,
  pub alpha: f64,
  pub illumination: Illumination,
  pub ambient_map: Option<String>,
  pub diffuse_map: Option<String>,
  pub specular_map: Option<String>,
  pub specular_exponent_map: Option<String>,
  pub dissolve_map: Option<String>,
  pub displacement_map: Option<String>,
  pub decal_map: Option<String>,
  pub bump_map: Option<String>,
}

/// How a given material is supposed to be illuminated.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[allow(missing_docs)]
pub enum Illumination {
  Ambient,
  AmbientDiffuse,
  AmbientDiffuseSpecular,
  ReflectionRayTrace,
}

#[derive(Clone, Copy, Debug)]
#[allow(missing_docs)]
pub struct Color {
  pub r: f64,
  pub g: f64,
  pub b: f64,
}

fn fuzzy_cmp(a: f64, b: f64, delta: f64) -> Ordering {
  if (a - b).abs() <= delta {
    Equal
  } else if a < b {
    Less
  } else {
    Greater
  }
}

fn fuzzy_opt_cmp(a: Option<f64>, b: Option<f64>, delta: f64) -> Ordering {
  match (a, b) {
    (None, None) => Equal,
    (Some(_), None) => Greater,
    (None, Some(_)) => Less,
    (Some(a), Some(b)) => fuzzy_cmp(a, b, delta),
  }
}

impl PartialEq for Color {
  fn eq(&self, other: &Color) -> bool {
    self.partial_cmp(other).unwrap() == Equal
  }
}

impl PartialOrd for Color {
  fn partial_cmp(&self, other: &Color) -> Option<Ordering> {
    Some(
      fuzzy_cmp(self.r, other.r, 0.00001)
        .lexico(|| fuzzy_cmp(self.g, other.g, 0.00001))
        .lexico(|| fuzzy_cmp(self.b, other.b, 0.00001)),
    )
  }
}

impl PartialEq for Material {
  fn eq(&self, other: &Material) -> bool {
    self.partial_cmp(other).unwrap() == Equal
  }
}

impl PartialOrd for Material {
  fn partial_cmp(&self, other: &Material) -> Option<Ordering> {
    Some(
      self
        .name
        .cmp(&other.name)
        .lexico(|| {
          fuzzy_cmp(
            self.specular_coefficient,
            other.specular_coefficient,
            0.00001,
          )
        })
        .lexico(|| {
          self
            .color_ambient
            .partial_cmp(&other.color_ambient)
            .unwrap()
        })
        .lexico(|| {
          self
            .color_diffuse
            .partial_cmp(&other.color_diffuse)
            .unwrap()
        })
        .lexico(|| {
          self
            .color_specular
            .partial_cmp(&other.color_specular)
            .unwrap()
        })
        .lexico(|| fuzzy_opt_cmp(self.optical_density, other.optical_density, 0.00001))
        .lexico(|| fuzzy_cmp(self.alpha, other.alpha, 0.00001))
        .lexico(|| self.illumination.cmp(&other.illumination))
        .lexico(|| self.ambient_map.cmp(&other.ambient_map))
        .lexico(|| self.diffuse_map.cmp(&other.diffuse_map))
        .lexico(|| self.specular_map.cmp(&other.specular_map))
        .lexico(|| self.specular_exponent_map.cmp(&other.specular_exponent_map))
        .lexico(|| self.dissolve_map.cmp(&other.dissolve_map))
        .lexico(|| self.displacement_map.cmp(&other.displacement_map))
        .lexico(|| self.decal_map.cmp(&other.decal_map))
        .lexico(|| self.bump_map.cmp(&other.bump_map))
    )
  }
}

struct Parser<'a> {
  line_number: usize,
  lexer: PeekableLexer<'a>,
}

impl<'a> Parser<'a> {
  fn new(input: &'a str) -> Parser<'a> {
    Parser {
      line_number: 1,
      lexer: PeekableLexer::new(Lexer::new(input)),
    }
  }

  fn error_raw(&self, msg: String) -> ParseError {
    ParseError {
      line_number: self.line_number,
      message: msg,
    }
  }

  fn error<A>(&self, msg: String) -> Result<A, ParseError> {
    Err(self.error_raw(msg))
  }

  fn next(&mut self) -> Option<&'a str> {
    let ret = self.lexer.next_str();
    if let Some("\n") = ret {
      self.line_number += 1;
    }
    ret
  }

  fn advance(&mut self) {
    self.next();
  }

  fn peek(&mut self) -> Option<&'a str> {
    self.lexer.peek_str()
  }

  /// Possibly skips over some newlines.
  fn zero_or_more_newlines(&mut self) {
    while let Some("\n") = self.peek() {
      self.advance();
    }
  }
  /// Skips over some newlines, failing if it didn't manage to skip any.
  fn one_or_more_newlines(&mut self) -> Result<(), ParseError> {
    match self.peek() {
      None => {} // allow eof
      Some("\n") => {}
      Some(s) => return self.error(format!("Expected newline but got {}", s)),
    }

    self.zero_or_more_newlines();

    Ok(())
  }

  fn parse_newmtl(&mut self) -> Result<&'a str, ParseError> {
    match self.next() {
      None => return self.error("Expected `newmtl` but got end of input.".to_owned()),
      Some("newmtl") => {}
      Some(s) => return self.error(format!("Expected `newmtl` but got {}.", s)),
    }

    match self.next() {
      None => return self.error("Expected material name but got end of input.".to_owned()),
      Some(s) => Ok(s),
    }
  }

  fn parse_f64(&mut self) -> Result<f64, ParseError> {
    match self.next() {
      None => self.error("Expected f64 but got end of input.".to_owned()),
      Some(s) => {
        lexical::parse(&s).map_err(|_| self.error_raw(format!("Expected f64 but got {}.", s)))
      }
    }
  }

  fn parse_usize(&mut self) -> Result<usize, ParseError> {
    match self.next() {
      None => self.error("Expected usize but got end of input.".to_owned()),
      Some(s) => {
        lexical::parse(&s).map_err(|_| self.error_raw(format!("Expected usize but got {}.", s)))
      }
    }
  }

  fn parse_tag(&mut self, tag: &str) -> Result<(), ParseError> {
    match self.next() {
      None => self.error(format!("Expected `{}` but got end of input.", tag)),
      Some(s) if s != tag => self.error(format!("Expected `{}` but got {}.", tag, s)),
      _ => Ok(()),
    }
  }

  fn parse_color(&mut self) -> Result<Color, ParseError> {
    let r = self.parse_f64()?;
    let g = self.parse_f64()?;
    let b = self.parse_f64()?;

    Ok(Color { r, g, b })
  }

  fn parse_specular_coefficeint(&mut self) -> Result<f64, ParseError> {
    self.parse_tag("Ns")?;
    self.parse_f64()
  }

  fn parse_ambient_color(&mut self) -> Result<Color, ParseError> {
    self.parse_tag("Ka")?;
    self.parse_color()
  }

  fn parse_diffuse_color(&mut self) -> Result<Color, ParseError> {
    self.parse_tag("Kd")?;
    self.parse_color()
  }

  fn parse_specular_color(&mut self) -> Result<Color, ParseError> {
    self.parse_tag("Ks")?;
    self.parse_color()
  }

  fn parse_emissive_color(&mut self) -> Result<Option<Color>, ParseError> {
    if self.peek() != Some("Ke") {
      return Ok(None);
    }
    self.parse_tag("Ke")?;
    self.parse_color().map(|c| Some(c))
  }

  fn parse_optical_density(&mut self) -> Result<Option<f64>, ParseError> {
    match self.peek() {
      Some("Ni") => {}
      _ => return Ok(None),
    }

    self.parse_tag("Ni")?;
    let optical_density = self.parse_f64()?;
    Ok(Some(optical_density))
  }

  fn parse_dissolve(&mut self) -> Result<f64, ParseError> {
    self.parse_tag("d")?;
    self.parse_f64()
  }

  fn parse_illumination(&mut self) -> Result<Illumination, ParseError> {
    self.parse_tag("illum")?;
    match self.parse_usize()? {
      0 => Ok(Illumination::Ambient),
      1 => Ok(Illumination::AmbientDiffuse),
      2 => Ok(Illumination::AmbientDiffuseSpecular),
      3 => Ok(Illumination::ReflectionRayTrace),
      n => self.error(format!("Unknown illumination model: {}.", n)),
    }
  }

  fn parse_map(&mut self, name: &'static str) -> Result<Option<&'a str>, ParseError> {
    match self.peek() {
      Some(s) => if s != name { return Ok(None) },
      _ => return Ok(None),
    }

    self.parse_tag(name)?;
    match self.next() {
      None => self.error("Expected texture path but got end of input.".to_owned()),
      Some(s) => Ok(Some(s)),
    }
  }

  fn parse_material(&mut self) -> Result<Material, ParseError> {
    let name = self.parse_newmtl()?;
    self.one_or_more_newlines()?;
    let spec_coeff = self.parse_specular_coefficeint()?;
    self.one_or_more_newlines()?;
    let amb = self.parse_ambient_color()?;
    self.one_or_more_newlines()?;
    let diff = self.parse_diffuse_color()?;
    self.one_or_more_newlines()?;
    let spec = self.parse_specular_color()?;
    self.one_or_more_newlines()?;
    let emit = self.parse_emissive_color()?;
    if emit.is_some() {
      self.one_or_more_newlines()?;
    }
    let optical_density = self.parse_optical_density()?;
    if optical_density.is_some() {
      self.one_or_more_newlines()?;
    }
    let dissolve = self.parse_dissolve()?;
    self.one_or_more_newlines()?;
    let illum = self.parse_illumination()?;
    self.one_or_more_newlines()?;

    // Parse maps
    // Color textures
    let ambient_map = self.parse_map("map_Ka")?;
    if ambient_map.is_some() {
      self.one_or_more_newlines()?;
    }
    let diffuse_map = self.parse_map("map_Kd")?;
    if diffuse_map.is_some() {
      self.one_or_more_newlines()?;
    }
    let specular_map = self.parse_map("map_Ks")?;
    if specular_map.is_some() {
      self.one_or_more_newlines()?;
    }

    // Scalar textures
    let spec_exp_map = self.parse_map("map_Ns")?;
    if spec_exp_map.is_some() {
      self.one_or_more_newlines()?;
    }
    let dissolve_map = self.parse_map("map_d")?;
    if dissolve_map.is_some() {
      self.one_or_more_newlines()?;
    }
    let disp_map = self.parse_map("disp")?;
    if disp_map.is_some() {
      self.one_or_more_newlines()?;
    }

    // Decal (roughness) texture
    let decal = self.parse_map("decal")?;
    if decal.is_some() {
      self.one_or_more_newlines()?;
    }

    // Bump texture
    let mut bump = self.parse_map("map_bump")?;
    if bump.is_none() {
      // Some implementations use map_bump instead
      bump = self.parse_map("map_bump")?;
    }
    if bump.is_some() {
      self.one_or_more_newlines()?;
    }

    Ok(Material {
      name: name.to_owned(),
      specular_coefficient: spec_coeff,
      color_ambient: amb,
      color_diffuse: diff,
      color_specular: spec,
      color_emissive: emit,
      optical_density,
      alpha: dissolve,
      illumination: illum,
      ambient_map: ambient_map.map(|s| s.to_owned()),
      diffuse_map: diffuse_map.map(|s| s.to_owned()),
      specular_map: specular_map.map(|s| s.to_owned()),
      specular_exponent_map: spec_exp_map.map(|s| s.to_owned()),
      dissolve_map: dissolve_map.map(|s| s.to_owned()),
      displacement_map: disp_map.map(|s| s.to_owned()),
      decal_map: decal.map(|s| s.to_owned()),
      bump_map: bump.map(|s| s.to_owned()),
    })
  }

  fn parse_mtlset(&mut self) -> Result<MtlSet, ParseError> {
    self.zero_or_more_newlines();

    let mut ret = Vec::new();

    loop {
      match self.peek() {
        Some("newmtl") => {
          ret.push(self.parse_material()?);
        }
        _ => break,
      }
    }

    match self.peek() {
      None => {}
      Some(s) => return self.error(format!("Expected end of input but got {}.", s)),
    }

    Ok(MtlSet { materials: ret })
  }
}

/// Parses a wavefront `.mtl` file, returning either the successfully parsed
/// file, or an error. Support in this parser for the full file format is
/// best-effort and realistically I will only end up supporting the subset
/// of the file format which falls under the "shit I see exported from blender"
/// category.
pub fn parse<S: AsRef<str>>(input: S) -> Result<MtlSet, ParseError> {
  Parser::new(input.as_ref()).parse_mtlset()
}

#[test]
fn test_parse() {
  use self::Illumination::AmbientDiffuseSpecular;

  let test_case = r#"
# Blender MTL File: 'None'
# Material Count: 2

# name
newmtl Material
# Phong specular coefficient
Ns 96.078431
# ambient color (weighted)
Ka 0.000000 0.000000 0.000000
# diffuse color (weighted)
Kd 0.640000 0.640000 0.640000
# dissolve factor (weighted)
Ks 0.500000 0.500000 0.500000
# emissive color (weighted)
Ke 0.100000 0.100000 0.100000
# optical density (refraction)
Ni 1.000000
# alpha
d 1.000000
# illumination: 0=ambient, 1=ambient+diffuse, 2=ambient+diffuse+specular
illum 2

newmtl None
Ns 0
# ambient
Ka 0.000000 0.000000 0.000000
# diffuse
Kd 0.8 0.8 0.8
# specular
Ks 0.8 0.8 0.8
d 1
illum 2"#;

  let expected = Ok(MtlSet {
    materials: vec![
      Material {
        name: "Material".to_owned(),
        specular_coefficient: 96.078431,
        color_ambient: Color {
          r: 0.0,
          g: 0.0,
          b: 0.0,
        },
        color_diffuse: Color {
          r: 0.64,
          g: 0.64,
          b: 0.64,
        },
        color_specular: Color {
          r: 0.5,
          g: 0.5,
          b: 0.5,
        },
        color_emissive: Some(Color {
          r: 0.1,
          g: 0.1,
          b: 0.1,
        }),
        optical_density: Some(1.0),
        alpha: 1.0,
        illumination: AmbientDiffuseSpecular,
        ambient_map: None,
        diffuse_map: None,
        specular_map: None,
        specular_exponent_map: None,
        dissolve_map: None,
        displacement_map: None,
        decal_map: None,
        bump_map: None,
      },
      Material {
        name: "None".to_owned(),
        specular_coefficient: 0.0,
        color_ambient: Color {
          r: 0.0,
          g: 0.0,
          b: 0.0,
        },
        color_diffuse: Color {
          r: 0.8,
          g: 0.8,
          b: 0.8,
        },
        color_specular: Color {
          r: 0.8,
          g: 0.8,
          b: 0.8,
        },
        color_emissive: None,
        optical_density: None,
        alpha: 1.0,
        illumination: AmbientDiffuseSpecular,
        ambient_map: None,
        diffuse_map: None,
        specular_map: None,
        specular_exponent_map: None,
        dissolve_map: None,
        displacement_map: None,
        decal_map: None,
        bump_map: None,
      },
    ],
  });

  assert_eq!(parse(test_case), expected);
}

#[test]
fn test_cube() {
  use self::Illumination::AmbientDiffuseSpecular;

  let test_case = r#"
# Blender MTL File: 'cube.blend'
# Material Count: 1

newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Ka cube-ambient.png
map_Kd cube-diff.png
map_Ks cube-spec-color.png
map_Ns cube-spec-exp.mps
map_d cube-alpha.mps
disp cube-disp.mps
decal cube-roughness.mps
map_bump cube-bump.mpb
"#;

  let expected = Ok(MtlSet {
    materials: vec![Material {
      name: "Material".to_owned(),
      specular_coefficient: 96.078431,
      color_ambient: Color {
        r: 0.0,
        g: 0.0,
        b: 0.0,
      },
      color_diffuse: Color {
        r: 0.64,
        g: 0.64,
        b: 0.64,
      },
      color_specular: Color {
        r: 0.5,
        g: 0.5,
        b: 0.5,
      },
      color_emissive: Some(Color {
        r: 0.0,
        g: 0.0,
        b: 0.0,
      }),
      optical_density: Some(1.0),
      alpha: 1.0,
      illumination: AmbientDiffuseSpecular,
      ambient_map: Some("cube-ambient.png".to_owned()),
      diffuse_map: Some("cube-diff.png".to_owned()),
      specular_map: Some("cube-spec-color.png".to_owned()),
      specular_exponent_map: Some("cube-spec-exp.mps".to_owned()),
      dissolve_map: Some("cube-alpha.mps".to_owned()),
      displacement_map: Some("cube-disp.mps".to_owned()),
      decal_map: Some("cube-roughness.mps".to_owned()),
      bump_map: Some("cube-bump.mpb".to_owned()),
    }],
  });

  assert_eq!(parse(test_case), expected);
}

#[test]
fn test_missing_maps() {
  use self::Illumination::AmbientDiffuseSpecular;

  let test_case = r#"
newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Ka ambient.png
map_Ns spec-exp.mps
map_d alpha.mps
map_bump bump.mpb
"#;

  let expected = Ok(MtlSet {
    materials: vec![Material {
      name: "Material".to_owned(),
      specular_coefficient: 96.078431,
      color_ambient: Color {
        r: 0.0,
        g: 0.0,
        b: 0.0,
      },
      color_diffuse: Color {
        r: 0.64,
        g: 0.64,
        b: 0.64,
      },
      color_specular: Color {
        r: 0.5,
        g: 0.5,
        b: 0.5,
      },
      color_emissive: Some(Color {
        r: 0.0,
        g: 0.0,
        b: 0.0,
      }),
      optical_density: Some(1.0),
      alpha: 1.0,
      illumination: AmbientDiffuseSpecular,
      ambient_map: Some("ambient.png".to_owned()),
      diffuse_map: None,
      specular_map: None,
      specular_exponent_map: Some("spec-exp.mps".to_owned()),
      dissolve_map: Some("alpha.mps".to_owned()),
      displacement_map: None,
      decal_map: None,
      bump_map: Some("bump.mpb".to_owned()),
    }],
  });

  assert_eq!(parse(test_case), expected);
}