Skip to content

Commit

Permalink
Implement GridRotation::inverse() directly.
Browse files Browse the repository at this point in the history
This compiles to a single lookup table instead of a loop.
  • Loading branch information
kpreid committed Oct 3, 2023
1 parent 3d4b154 commit 6ea391a
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions all-is-cubes/src/math/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,23 @@ impl GridRotation {
/// assert_eq!(rotation * rotation.inverse(), GridRotation::IDENTITY);
/// }
/// ```
#[rustfmt::skip]
#[must_use]
pub fn inverse(self) -> Self {
// TODO: Make this more efficient. Can we do it without writing out another 48-element match?
self.iterate().last().unwrap()
#[inline]
pub const fn inverse(self) -> Self {
use GridRotation::*;
match self {
RXYZ => RXYZ, RXYz => RXYz, RXyZ => RXyZ, RXyz => RXyz, RxYZ => RxYZ,
RxYz => RxYz, RxyZ => RxyZ, Rxyz => Rxyz, RXZY => RXZY, RXZy => RXzY,
RXzY => RXZy, RXzy => RXzy, RxZY => RxZY, RxZy => RxzY, RxzY => RxZy,
Rxzy => Rxzy, RYXZ => RYXZ, RYXz => RYXz, RYxZ => RyXZ, RYxz => RyXz,
RyXZ => RYxZ, RyXz => RYxz, RyxZ => RyxZ, Ryxz => Ryxz, RYZX => RZXY,
RYZx => RzXY, RYzX => RZXy, RYzx => RzXy, RyZX => RZxY, RyZx => RzxY,
RyzX => RZxy, Ryzx => Rzxy, RZXY => RYZX, RZXy => RYzX, RZxY => RyZX,
RZxy => RyzX, RzXY => RYZx, RzXy => RYzx, RzxY => RyZx, Rzxy => Ryzx,
RZYX => RZYX, RZYx => RzYX, RZyX => RZyX, RZyx => RzyX, RzYX => RZYx,
RzYx => RzYx, RzyX => RZyx, Rzyx => Rzyx,
}
}

/// Generates the sequence of rotations that may be obtained by concatenating/multiplying
Expand Down Expand Up @@ -541,6 +554,20 @@ mod tests {
}
}

/// We can compute the inverse via iterate().
/// This test also serves to regenerate the inverse table.
#[test]
fn inverse_from_iterate() {
let mut ok = true;
for rot in GridRotation::ALL {
let iter_inv = rot.iterate().last().unwrap();
let inv = rot.inverse();
println!("{rot:?} => {iter_inv:?}, // {inv:?}");
ok = ok && iter_inv == inv;
}
assert!(ok);
}

#[test]
fn is_reflection_consistency() {
for a in GridRotation::ALL {
Expand Down

0 comments on commit 6ea391a

Please sign in to comment.