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
//! Parses `.obj` format which stores 3D mesh data

use std::collections::HashMap;
use std::io::BufRead;
use vec_map::VecMap;

use crate::error::ObjResult;
use crate::raw::lexer::lex;
use crate::raw::util::parse_args;

macro_rules! parse_args {
    {
        $first:expr, $rest:expr,
        $($pat:pat => $type:ident::$name:ident[$exp:expr]),*,
        ! => $error:expr
    } => (
        match split_vertex_group($first)[..] {
            $($pat => $type::$name({
                let mut points = vec![$exp];
                for param in $rest {
                    match split_vertex_group(param)[..] {
                        $pat => points.push($exp),
                        _ => $error
                    }
                }
                points
            }),)*
            _ => $error
        }
    )
}

// Helper function for handling the indexes.
//
// If total size of the collection is 5:
//
// - ["1", "2", "3", "4", "5"] → [0, 1, 2, 3, 4]
// - ["-5", "-4", "-3", "-2", "-1"] → [0, 1, 2, 3, 4]
// - ["0"] → Error
// - ["6"] → Error
// - ["-6"] → Error
//
// If the index is < 0, then it represents an offset from the end of
// the current list. So -1 is the most recently added vertex.
//
// If the index is > 0 then it's simply the position in the list such
// that 1 is the first vertex.
fn try_index<T>(collection: &[T], input: &str) -> ObjResult<usize> {
    use crate::error::{LoadError, LoadErrorKind, ObjError};
    use std::convert::TryInto;

    let len: isize = collection.len().try_into().map_err(|_| {
        ObjError::Load(LoadError::new(
            LoadErrorKind::IndexOutOfRange,
            "Too many items in collection",
        ))
    })?;

    // Should be [-len, -1] ∪ [1, len]
    let index: isize = input.parse()?;

    let ret = if index < -len {
        // (∞, -len)
        make_error!(IndexOutOfRange, "Too small index value");
    } else if index < 0 {
        // [-len, 0)
        len + index
    } else if index == 0 {
        // {0}
        make_error!(IndexOutOfRange, "Index value shouldn't be zero");
    } else if index <= len {
        // (0, len]
        index - 1
    } else {
        // (len, ∞)
        make_error!(IndexOutOfRange, "Too big index value");
    };

    Ok(ret as usize)
}

/// Parses a wavefront `.obj` format.
pub fn parse_obj<T: BufRead>(input: T) -> ObjResult<RawObj> {
    let mut name = None;
    let mut material_libraries = Vec::new();

    let mut positions = Vec::new();
    let mut tex_coords = Vec::new();
    let mut normals = Vec::new();
    let mut param_vertices = Vec::new();

    let mut points = Vec::new();
    let mut lines = Vec::new();
    let mut polygons = Vec::new();

    let counter = Counter::new(&points, &lines, &polygons);
    let mut group_builder = counter.hash_map("default".to_string());
    let mut mesh_builder = counter.hash_map(String::new());
    let mut smoothing_builder = counter.vec_map();
    let mut merging_builder = counter.vec_map();

    lex(input, |stmt, args: &[&str]| {
        match stmt {
            // Vertex data
            "v" => positions.push(match parse_args(args)?[..] {
                [x, y, z, w] => (x, y, z, w),
                [x, y, z] => (x, y, z, 1.0),
                _ => make_error!(WrongNumberOfArguments, "Expected 3 or 4 arguments"),
            }),
            "vt" => tex_coords.push(match parse_args(args)?[..] {
                [u, v, w] => (u, v, w),
                [u, v] => (u, v, 0.0),
                [u] => (u, 0.0, 0.0),
                _ => make_error!(WrongNumberOfArguments, "Expected 1, 2 or 3 arguments"),
            }),
            "vn" => normals.push(match parse_args(args)?[..] {
                [x, y, z] => (x, y, z),
                _ => make_error!(WrongNumberOfArguments, "Expected 3 arguments"),
            }),
            "vp" => param_vertices.push(match parse_args(args)?[..] {
                [u, v, w] => (u, v, w),
                [u, v] => (u, v, 1.0),
                [u] => (u, 0.0, 1.0),
                _ => make_error!(WrongNumberOfArguments, "Expected 1, 2 or 3 arguments"),
            }),

            // Free-form curve / surface attributes
            // TODO: Use rational information
            "cstype" => {
                let geometry = match args {
                    ["rat", ty] => *ty,
                    [ty] => *ty,
                    _ => make_error!(WrongTypeOfArguments, "Expected 'rat xxx' or 'xxx' format"),
                };

                match geometry {
                    "bmatrix" => unimplemented!(),
                    "bezier" => unimplemented!(),
                    "bspline" => unimplemented!(),
                    "cardinal" => unimplemented!(),
                    "taylor" => unimplemented!(),
                    _ => make_error!(
                        WrongTypeOfArguments,
                        "Expected one of 'bmatrix', 'bezier', 'bspline', 'cardinal' and 'taylor'"
                    ),
                }
            }
            "deg" => match parse_args(args)?[..] {
                [_deg_u, _deg_v] => unimplemented!(),
                [_deg_u] => unimplemented!(),
                _ => make_error!(WrongNumberOfArguments, "Expected 1 or 2 arguments"),
            },
            "bmat" => unimplemented!(),
            "step" => unimplemented!(),

            // Elements
            "p" => {
                for v in args {
                    let v = try_index(&positions, v)?;
                    points.push(v);
                }
            }
            "l" => match args {
                [] => make_error!(WrongNumberOfArguments, "Expected at least 2 arguments"),
                [first, rest @ ..] => {
                    if args.len() < 2 {
                        make_error!(WrongNumberOfArguments, "Expected at least 2 arguments")
                    }

                    let line = parse_args! {
                        first, rest,
                        [p] => Line::P[try_index(&positions, p)?],
                        [p, t] => Line::PT[(try_index(&positions, p)?, try_index(&tex_coords, t)?)],
                        ! => make_error!(WrongTypeOfArguments, "Unexpected vertex format, expected `#`, or `#/#`")
                    };

                    lines.push(line);
                }
            },
            "fo" | "f" => match args {
                [] => make_error!(WrongNumberOfArguments, "Expected at least 3 arguments"),
                [first, rest @ ..] => {
                    if args.len() < 3 {
                        make_error!(WrongNumberOfArguments, "Expected at least 3 arguments")
                    }

                    let polygon = parse_args! {
                        first, rest,
                        [p] => Polygon::P[try_index(&positions, p)?],
                        [p, t] => Polygon::PT[(try_index(&positions, p)?, try_index(&tex_coords, t)?)],
                        [p, "", n] => Polygon::PN[(try_index(&positions, p)?, try_index(&normals, n)?)],
                        [p, t, n] => Polygon::PTN[(try_index(&positions, p)?, try_index(&tex_coords, t)?, try_index(&normals, n)?)],
                        ! => make_error!(WrongTypeOfArguments, "Unexpected vertex format, expected `#`, `#/#`, `#//#`, or `#/#/#`")
                    };

                    polygons.push(polygon);
                }
            },
            "curv" => unimplemented!(),
            "curv2" => unimplemented!(),
            "surf" => unimplemented!(),

            // Free-form curve / surface body statements
            "parm" => unimplemented!(),
            "trim" => unimplemented!(),
            "hole" => unimplemented!(),
            "scrv" => unimplemented!(),
            "sp" => unimplemented!(),
            "end" => unimplemented!(),

            // Connectivity between free-form surfaces
            "con" => unimplemented!(),

            // Grouping
            "g" => match args {
                [name] => group_builder.start((*name).to_string()),
                _ => make_error!(
                    WrongNumberOfArguments,
                    "Expected group name parameter, but nothing has been supplied"
                ),
            },
            "s" => match args {
                ["off"] | ["0"] => smoothing_builder.end(),
                [param] => smoothing_builder.start(param.parse()?),
                _ => make_error!(WrongNumberOfArguments, "Expected only 1 argument"),
            },
            "mg" => match args {
                ["off"] | ["0"] => merging_builder.end(),
                [param] => merging_builder.start(param.parse()?),
                _ => make_error!(WrongNumberOfArguments, "Expected only 1 argument"),
            },
            "o" => {
                name = match args {
                    [] => None,
                    _ => Some(args.join(" ")),
                    // TODO: "name a  b" will be parsed as "name a b"
                }
            }

            // Display / render attributes
            "bevel" => unimplemented!(),
            "c_interp" => unimplemented!(),
            "d_interp" => unimplemented!(),
            "lod" => unimplemented!(),
            "usemtl" => match args {
                [material] => mesh_builder.start((*material).to_string()),
                _ => make_error!(WrongNumberOfArguments, "Expected only 1 argument"),
            },
            "mtllib" => {
                material_libraries.reserve(args.len());
                for &path in args {
                    material_libraries.push(path.to_string());
                }
            }
            "shadow_obj" => unimplemented!(),
            "trace_obj" => unimplemented!(),
            "ctech" => unimplemented!(),
            "stech" => unimplemented!(),

            // Unexpected statement
            _ => make_error!(UnexpectedStatement, "Received unknown statement"),
        }

        Ok(())
    })?;

    group_builder.end();
    mesh_builder.end();
    smoothing_builder.end();
    merging_builder.end();

    Ok(RawObj {
        name,
        material_libraries,

        positions,
        tex_coords,
        normals,
        param_vertices,

        points,
        lines,
        polygons,

        groups: group_builder.result,
        meshes: mesh_builder.result,
        smoothing_groups: smoothing_builder.result,
        merging_groups: merging_builder.result,
    })
}

/// Splits a string with '/'.
fn split_vertex_group(input: &str) -> Vec<&str> {
    input.split('/').collect()
}

/// Counts current total count of parsed `points`, `lines` and `polygons`.
struct Counter {
    points: *const Vec<Point>,
    lines: *const Vec<Line>,
    polygons: *const Vec<Polygon>,
}

impl Counter {
    /// Constructs a new `Counter`.
    fn new(
        points: *const Vec<Point>,
        lines: *const Vec<Line>,
        polygons: *const Vec<Polygon>,
    ) -> Self {
        Counter {
            points,
            lines,
            polygons,
        }
    }

    /// Returns a current count of parsed `(points, lines, polygons)`.
    fn get(&self) -> (usize, usize, usize) {
        unsafe {
            (
                (*self.points).len(),
                (*self.lines).len(),
                (*self.polygons).len(),
            )
        }
    }

    /// Creates a `HashMap<String, Group>` builder which references `self` as counter.
    fn hash_map<'a>(&'a self, input: String) -> GroupBuilder<'a, HashMap<String, Group>, String> {
        let mut result = HashMap::with_capacity(1);
        result.insert(input.clone(), Group::new((0, 0, 0)));

        GroupBuilder {
            counter: self,
            current: Some(input),
            result,
        }
    }

    /// Creates a `VecMap<Group>` builder which references `self` as counter.
    fn vec_map<'a>(&'a self) -> GroupBuilder<'a, VecMap<Group>, usize> {
        GroupBuilder {
            counter: self,
            current: None,
            result: VecMap::new(),
        }
    }
}

/// Helper for creating `groups`, `meshes`, `smoothing_groups` and `merging_groups` member of
/// `Obj`.
struct GroupBuilder<'a, T, K> {
    counter: &'a Counter,
    current: Option<K>, // Some(K) if some group has been started
    // None    otherwise
    result: T,
}

impl<'a, T, K> GroupBuilder<'a, T, K>
where
    T: Map<K, Group>,
    K: Clone + Key,
{
    /// Starts a group whose name is `input`.
    fn start(&mut self, input: K) {
        let count = self.counter.get();
        if let Some(ref current) = self.current {
            if *current == input {
                return;
            }
            if self.result.get_mut(current).unwrap().end(count) {
                let res = self.result.remove(&current);
                assert!(res.is_some());
            }
        }
        (|| {
            if let Some(ref mut group) = self.result.get_mut(&input) {
                group.start(count);
                return;
            }
            let res = self.result.insert(input.clone(), Group::new(count));
            assert!(res.is_none());
        })();
        self.current = Some(input);
    }

    /// Ends a current group.
    fn end(&mut self) {
        if let Some(ref current) = self.current {
            if self
                .result
                .get_mut(current)
                .unwrap()
                .end(self.counter.get())
            {
                let result = self.result.remove(current);
                assert!(result.is_some());
            }
        } else {
            return;
        }
        self.current = None;
    }
}

/// Constant which is used to represent undefined bound of range.
const UNDEFINED: usize = ::std::usize::MAX;

impl Group {
    fn new(count: (usize, usize, usize)) -> Self {
        let mut ret = Group {
            points: Vec::with_capacity(1),
            lines: Vec::with_capacity(1),
            polygons: Vec::with_capacity(1),
        };
        ret.start(count);
        ret
    }

    fn start(&mut self, count: (usize, usize, usize)) {
        self.points.push(Range {
            start: count.0,
            end: UNDEFINED,
        });
        self.lines.push(Range {
            start: count.1,
            end: UNDEFINED,
        });
        self.polygons.push(Range {
            start: count.2,
            end: UNDEFINED,
        })
    }

    /// Closes group, return true if self is empty
    fn end(&mut self, count: (usize, usize, usize)) -> bool {
        end(&mut self.points, count.0);
        end(&mut self.lines, count.1);
        end(&mut self.polygons, count.2);

        fn end(vec: &mut Vec<Range>, end: usize) {
            let last = vec.len() - 1;
            assert_eq!(vec[last].end, UNDEFINED);
            if vec[last].start != end {
                vec[last].end = end;
            } else {
                vec.pop();
            }
        }

        self.points.is_empty() && self.lines.is_empty() && self.polygons.is_empty()
    }
}

/// Custom trait to interface `HashMap` and `VecMap`.
trait Map<K: Key, V> {
    /// Interface of `insert` function.
    fn insert(&mut self, _: K, _: V) -> Option<V>;
    /// Interface of `get_mut` function.
    fn get_mut(&mut self, k: &K) -> Option<&mut V>;
    /// Interface of `remove` function.
    fn remove(&mut self, k: &K) -> Option<V>;
}

impl<V> Map<String, V> for HashMap<String, V> {
    fn insert(&mut self, k: String, v: V) -> Option<V> {
        self.insert(k, v)
    }
    fn get_mut(&mut self, k: &String) -> Option<&mut V> {
        self.get_mut(k)
    }
    fn remove(&mut self, k: &String) -> Option<V> {
        self.remove(k)
    }
}

impl<V> Map<usize, V> for VecMap<V> {
    fn insert(&mut self, k: usize, v: V) -> Option<V> {
        self.insert(k, v)
    }
    fn get_mut(&mut self, k: &usize) -> Option<&mut V> {
        self.get_mut(*k)
    }
    fn remove(&mut self, k: &usize) -> Option<V> {
        self.remove(*k)
    }
}

/// A trait which should be implemented by a type passed into `Key` of `Map`.
trait Key: Eq {}

impl Key for String {}
impl Key for usize {}

/// Low-level Rust binding for `.obj` format.
pub struct RawObj {
    /// Name of the object.
    pub name: Option<String>,
    /// `.mtl` files which required by this object.
    pub material_libraries: Vec<String>,

    /// Position vectors of each vertex.
    pub positions: Vec<(f32, f32, f32, f32)>,
    /// Texture coordinates of each vertex.
    pub tex_coords: Vec<(f32, f32, f32)>,
    /// Normal vectors of each vertex.
    pub normals: Vec<(f32, f32, f32)>,
    /// Parametric vertices.
    pub param_vertices: Vec<(f32, f32, f32)>,

    /// Points which stores the index data of position vectors.
    pub points: Vec<Point>,
    /// Lines which store the index data of vectors.
    pub lines: Vec<Line>,
    /// Polygons which store the index data of vectors.
    pub polygons: Vec<Polygon>,

    /// Groups of multiple geometries.
    pub groups: HashMap<String, Group>,
    /// Geometries which consist in a same material.
    pub meshes: HashMap<String, Group>,
    /// Smoothing groups.
    pub smoothing_groups: VecMap<Group>,
    /// Merging groups.
    pub merging_groups: VecMap<Group>,
}

/// The `Point` type which stores the index of the position vector.
pub type Point = usize;

/// The `Line` type.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Line {
    /// A series of line segments which contain only the position data of each vertex
    P(Vec<usize>),
    /// A series of line segments which contain both position and texture coordinate
    /// data of each vertex
    PT(Vec<(usize, usize)>),
}

/// The `Polygon` type.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Polygon {
    /// A polygon which contains only the position data of each vertex.
    P(Vec<usize>),
    /// A polygon which contains both position and texture coordinate data of each vertex.
    PT(Vec<(usize, usize)>),
    /// A polygon which contains both position and normal data of each vertex.
    PN(Vec<(usize, usize)>),
    /// A polygon which contains all position, texture coordinate and normal data of each vertex.
    PTN(Vec<(usize, usize, usize)>),
}

/// A group which contains ranges of points, lines and polygons
#[derive(Clone, Debug)]
pub struct Group {
    /// Multiple range of points
    pub points: Vec<Range>,
    /// Multiple range of lines
    pub lines: Vec<Range>,
    /// Multiple range of polygons
    pub polygons: Vec<Range>,
}

/// A struct which represent `[start, end)` range.
#[derive(Copy, PartialEq, Eq, Clone, Debug)]
pub struct Range {
    /// The lower bound of the range (inclusive).
    pub start: usize,
    /// The upper bound of the range (exclusive).
    pub end: usize,
}