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
use ilattice::glam::{IVec3, UVec3};

/// Either the X, Y, or Z axis.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum Axis {
    X = 0,
    Y = 1,
    Z = 2,
}

impl Axis {
    /// The index for a point's component on this axis.
    #[inline]
    pub fn index(&self) -> usize {
        *self as usize
    }

    #[inline]
    pub const fn get_unit_vector(&self) -> UVec3 {
        match self {
            Axis::X => UVec3::X,
            Axis::Y => UVec3::Y,
            Axis::Z => UVec3::Z,
        }
    }
}

/// One of the six possible `{N, U, V}` --> `{X, Y, Z}` mappings.
///
/// This can be combined with a `-1` or `+1` sign for the **N**ormal axis to
/// make an [`OrientedBlockFace`][crate::OrientedBlockFace].
///
/// See the [`geometry` module documentation][crate::geometry] for more
/// information on `{N, U, V}` space.
///
/// # Even and Odd
///
/// Even permutations:
/// - [AxisPermutation::Xyz]
/// - [AxisPermutation::Zxy]
/// - [AxisPermutation::Yzx]
///
/// Odd permutations:
/// - [AxisPermutation::Zyx]
/// - [AxisPermutation::Xzy]
/// - [AxisPermutation::Yxz]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum AxisPermutation {
    // Even permutations
    Xyz,
    Zxy,
    Yzx,
    // Odd permutations
    Zyx,
    Xzy,
    Yxz,
}

impl AxisPermutation {
    #[inline]
    pub const fn even_with_normal_axis(axis: Axis) -> Self {
        match axis {
            Axis::X => AxisPermutation::Xyz,
            Axis::Y => AxisPermutation::Yzx,
            Axis::Z => AxisPermutation::Zxy,
        }
    }

    #[inline]
    pub const fn odd_with_normal_axis(axis: Axis) -> Self {
        match axis {
            Axis::X => AxisPermutation::Xzy,
            Axis::Y => AxisPermutation::Yxz,
            Axis::Z => AxisPermutation::Zyx,
        }
    }

    #[inline]
    pub const fn sign(&self) -> i32 {
        match self {
            AxisPermutation::Xyz => 1,
            AxisPermutation::Zxy => 1,
            AxisPermutation::Yzx => 1,
            AxisPermutation::Zyx => -1,
            AxisPermutation::Xzy => -1,
            AxisPermutation::Yxz => -1,
        }
    }

    /// Returns the [`Axes`] in the order specified by the permutation.
    ///
    /// # Example
    ///
    /// ```
    /// # use block_mesh::*;
    /// let xyz = AxisPermutation::Xyz;
    /// assert_eq!(xyz.axes(), [Axis::X, Axis::Y, Axis::Z]);
    /// ```
    ///
    /// [`Axes`]: Axis
    #[inline]
    pub const fn axes(&self) -> [Axis; 3] {
        match self {
            AxisPermutation::Xyz => [Axis::X, Axis::Y, Axis::Z],
            AxisPermutation::Zxy => [Axis::Z, Axis::X, Axis::Y],
            AxisPermutation::Yzx => [Axis::Y, Axis::Z, Axis::X],
            AxisPermutation::Zyx => [Axis::Z, Axis::Y, Axis::X],
            AxisPermutation::Xzy => [Axis::X, Axis::Z, Axis::Y],
            AxisPermutation::Yxz => [Axis::Y, Axis::X, Axis::Z],
        }
    }
}

/// Either the -X, +X, -Y, +Y, -Z, or +Z axis.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum SignedAxis {
    NegX = 0,
    PosX = 1,
    NegY = 2,
    PosY = 3,
    NegZ = 4,
    PosZ = 5,
}

impl SignedAxis {
    #[inline]
    pub fn new(sign: i32, axis: Axis) -> Self {
        assert!(sign != 0);

        match (sign > 0, axis) {
            (false, Axis::X) => Self::NegX,
            (false, Axis::Y) => Self::NegY,
            (false, Axis::Z) => Self::NegZ,
            (true, Axis::X) => Self::PosX,
            (true, Axis::Y) => Self::PosY,
            (true, Axis::Z) => Self::PosZ,
        }
    }

    #[inline]
    pub fn unsigned_axis(&self) -> Axis {
        match self {
            Self::NegX => Axis::X,
            Self::NegY => Axis::Y,
            Self::NegZ => Axis::Z,
            Self::PosX => Axis::X,
            Self::PosY => Axis::Y,
            Self::PosZ => Axis::Z,
        }
    }

    #[inline]
    pub fn signum(&self) -> i32 {
        match self {
            Self::NegX => -1,
            Self::NegY => -1,
            Self::NegZ => -1,
            Self::PosX => 1,
            Self::PosY => 1,
            Self::PosZ => 1,
        }
    }

    #[inline]
    pub fn get_unit_vector(&self) -> IVec3 {
        match self {
            Self::NegX => -IVec3::X,
            Self::NegY => -IVec3::Y,
            Self::NegZ => -IVec3::Z,
            Self::PosX => IVec3::X,
            Self::PosY => IVec3::Y,
            Self::PosZ => IVec3::Z,
        }
    }

    #[inline]
    pub fn from_vector(v: IVec3) -> Option<Self> {
        match v.to_array() {
            [x, 0, 0] => Some(SignedAxis::new(x, Axis::X)),
            [0, y, 0] => Some(SignedAxis::new(y, Axis::Y)),
            [0, 0, z] => Some(SignedAxis::new(z, Axis::Z)),
            _ => None,
        }
    }
}