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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
//! Axis-aligned unit vectors: the [`Face6`] and [`Face7`] types.
//! This module is private but reexported by its parent.

use core::fmt;
use core::ops;

use euclid::Vector3D;

use manyfmt::formats::Unquote;
use manyfmt::Refmt as _;
/// Acts as polyfill for float methods
#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
use num_traits::float::FloatCore as _;

use crate::math::{
    Axis, ConciseDebug, Cube, FreeCoordinate, FreeVector, Geometry, GridCoordinate, GridPoint,
    GridRotation, GridVector, Gridgid, LineVertex, Zero,
};

/// Identifies a face of a cube or an orthogonal unit vector.
///
/// See also the similar type [`Face7`], which adds a “zero” or “within the cube”
/// variant. The two enums use the same discriminant numbering.
///
#[doc = include_str!("../serde-warning.md")]
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, exhaust::Exhaust)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum Face6 {
    /// Negative X; the face whose normal vector is `(-1, 0, 0)`.
    NX = 1,
    /// Negative Y; the face whose normal vector is `(0, -1, 0)`; downward.
    NY = 2,
    /// Negative Z; the face whose normal vector is `(0, 0, -1)`.
    NZ = 3,
    /// Positive X; the face whose normal vector is `(1, 0, 0)`.
    PX = 4,
    /// Positive Y; the face whose normal vector is `(0, 1, 0)`; upward.
    PY = 5,
    /// Positive Z; the face whose normal vector is `(0, 0, 1)`.
    PZ = 6,
}

/// Identifies a face of a cube or an orthogonal unit vector, except for
/// [`Within`](Face7::Within) meaning “zero distance and undefined direction”.
///
/// This is essentially `Option<`[`Face6`]`>`, except with `Face`-specific methods
/// provided. The two enums use the same discriminant numbering.
///
#[doc = include_str!("../serde-warning.md")]
#[allow(clippy::upper_case_acronyms)]
#[allow(clippy::exhaustive_enums)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, exhaust::Exhaust)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(u8)]
pub enum Face7 {
    /// The interior volume of a cube, or an undefined direction. Corresponds to the vector `(0, 0, 0)`.
    Within = 0,
    /// Negative X; the face whose normal vector is `(-1, 0, 0)`.
    NX,
    /// Negative Y; the face whose normal vector is `(0, -1, 0)`; downward.
    NY,
    /// Negative Z; the face whose normal vector is `(0, 0, -1)`.
    NZ,
    /// Positive X; the face whose normal vector is `(1, 0, 0)`.
    PX,
    /// Positive Y; the face whose normal vector is `(0, 1, 0)`; upward.
    PY,
    /// Positive Z; the face whose normal vector is `(0, 0, 1)`.
    PZ,
}

impl Face6 {
    /// All the values of [`Face6`].
    pub const ALL: [Face6; 6] = [
        Face6::NX,
        Face6::NY,
        Face6::NZ,
        Face6::PX,
        Face6::PY,
        Face6::PZ,
    ];

    /// Inverse function of `face as u8`, converting the number to [`Face6`].
    #[inline]
    pub const fn from_discriminant(d: u8) -> Option<Self> {
        match d {
            1 => Some(Self::NX),
            2 => Some(Self::NY),
            3 => Some(Self::NZ),
            4 => Some(Self::PX),
            5 => Some(Self::PY),
            6 => Some(Self::PZ),
            _ => None,
        }
    }

    /// Returns the [`Face6`] whose normal vector is closest in direction to the given
    /// vector.
    ///
    /// Edge cases:
    /// *   Ties are broken by preferring Z faces over Y faces, and Y faces over X faces.
    /// *   If all magnitudes are zero, the Z axis's sign is used. (Remember that floating-point
    ///     numbers include distinct positive and negative zeroes).
    /// *   If any coordinate is NaN, returns [`None`].
    #[allow(unused)] // TODO: I expect to use this in block placement
    fn from_snapped_vector(vector: FreeVector) -> Option<Self> {
        let Vector3D { x, y, z, _unit } = vector;

        // This isn't the likely case, but if we check it first, the generated code for signum()
        // can avoid redundant NaN checks.
        if x.is_nan() || y.is_nan() || z.is_nan() {
            return None;
        }

        // Note that the Rust signum() reads the sign of zeroes rather than returning zero for zero
        // (as would be mathematically conventional --
        // <https://en.wikipedia.org/w/index.php?title=Sign_function&oldid=1177447019>).
        // Duplicating the calls in each branch helps avoid redundant NaN checks.
        let (neg_face, sign) = if x.abs() > y.abs() && x.abs() > z.abs() {
            (Face6::NX, x.signum())
        } else if y.abs() > z.abs() {
            (Face6::NY, y.signum())
        } else {
            (Face6::NZ, z.signum())
        };
        Some(if sign < 0. {
            neg_face
        } else {
            neg_face.opposite()
        })
    }

    /// Returns which axis this face's normal vector is parallel to.
    #[inline]
    #[must_use]
    pub const fn axis(self) -> Axis {
        match self {
            Self::NX | Self::PX => Axis::X,
            Self::NY | Self::PY => Axis::Y,
            Self::NZ | Self::PZ => Axis::Z,
        }
    }

    /// Returns whether this face is a “positive” face: one whose unit vector's nonzero
    /// coordinate is positive.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::Face6;
    ///
    /// assert_eq!(Face6::PX.is_positive(), true);
    /// assert_eq!(Face6::NX.is_positive(), false);
    /// ```
    #[inline]
    pub fn is_positive(self) -> bool {
        matches!(self, Self::PX | Self::PY | Self::PZ)
    }

    /// Returns whether this face is a negative face: one whose unit vector's nonzero
    /// coordinate is negative.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::Face6;
    ///
    /// assert_eq!(Face6::PX.is_negative(), false);
    /// assert_eq!(Face6::NX.is_negative(), true);
    /// ```
    #[inline]
    pub fn is_negative(self) -> bool {
        matches!(self, Self::NX | Self::NY | Self::NZ)
    }

    #[inline]
    pub(crate) fn signum(self) -> GridCoordinate {
        match self {
            Self::NX | Self::NY | Self::NZ => -1,
            Self::PX | Self::PY | Self::PZ => 1,
        }
    }

    /// Returns the opposite face (maps [`PX`](Self::PX) to [`NX`](Self::NX) and so on).
    #[inline]
    #[must_use]
    pub const fn opposite(self) -> Face6 {
        match self {
            Face6::NX => Face6::PX,
            Face6::NY => Face6::PY,
            Face6::NZ => Face6::PZ,
            Face6::PX => Face6::NX,
            Face6::PY => Face6::NY,
            Face6::PZ => Face6::NZ,
        }
    }

    /// Returns the face whose normal is the cross product of these faces' normals.
    /// Since cross products may be zero, the result is a [`Face7`].
    #[inline]
    #[must_use]
    pub const fn cross(self, other: Self) -> Face7 {
        self.into7().cross(other.into7())
    }
    /// Returns the axis-aligned unit vector normal to this face.
    #[inline]
    #[must_use]
    pub fn normal_vector<S, U>(self) -> Vector3D<S, U>
    where
        S: Zero + num_traits::One + ops::Neg<Output = S>,
    {
        self.into7().normal_vector()
    }

    /// Dot product of this face as a unit vector and the given vector,
    /// implemented by selecting the relevant component.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::{Face6, FreeVector};
    ///
    /// let sample_vector = FreeVector::new(1.0, 2.0, 5.0_f64);
    /// for face in Face6::ALL {
    ///     assert_eq!(face.dot(sample_vector), face.normal_vector().dot(sample_vector));
    /// }
    /// ```
    #[inline]
    #[must_use]
    pub fn dot<S, U>(self, vector: Vector3D<S, U>) -> S
    where
        S: Zero + ops::Neg<Output = S>,
    {
        self.into7().dot(vector)
    }

    /// Returns a [`Gridgid`] transformation which, if given points on the square
    /// with x ∈ [0, scale], y ∈ [0, scale], and z = 0, converts them to points that lie
    /// on the faces of the cube with x ∈ [0, scale], y ∈ [0, scale], and z ∈ [0, scale].
    ///
    /// Specifically, `Face6::NZ.face_transform()` is the identity and all others are
    /// consistent with that. Note that there are arbitrary choices in the rotation
    /// of all other faces. (TODO: Document those choices and test them.)
    ///
    /// To work with floating-point coordinates, use `.face_transform().to_matrix().to_free()`.
    #[must_use]
    #[rustfmt::skip]
    #[allow(clippy::missing_inline_in_public_items)]
    pub const fn face_transform(self, scale: GridCoordinate) -> Gridgid {
        use GridRotation::*;
        match self {
            Face6::NX => Gridgid::from_rotation_about_origin(RYZX),
            Face6::NY => Gridgid::from_rotation_about_origin(RZXY),
            Face6::NZ => Gridgid::from_rotation_about_origin(RXYZ),

            // Positives are same as negatives but with translation and an arbitrary choice of rotation.
            // PX rotates about Y.
            Face6::PX => Gridgid {
                rotation: RyZx,
                translation: GridVector::new(scale, scale, 0),
            },
            // PY rotates about X.
            Face6::PY => Gridgid {
                rotation: RZxy,
                translation: GridVector::new(scale, scale, 0),
            },
            // PZ rotates about Y.
            Face6::PZ => Gridgid {
                rotation: RXyz,
                translation: GridVector::new(0, scale, scale),
            },
        }
    }

    /// Helper to convert in const context; equivalent to `.into()`.
    #[inline]
    const fn into7(self) -> Face7 {
        match self {
            Face6::NX => Face7::NX,
            Face6::NY => Face7::NY,
            Face6::NZ => Face7::NZ,
            Face6::PX => Face7::PX,
            Face6::PY => Face7::PY,
            Face6::PZ => Face7::PZ,
        }
    }
}

impl Face7 {
    /// All the values of [`Face7`], with [`Face7::Within`] listed first.
    pub const ALL: [Face7; 7] = [
        Face7::Within,
        Face7::NX,
        Face7::NY,
        Face7::NZ,
        Face7::PX,
        Face7::PY,
        Face7::PZ,
    ];

    /// Inverse function of `face as u8`, converting the number to [`Face7`].
    #[inline]
    pub const fn from_discriminant(d: u8) -> Option<Self> {
        match d {
            0 => Some(Self::Within),
            1 => Some(Self::NX),
            2 => Some(Self::NY),
            3 => Some(Self::NZ),
            4 => Some(Self::PX),
            5 => Some(Self::PY),
            6 => Some(Self::PZ),
            _ => None,
        }
    }

    /// Returns which axis this face's normal vector is parallel to,
    /// or [`None`] if the face is [`Face7::Within`].
    #[inline]
    #[must_use]
    pub const fn axis(self) -> Option<Axis> {
        match self {
            Face7::Within => None,
            Face7::NX | Face7::PX => Some(Axis::X),
            Face7::NY | Face7::PY => Some(Axis::Y),
            Face7::NZ | Face7::PZ => Some(Axis::Z),
        }
    }

    /// Returns whether this face is a “positive” face: one whose unit vector's nonzero
    /// coordinate is positive.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::Face7;
    ///
    /// assert_eq!(Face7::PX.is_positive(), true);
    /// assert_eq!(Face7::NX.is_positive(), false);
    /// assert_eq!(Face7::Within.is_positive(), false);
    /// ```
    #[inline]
    pub fn is_positive(self) -> bool {
        matches!(self, Face7::PX | Face7::PY | Face7::PZ)
    }

    /// Returns whether this face is a negative face: one whose unit vector's nonzero
    /// coordinate is negative.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::Face7;
    ///
    /// assert_eq!(Face7::PX.is_negative(), false);
    /// assert_eq!(Face7::NX.is_negative(), true);
    /// assert_eq!(Face7::Within.is_negative(), false);
    /// ```
    #[inline]
    pub fn is_negative(self) -> bool {
        matches!(self, Face7::NX | Face7::NY | Face7::NZ)
    }

    /// Returns the opposite face (maps [`PX`](Self::PX) to [`NX`](Self::NX) and so on).
    #[inline]
    #[must_use]
    pub const fn opposite(self) -> Face7 {
        match self {
            Face7::Within => Face7::Within,
            Face7::NX => Face7::PX,
            Face7::NY => Face7::PY,
            Face7::NZ => Face7::PZ,
            Face7::PX => Face7::NX,
            Face7::PY => Face7::NY,
            Face7::PZ => Face7::NZ,
        }
    }

    /// Returns the face whose normal is the cross product of these faces' normals.
    #[inline]
    #[must_use]
    pub const fn cross(self, other: Self) -> Self {
        use Face7::*;
        match (self, other) {
            // Zero input
            (Within, _) => Within,
            (_, Within) => Within,

            // Equal vectors
            (NX, NX) => Within,
            (NY, NY) => Within,
            (NZ, NZ) => Within,
            (PX, PX) => Within,
            (PY, PY) => Within,
            (PZ, PZ) => Within,

            // Opposite vectors
            (NX, PX) => Within,
            (NY, PY) => Within,
            (NZ, PZ) => Within,
            (PX, NX) => Within,
            (PY, NY) => Within,
            (PZ, NZ) => Within,

            (NX, NY) => PZ,
            (NX, NZ) => NY,
            (NX, PY) => NZ,
            (NX, PZ) => PY,

            (NY, NX) => NZ,
            (NY, NZ) => PX,
            (NY, PX) => PZ,
            (NY, PZ) => NX,

            (NZ, NX) => PY,
            (NZ, NY) => NX,
            (NZ, PX) => NY,
            (NZ, PY) => PX,

            (PX, NY) => NZ,
            (PX, NZ) => PY,
            (PX, PY) => PZ,
            (PX, PZ) => NY,

            (PY, NX) => PZ,
            (PY, NZ) => NX,
            (PY, PX) => NZ,
            (PY, PZ) => PX,

            (PZ, NX) => NY,
            (PZ, NY) => PX,
            (PZ, PX) => PY,
            (PZ, PY) => NX,
        }
    }

    /// Returns the vector normal to this face. [`Within`](Self::Within) is assigned the
    /// zero vector.
    #[inline]
    #[must_use]
    pub fn normal_vector<S, U>(self) -> Vector3D<S, U>
    where
        S: Zero + num_traits::One + ops::Neg<Output = S>,
    {
        match self {
            Face7::Within => Vector3D::new(S::zero(), S::zero(), S::zero()),
            Face7::NX => Vector3D::new(-S::one(), S::zero(), S::zero()),
            Face7::NY => Vector3D::new(S::zero(), -S::one(), S::zero()),
            Face7::NZ => Vector3D::new(S::zero(), S::zero(), -S::one()),
            Face7::PX => Vector3D::new(S::one(), S::zero(), S::zero()),
            Face7::PY => Vector3D::new(S::zero(), S::one(), S::zero()),
            Face7::PZ => Vector3D::new(S::zero(), S::zero(), S::one()),
        }
    }

    /// Dot product of this face as a unit vector and the given vector,
    /// implemented by selecting the relevant component.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::{Face7, FreeVector};
    ///
    /// let sample_vector = FreeVector::new(1.0, 2.0, 5.0_f64);
    /// for face in Face7::ALL {
    ///     assert_eq!(face.dot(sample_vector), face.normal_vector().dot(sample_vector));
    /// }
    /// ```
    #[inline]
    #[must_use]
    pub fn dot<S, U>(self, vector: Vector3D<S, U>) -> S
    where
        S: Zero + ops::Neg<Output = S>,
    {
        match self {
            Face7::Within => S::zero(),
            Face7::NX => -vector.x,
            Face7::NY => -vector.y,
            Face7::NZ => -vector.z,
            Face7::PX => vector.x,
            Face7::PY => vector.y,
            Face7::PZ => vector.z,
        }
    }
}

impl ops::Neg for Face6 {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self::Output {
        self.opposite()
    }
}
impl ops::Neg for Face7 {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self::Output {
        self.opposite()
    }
}

impl From<Face6> for Face7 {
    #[inline]
    fn from(value: Face6) -> Self {
        value.into7()
    }
}
impl TryFrom<Face7> for Face6 {
    type Error = Faceless;
    #[inline]
    fn try_from(value: Face7) -> Result<Face6, Self::Error> {
        match value {
            Face7::Within => Err(Faceless),
            Face7::NX => Ok(Face6::NX),
            Face7::NY => Ok(Face6::NY),
            Face7::NZ => Ok(Face6::NZ),
            Face7::PX => Ok(Face6::PX),
            Face7::PY => Ok(Face6::PY),
            Face7::PZ => Ok(Face6::PZ),
        }
    }
}

impl TryFrom<GridVector> for Face6 {
    /// Returns the original vector on failure.
    /// (An error message would probably be too lacking context to be helpful.)
    type Error = GridVector;

    /// Recovers a `Face6` from its corresponding unit normal vector. All other vectors
    /// are rejected.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::{Face6, GridVector};
    ///
    /// // A Face6 may be converted from its normal vector.
    /// for face in Face6::ALL {
    ///     assert_eq!(Face6::try_from(face.normal_vector()), Ok(face));
    /// }
    ///
    /// // If the vector does not correspond to any Face6, it is returned.
    /// let v = GridVector::new(1, 2, 3);
    /// assert_eq!(Face6::try_from(v), Err(v));
    /// ```
    #[inline]
    fn try_from(value: GridVector) -> Result<Self, Self::Error> {
        let f7 = Face7::try_from(value)?;
        Face6::try_from(f7).map_err(|_| value)
    }
}
impl TryFrom<GridVector> for Face7 {
    /// Returns the original vector on failure.
    /// (An error message would probably be too lacking context to be helpful.)
    type Error = GridVector;

    /// Recovers a [`Face7`] from its corresponding unit normal vector. All other vectors
    /// are rejected.
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::{Face7, GridVector};
    ///
    /// // A Face7 may be converted from its normal vector.
    /// for face in Face7::ALL {
    ///     assert_eq!(Face7::try_from(face.normal_vector()), Ok(face));
    /// }
    ///
    /// // If the vector does not correspond to any Face7, it is returned.
    /// let v = GridVector::new(1, 2, 3);
    /// assert_eq!(Face7::try_from(v), Err(v));
    /// ```
    #[rustfmt::skip]
    #[allow(clippy::missing_inline_in_public_items)] // unsure whether good
    fn try_from(value: GridVector) -> Result<Self, Self::Error> {
        use Face7::*;
        match value {
            GridVector { _unit: _, x: 0, y: 0, z: 0 } => Ok(Within),
            GridVector { _unit: _, x: 1, y: 0, z: 0 } => Ok(PX),
            GridVector { _unit: _, x: 0, y: 1, z: 0 } => Ok(PY),
            GridVector { _unit: _, x: 0, y: 0, z: 1 } => Ok(PZ),
            GridVector { _unit: _, x: -1, y: 0, z: 0 } => Ok(NX),
            GridVector { _unit: _, x: 0, y: -1, z: 0 } => Ok(NY),
            GridVector { _unit: _, x: 0, y: 0, z: -1 } => Ok(NZ),
            not_unit_vector => Err(not_unit_vector),
        }
    }
}

/// Error resulting from providing [`Face7::Within`] where a definite nonzero direction
/// is needed, such as converting to a [`Face6`].
#[derive(Copy, Clone, Debug, Eq, PartialEq, displaydoc::Display)]
#[displaydoc("Face7::Within does not have a direction or axis")]
#[allow(clippy::exhaustive_structs)]
pub struct Faceless;

#[cfg(feature = "rerun")]
impl From<Face6> for re_types::view_coordinates::SignedAxis3 {
    #[inline]
    fn from(face: Face6) -> Self {
        use re_types::view_coordinates::{Axis3, Sign, SignedAxis3};
        match face {
            Face6::NX => SignedAxis3 {
                sign: Sign::Negative,
                axis: Axis3::X,
            },
            Face6::NY => SignedAxis3 {
                sign: Sign::Negative,
                axis: Axis3::Y,
            },
            Face6::NZ => SignedAxis3 {
                sign: Sign::Negative,
                axis: Axis3::Z,
            },
            Face6::PX => SignedAxis3 {
                sign: Sign::Positive,
                axis: Axis3::X,
            },
            Face6::PY => SignedAxis3 {
                sign: Sign::Positive,
                axis: Axis3::Y,
            },
            Face6::PZ => SignedAxis3 {
                sign: Sign::Positive,
                axis: Axis3::Z,
            },
        }
    }
}

/// Container for values keyed by [`Face6`]s. Always holds exactly six elements.
#[allow(clippy::exhaustive_structs)]
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, exhaust::Exhaust)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct FaceMap<V> {
    /// The value whose key is [`Face6::NX`].
    pub nx: V,
    /// The value whose key is [`Face6::NY`].
    pub ny: V,
    /// The value whose key is [`Face6::NZ`].
    pub nz: V,
    /// The value whose key is [`Face6::PX`].
    pub px: V,
    /// The value whose key is [`Face6::PY`].
    pub py: V,
    /// The value whose key is [`Face6::PZ`].
    pub pz: V,
}

#[allow(clippy::missing_inline_in_public_items)] // all methods are generic code
impl<V> FaceMap<V> {
    /// Constructs a [`FaceMap`] by using the provided function to compute
    /// a value for each [`Face6`] enum variant.
    #[inline]
    pub fn from_fn(mut f: impl FnMut(Face6) -> V) -> Self {
        Self {
            nx: f(Face6::NX),
            ny: f(Face6::NY),
            nz: f(Face6::NZ),
            px: f(Face6::PX),
            py: f(Face6::PY),
            pz: f(Face6::PZ),
        }
    }

    /// Constructs a [`FaceMap`] whose negative and positive directions are equal.
    // TODO: Evaluate whether this is a good API.
    #[inline]
    #[doc(hidden)] // used by all-is-cubes-content
    pub fn symmetric([x, y, z]: [V; 3]) -> Self
    where
        V: Default + Clone,
    {
        Self {
            nx: x.clone(),
            px: x,
            ny: y.clone(),
            py: y,
            nz: z.clone(),
            pz: z,
        }
    }

    /// Returns a vector containing the values for each negative face.
    pub fn negatives<U>(self) -> Vector3D<V, U>
    where
        V: Copy,
    {
        Vector3D::new(self.nx, self.ny, self.nz)
    }

    /// Returns a vector containing the values for each positive face.
    pub fn positives<U>(self) -> Vector3D<V, U>
    where
        V: Copy,
    {
        Vector3D::new(self.px, self.py, self.pz)
    }

    /// Iterate over the map's key-value pairs by reference, in the same order as [`Face6::ALL`].
    pub fn iter(&self) -> impl Iterator<Item = (Face6, &V)> {
        Face6::ALL.iter().copied().map(move |f| (f, &self[f]))
    }

    /// Iterate over the map's key-value pairs by mutable reference, in the same order as [`Face6::ALL`].
    pub fn iter_mut(&mut self) -> impl Iterator<Item = (Face6, &mut V)> {
        [
            (Face6::NX, &mut self.nx),
            (Face6::NY, &mut self.ny),
            (Face6::NZ, &mut self.nz),
            (Face6::PX, &mut self.px),
            (Face6::PY, &mut self.py),
            (Face6::PZ, &mut self.pz),
        ]
        .into_iter()
    }

    /// Iterate over the map values by reference, in the same order as [`Face6::ALL`].
    pub fn values(&self) -> impl Iterator<Item = &V> {
        Face6::ALL.iter().copied().map(move |f| &self[f])
    }

    /// Convert to an array, whose elements are arranged in the same order as [`Face6::ALL`].
    pub fn into_values(self) -> [V; 6] {
        [self.nx, self.ny, self.nz, self.px, self.py, self.pz]
    }

    /// Convert to an iterator, whose items are arranged in the same order as [`Face6::ALL`].
    pub fn into_values_iter(self) -> impl Iterator<Item = V> {
        // TODO: eliminate this as not really useful in Rust 2021
        self.into_values().into_iter()
    }

    /// Transform values.
    pub fn map<U>(self, mut f: impl FnMut(Face6, V) -> U) -> FaceMap<U> {
        FaceMap {
            nx: f(Face6::NX, self.nx),
            ny: f(Face6::NY, self.ny),
            nz: f(Face6::NZ, self.nz),
            px: f(Face6::PX, self.px),
            py: f(Face6::PY, self.py),
            pz: f(Face6::PZ, self.pz),
        }
    }

    /// Combine two [`FaceMap`]s using a function applied to each pair of corresponding values.
    pub fn zip<U, R>(self, other: FaceMap<U>, mut f: impl FnMut(Face6, V, U) -> R) -> FaceMap<R> {
        FaceMap {
            nx: f(Face6::NX, self.nx, other.nx),
            ny: f(Face6::NY, self.ny, other.ny),
            nz: f(Face6::NZ, self.nz, other.nz),
            px: f(Face6::PX, self.px, other.px),
            py: f(Face6::PY, self.py, other.py),
            pz: f(Face6::PZ, self.pz, other.pz),
        }
    }

    /// Returns this map with one entry's value replaced.
    ///
    /// This may be used for constructing a map with only one interesting entry:
    ///
    /// ```
    /// # extern crate all_is_cubes_base as all_is_cubes;
    /// use all_is_cubes::math::{Face6, FaceMap};
    ///
    /// assert_eq!(
    ///     FaceMap::default().with(Face6::PY, 10),
    ///     {
    ///         let mut m = FaceMap::default();
    ///         m[Face6::PY] = 10;
    ///         m
    ///     },
    /// );
    /// ```
    #[inline]
    #[must_use]
    pub fn with(mut self, face: Face6, value: V) -> Self {
        self[face] = value;
        self
    }

    /// Shuffle the values in this map according to the given rotation.
    #[must_use]
    pub fn rotate(self, rotation: GridRotation) -> Self {
        // TODO: Can we make this cleaner? (If GridRotation had a way to ask it what swaps
        // it corresponds to, that might also be useful for Vol rotations.)
        let to_source = rotation.inverse();
        let mut source = self.map(|_, value| Some(value));
        Self::from_fn(|face| source[to_source.transform(face)].take().unwrap())
    }
}

impl<V: Clone> FaceMap<V> {
    /// Constructs a [`FaceMap`] containing clones of the provided value.
    #[inline]
    pub fn repeat(value: V) -> Self {
        Self {
            nx: value.clone(),
            ny: value.clone(),
            nz: value.clone(),
            px: value.clone(),
            py: value.clone(),
            pz: value,
        }
    }
}

impl<V: Copy> FaceMap<V> {
    /// Constructs a [`FaceMap`] containing copies of the provided value.
    ///
    /// This is practically identical to [`FaceMap::repeat()`] except that it is a
    /// `const fn`. It may be removed from future major versions once Rust supports const
    /// trait function calls.
    #[inline]
    pub const fn repeat_copy(value: V) -> Self {
        Self {
            nx: value,
            ny: value,
            nz: value,
            px: value,
            py: value,
            pz: value,
        }
    }
}

impl<V> ops::Index<Face6> for FaceMap<V> {
    type Output = V;
    #[inline]
    fn index(&self, face: Face6) -> &V {
        match face {
            Face6::NX => &self.nx,
            Face6::NY => &self.ny,
            Face6::NZ => &self.nz,
            Face6::PX => &self.px,
            Face6::PY => &self.py,
            Face6::PZ => &self.pz,
        }
    }
}

impl<V> ops::IndexMut<Face6> for FaceMap<V> {
    #[inline]
    fn index_mut(&mut self, face: Face6) -> &mut V {
        match face {
            Face6::NX => &mut self.nx,
            Face6::NY => &mut self.ny,
            Face6::NZ => &mut self.nz,
            Face6::PX => &mut self.px,
            Face6::PY => &mut self.py,
            Face6::PZ => &mut self.pz,
        }
    }
}

impl<V> fmt::Debug for FaceMap<V>
where
    V: fmt::Debug + PartialEq,
{
    /// In addition to the usual formatting behaviors, [`FaceMap`] will detect whether
    /// elements are equal and avoid redundant printing.
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let FaceMap {
            nx,
            ny,
            nz,
            px,
            py,
            pz,
        } = self;

        let mut dm = f.debug_map();

        if nx == ny && nx == nz && nx == px && nx == py && nx == pz {
            dm.entry(&"all".refmt(&Unquote), nx);
        } else if nx == ny && nx == nz && px == py && px == pz {
            dm.entry(&"−all".refmt(&Unquote), nx);
            dm.entry(&"+all".refmt(&Unquote), px);
        } else if nx == px && ny == py && nz == pz {
            dm.entry(&"x".refmt(&Unquote), nx);
            dm.entry(&"y".refmt(&Unquote), ny);
            dm.entry(&"z".refmt(&Unquote), nz);
        } else {
            dm.entry(&"−x".refmt(&Unquote), nx);
            dm.entry(&"−y".refmt(&Unquote), ny);
            dm.entry(&"−z".refmt(&Unquote), nz);
            dm.entry(&"+x".refmt(&Unquote), px);
            dm.entry(&"+y".refmt(&Unquote), py);
            dm.entry(&"+z".refmt(&Unquote), pz);
        };

        dm.finish()
    }
}

macro_rules! impl_binary_operator_for_facemap {
    ($trait:ident :: $method:ident) => {
        impl<V: ops::$trait> ops::$trait for FaceMap<V> {
            type Output = FaceMap<V::Output>;
            /// Apply the operator pairwise to the values for all six faces.
            #[inline]
            fn $method(self, other: FaceMap<V>) -> FaceMap<V::Output> {
                self.zip(other, |_, a, b| <V as ops::$trait>::$method(a, b))
            }
        }
    };
}
impl_binary_operator_for_facemap!(BitAnd::bitand);
impl_binary_operator_for_facemap!(BitOr::bitor);
impl_binary_operator_for_facemap!(BitXor::bitxor);
impl_binary_operator_for_facemap!(Add::add);
impl_binary_operator_for_facemap!(Mul::mul);
impl_binary_operator_for_facemap!(Sub::sub);
impl_binary_operator_for_facemap!(Div::div);
impl_binary_operator_for_facemap!(Rem::rem);

/// The combination of a [`Cube`] and [`Face7`] identifying one face of it or the interior.
/// This pattern appears in cursor selection and collision detection.
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
#[allow(clippy::exhaustive_structs)]
#[allow(missing_docs)]
pub struct CubeFace {
    pub cube: Cube,
    pub face: Face7,
}

impl CubeFace {
    #[allow(missing_docs)]
    #[inline]
    pub fn new(cube: impl Into<Cube>, face: Face7) -> Self {
        Self {
            cube: cube.into(),
            face,
        }
    }

    /// Computes the cube that is adjacent in the direction of [`self.face`](Self::face).
    /// Equal to [`self.cube`](Self::cube) if the face is [`Face7::Within`].
    ///
    /// May panic if the cube coordinates overflow.
    #[inline]
    pub fn adjacent(self) -> Cube {
        self.cube + self.face.normal_vector()
    }
}

impl fmt::Debug for CubeFace {
    #[allow(clippy::missing_inline_in_public_items)]
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            fmt,
            "CubeFace({:?}, {:?})",
            self.cube.refmt(&ConciseDebug),
            self.face,
        )
    }
}

// TODO: This is a quick kludge to get some debug rendering going. We should offer more controls, probably
impl Geometry for CubeFace {
    type Coord = GridCoordinate;

    #[inline]
    fn translate(mut self, offset: GridVector) -> Self {
        self.cube += offset;
        self
    }

    #[allow(clippy::missing_inline_in_public_items)]
    fn wireframe_points<E>(&self, output: &mut E)
    where
        E: Extend<LineVertex>,
    {
        // TODO: How much to offset the lines should be a parameter of the wireframe_points process.
        let expansion = 0.005;
        let aab = self.cube.aab().expand(expansion);
        aab.wireframe_points(output);

        // Draw an X on the face.
        if let Ok(face) = Face6::try_from(self.face) {
            let face_transform = face.face_transform(1);
            const X_POINTS: [GridPoint; 4] = [
                GridPoint::new(0, 0, 0),
                GridPoint::new(1, 1, 0),
                GridPoint::new(1, 0, 0),
                GridPoint::new(0, 1, 0),
            ];
            // TODO: this is a messy kludge and really we should be stealing corner points
            // from the AAB instead, but there isn't yet a good way to do that.
            output.extend(X_POINTS.into_iter().map(|p| {
                LineVertex::from(
                    (face_transform.transform_point(p))
                        .map(|c| (FreeCoordinate::from(c) - 0.5) * (1. + expansion * 2.) + 0.5)
                        + self.cube.aab().lower_bounds_v(),
                )
            }));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::String;
    use alloc::vec::Vec;
    use exhaust::Exhaust;
    use pretty_assertions::assert_eq;

    #[test]
    fn from_snapped_vector_roundtrip() {
        for face in Face6::ALL {
            let normal = face.normal_vector();
            let snapped = Face6::from_snapped_vector(normal);
            assert_eq!(Some(face), snapped, "from {normal:?}");
        }
    }

    #[test]
        #[rustfmt::skip]
        fn from_snapped_vector_cases() {
            for (face, vector, comment) in [
                (Some(Face6::PZ), [0., 0., 0.], "zero tie, positive Z, positive other"),
                (Some(Face6::PZ), [-0., -0., 0.], "zero tie, positive Z, negative other"),
                (Some(Face6::NZ), [0., 0., -0.], "zero tie, negative Z, positive other"),
                (Some(Face6::NZ), [-0., -0., -0.], "zero tie, negative Z, negative other"),

                (Some(Face6::NZ), [-2., -3., -3.], "2-axis tie YZ, negative"),
                (Some(Face6::NY), [-3., -3., -2.], "2-axis tie XY, negative"),
                (Some(Face6::PZ), [2., 3., 3.], "2-axis tie YZ, positive"),
                (Some(Face6::PY), [3., 3., 2.], "2-axis tie XY, positive"),
    
                (None, [f64::NAN, 1.0, 1.0], "NaN X"),
                (None, [1.0, f64::NAN, 1.0], "NaN Y"),
                (None, [1.0, 1.0, f64::NAN], "NaN Z"),
            ] {
                let vector = FreeVector::from(vector);
                assert_eq!(face, Face6::from_snapped_vector(vector), "{comment}, {vector:?}");
            }
        }

    #[test]
    fn cross_6() {
        for face1 in Face6::ALL {
            for face2 in Face6::ALL {
                // Cross product of faces is identical to cross product of vectors.
                assert_eq!(
                    face1.cross(face2).normal_vector::<f64, ()>(),
                    face1.normal_vector().cross(face2.normal_vector()),
                    "{face1:?} cross {face2:?}",
                );
            }
        }
    }

    #[test]
    fn cross_7() {
        for face1 in Face7::ALL {
            for face2 in Face7::ALL {
                // Cross product of faces is identical to cross product of vectors.
                assert_eq!(
                    face1.cross(face2).normal_vector::<f64, ()>(),
                    face1.normal_vector().cross(face2.normal_vector()),
                    "{face1:?} cross {face2:?}",
                );
            }
        }
    }

    #[test]
    fn face_transform_does_not_reflect() {
        for face in Face6::ALL {
            assert!(!face.face_transform(7).rotation.is_reflection());
        }
    }

    // TODO: More tests of face.face_transform()

    #[test]
    fn face_map_debug_cmp() {
        let strings = FaceMap::<bool>::exhaust()
            .map(|fm| format!("{fm:?}"))
            .collect::<Vec<String>>();
        assert_eq!(
            strings.iter().map(String::as_str).collect::<Vec<_>>(),
            vec![
                "{all: false}",
                "{−x: false, −y: false, −z: false, +x: false, +y: false, +z: true}",
                "{−x: false, −y: false, −z: false, +x: false, +y: true, +z: false}",
                "{−x: false, −y: false, −z: false, +x: false, +y: true, +z: true}",
                "{−x: false, −y: false, −z: false, +x: true, +y: false, +z: false}",
                "{−x: false, −y: false, −z: false, +x: true, +y: false, +z: true}",
                "{−x: false, −y: false, −z: false, +x: true, +y: true, +z: false}",
                "{−all: false, +all: true}",
                "{−x: false, −y: false, −z: true, +x: false, +y: false, +z: false}",
                "{x: false, y: false, z: true}",
                "{−x: false, −y: false, −z: true, +x: false, +y: true, +z: false}",
                "{−x: false, −y: false, −z: true, +x: false, +y: true, +z: true}",
                "{−x: false, −y: false, −z: true, +x: true, +y: false, +z: false}",
                "{−x: false, −y: false, −z: true, +x: true, +y: false, +z: true}",
                "{−x: false, −y: false, −z: true, +x: true, +y: true, +z: false}",
                "{−x: false, −y: false, −z: true, +x: true, +y: true, +z: true}",
                "{−x: false, −y: true, −z: false, +x: false, +y: false, +z: false}",
                "{−x: false, −y: true, −z: false, +x: false, +y: false, +z: true}",
                "{x: false, y: true, z: false}",
                "{−x: false, −y: true, −z: false, +x: false, +y: true, +z: true}",
                "{−x: false, −y: true, −z: false, +x: true, +y: false, +z: false}",
                "{−x: false, −y: true, −z: false, +x: true, +y: false, +z: true}",
                "{−x: false, −y: true, −z: false, +x: true, +y: true, +z: false}",
                "{−x: false, −y: true, −z: false, +x: true, +y: true, +z: true}",
                "{−x: false, −y: true, −z: true, +x: false, +y: false, +z: false}",
                "{−x: false, −y: true, −z: true, +x: false, +y: false, +z: true}",
                "{−x: false, −y: true, −z: true, +x: false, +y: true, +z: false}",
                "{x: false, y: true, z: true}",
                "{−x: false, −y: true, −z: true, +x: true, +y: false, +z: false}",
                "{−x: false, −y: true, −z: true, +x: true, +y: false, +z: true}",
                "{−x: false, −y: true, −z: true, +x: true, +y: true, +z: false}",
                "{−x: false, −y: true, −z: true, +x: true, +y: true, +z: true}",
                "{−x: true, −y: false, −z: false, +x: false, +y: false, +z: false}",
                "{−x: true, −y: false, −z: false, +x: false, +y: false, +z: true}",
                "{−x: true, −y: false, −z: false, +x: false, +y: true, +z: false}",
                "{−x: true, −y: false, −z: false, +x: false, +y: true, +z: true}",
                "{x: true, y: false, z: false}",
                "{−x: true, −y: false, −z: false, +x: true, +y: false, +z: true}",
                "{−x: true, −y: false, −z: false, +x: true, +y: true, +z: false}",
                "{−x: true, −y: false, −z: false, +x: true, +y: true, +z: true}",
                "{−x: true, −y: false, −z: true, +x: false, +y: false, +z: false}",
                "{−x: true, −y: false, −z: true, +x: false, +y: false, +z: true}",
                "{−x: true, −y: false, −z: true, +x: false, +y: true, +z: false}",
                "{−x: true, −y: false, −z: true, +x: false, +y: true, +z: true}",
                "{−x: true, −y: false, −z: true, +x: true, +y: false, +z: false}",
                "{x: true, y: false, z: true}",
                "{−x: true, −y: false, −z: true, +x: true, +y: true, +z: false}",
                "{−x: true, −y: false, −z: true, +x: true, +y: true, +z: true}",
                "{−x: true, −y: true, −z: false, +x: false, +y: false, +z: false}",
                "{−x: true, −y: true, −z: false, +x: false, +y: false, +z: true}",
                "{−x: true, −y: true, −z: false, +x: false, +y: true, +z: false}",
                "{−x: true, −y: true, −z: false, +x: false, +y: true, +z: true}",
                "{−x: true, −y: true, −z: false, +x: true, +y: false, +z: false}",
                "{−x: true, −y: true, −z: false, +x: true, +y: false, +z: true}",
                "{x: true, y: true, z: false}",
                "{−x: true, −y: true, −z: false, +x: true, +y: true, +z: true}",
                "{−all: true, +all: false}",
                "{−x: true, −y: true, −z: true, +x: false, +y: false, +z: true}",
                "{−x: true, −y: true, −z: true, +x: false, +y: true, +z: false}",
                "{−x: true, −y: true, −z: true, +x: false, +y: true, +z: true}",
                "{−x: true, −y: true, −z: true, +x: true, +y: false, +z: false}",
                "{−x: true, −y: true, −z: true, +x: true, +y: false, +z: true}",
                "{−x: true, −y: true, −z: true, +x: true, +y: true, +z: false}",
                "{all: true}",
            ],
        );
    }

    /// Test the ordering of all [`FaceMap`] methods that explicitly produce an ordered result.
    #[test]
    fn face_map_iter_in_enum_order() {
        let mut map = FaceMap::from_fn(|f| f);
        let expected_both: Vec<(Face6, Face6)> = Face6::ALL.into_iter().zip(Face6::ALL).collect();

        // FaceMap::iter()
        assert_eq!(
            expected_both,
            map.iter().map(|(k, &v)| (k, v)).collect::<Vec<_>>(),
        );

        // FaceMap::iter_mut()
        assert_eq!(
            expected_both,
            map.iter_mut().map(|(k, &mut v)| (k, v)).collect::<Vec<_>>(),
        );

        // FaceMap::values()
        assert_eq!(
            Face6::ALL.to_vec(),
            map.values().copied().collect::<Vec<_>>(),
        );

        // FaceMap::into_values()
        assert_eq!(Face6::ALL, map.into_values());
    }

    #[test]
    fn face_map_rotate() {
        assert_eq!(
            FaceMap {
                nx: 10,
                px: 20,
                ny: 11,
                py: 21,
                nz: 12,
                pz: 22,
            }
            .rotate(GridRotation::RyXZ),
            FaceMap {
                nx: 11,
                px: 21,
                ny: 20,
                py: 10,
                nz: 12,
                pz: 22,
            }
        )
    }

    // TODO: More tests of FaceMap

    #[test]
    fn cubeface_format() {
        let cube_face = CubeFace {
            cube: Cube::new(1, 2, 3),
            face: Face7::NY,
        };
        assert_eq!(&format!("{cube_face:#?}"), "CubeFace((+1, +2, +3), NY)");
    }
}