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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
//! Rotations which exchange axes (thus not leaving the integer grid).
//! This module is private but reexported by its parent.
#![allow(clippy::large_stack_arrays)] // effectively-false positive on Arbitrary derive
use core::marker::PhantomData;
use core::ops::Mul;
use crate::math::{Face6, GridCoordinate, GridMatrix, GridSize, GridVector, Gridgid, Vector3D};
#[cfg(doc)]
use crate::math::GridAab;
/// Represents a discrete (grid-aligned) rotation, or exchange of axes.
///
/// Compared to a [`GridMatrix`], this cannot specify scale, translation, or skew;
/// it is used for identifying the rotations of blocks.
///
/// Each of the variant names specifies the three unit vectors which (*x*, *y*, *z*),
/// respectively, should be multiplied by to perform the rotation.
/// Lowercase refers to a negated unit vector.
///
/// See also:
///
/// * [`Face6`] is less general, in that it specifies a single axis but not
/// rotation about that axis.
/// * [`GridMatrix`] is more general, specifying an affine transformation.
///
#[doc = include_str!("../serde-warning.md")]
#[rustfmt::skip]
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::exhaustive_enums)]
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum GridRotation {
// TODO: shuffle or explicitly number these to choose a meaningful numbering
RXYZ, RXYz, RXyZ, RXyz, RxYZ, RxYz, RxyZ, Rxyz,
RXZY, RXZy, RXzY, RXzy, RxZY, RxZy, RxzY, Rxzy,
RYXZ, RYXz, RYxZ, RYxz, RyXZ, RyXz, RyxZ, Ryxz,
RYZX, RYZx, RYzX, RYzx, RyZX, RyZx, RyzX, Ryzx,
RZXY, RZXy, RZxY, RZxy, RzXY, RzXy, RzxY, Rzxy,
RZYX, RZYx, RZyX, RZyx, RzYX, RzYx, RzyX, Rzyx,
}
impl GridRotation {
/// All 48 possible rotations.
///
/// Warning: TODO: The ordering of these rotations is not yet stable.
/// The current ordering is based on the six axis permutations followed by rotations.
#[rustfmt::skip]
pub const ALL: [Self; 48] = {
use GridRotation::*;
[
RXYZ, RXYz, RXyZ, RXyz, RxYZ, RxYz, RxyZ, Rxyz,
RXZY, RXZy, RXzY, RXzy, RxZY, RxZy, RxzY, Rxzy,
RYXZ, RYXz, RYxZ, RYxz, RyXZ, RyXz, RyxZ, Ryxz,
RYZX, RYZx, RYzX, RYzx, RyZX, RyZx, RyzX, Ryzx,
RZXY, RZXy, RZxY, RZxy, RzXY, RzXy, RzxY, Rzxy,
RZYX, RZYx, RZyX, RZyx, RzYX, RzYx, RzyX, Rzyx,
]
};
/// All possible rotations that are not reflections.
///
/// Warning: TODO: The ordering of these rotations is not yet stable.
#[rustfmt::skip]
pub const ALL_BUT_REFLECTIONS: [Self; 24] = {
use GridRotation::*;
[
RXYZ, RXyz, RxYz, RxyZ,
RXZy, RXzY, RxZY, Rxzy,
RYXz, RYxZ, RyXZ, Ryxz,
RYZX, RYzx, RyZx, RyzX,
RZXY, RZxy, RzXy, RzxY,
RZYx, RZyX, RzYX, Rzyx,
]
};
/// The identity rotation, also known as [`RXYZ`](Self::RXYZ).
pub const IDENTITY: Self = Self::RXYZ;
/// The rotation that is clockwise in our Y-up right-handed coordinate system.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::{Face6::*, GridRotation};
///
/// assert_eq!(GridRotation::CLOCKWISE.transform(PX), PZ);
/// assert_eq!(GridRotation::CLOCKWISE.transform(PZ), NX);
/// assert_eq!(GridRotation::CLOCKWISE.transform(NX), NZ);
/// assert_eq!(GridRotation::CLOCKWISE.transform(NZ), PX);
///
/// assert_eq!(GridRotation::CLOCKWISE.transform(PY), PY);
/// ```
pub const CLOCKWISE: Self = Self::RZYx;
/// The rotation that is counterclockwise in our Y-up right-handed coordinate system.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::{Face6::*, GridRotation};
///
/// assert_eq!(GridRotation::COUNTERCLOCKWISE.transform(PX), NZ);
/// assert_eq!(GridRotation::COUNTERCLOCKWISE.transform(NZ), NX);
/// assert_eq!(GridRotation::COUNTERCLOCKWISE.transform(NX), PZ);
/// assert_eq!(GridRotation::COUNTERCLOCKWISE.transform(PZ), PX);
///
/// assert_eq!(GridRotation::COUNTERCLOCKWISE.transform(PY), PY);
/// ```
pub const COUNTERCLOCKWISE: Self = Self::RzYX;
/// Constructs a rotation from a basis: that is, the returned rotation will
/// rotate `PX` into `basis[0]`, `PY` into `basis[1]`, and `PZ` into `basis[2]`.
///
/// Panics if the three provided axes are not mutually perpendicular.
#[inline]
pub fn from_basis(basis: impl Into<[Face6; 3]>) -> Self {
Self::from_basis_impl(basis.into())
}
fn from_basis_impl(basis: [Face6; 3]) -> Self {
use {Face6::*, GridRotation::*};
match basis {
[PX, PY, PZ] => RXYZ,
[PX, PZ, PY] => RXZY,
[PY, PX, PZ] => RYXZ,
[PY, PZ, PX] => RYZX,
[PZ, PX, PY] => RZXY,
[PZ, PY, PX] => RZYX,
[PX, PY, NZ] => RXYz,
[PX, PZ, NY] => RXZy,
[PY, PX, NZ] => RYXz,
[PY, PZ, NX] => RYZx,
[PZ, PX, NY] => RZXy,
[PZ, PY, NX] => RZYx,
[PX, NY, PZ] => RXyZ,
[PX, NZ, PY] => RXzY,
[PY, NX, PZ] => RYxZ,
[PY, NZ, PX] => RYzX,
[PZ, NX, PY] => RZxY,
[PZ, NY, PX] => RZyX,
[PX, NY, NZ] => RXyz,
[PX, NZ, NY] => RXzy,
[PY, NX, NZ] => RYxz,
[PY, NZ, NX] => RYzx,
[PZ, NX, NY] => RZxy,
[PZ, NY, NX] => RZyx,
[NX, PY, PZ] => RxYZ,
[NX, PZ, PY] => RxZY,
[NY, PX, PZ] => RyXZ,
[NY, PZ, PX] => RyZX,
[NZ, PX, PY] => RzXY,
[NZ, PY, PX] => RzYX,
[NX, PY, NZ] => RxYz,
[NX, PZ, NY] => RxZy,
[NY, PX, NZ] => RyXz,
[NY, PZ, NX] => RyZx,
[NZ, PX, NY] => RzXy,
[NZ, PY, NX] => RzYx,
[NX, NY, PZ] => RxyZ,
[NX, NZ, PY] => RxzY,
[NY, NX, PZ] => RyxZ,
[NY, NZ, PX] => RyzX,
[NZ, NX, PY] => RzxY,
[NZ, NY, PX] => RzyX,
[NX, NY, NZ] => Rxyz,
[NX, NZ, NY] => Rxzy,
[NY, NX, NZ] => Ryxz,
[NY, NZ, NX] => Ryzx,
[NZ, NX, NY] => Rzxy,
[NZ, NY, NX] => Rzyx,
_ => panic!("Invalid basis given to GridRotation::from_basis: {basis:?}"),
}
}
/// Find the rotation (without reflection) which rotates `source` to `destination`.
/// and leaves `up` unaffected. (This might also be considered a “look at” operation).
///
/// If it is not possible to leave `up` unaffected, returns [`None`]. (Trying two
/// perpendicular `up` directions will always succeed.)
#[allow(clippy::missing_inline_in_public_items)]
pub fn from_to(source: Face6, destination: Face6, up: Face6) -> Option<Self> {
let perpendicular = source.cross(up);
if source == destination {
Some(Self::IDENTITY)
} else if let Ok(perpendicular) = Face6::try_from(perpendicular) {
// Find rotation from the frame source=NZ up=PY to the actual given one.
let canonical_to_given = Self::from_basis([perpendicular, up, source.opposite()]);
let given_to_canonical = canonical_to_given.inverse();
debug_assert!(!canonical_to_given.is_reflection());
// The destination expressed in that frame.
let canonical_destination = given_to_canonical.transform(destination);
// Find which of the four rotations in a plane matches.
use Face6::*;
let canonical_rotation = match canonical_destination {
NY | PY => {
// Tried to rotate into the up vector.
return None;
}
NZ => Self::IDENTITY,
PX => Self::CLOCKWISE,
PZ => Self::RxYz,
NX => Self::COUNTERCLOCKWISE,
};
// Transform that rotation into the given frame.
Some(canonical_to_given * canonical_rotation * given_to_canonical)
} else {
// perpendicular == Face7::Within, therefore
// up was parallel to source, or one of them was Within.
None
}
}
/// Returns the basis vectors for the unrotated coordinate system in
/// the rotated coordinate system.
// TODO: public? do we want this to be our API?
#[doc(hidden)]
#[inline]
#[rustfmt::skip] // dense data layout
pub const fn to_basis(self) -> Vector3D<Face6, ()> {
use {Face6::*, GridRotation::*};
match self {
RXYZ => Vector3D { x: PX, y: PY, z: PZ, _unit: PhantomData },
RXZY => Vector3D { x: PX, y: PZ, z: PY, _unit: PhantomData },
RYXZ => Vector3D { x: PY, y: PX, z: PZ, _unit: PhantomData },
RYZX => Vector3D { x: PY, y: PZ, z: PX, _unit: PhantomData },
RZXY => Vector3D { x: PZ, y: PX, z: PY, _unit: PhantomData },
RZYX => Vector3D { x: PZ, y: PY, z: PX, _unit: PhantomData },
RXYz => Vector3D { x: PX, y: PY, z: NZ, _unit: PhantomData },
RXZy => Vector3D { x: PX, y: PZ, z: NY, _unit: PhantomData },
RYXz => Vector3D { x: PY, y: PX, z: NZ, _unit: PhantomData },
RYZx => Vector3D { x: PY, y: PZ, z: NX, _unit: PhantomData },
RZXy => Vector3D { x: PZ, y: PX, z: NY, _unit: PhantomData },
RZYx => Vector3D { x: PZ, y: PY, z: NX, _unit: PhantomData },
RXyZ => Vector3D { x: PX, y: NY, z: PZ, _unit: PhantomData },
RXzY => Vector3D { x: PX, y: NZ, z: PY, _unit: PhantomData },
RYxZ => Vector3D { x: PY, y: NX, z: PZ, _unit: PhantomData },
RYzX => Vector3D { x: PY, y: NZ, z: PX, _unit: PhantomData },
RZxY => Vector3D { x: PZ, y: NX, z: PY, _unit: PhantomData },
RZyX => Vector3D { x: PZ, y: NY, z: PX, _unit: PhantomData },
RXyz => Vector3D { x: PX, y: NY, z: NZ, _unit: PhantomData },
RXzy => Vector3D { x: PX, y: NZ, z: NY, _unit: PhantomData },
RYxz => Vector3D { x: PY, y: NX, z: NZ, _unit: PhantomData },
RYzx => Vector3D { x: PY, y: NZ, z: NX, _unit: PhantomData },
RZxy => Vector3D { x: PZ, y: NX, z: NY, _unit: PhantomData },
RZyx => Vector3D { x: PZ, y: NY, z: NX, _unit: PhantomData },
RxYZ => Vector3D { x: NX, y: PY, z: PZ, _unit: PhantomData },
RxZY => Vector3D { x: NX, y: PZ, z: PY, _unit: PhantomData },
RyXZ => Vector3D { x: NY, y: PX, z: PZ, _unit: PhantomData },
RyZX => Vector3D { x: NY, y: PZ, z: PX, _unit: PhantomData },
RzXY => Vector3D { x: NZ, y: PX, z: PY, _unit: PhantomData },
RzYX => Vector3D { x: NZ, y: PY, z: PX, _unit: PhantomData },
RxYz => Vector3D { x: NX, y: PY, z: NZ, _unit: PhantomData },
RxZy => Vector3D { x: NX, y: PZ, z: NY, _unit: PhantomData },
RyXz => Vector3D { x: NY, y: PX, z: NZ, _unit: PhantomData },
RyZx => Vector3D { x: NY, y: PZ, z: NX, _unit: PhantomData },
RzXy => Vector3D { x: NZ, y: PX, z: NY, _unit: PhantomData },
RzYx => Vector3D { x: NZ, y: PY, z: NX, _unit: PhantomData },
RxyZ => Vector3D { x: NX, y: NY, z: PZ, _unit: PhantomData },
RxzY => Vector3D { x: NX, y: NZ, z: PY, _unit: PhantomData },
RyxZ => Vector3D { x: NY, y: NX, z: PZ, _unit: PhantomData },
RyzX => Vector3D { x: NY, y: NZ, z: PX, _unit: PhantomData },
RzxY => Vector3D { x: NZ, y: NX, z: PY, _unit: PhantomData },
RzyX => Vector3D { x: NZ, y: NY, z: PX, _unit: PhantomData },
Rxyz => Vector3D { x: NX, y: NY, z: NZ, _unit: PhantomData },
Rxzy => Vector3D { x: NX, y: NZ, z: NY, _unit: PhantomData },
Ryxz => Vector3D { x: NY, y: NX, z: NZ, _unit: PhantomData },
Ryzx => Vector3D { x: NY, y: NZ, z: NX, _unit: PhantomData },
Rzxy => Vector3D { x: NZ, y: NX, z: NY, _unit: PhantomData },
Rzyx => Vector3D { x: NZ, y: NY, z: NX, _unit: PhantomData },
}
}
/// Expresses this rotation as a [`Gridgid`] transform which rotates “in place” the
/// points within the volume defined by coordinates in the range [0, size].
///
/// That is, a [`GridAab`] of that volume will be unchanged by rotation:
///
/// ```
/// # mod all_is_cubes {
/// # pub mod block { pub use all_is_cubes_base::resolution::Resolution; }
/// # pub use all_is_cubes_base::math;
/// # }
/// use all_is_cubes::block::Resolution;
/// use all_is_cubes::math::{GridAab, GridRotation};
///
/// let b = GridAab::for_block(Resolution::R8);
/// let rotation = GridRotation::CLOCKWISE.to_positive_octant_transform(8);
/// assert_eq!(b.transform(rotation), Some(b));
/// ```
///
/// Such matrices are suitable for rotating the voxels of a block, provided
/// that the voxel coordinates are then transformed with [`GridMatrix::transform_cube`],
/// *not* [`GridMatrix::transform_point`]
/// (due to the lower-corner format of cube coordinates).
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::{GridAab, Cube, GridRotation};
///
/// let rotation = GridRotation::CLOCKWISE.to_positive_octant_transform(4);
/// assert_eq!(rotation.transform_cube(Cube::new(0, 0, 0)), Cube::new(3, 0, 0));
/// assert_eq!(rotation.transform_cube(Cube::new(3, 0, 0)), Cube::new(3, 0, 3));
/// assert_eq!(rotation.transform_cube(Cube::new(3, 0, 3)), Cube::new(0, 0, 3));
/// assert_eq!(rotation.transform_cube(Cube::new(0, 0, 3)), Cube::new(0, 0, 0));
/// ```
//
// TODO: add tests
#[inline]
pub fn to_positive_octant_transform(self, size: GridCoordinate) -> Gridgid {
fn offset(face: Face6, size: GridCoordinate) -> GridVector {
if face.is_positive() {
GridVector::zero()
} else {
face.normal_vector() * -size
}
}
let basis = self.to_basis();
Gridgid {
rotation: self,
translation: offset(basis.x, size) + offset(basis.y, size) + offset(basis.z, size),
}
}
/// Expresses this rotation as a matrix without any translation.
// TODO: add tests
#[inline]
pub fn to_rotation_matrix(self) -> GridMatrix {
let basis = self.to_basis();
GridMatrix {
x: basis.x.normal_vector(),
y: basis.y.normal_vector(),
z: basis.z.normal_vector(),
w: Vector3D::zero(),
}
}
/// Rotate the face by this rotation.
// TODO: test equivalence with matrix
#[inline]
pub fn transform(self, face: Face6) -> Face6 {
// TODO: there ought to be a much cleaner way to express this
// ... and it should be a const fn, too
let p = self.to_basis()[face.axis()];
if face.is_negative() {
p.opposite()
} else {
p
}
}
/// Rotate the vector by this rotation.
///
/// May panic or wrap if `vector` has any components equal to [`GridCoordinate::MIN`].
#[inline]
pub fn transform_vector(self, vector: GridVector) -> GridVector {
#[inline(always)]
fn inner(rotation: GridRotation, vector: GridVector) -> Option<GridVector> {
let basis = rotation.to_basis();
let mut result = GridVector::zero();
result[basis.x.axis()] = vector.x.checked_mul(basis.x.signum())?;
result[basis.y.axis()] = vector.y.checked_mul(basis.y.signum())?;
result[basis.z.axis()] = vector.z.checked_mul(basis.z.signum())?;
Some(result)
}
// Shared handling to print the vector, and also to generate only one set of panic code
// rather than three.
// TODO: when overflow_checks disabled, don't panic
match inner(self, vector) {
Some(v) => v,
None => panic!(
"overflow due to sign change in GridVector::transform_vector({self:?}, {vector:?})"
),
}
// Implementation note: The following code would seem to be simpler and avoid
// a zero initialization,
//
// let inverse_basis = self.inverse().to_basis();
// GridVector {
// x: inverse_basis.x.dot(vector),
// y: inverse_basis.y.dot(vector),
// z: inverse_basis.z.dot(vector),
// _unit: PhantomData,
// }
//
// but the actual generated machine code is larger and involves computed jumps.
}
/// Rotate the size value by this rotation.
///
/// This is similar to [`GridRotation::transform_vector()`] except that the components
/// are only swapped, not negated, and there is no possibility of numeric overflow.
#[inline]
pub fn transform_size(self, size: GridSize) -> GridSize {
let basis = self.to_basis();
let mut result = GridSize::zero();
result[basis.x.axis()] = size.width;
result[basis.y.axis()] = size.height;
result[basis.z.axis()] = size.depth;
result
}
/// Returns whether this is a reflection.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::{GridRotation, Face6::*};
///
/// assert!(!GridRotation::IDENTITY.is_reflection());
/// assert!(!GridRotation::from_basis([PX, PZ, NY]).is_reflection());
/// assert!(GridRotation::from_basis([PX, PZ, PY]).is_reflection());
/// ```
#[inline]
pub const fn is_reflection(self) -> bool {
// In a coordinate system of the *same handedness*, the cross product computes
// the same
let Vector3D { x, y, z, _unit } = self.to_basis();
// u8 casts are a kludge to make == work as a const fn.
x.cross(y) as u8 != z as u8
}
/// Returns the inverse of this rotation; the one which undoes this.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::GridRotation;
///
/// for &rotation in &GridRotation::ALL {
/// assert_eq!(rotation * rotation.inverse(), GridRotation::IDENTITY);
/// }
/// ```
#[rustfmt::skip]
#[must_use]
#[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
/// this rotation with itself repeatedly.
///
/// The first element of the iterator will always be the identity, i.e. this rotation
/// applied zero times. The iterator ends when the sequence would repeat itself, i.e.
/// just before it would produce the identity again.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::Face6::*;
/// use all_is_cubes::math::GridRotation;
///
/// assert_eq!(
/// GridRotation::IDENTITY.iterate().collect::<Vec<_>>(),
/// vec![GridRotation::IDENTITY],
/// );
///
/// let x_reflection = GridRotation::from_basis([NX, PY, PZ]);
/// assert_eq!(
/// x_reflection.iterate().collect::<Vec<_>>(),
/// vec![GridRotation::IDENTITY, x_reflection],
/// );
///
/// assert_eq!(
/// GridRotation::CLOCKWISE.iterate().collect::<Vec<_>>(),
/// vec![
/// GridRotation::IDENTITY,
/// GridRotation::CLOCKWISE,
/// GridRotation::CLOCKWISE * GridRotation::CLOCKWISE,
/// GridRotation::COUNTERCLOCKWISE,
/// ],
/// );
/// ```
#[allow(clippy::missing_inline_in_public_items)]
pub fn iterate(self) -> impl Iterator<Item = Self> {
let mut item = Self::IDENTITY;
core::iter::once(Self::IDENTITY).chain(core::iter::from_fn(move || {
item = item * self;
if item == Self::IDENTITY {
// Cycled back to start; time to stop
None
} else {
Some(item)
}
}))
}
}
impl Default for GridRotation {
/// Returns the identity (no rotation).
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl num_traits::One for GridRotation {
/// Returns the identity (no rotation).
#[inline]
fn one() -> Self {
Self::IDENTITY
}
}
impl Mul<Self> for GridRotation {
type Output = Self;
/// Multiplication is concatenation: `self * rhs` is equivalent to
/// applying `rhs` and then applying `self`.
///
/// ```
/// # extern crate all_is_cubes_base as all_is_cubes;
/// use all_is_cubes::math::{Face6, Face6::*, GridRotation, GridPoint};
///
/// let transform_1 = GridRotation::from_basis([NY, PX, PZ]);
/// let transform_2 = GridRotation::from_basis([PY, PZ, PX]);
///
/// // Demonstrate the directionality of concatenation.
/// for face in Face6::ALL {
/// assert_eq!(
/// (transform_1 * transform_2).transform(face),
/// transform_1.transform(transform_2.transform(face)),
/// );
/// }
/// ```
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
Self::from_basis(rhs.to_basis().map(|v| self.transform(v)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::math::GridPoint;
use num_traits::One;
use std::collections::HashSet;
use Face6::*;
#[test]
fn identity() {
assert_eq!(GridRotation::IDENTITY, GridRotation::one());
assert_eq!(GridRotation::IDENTITY, GridRotation::default());
assert_eq!(
GridRotation::IDENTITY,
GridRotation::from_basis([PX, PY, PZ])
);
}
#[test]
fn ccw_cw() {
assert_eq!(
GridRotation::IDENTITY,
GridRotation::COUNTERCLOCKWISE * GridRotation::CLOCKWISE
);
}
#[test]
fn inverse_axioms() {
assert_eq!(GridRotation::IDENTITY.inverse(), GridRotation::IDENTITY);
for rot in GridRotation::ALL {
assert_eq!(rot * rot.inverse(), GridRotation::IDENTITY, "{rot:?}");
assert_eq!(rot.inverse().inverse(), rot, "{rot:?}");
}
}
#[test]
fn inverse_effect() {
let v = GridVector::new(1, 5, 100);
for rot in GridRotation::ALL {
assert_eq!(
rot.transform_vector(rot.inverse().transform_vector(v)),
v,
"{rot:?}"
);
assert_eq!(
rot.inverse().transform_vector(rot.transform_vector(v)),
v,
"{rot:?}"
);
}
}
/// 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 {
for b in GridRotation::ALL {
assert_eq!(
a.is_reflection() ^ b.is_reflection(),
(a * b).is_reflection(),
"{a:?}, {b:?}",
);
}
}
}
/// Test that `GridRotation::ALL` is complete.
/// TODO: Also test numbering/ordering properties when that is stable.
#[test]
fn enumeration() {
let mut set = HashSet::new();
for rot in GridRotation::ALL {
set.insert(rot);
}
assert_eq!(set.len(), GridRotation::ALL.len());
assert_eq!(48, GridRotation::ALL.len());
}
/// Test that `GridRotation::ALL_BUT_REFLECTIONS` is complete.
#[test]
fn all_but_reflections() {
let mut set = HashSet::new();
for rot in GridRotation::ALL_BUT_REFLECTIONS {
assert!(!rot.is_reflection(), "{rot:?} is a reflection");
set.insert(rot);
}
assert_eq!(set.len(), GridRotation::ALL_BUT_REFLECTIONS.len());
// Half of all possible axis transformations have no reflection
assert_eq!(
GridRotation::ALL.len(),
GridRotation::ALL_BUT_REFLECTIONS.len() * 2,
);
}
/// Test that `transform_vector()` and `to_rotation_matrix()` do the same thing.
#[test]
fn equivalent_rotation_matrix() {
for rot in GridRotation::ALL {
let point = GridPoint::new(1, 20, 300);
assert_eq!(
rot.transform_vector(point.to_vector()).to_point(),
rot.to_rotation_matrix().transform_point(point),
);
}
}
#[test]
fn equivalent_transform_vector_transform_size() {
for rot in GridRotation::ALL {
let vector = GridVector::new(1, 20, 300);
assert_eq!(
rot.transform_vector(vector).abs().to_u32(),
rot.transform_size(GridSize::from(vector.to_u32()))
.to_vector()
)
}
}
/// The set of possible inputs is small enough to test its properties exhaustively
#[test]
fn from_to_exhaustive() {
for from_face in Face6::ALL {
for to_face in Face6::ALL {
for up_face in Face6::ALL {
let result = GridRotation::from_to(from_face, to_face, up_face);
let info = (from_face, to_face, up_face, result);
match result {
Some(result) => {
assert!(!result.is_reflection());
assert_eq!(
result.transform(from_face),
to_face,
"wrong from-to: {info:?}"
);
assert_eq!(
result.transform(up_face),
up_face,
"did not preserve up vector: {info:?}"
);
}
None => {
assert!(
up_face.axis() == from_face.axis()
|| up_face.axis() == to_face.axis(),
"returned None incorrectly: {info:?}"
);
}
}
}
}
}
}
}