redges 0.3.0

A radial edge is a data structure for topological operations.
Documentation
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
//! Helper structure/methods for exporting and loading to/from wavefront files (only geometry).
use std::fmt::{Debug, Display};
use std::fs::File;
use std::io::Write;
use std::io::{BufRead, BufReader};
use std::ops::AddAssign;

use linear_isomorphic::*;

type Vec3 = nalgebra::Vector3<f32>;
type Vec2 = nalgebra::Vector2<f32>;

/// Wavefront data.
#[derive(Debug)]
pub struct ObjData {
    /// Vertex data.
    pub vertices: Vec<Vec3>,
    /// Normal data.
    pub normals: Vec<Vec3>,
    /// Uv data.
    pub uvs: Vec<Vec2>,
    /// Vertex topology.
    pub vertex_face_indices: Vec<Vec<u64>>,
    /// Normal topology.
    pub normal_face_indices: Vec<Vec<u64>>,
    /// Texture topology.
    pub uv_face_indices: Vec<Vec<u64>>,
    /// Specified lines (part of the standard, most parsers forget this exists).
    pub lines: Vec<[u64; 2]>,
    /// List of objects, use this to index into the above arrays if you
    /// wish to extract a specific object.
    pub objects: Vec<ObjectRanges>,
}

/// Metadata needed t extract a single object in the wavefront.
#[derive(Debug)]
pub struct ObjectRanges {
    /// Name of the object.
    pub name: String,
    /// Indices of the position range of the object.
    pub positions: (u64, u64),
    /// Indices of the normal range of the object.
    pub normals: (u64, u64),
    /// Indices of the uv range of the object.
    pub uvs: (u64, u64),
    /// Indices of the lines range of the object.
    pub lines: (u64, u64),
    /// Indices of the polygonal faces range of the object.
    pub polygons: [(u64, u64); 3],
}

impl ObjData {
    /// Load wavefront dta from a file.
    pub fn from_disk_file(path: &str) -> Self {
        let file = File::open(path).unwrap();
        let reader = BufReader::new(file);

        let mut vert_topology = Vec::<Vec<u64>>::new();
        let mut normal_topology = Vec::<Vec<u64>>::new();
        let mut uv_topology = Vec::<Vec<u64>>::new();
        let mut lines = Vec::<[u64; 2]>::new();

        let mut vertices = Vec::<Vec3>::new();
        let mut normals = Vec::<Vec3>::new();
        let mut uvs = Vec::<Vec2>::new();
        let mut object_ranges = Vec::<ObjectRanges>::new();
        for line in reader.lines() {
            let line_str = line.unwrap();
            let line_str = line_str.trim();
            let tokens = line_str.split(" ").collect::<Vec<&str>>();
            match tokens[0] {
                "o" => {
                    object_ranges.push(ObjectRanges {
                        name: tokens[1..].join(" "),
                        positions: (vertices.len() as u64, 0),
                        normals: (normals.len() as u64, 0),
                        uvs: (uvs.len() as u64, 0),
                        lines: (lines.len() as u64, 0),
                        polygons: [
                            (vert_topology.len() as u64, 0),
                            (normal_topology.len() as u64, 0),
                            (uv_topology.len() as u64, 0),
                        ],
                    });
                }
                "v" => {
                    add_vec_3d(&tokens[1..], &mut vertices);
                }
                "vn" => {
                    add_vec_3d(&tokens[1..], &mut normals);
                }
                "vt" => {
                    add_vec_2d(&tokens[1..], &mut uvs);
                }
                "l" => {
                    add_line(&tokens[1..], &mut lines);
                }
                "f" => add_face(
                    &tokens[1..],
                    &mut vert_topology,
                    &mut normal_topology,
                    &mut uv_topology,
                ),
                _ => continue,
            }

            if let Some(or) = object_ranges.last_mut() {
                or.positions.1 = vertices.len() as u64;
                or.normals.1 = normals.len() as u64;
                or.uvs.1 = uvs.len() as u64;
                or.lines.1 = lines.len() as u64;

                or.polygons[0].1 = vert_topology.len() as u64;
                or.polygons[1].1 = normal_topology.len() as u64;
                or.polygons[2].1 = uv_topology.len() as u64;
            }
        }

        ObjData {
            vertices,
            normals,
            uvs,
            vertex_face_indices: vert_topology,
            normal_face_indices: normal_topology,
            uv_face_indices: uv_topology,
            lines,
            objects: object_ranges,
        }
    }

    /// Write a compatible mesh representation to disk.
    pub fn export<'a, T>(mesh: &'a T, path: &str)
    where
        T: WaveFrontCompatible<'a>,
    {
        std::fs::create_dir_all(std::path::Path::new(path).parent().unwrap()).unwrap();
        let mut file = File::create(path).unwrap();

        let one = 1;
        for point in mesh.pos_iterator() {
            writeln!(file, "v {} {} {}", point[0], point[1], point[2]).unwrap();
        }

        for norm in mesh.norm_iterator() {
            writeln!(file, "vn {} {} {}", norm[0], norm[1], norm[2]).unwrap();
        }

        for uv in mesh.uv_iterator() {
            writeln!(file, "vt {} {}", uv[0], uv[1]).unwrap();
        }

        for line in mesh.segment_iterator() {
            writeln!(file, "l {} {}", line[0] + one, line[1] + one).unwrap();
        }

        let mut indices = Vec::new();
        let mut vert_face_count = 0;
        for face in mesh.pos_index_iterator() {
            vert_face_count += 1;

            indices.push(Vec::new());
            let face_ids: &mut Vec<_> = indices.last_mut().unwrap();

            for pos_id in face {
                face_ids.push([Some(pos_id), None, None]);
            }
        }

        let mut uv_face_count = 0;
        for face in mesh.uv_index_iterator() {
            for (local_id, uv_id) in face.enumerate() {
                indices[uv_face_count][local_id][1] = Some(uv_id);
            }
            uv_face_count += 1;
        }

        debug_assert!(uv_face_count == 0 || uv_face_count == vert_face_count);

        let mut norm_face_count = 0;
        for face in mesh.norm_index_iterator() {
            for (local_id, norm_id) in face.enumerate() {
                indices[norm_face_count][local_id][2] = Some(norm_id);
            }
            norm_face_count += 1;
        }

        debug_assert!(norm_face_count == 0 || norm_face_count == vert_face_count);

        for face in indices {
            let mut face_data = "f ".to_string();

            for [pos_id, uv_id, norm_id] in face {
                let pos_str = (pos_id.unwrap() + one).to_string();
                let uv_str = match uv_id {
                    None => "".to_string(),
                    Some(x) => (x + one).to_string(),
                };
                let norm_str = match norm_id {
                    None => "".to_string(),
                    Some(x) => (x + one).to_string(),
                };

                face_data.push_str(format!("{}/{}/{} ", pos_str, uv_str, norm_str).as_str());
            }

            face_data.push('\n');

            write!(file, "{}", face_data).unwrap();
        }
    }
}

/// Trait needed to export a mesh representation as an obj file (only geometry information).
/// very useful for inspecting meshes.
pub trait WaveFrontCompatible<'a> {
    /// Underlying scalar representation for vector data, usually f32 or f64.
    type Scalar: num_traits::Float + Debug + AddAssign + Display;

    /// A way to iterate over the vertex positions.
    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]>;
    /// A way to iterate over the texture coordinates.
    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]>;
    /// A way to iterate over the norms.
    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]>;

    /// A way to iterate over the lines/segments.
    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]>;
    /// A way to iterate over the vertex topology.
    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>>;
    /// A way to iterate over the uv topology.
    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>>;
    /// A way to iterate over the normal topology.
    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>>;
}

impl<'a> WaveFrontCompatible<'a> for ObjData {
    type Scalar = f32;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [f32; 3]> {
        self.vertices.iter().map(|v| [v.x, v.y, v.z])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [f32; 2]> {
        self.uvs.iter().map(|v| [v.x, v.y])
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [f32; 3]> {
        self.normals.iter().map(|v| [v.x, v.y, v.z])
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        self.lines.iter().map(|[i, j]| [*i as usize, *j as usize])
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        self.vertex_face_indices
            .iter()
            .map(|l| l.iter().map(|i| *i as usize))
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        self.uv_face_indices
            .iter()
            .map(|l| l.iter().map(|i| *i as usize))
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        self.normal_face_indices
            .iter()
            .map(|l| l.iter().map(|i| *i as usize))
    }
}

impl<'a, V, S> WaveFrontCompatible<'a> for Vec<V>
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        std::iter::empty()
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S> WaveFrontCompatible<'a> for (&Vec<V>, &Vec<usize>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        std::iter::empty()
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        debug_assert!(self.1.len() % 3 == 0);
        self.1.chunks(3).map(|chunk| chunk.iter().copied())
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S> WaveFrontCompatible<'a> for (Vec<V>, Vec<usize>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        std::iter::empty()
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        debug_assert!(self.1.len() % 3 == 0);
        self.1.chunks(3).map(|chunk| chunk.iter().copied())
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S, I> WaveFrontCompatible<'a> for (&Vec<V>, &Vec<Vec<I>>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
    usize: TryFrom<I>,
    I: num_traits::PrimInt + std::fmt::Display,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        std::iter::empty()
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        self.1.iter().map(|face| {
            face.iter()
                .map(|i| usize::try_from(*i).unwrap_or(usize::MAX))
        })
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S, I> WaveFrontCompatible<'a> for (Vec<V>, Vec<Vec<I>>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
    usize: TryFrom<I>,
    I: num_traits::PrimInt + std::fmt::Display,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        std::iter::empty()
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        self.1.iter().map(|face| {
            face.iter()
                .map(|i| usize::try_from(*i).unwrap_or(usize::MAX))
        })
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S, I> WaveFrontCompatible<'a> for (&Vec<V>, &Vec<[I; 2]>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
    usize: TryFrom<I>,
    I: num_traits::PrimInt + Display,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        self.1.iter().map(|[i, j]| {
            [
                usize::try_from(*i).unwrap_or(usize::MAX),
                usize::try_from(*j).unwrap_or(usize::MAX),
            ]
        })
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

impl<'a, V, S, I> WaveFrontCompatible<'a> for (Vec<V>, Vec<[I; 2]>)
where
    V: VectorSpace<Scalar = S>,
    S: num_traits::Float + AddAssign + Display + Debug,
    usize: TryFrom<I>,
    I: num_traits::PrimInt + Display,
{
    type Scalar = S;

    fn pos_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        self.0.iter().map(|v| [v[0], v[1], v[2]])
    }

    fn uv_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 2]> {
        std::iter::empty()
    }

    fn norm_iterator(&'a self) -> impl Iterator<Item = [Self::Scalar; 3]> {
        std::iter::empty()
    }

    fn segment_iterator(&'a self) -> impl Iterator<Item = [usize; 2]> {
        self.1.iter().map(|[i, j]| {
            [
                usize::try_from(*i).unwrap_or(usize::MAX),
                usize::try_from(*j).unwrap_or(usize::MAX),
            ]
        })
    }

    fn pos_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn uv_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }

    fn norm_index_iterator(&'a self) -> impl Iterator<Item = impl Iterator<Item = usize>> {
        let empty_iterator: std::iter::Empty<std::iter::Empty<usize>> = std::iter::empty();
        empty_iterator
    }
}

fn add_vec_3d(tokens: &[&str], vecs: &mut Vec<Vec3>) {
    let vec = Vec3::new(
        tokens[0].parse::<f32>().unwrap(),
        tokens[1].parse::<f32>().unwrap(),
        tokens[2].parse::<f32>().unwrap(),
    );
    vecs.push(vec);
}

fn add_vec_2d(tokens: &[&str], vecs: &mut Vec<Vec2>) {
    let vec = Vec2::new(
        tokens[0].parse::<f32>().unwrap(),
        tokens[1].parse::<f32>().unwrap(),
    );
    vecs.push(vec);
}

fn add_line(tokens: &[&str], vecs: &mut Vec<[u64; 2]>) {
    assert!(tokens.len() == 2, "have {} expected 2", tokens.len());
    let index_1 = tokens[0].parse::<u64>().unwrap() - 1;
    let index_2 = tokens[1].parse::<u64>().unwrap() - 1;

    vecs.push([index_1, index_2])
}

fn add_face(
    tokens: &[&str],
    vert_topology: &mut Vec<Vec<u64>>,
    normal_topology: &mut Vec<Vec<u64>>,
    uv_topology: &mut Vec<Vec<u64>>,
) {
    assert!(
        tokens.len() >= 3,
        "have {} out of a minimum of 3",
        tokens.len()
    );
    vert_topology.push(Vec::new());
    uv_topology.push(Vec::new());
    normal_topology.push(Vec::new());

    for token in tokens {
        let inner_tokens = token.split("/").collect::<Vec<&str>>();

        assert!(inner_tokens.len() <= 3 && !inner_tokens.is_empty());

        vert_topology
            .last_mut()
            .unwrap()
            .push(inner_tokens[0].parse::<u64>().unwrap() - 1);

        // Only positions are specified.
        if inner_tokens.len() == 1 {
            continue;
        }

        // Only positions and texture coordinates are specified.
        if inner_tokens.len() == 2 {
            uv_topology
                .last_mut()
                .unwrap()
                .push(inner_tokens[1].parse::<u64>().unwrap() - 1);
            continue;
        }

        if !inner_tokens[1].is_empty() {
            uv_topology
                .last_mut()
                .unwrap()
                .push(inner_tokens[1].parse::<u64>().unwrap() - 1);
        }

        if !inner_tokens[2].is_empty() {
            normal_topology
                .last_mut()
                .unwrap()
                .push(inner_tokens[2].parse::<u64>().unwrap() - 1);
        }
    }

    if uv_topology.last().unwrap().is_empty() {
        uv_topology.pop();
    }
    if normal_topology.last().unwrap().is_empty() {
        normal_topology.pop();
    }
}