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
use std::marker::PhantomData;
use std::ops::Range;

/// The `SharedVertex` trait is meant to be used with the `IndexedPolygon` trait.
/// This trait is meant as a way to calculate the shared vertices that are
/// required to build the implementors mesh.
pub trait SharedVertex<V>: Sized {
    /// return the shared vertex at offset `i`
    fn shared_vertex(&self, i: usize) -> V;

    /// return the number of shared vertices required to represent the mesh
    fn shared_vertex_count(&self) -> usize;

    /// create an iterator that returns each shared vertex that is required to
    /// build the mesh.
    fn shared_vertex_iter<'a>(&'a self) -> SharedVertexIterator<'a, Self, V> {
        SharedVertexIterator {
            base: self,
            idx: 0..self.shared_vertex_count(),
            phantom_v: PhantomData,
        }
    }
}

/// An iterator that yields the shared vertices of the mesh
pub struct SharedVertexIterator<'a, T: 'a, V> {
    base: &'a T,
    idx: Range<usize>,
    phantom_v: PhantomData<V>,
}

impl<'a, T: SharedVertex<V>, V> Iterator for SharedVertexIterator<'a, T, V> {
    type Item = V;

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.idx.size_hint()
    }

    fn next(&mut self) -> Option<V> {
        self.idx.next().map(|idx| self.base.shared_vertex(idx))
    }
}

/// The `IndexedPolygon` trait is used with the `SharedVertex` trait in order to build
/// a mesh. `IndexedPolygon` calculates each polygon face required to build an implementors mesh.
/// each face is always returned in indexed form that points to the correct vertice supplied
/// by the `SharedVertex` trait.
pub trait IndexedPolygon<V>: Sized {
    /// return a polygon with indices to the shared vertex
    fn indexed_polygon(&self, i: usize) -> V;

    /// return the number of polygons that are needed to represent this mesh
    fn indexed_polygon_count(&self) -> usize;

    /// create a iterator that will return a polygon for each face in the source mesh
    fn indexed_polygon_iter<'a>(&'a self) -> IndexedPolygonIterator<'a, Self, V> {
        IndexedPolygonIterator {
            base: self,
            idx: 0..self.indexed_polygon_count(),
            phantom_v: PhantomData,
        }
    }
}

/// An iterator that yields the indices of the mesh
pub struct IndexedPolygonIterator<'a, T: 'a, V> {
    base: &'a T,
    idx: Range<usize>,
    phantom_v: PhantomData<V>,
}

impl<'a, T: IndexedPolygon<V>, V> Iterator for IndexedPolygonIterator<'a, T, V> {
    type Item = V;

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.idx.size_hint()
    }

    fn next(&mut self) -> Option<V> {
        self.idx.next().map(|idx| self.base.indexed_polygon(idx))
    }
}