building_blocks_core/
axis.rs

1use crate::{Point2i, Point3i, PointN};
2
3/// Either the X or Y axis.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub enum Axis2 {
6    X = 0,
7    Y = 1,
8}
9
10impl Axis2 {
11    /// The index for a point's component on this axis.
12    #[inline]
13    pub fn index(&self) -> usize {
14        *self as usize
15    }
16
17    #[inline]
18    pub fn get_unit_vector(&self) -> Point2i {
19        match self {
20            Axis2::X => PointN([1, 0]),
21            Axis2::Y => PointN([0, 1]),
22        }
23    }
24}
25
26#[derive(Clone, Copy, Debug, Eq, PartialEq)]
27pub struct SignedAxis2 {
28    pub sign: i32,
29    pub axis: Axis2,
30}
31
32impl SignedAxis2 {
33    #[inline]
34    pub fn new(sign: i32, axis: Axis2) -> Self {
35        Self { sign, axis }
36    }
37
38    #[inline]
39    pub fn get_vector(&self) -> Point2i {
40        self.axis.get_unit_vector() * self.sign
41    }
42
43    #[inline]
44    pub fn from_vector(v: Point2i) -> Option<Self> {
45        match v {
46            PointN([x, 0]) => Some(SignedAxis2::new(x, Axis2::X)),
47            PointN([0, y]) => Some(SignedAxis2::new(y, Axis2::Y)),
48            _ => None,
49        }
50    }
51}
52
53/// Either the X, Y, or Z axis.
54#[derive(Clone, Copy, Debug, Eq, PartialEq)]
55pub enum Axis3 {
56    X = 0,
57    Y = 1,
58    Z = 2,
59}
60
61impl Axis3 {
62    /// The index for a point's component on this axis.
63    #[inline]
64    pub fn index(&self) -> usize {
65        *self as usize
66    }
67
68    #[inline]
69    pub const fn get_unit_vector(&self) -> Point3i {
70        match self {
71            Axis3::X => PointN([1, 0, 0]),
72            Axis3::Y => PointN([0, 1, 0]),
73            Axis3::Z => PointN([0, 0, 1]),
74        }
75    }
76}
77
78#[derive(Clone, Copy, Debug, Eq, PartialEq)]
79pub enum Axis3Permutation {
80    // Even permutations
81    Xyz,
82    Zxy,
83    Yzx,
84    // Odd permutations
85    Zyx,
86    Xzy,
87    Yxz,
88}
89
90impl Axis3Permutation {
91    #[inline]
92    pub const fn even_with_normal_axis(axis: Axis3) -> Self {
93        match axis {
94            Axis3::X => Axis3Permutation::Xyz,
95            Axis3::Y => Axis3Permutation::Yzx,
96            Axis3::Z => Axis3Permutation::Zxy,
97        }
98    }
99
100    #[inline]
101    pub const fn odd_with_normal_axis(axis: Axis3) -> Self {
102        match axis {
103            Axis3::X => Axis3Permutation::Xzy,
104            Axis3::Y => Axis3Permutation::Yxz,
105            Axis3::Z => Axis3Permutation::Zyx,
106        }
107    }
108
109    #[inline]
110    pub const fn sign(&self) -> i32 {
111        match self {
112            Axis3Permutation::Xyz => 1,
113            Axis3Permutation::Zxy => 1,
114            Axis3Permutation::Yzx => 1,
115            Axis3Permutation::Zyx => -1,
116            Axis3Permutation::Xzy => -1,
117            Axis3Permutation::Yxz => -1,
118        }
119    }
120
121    #[inline]
122    pub const fn axes(&self) -> [Axis3; 3] {
123        match self {
124            Axis3Permutation::Xyz => [Axis3::X, Axis3::Y, Axis3::Z],
125            Axis3Permutation::Zxy => [Axis3::Z, Axis3::X, Axis3::Y],
126            Axis3Permutation::Yzx => [Axis3::Y, Axis3::Z, Axis3::X],
127            Axis3Permutation::Zyx => [Axis3::Z, Axis3::Y, Axis3::X],
128            Axis3Permutation::Xzy => [Axis3::X, Axis3::Z, Axis3::Y],
129            Axis3Permutation::Yxz => [Axis3::Y, Axis3::X, Axis3::Z],
130        }
131    }
132}
133
134#[derive(Clone, Copy, Debug, Eq, PartialEq)]
135pub struct SignedAxis3 {
136    pub sign: i32,
137    pub axis: Axis3,
138}
139
140impl SignedAxis3 {
141    #[inline]
142    pub const fn new(sign: i32, axis: Axis3) -> Self {
143        Self { sign, axis }
144    }
145
146    #[inline]
147    pub fn get_vector(&self) -> Point3i {
148        self.axis.get_unit_vector() * self.sign
149    }
150
151    #[inline]
152    pub const fn from_vector(v: Point3i) -> Option<Self> {
153        match v {
154            PointN([x, 0, 0]) => Some(SignedAxis3::new(x, Axis3::X)),
155            PointN([0, y, 0]) => Some(SignedAxis3::new(y, Axis3::Y)),
156            PointN([0, 0, z]) => Some(SignedAxis3::new(z, Axis3::Z)),
157            _ => None,
158        }
159    }
160}