fyrox-impl 1.0.1

Feature-rich, easy-to-use, 2D/3D game engine with a scene editor. Like Godot, but in Rust.
Documentation
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
// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Orthogonal transformations are some combination of 90-degree turns and
//! flips across the x-axis and y-axis. These transformations are important to
//! tiles because they are constrained along the tile grid.
//!
//! [`OrthoTransformation`] represents some combination of flips and turns,
//! and [`OrthoTransform`] is a class for objects that can be transformed.

use std::{
    fmt::{Debug, Display},
    ops::{Add, AddAssign, Neg},
};

use fxhash::FxHashMap;

use crate::core::{
    algebra::{Matrix2, Scalar, SimdPartialOrd, Vector2},
    math::{Number, Rect},
    reflect::prelude::*,
    visitor::prelude::*,
};

use super::OptionTileRect;

/// The amount of an orthogonal transformation.
/// The tranformation either includes an optional initial x-flip that turns (1,0) into (-1,0), or not.
/// Following the optional flip, the object being transformed is rotated counter-clockwise by some number
/// of right-angle turns: 0, 1, 2, or 3. Multiple transformations can be chained together for arbitrary
/// sequences of x-flips, y-flis, clockwise and counter-clockwise rotations.
///
/// These transformations are useful in situations where positions are
/// restricted to an orthogonal grid, as in a tile map.
#[derive(Copy, Clone, Hash, Eq, PartialEq, Visit, Reflect)]
pub struct OrthoTransformation(i8);

/// A map from `Vector2<i32>` to values. It can be transformed to flip and rotate the positions of the values.
#[derive(Clone, Debug, Visit)]
pub struct OrthoTransformMap<V: Visit + Default> {
    transform: OrthoTransformation,
    map: FxHashMap<Vector2<i32>, V>,
}

impl Default for OrthoTransformation {
    fn default() -> Self {
        Self::identity()
    }
}

const ROTATION_MATRICES: [Matrix2<f32>; 4] = [
    Matrix2::new(1.0, 0.0, 0.0, 1.0),
    Matrix2::new(0.0, -1.0, 1.0, 0.0),
    Matrix2::new(-1.0, 0.0, 0.0, -1.0),
    Matrix2::new(0.0, 1.0, -1.0, 0.0),
];

const X_FLIP_MATRIX: Matrix2<f32> = Matrix2::new(-1.0, 0.0, 0.0, 1.0);

impl OrthoTransformation {
    /// The transform that does nothing. It has no flip, and rotates by 0 right-angle turns.
    #[inline]
    pub const fn identity() -> Self {
        Self(1)
    }
    /// The transformation that first does an optional x-flip,
    /// then rotates counter-clockwise by the given amount.
    /// The rotation is measured in units of 90-degree rotations.
    /// Positive rotation is counter-clockwise. Negative rotation is clockwise.
    #[inline]
    pub const fn new(flipped: bool, rotation: i8) -> Self {
        let rotation = rotation.rem_euclid(4);
        Self(if flipped { -rotation - 1 } else { rotation + 1 })
    }
    /// An iterator over all 8 possible OrthoTransformations.
    pub fn all() -> impl Iterator<Item = OrthoTransformation> {
        [-4i8, -3, -2, -1, 1, 2, 3, 4]
            .into_iter()
            .map(OrthoTransformation)
    }
    /// True if this tranformation is the idenity transformation that leaves the transformed object unmodified.
    #[inline]
    pub const fn is_identity(&self) -> bool {
        self.0 == 1
    }
    /// Reverse this transformation, to it would return a transformed object
    /// back to where it started.
    /// In other words: `x.transformed(t).transformed(t.inverted()) == x`.
    #[inline]
    pub const fn inverted(self) -> Self {
        Self(match self.0 {
            1 => 1,
            2 => 4,
            3 => 3,
            4 => 2,
            -1 => -1,
            -2 => -2,
            -3 => -3,
            -4 => -4,
            _ => unreachable!(),
        })
    }
    /// True if this transform starts with an x-flip.
    #[inline]
    pub const fn is_flipped(&self) -> bool {
        self.0 < 0
    }
    /// The amount of rotation following the optional x-flip.
    /// The value is always 0, 1, 2, or 3, representing counter-clockwise rotations of
    /// 0, 90, 180, or 270 degrees.
    #[inline]
    pub const fn rotation(&self) -> i8 {
        self.0.abs() - 1
    }
    /// Matrix representation of this transformation.
    pub fn matrix(&self) -> Matrix2<f32> {
        let matrix = if self.is_flipped() {
            X_FLIP_MATRIX
        } else {
            Matrix2::identity()
        };
        ROTATION_MATRICES[self.rotation() as usize] * matrix
    }
}

impl Debug for OrthoTransformation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("OrthoTransformation")
            .field(&self.0)
            .field(&self.is_flipped())
            .field(&self.rotation())
            .finish()
    }
}

impl Display for OrthoTransformation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let rotation = match self.rotation() {
            0 => 0,
            1 => 90,
            2 => 180,
            3 => 270,
            _ => unreachable!(),
        };
        if self.is_flipped() {
            write!(f, "rotate({rotation})(flipped)")
        } else {
            write!(f, "rotate({rotation})")
        }
    }
}

/// Trait for objects that can perform a 2D orthogonal transformation.
/// In other words, they can be flipped along the x and y axis,
/// and they can be rotated by multiples of 90 degrees.
pub trait OrthoTransform: Sized {
    /// Flip the object parallel to the x axis, so x becomes -x.
    fn x_flipped(self) -> Self;
    /// Flip the object parallel to the y axis, so y becomes -y.
    fn y_flipped(self) -> Self {
        self.x_flipped().rotated(2)
    }
    /// Rotate the object counter-clockwise by the given amount.
    fn rotated(self, amount: i8) -> Self;
    /// Transform the object by the given amount.
    fn transformed(self, amount: OrthoTransformation) -> Self {
        (if amount.is_flipped() {
            self.x_flipped()
        } else {
            self
        })
        .rotated(amount.rotation())
    }
}

impl OrthoTransform for OrthoTransformation {
    fn x_flipped(self) -> Self {
        Self(match self.0 {
            1 => -1,
            2 => -4,
            3 => -3,
            4 => -2,
            -1 => 1,
            -2 => 4,
            -3 => 3,
            -4 => 2,
            _ => unreachable!(),
        })
    }
    fn rotated(self, amount: i8) -> Self {
        let amount = amount.rem_euclid(4);
        if self.0 > 0 {
            Self((self.0 + amount - 1).rem_euclid(4) + 1)
        } else {
            Self(-(self.0.abs() + amount - 1).rem_euclid(4) - 1)
        }
    }
}

impl<V: Neg<Output = V> + Scalar + Clone> OrthoTransform for Vector2<V> {
    fn x_flipped(self) -> Self {
        Self::new(-self.x.clone(), self.y.clone())
    }

    fn rotated(self, amount: i8) -> Self {
        let amount = amount.rem_euclid(4);
        match amount {
            0 => self,
            1 => Self::new(-self.y.clone(), self.x.clone()),
            2 => Self::new(-self.x.clone(), -self.y.clone()),
            3 => Self::new(self.y.clone(), -self.x.clone()),
            _ => unreachable!(),
        }
    }
}

impl<V: Number + SimdPartialOrd + Add + AddAssign + Neg<Output = V> + Scalar> OrthoTransform
    for Rect<V>
{
    fn x_flipped(self) -> Self {
        Rect::from_points(
            self.position.x_flipped(),
            (self.position + self.size).x_flipped(),
        )
    }

    fn rotated(self, amount: i8) -> Self {
        Rect::from_points(
            self.position.rotated(amount),
            (self.position + self.size).rotated(amount),
        )
    }
}

impl<V: Visit + Default> OrthoTransformMap<V> {
    /// Bounding rectangle the contains the keys.
    pub fn bounding_rect(&self) -> OptionTileRect {
        let mut result = OptionTileRect::default();
        for position in self.keys() {
            result.push(position);
        }
        result
    }

    /// Clear the elements of the map.
    #[inline]
    pub fn clear(&mut self) {
        self.map.clear()
    }
    /// True if the map contains no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }
    /// True if the map contains an element at the given position.
    #[inline]
    pub fn contains_key(&self, position: Vector2<i32>) -> bool {
        self.map
            .contains_key(&position.transformed(self.transform.inverted()))
    }
    /// Remove and return the element at the given position, if one exists.
    #[inline]
    pub fn remove(&mut self, position: Vector2<i32>) -> Option<V> {
        self.map
            .remove(&position.transformed(self.transform.inverted()))
    }
    /// Return a reference to the element at the given position, if any.
    #[inline]
    pub fn get(&self, position: Vector2<i32>) -> Option<&V> {
        self.map
            .get(&position.transformed(self.transform.inverted()))
    }
    /// Return a reference to the element at the given position, if any.
    #[inline]
    pub fn get_mut(&mut self, position: Vector2<i32>) -> Option<&mut V> {
        self.map
            .get_mut(&position.transformed(self.transform.inverted()))
    }
    /// Put an element into the map at the given position, and return the element that was previously at that position.
    #[inline]
    pub fn insert(&mut self, position: Vector2<i32>, value: V) -> Option<V> {
        self.map
            .insert(position.transformed(self.transform.inverted()), value)
    }
    /// Iterate through the map.
    #[inline]
    pub fn iter(&self) -> Iter<V> {
        Iter(self.transform, self.map.iter())
    }
    /// Iterate through the keys.
    #[inline]
    pub fn keys(&self) -> Keys<V> {
        Keys(self.transform, self.map.keys())
    }
    /// Iterate through the values.
    #[inline]
    pub fn values(&self) -> std::collections::hash_map::Values<Vector2<i32>, V> {
        self.map.values()
    }
}

impl<V: Visit + Default> Default for OrthoTransformMap<V> {
    fn default() -> Self {
        Self {
            transform: Default::default(),
            map: Default::default(),
        }
    }
}

impl<V: Visit + Default> OrthoTransform for OrthoTransformMap<V> {
    fn x_flipped(self) -> Self {
        Self {
            transform: self.transform.x_flipped(),
            map: self.map,
        }
    }

    fn rotated(self, amount: i8) -> Self {
        Self {
            transform: self.transform.rotated(amount),
            map: self.map,
        }
    }
}
/// Iterator for [`OrthoTransformMap`].
pub struct Iter<'a, V>(
    OrthoTransformation,
    std::collections::hash_map::Iter<'a, Vector2<i32>, V>,
);

/// Keys iterator for [`OrthoTransformMap`].
pub struct Keys<'a, V>(
    OrthoTransformation,
    std::collections::hash_map::Keys<'a, Vector2<i32>, V>,
);

impl<'a, V> Iterator for Iter<'a, V> {
    type Item = (Vector2<i32>, &'a V);

    fn next(&mut self) -> Option<Self::Item> {
        let (k, v) = self.1.next()?;
        Some((k.transformed(self.0), v))
    }
}
impl<V> Iterator for Keys<'_, V> {
    type Item = Vector2<i32>;

    fn next(&mut self) -> Option<Self::Item> {
        let k = self.1.next()?;
        Some(k.transformed(self.0))
    }
}

/// TransformSetCell represents a position within a transform set page and the corresponding transform.
/// This object fascilitates using a transform set page to transform tiles by moving between positions
/// in the page based on the desired transformation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TransformSetCell(Vector2<i32>, OrthoTransformation);

fn transform_to_cell_position(rotation: i8) -> Vector2<i32> {
    match rotation {
        -1 => Vector2::new(3, 0),
        -2 => Vector2::new(3, 1),
        -3 => Vector2::new(2, 1),
        -4 => Vector2::new(2, 0),
        1 => Vector2::new(0, 0),
        2 => Vector2::new(1, 0),
        3 => Vector2::new(1, 1),
        4 => Vector2::new(0, 1),
        _ => panic!(),
    }
}

fn cell_position_to_transform(position: Vector2<i32>) -> i8 {
    match (position.x, position.y) {
        (3, 0) => -1,
        (3, 1) => -2,
        (2, 1) => -3,
        (2, 0) => -4,
        (0, 0) => 1,
        (1, 0) => 2,
        (1, 1) => 3,
        (0, 1) => 4,
        _ => panic!(),
    }
}

impl TransformSetCell {
    /// Convert this cell into the corresponding position within a TransformSet page.
    pub fn into_position(self) -> Vector2<i32> {
        self.0 + transform_to_cell_position(self.1 .0)
    }
    /// Convert a position within a TransformSet into a transform set cell that specifies the corresponding orthogonal transformation.
    pub fn from_position(position: Vector2<i32>) -> Self {
        let rem = Vector2::new(position.x.rem_euclid(4), position.y.rem_euclid(2));
        let pos = Vector2::new(position.x - rem.x, position.y - rem.y);
        TransformSetCell(pos, OrthoTransformation(cell_position_to_transform(rem)))
    }
    /// Replace the transformation of this cell with a different transformation.
    /// This is part of the process of rotating a tile:
    /// 1. Find the transform set [`TileDefinitionHandle`](super::TileDefinitionHandle) from the tile's data.
    /// 2. Convert the tile position from the `TileDefinitonHandle` into a `TransformSetCell` using [TransformSetCell::from_position].
    /// 3. Use `with_transformation` to replace the transformation from the `TransformSetCell` with the desired transformation.
    /// 4. Convert the updated `TransformSetCell` into a tile position using [TransformSetCell::into_position].
    /// 5. Get the `TileDefinitionHandle` from the transform set page at that position.
    pub fn with_transformation(self, trans: OrthoTransformation) -> Self {
        TransformSetCell(self.0, trans)
    }
}

impl OrthoTransform for TransformSetCell {
    fn x_flipped(self) -> Self {
        Self(self.0, self.1.x_flipped())
    }

    fn rotated(self, amount: i8) -> Self {
        Self(self.0, self.1.rotated(amount))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::algebra::Point2;
    use OrthoTransformation as Trans;
    use TransformSetCell as Cell;

    #[test]
    fn identity() {
        assert_eq!(Trans::identity(), Trans::new(false, 0));
        for v in [
            Vector2::new(2, 1),
            Vector2::new(1, 2),
            Vector2::new(-1, 5),
            Vector2::new(-2, -2),
        ] {
            assert_eq!(v.transformed(Trans::identity()), v);
        }
    }
    fn matrix_check(trans: OrthoTransformation) {
        let v = Vector2::new(1.0, 0.5);
        let m = trans.matrix().to_homogeneous();
        let p = m.transform_point(&Point2::from(v)).coords;
        assert_eq!(p, v.transformed(trans), "{trans}");
    }
    #[test]
    fn matrix() {
        for i in 0..4 {
            matrix_check(Trans::new(false, i))
        }
        for i in 0..4 {
            matrix_check(Trans::new(true, i))
        }
    }
    #[test]
    fn rotate_4() {
        assert_eq!(Trans::identity(), Trans::new(false, 4))
    }
    #[test]
    fn invert() {
        for i in [-4, -3, -2, -1, 1, 2, 3, 4] {
            assert_eq!(
                Trans(i).transformed(Trans(i).inverted()),
                Trans::identity(),
                "{:?}: {:?} {:?}",
                i,
                Trans(i),
                Trans(i).inverted()
            );
        }
    }
    #[test]
    fn inverse_undoes_transform() {
        for i in [-4, -3, -2, -1, 1, 2, 3, 4] {
            assert_eq!(
                Vector2::new(2, 3)
                    .transformed(Trans(i))
                    .transformed(Trans(i).inverted()),
                Vector2::new(2, 3),
            );
        }
    }
    #[test]
    fn rotate_trans() {
        assert_eq!(Trans::new(false, 0).rotated(0), Trans::new(false, 0));
        assert_eq!(Trans::new(true, 0).rotated(0), Trans::new(true, 0));
        assert_eq!(Trans::new(true, 2).rotated(0), Trans::new(true, 2));
        assert_eq!(Trans::new(false, 0).rotated(1), Trans::new(false, 1));
        assert_eq!(Trans::new(true, 0).rotated(1), Trans::new(true, 1));
        assert_eq!(Trans::new(true, 2).rotated(1), Trans::new(true, 3));
        assert_eq!(Trans::new(false, 0).rotated(2), Trans::new(false, 2));
        assert_eq!(Trans::new(true, 0).rotated(2), Trans::new(true, 2));
        assert_eq!(Trans::new(true, 2).rotated(2), Trans::new(true, 0));
    }
    #[test]
    fn flipped_trans() {
        assert_eq!(Trans::new(false, 0).x_flipped(), Trans::new(true, 0));
        assert_eq!(Trans::new(true, 0).x_flipped(), Trans::new(false, 0));
        assert_eq!(Trans::new(true, 2).x_flipped(), Trans::new(false, 2));
        assert_eq!(Trans::new(true, 1).x_flipped(), Trans::new(false, 3));
        assert_eq!(Trans::new(false, 0).y_flipped(), Trans::new(true, 2));
        assert_eq!(Trans::new(true, 0).y_flipped(), Trans::new(false, 2));
        assert_eq!(Trans::new(true, 2).y_flipped(), Trans::new(false, 0));
        assert_eq!(Trans::new(true, 1).y_flipped(), Trans::new(false, 1));
    }
    #[test]
    fn rotate_vector() {
        assert_eq!(Vector2::new(1, 0).rotated(0), Vector2::new(1, 0));
        assert_eq!(Vector2::new(0, 1).rotated(0), Vector2::new(0, 1));
        assert_eq!(Vector2::new(2, 3).rotated(0), Vector2::new(2, 3));
        assert_eq!(Vector2::new(1, 0).rotated(1), Vector2::new(0, 1));
        assert_eq!(Vector2::new(0, 1).rotated(1), Vector2::new(-1, 0));
        assert_eq!(Vector2::new(2, 3).rotated(1), Vector2::new(-3, 2));
        assert_eq!(Vector2::new(1, 0).rotated(2), Vector2::new(-1, 0));
        assert_eq!(Vector2::new(0, 1).rotated(2), Vector2::new(0, -1));
        assert_eq!(Vector2::new(2, 3).rotated(2), Vector2::new(-2, -3));
        assert_eq!(Vector2::new(1, 0).rotated(3), Vector2::new(0, -1));
        assert_eq!(Vector2::new(0, 1).rotated(3), Vector2::new(1, 0));
        assert_eq!(Vector2::new(2, 3).rotated(3), Vector2::new(3, -2));
        assert_eq!(Vector2::new(1, 0).rotated(4), Vector2::new(1, 0));
        assert_eq!(Vector2::new(0, 1).rotated(4), Vector2::new(0, 1));
        assert_eq!(Vector2::new(2, 3).rotated(4), Vector2::new(2, 3));
    }
    #[test]
    fn flipped_vector() {
        assert_eq!(Vector2::new(1, 0).x_flipped(), Vector2::new(-1, 0));
        assert_eq!(Vector2::new(0, 1).x_flipped(), Vector2::new(0, 1));
        assert_eq!(Vector2::new(2, 3).x_flipped(), Vector2::new(-2, 3));
        assert_eq!(Vector2::new(1, 0).y_flipped(), Vector2::new(1, 0));
        assert_eq!(Vector2::new(0, 1).y_flipped(), Vector2::new(0, -1));
        assert_eq!(Vector2::new(2, 3).y_flipped(), Vector2::new(2, -3));
    }
    #[test]
    fn flipped() {
        assert!(!Trans::new(false, -3).is_flipped());
        assert!(!Trans::new(false, -2).is_flipped());
        assert!(!Trans::new(false, -1).is_flipped());
        assert!(!Trans::new(false, 0).is_flipped());
        assert!(!Trans::new(false, 1).is_flipped());
        assert!(!Trans::new(false, 2).is_flipped());
        assert!(!Trans::new(false, 3).is_flipped());
        assert!(!Trans::new(false, 4).is_flipped());
        assert!(!Trans::new(false, 5).is_flipped());
        assert!(Trans::new(true, -3).is_flipped());
        assert!(Trans::new(true, -2).is_flipped());
        assert!(Trans::new(true, -1).is_flipped());
        assert!(Trans::new(true, 0).is_flipped());
        assert!(Trans::new(true, 1).is_flipped());
        assert!(Trans::new(true, 2).is_flipped());
        assert!(Trans::new(true, 3).is_flipped());
        assert!(Trans::new(true, 4).is_flipped());
        assert!(Trans::new(true, 5).is_flipped());
    }
    #[test]
    fn rotate_amount() {
        assert_eq!(Trans::new(false, -3).rotation(), 1);
        assert_eq!(Trans::new(false, -2).rotation(), 2);
        assert_eq!(Trans::new(false, -1).rotation(), 3);
        assert_eq!(Trans::new(false, 0).rotation(), 0);
        assert_eq!(Trans::new(false, 1).rotation(), 1);
        assert_eq!(Trans::new(false, 2).rotation(), 2);
        assert_eq!(Trans::new(false, 3).rotation(), 3);
        assert_eq!(Trans::new(false, 4).rotation(), 0);
        assert_eq!(Trans::new(false, 5).rotation(), 1);
    }
    #[test]
    fn flipped_rotate_amount() {
        assert_eq!(Trans::new(true, -3).rotation(), 1);
        assert_eq!(Trans::new(true, -2).rotation(), 2);
        assert_eq!(Trans::new(true, -1).rotation(), 3);
        assert_eq!(Trans::new(true, 0).rotation(), 0);
        assert_eq!(Trans::new(true, 1).rotation(), 1);
        assert_eq!(Trans::new(true, 2).rotation(), 2);
        assert_eq!(Trans::new(true, 3).rotation(), 3);
        assert_eq!(Trans::new(true, 4).rotation(), 0);
        assert_eq!(Trans::new(true, 5).rotation(), 1);
    }
    #[test]
    fn double_x_flip() {
        assert_eq!(Trans::identity(), Trans::identity().x_flipped().x_flipped())
    }
    #[test]
    fn double_y_flip() {
        assert_eq!(Trans::identity(), Trans::identity().y_flipped().y_flipped())
    }
    #[test]
    fn cell_from_position_0() {
        assert_eq!(
            Cell::from_position(Vector2::new(0, 0)),
            Cell(Vector2::new(0, 0), Trans::identity())
        );
    }
    #[test]
    fn cell_from_position_1() {
        assert_eq!(
            Cell::from_position(Vector2::new(1, 0)),
            Cell(Vector2::new(0, 0), Trans::identity().rotated(1))
        );
    }
    #[test]
    fn cell_from_position_2() {
        assert_eq!(
            Cell::from_position(Vector2::new(2, 0)),
            Cell(Vector2::new(0, 0), Trans::identity().x_flipped().rotated(3))
        );
    }
    #[test]
    fn cell_from_position_negative_0() {
        assert_eq!(
            Cell::from_position(Vector2::new(0, -2)),
            Cell(Vector2::new(0, -2), Trans::identity())
        );
    }
    #[test]
    fn cell_into_position_0() {
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().rotated(0)).into_position(),
            Vector2::new(0, 0),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().rotated(1)).into_position(),
            Vector2::new(1, 0),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().rotated(2)).into_position(),
            Vector2::new(1, 1),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().rotated(3)).into_position(),
            Vector2::new(0, 1),
        );
    }
    #[test]
    fn cell_into_position_1() {
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().x_flipped().rotated(0)).into_position(),
            Vector2::new(3, 0),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().x_flipped().rotated(1)).into_position(),
            Vector2::new(3, 1),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().x_flipped().rotated(2)).into_position(),
            Vector2::new(2, 1),
        );
        assert_eq!(
            Cell(Vector2::new(0, 0), Trans::identity().x_flipped().rotated(3)).into_position(),
            Vector2::new(2, 0),
        );
    }
    #[test]
    fn cell_into_position_negative_0() {
        assert_eq!(
            Cell(Vector2::new(0, -2), Trans::identity().rotated(0)).into_position(),
            Vector2::new(0, -2),
        );
        assert_eq!(
            Cell(Vector2::new(0, -2), Trans::identity().rotated(1)).into_position(),
            Vector2::new(1, -2),
        );
        assert_eq!(
            Cell(Vector2::new(0, -2), Trans::identity().rotated(2)).into_position(),
            Vector2::new(1, -1),
        );
        assert_eq!(
            Cell(Vector2::new(0, -2), Trans::identity().rotated(3)).into_position(),
            Vector2::new(0, -1),
        );
    }
    #[test]
    fn cell_uniqueness() {
        let mut set = fxhash::FxHashSet::<Vector2<i32>>::default();
        for t in Trans::all() {
            set.insert(Cell(Vector2::new(0, 0), t).into_position());
        }
        assert_eq!(set.len(), 8, "{set:?}");
    }
    #[test]
    fn cell_correctness() {
        for x in 0..4 {
            for y in 0..2 {
                assert_eq!(
                    Cell::from_position(Vector2::new(x, y)).0,
                    Vector2::new(0, 0),
                    "{x}, {y}"
                );
            }
        }
        for x in 4..8 {
            for y in -2..0 {
                assert_eq!(
                    Cell::from_position(Vector2::new(x, y)).0,
                    Vector2::new(4, -2),
                    "{x}, {y}"
                );
            }
        }
    }
    #[test]
    fn cell_from_position_and_back() {
        for x in -3..4 {
            for y in -2..4 {
                let p = Vector2::new(x, y);
                assert_eq!(
                    Cell::from_position(p).into_position(),
                    p,
                    "Cell: {:?}",
                    Cell::from_position(p)
                );
            }
        }
    }
}