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
//! Helper methods and structures for working with cubes.
//!
//! ```ignore
//!         3  ---------  2
//!           /       / |
//!          /  up   /  |
//!      6  -------- 7  | 1
//!        |        |  /
//! west   |  south | /  east
//!        |        |/
//!      5  -------- 4
//! ```
//!
//!
//! ```ignore
//!         7  ---------  6
//!           /       / |
//!          /  up   /  |
//!      2  -------- 3  | 5
//!        |        |  /
//! east   |  north | /  west
//!        |        |/
//!      1  -------- 0
//! ```

use std::str::FromStr;

/// A 3D vector.
pub type Vector3<T> = [T; 3];

pub use self::Face::{
    Down,
    Up,
    North,
    South,
    West,
    East,
};

/// Cube faces (clockwise).
pub const QUADS: &'static [[usize; 4]; 6] = &[
    [1, 0, 5, 4], // down
    [7, 6, 3, 2], // up
    [0, 1, 2, 3], // north
    [4, 5, 6, 7], // south
    [5, 0, 3, 6], // west
    [1, 4, 7, 2]  // east
];

/// Cube vertices.
pub const VERTICES: &'static [Vector3<f32>; 8] = &[
    // This is the north surface
    [0.0, 0.0, 0.0], // 0
    [1.0, 0.0, 0.0], // 1
    [1.0, 1.0, 0.0], // 2
    [0.0, 1.0, 0.0], // 3

    // This is the south surface
    [1.0, 0.0, 1.0], // 4
    [0.0, 0.0, 1.0], // 5
    [0.0, 1.0, 1.0], // 6
    [1.0, 1.0, 1.0]  // 7
];

/// A value representing face direction.
#[repr(usize)]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
pub enum Face {
    /// Facing down.
    Down,
    /// Facing up.
    Up,
    /// Facing north.
    North,
    /// Facing south.
    South,
    /// Facing west.
    West,
    /// Facing east.
    East
}

impl Face {
    /// Computes vertices of the face.
    pub fn vertices(self, base: Vector3<f32>, scale: Vector3<f32>) -> [Vector3<f32>; 4] {
        use array::*;

        QUADS[self as usize].map(|i| VERTICES[i]).map(|v| {
            [
                base[0] + scale[0] * v[0],
                base[1] + scale[1] * v[1],
                base[2] + scale[2] * v[2]
            ]
        })
    }

    /// Gets the direction of face.
    pub fn direction(self) -> [i32; 3] {
        match self {
            Down  => [ 0, -1,  0],
            Up    => [ 0,  1,  0],
            North => [ 0,  0, -1],
            South => [ 0,  0,  1],
            West  => [-1,  0,  0],
            East  => [ 1,  0,  0]
        }
    }

    /// Gets the face in a specific direction.
    pub fn from_direction(d: [i32; 3]) -> Option<Self> {
        Some(match (d[0], d[1], d[2]) {
            ( 0, -1,  0) => Down,
            ( 0,  1,  0) => Up,
            ( 0,  0, -1) => North,
            ( 0,  0,  1) => South,
            (-1,  0,  0) => West,
            ( 1,  0,  0) => East,
            _ => return None
        })
    }

    /// Convert number to face.
    pub fn from_usize(number: usize) -> Option<Self> {
        Some(match number {
            0 => Down,
            1 => Up,
            2 => North,
            3 => South,
            4 => West,
            5 => East,
            _ => return None
        })
    }
}

/// The error parsing face from string.
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct ParseError;

impl FromStr for Face {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
        Ok(match s {
            "down"  => Down,
            "up"    => Up,
            "north" => North,
            "south" => South,
            "west"  => West,
            "east"  => East,
            _ => return Err(ParseError)
        })
    }
}

/// Iterates through each face on a cube.
#[derive(Copy, Clone)]
pub struct FaceIterator(usize);

impl FaceIterator {
    /// Creates a new face iterator.
    pub fn new() -> Self {
        FaceIterator(0)
    }
}

impl Iterator for FaceIterator {
    type Item = Face;

    fn next(&mut self) -> Option<<Self as Iterator>::Item> {
        let face = self.0;
        if face < 6 {
            self.0 += 1;
            Face::from_usize(face)
        } else {
            None
        }
    }
}