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
use crate::{Point2i, Point3i, PointN};

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

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

    #[inline]
    pub fn get_unit_vector(&self) -> Point2i {
        match self {
            Axis2::X => PointN([1, 0]),
            Axis2::Y => PointN([0, 1]),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SignedAxis2 {
    pub sign: i32,
    pub axis: Axis2,
}

impl SignedAxis2 {
    #[inline]
    pub fn new(sign: i32, axis: Axis2) -> Self {
        Self { sign, axis }
    }

    #[inline]
    pub fn get_vector(&self) -> Point2i {
        self.axis.get_unit_vector() * self.sign
    }

    #[inline]
    pub fn from_vector(v: Point2i) -> Option<Self> {
        match v {
            PointN([x, 0]) => Some(SignedAxis2::new(x, Axis2::X)),
            PointN([0, y]) => Some(SignedAxis2::new(y, Axis2::Y)),
            _ => None,
        }
    }
}

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

impl Axis3 {
    /// 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) -> Point3i {
        match self {
            Axis3::X => PointN([1, 0, 0]),
            Axis3::Y => PointN([0, 1, 0]),
            Axis3::Z => PointN([0, 0, 1]),
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Axis3Permutation {
    // Even permutations
    Xyz,
    Zxy,
    Yzx,
    // Odd permutations
    Zyx,
    Xzy,
    Yxz,
}

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

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

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

    #[inline]
    pub const fn axes(&self) -> [Axis3; 3] {
        match self {
            Axis3Permutation::Xyz => [Axis3::X, Axis3::Y, Axis3::Z],
            Axis3Permutation::Zxy => [Axis3::Z, Axis3::X, Axis3::Y],
            Axis3Permutation::Yzx => [Axis3::Y, Axis3::Z, Axis3::X],
            Axis3Permutation::Zyx => [Axis3::Z, Axis3::Y, Axis3::X],
            Axis3Permutation::Xzy => [Axis3::X, Axis3::Z, Axis3::Y],
            Axis3Permutation::Yxz => [Axis3::Y, Axis3::X, Axis3::Z],
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SignedAxis3 {
    pub sign: i32,
    pub axis: Axis3,
}

impl SignedAxis3 {
    #[inline]
    pub const fn new(sign: i32, axis: Axis3) -> Self {
        Self { sign, axis }
    }

    #[inline]
    pub fn get_vector(&self) -> Point3i {
        self.axis.get_unit_vector() * self.sign
    }

    #[inline]
    pub const fn from_vector(v: Point3i) -> Option<Self> {
        match v {
            PointN([x, 0, 0]) => Some(SignedAxis3::new(x, Axis3::X)),
            PointN([0, y, 0]) => Some(SignedAxis3::new(y, Axis3::Y)),
            PointN([0, 0, z]) => Some(SignedAxis3::new(z, Axis3::Z)),
            _ => None,
        }
    }
}