| 1 | package com.mojang.math;
|
| 2 |
|
| 3 | import java.util.Arrays;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.stream.Collectors;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.FrontAndTop;
|
| 8 | import net.minecraft.util.StringRepresentable;
|
| 9 | import net.minecraft.util.Util;
|
| 10 | import org.joml.Matrix3f;
|
| 11 | import org.joml.Matrix3fc;
|
| 12 | import org.joml.Vector3i;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public enum OctahedralGroup implements StringRepresentable {
|
| 16 | IDENTITY("identity", SymmetricGroup3.P123, false, false, false),
|
| 17 | ROT_180_FACE_XY("rot_180_face_xy", SymmetricGroup3.P123, true, true, false),
|
| 18 | ROT_180_FACE_XZ("rot_180_face_xz", SymmetricGroup3.P123, true, false, true),
|
| 19 | ROT_180_FACE_YZ("rot_180_face_yz", SymmetricGroup3.P123, false, true, true),
|
| 20 | ROT_120_NNN("rot_120_nnn", SymmetricGroup3.P231, false, false, false),
|
| 21 | ROT_120_NNP("rot_120_nnp", SymmetricGroup3.P312, true, false, true),
|
| 22 | ROT_120_NPN("rot_120_npn", SymmetricGroup3.P312, false, true, true),
|
| 23 | ROT_120_NPP("rot_120_npp", SymmetricGroup3.P231, true, false, true),
|
| 24 | ROT_120_PNN("rot_120_pnn", SymmetricGroup3.P312, true, true, false),
|
| 25 | ROT_120_PNP("rot_120_pnp", SymmetricGroup3.P231, true, true, false),
|
| 26 | ROT_120_PPN("rot_120_ppn", SymmetricGroup3.P231, false, true, true),
|
| 27 | ROT_120_PPP("rot_120_ppp", SymmetricGroup3.P312, false, false, false),
|
| 28 | ROT_180_EDGE_XY_NEG("rot_180_edge_xy_neg", SymmetricGroup3.P213, true, true, true),
|
| 29 | ROT_180_EDGE_XY_POS("rot_180_edge_xy_pos", SymmetricGroup3.P213, false, false, true),
|
| 30 | ROT_180_EDGE_XZ_NEG("rot_180_edge_xz_neg", SymmetricGroup3.P321, true, true, true),
|
| 31 | ROT_180_EDGE_XZ_POS("rot_180_edge_xz_pos", SymmetricGroup3.P321, false, true, false),
|
| 32 | ROT_180_EDGE_YZ_NEG("rot_180_edge_yz_neg", SymmetricGroup3.P132, true, true, true),
|
| 33 | ROT_180_EDGE_YZ_POS("rot_180_edge_yz_pos", SymmetricGroup3.P132, true, false, false),
|
| 34 | ROT_90_X_NEG("rot_90_x_neg", SymmetricGroup3.P132, false, false, true),
|
| 35 | ROT_90_X_POS("rot_90_x_pos", SymmetricGroup3.P132, false, true, false),
|
| 36 | ROT_90_Y_NEG("rot_90_y_neg", SymmetricGroup3.P321, true, false, false),
|
| 37 | ROT_90_Y_POS("rot_90_y_pos", SymmetricGroup3.P321, false, false, true),
|
| 38 | ROT_90_Z_NEG("rot_90_z_neg", SymmetricGroup3.P213, false, true, false),
|
| 39 | ROT_90_Z_POS("rot_90_z_pos", SymmetricGroup3.P213, true, false, false),
|
| 40 | INVERSION("inversion", SymmetricGroup3.P123, true, true, true),
|
| 41 | INVERT_X("invert_x", SymmetricGroup3.P123, true, false, false),
|
| 42 | INVERT_Y("invert_y", SymmetricGroup3.P123, false, true, false),
|
| 43 | INVERT_Z("invert_z", SymmetricGroup3.P123, false, false, true),
|
| 44 | ROT_60_REF_NNN("rot_60_ref_nnn", SymmetricGroup3.P312, true, true, true),
|
| 45 | ROT_60_REF_NNP("rot_60_ref_nnp", SymmetricGroup3.P231, true, false, false),
|
| 46 | ROT_60_REF_NPN("rot_60_ref_npn", SymmetricGroup3.P231, false, false, true),
|
| 47 | ROT_60_REF_NPP("rot_60_ref_npp", SymmetricGroup3.P312, false, false, true),
|
| 48 | ROT_60_REF_PNN("rot_60_ref_pnn", SymmetricGroup3.P231, false, true, false),
|
| 49 | ROT_60_REF_PNP("rot_60_ref_pnp", SymmetricGroup3.P312, true, false, false),
|
| 50 | ROT_60_REF_PPN("rot_60_ref_ppn", SymmetricGroup3.P312, false, true, false),
|
| 51 | ROT_60_REF_PPP("rot_60_ref_ppp", SymmetricGroup3.P231, true, true, true),
|
| 52 | SWAP_XY("swap_xy", SymmetricGroup3.P213, false, false, false),
|
| 53 | SWAP_YZ("swap_yz", SymmetricGroup3.P132, false, false, false),
|
| 54 | SWAP_XZ("swap_xz", SymmetricGroup3.P321, false, false, false),
|
| 55 | SWAP_NEG_XY("swap_neg_xy", SymmetricGroup3.P213, true, true, false),
|
| 56 | SWAP_NEG_YZ("swap_neg_yz", SymmetricGroup3.P132, false, true, true),
|
| 57 | SWAP_NEG_XZ("swap_neg_xz", SymmetricGroup3.P321, true, false, true),
|
| 58 | ROT_90_REF_X_NEG("rot_90_ref_x_neg", SymmetricGroup3.P132, true, false, true),
|
| 59 | ROT_90_REF_X_POS("rot_90_ref_x_pos", SymmetricGroup3.P132, true, true, false),
|
| 60 | ROT_90_REF_Y_NEG("rot_90_ref_y_neg", SymmetricGroup3.P321, true, true, false),
|
| 61 | ROT_90_REF_Y_POS("rot_90_ref_y_pos", SymmetricGroup3.P321, false, true, true),
|
| 62 | ROT_90_REF_Z_NEG("rot_90_ref_z_neg", SymmetricGroup3.P213, false, true, true),
|
| 63 | ROT_90_REF_Z_POS("rot_90_ref_z_pos", SymmetricGroup3.P213, true, false, true);
|
| 64 |
|
| 65 | public static final OctahedralGroup BLOCK_ROT_X_270 = ROT_90_X_POS;
|
| 66 | public static final OctahedralGroup BLOCK_ROT_X_180 = ROT_180_FACE_YZ;
|
| 67 | public static final OctahedralGroup BLOCK_ROT_X_90 = ROT_90_X_NEG;
|
| 68 | public static final OctahedralGroup BLOCK_ROT_Y_270 = ROT_90_Y_POS;
|
| 69 | public static final OctahedralGroup BLOCK_ROT_Y_180 = ROT_180_FACE_XZ;
|
| 70 | public static final OctahedralGroup BLOCK_ROT_Y_90 = ROT_90_Y_NEG;
|
| 71 | public static final OctahedralGroup BLOCK_ROT_Z_270 = ROT_90_Z_POS;
|
| 72 | public static final OctahedralGroup BLOCK_ROT_Z_180 = ROT_180_FACE_XY;
|
| 73 | public static final OctahedralGroup BLOCK_ROT_Z_90 = ROT_90_Z_NEG;
|
| 74 | private final Matrix3fc transformation;
|
| 75 | private final String name;
|
| 76 | private @Nullable Map<Direction, Direction> rotatedDirections;
|
| 77 | private final boolean invertX;
|
| 78 | private final boolean invertY;
|
| 79 | private final boolean invertZ;
|
| 80 | private final SymmetricGroup3 permutation;
|
| 81 | private static final OctahedralGroup[][] CAYLEY_TABLE = Util.make(() -> {
|
| 82 | OctahedralGroup[] values = values();
|
| 83 | OctahedralGroup[][] table = new OctahedralGroup[values.length][values.length];
|
| 84 | Map<Integer, OctahedralGroup> fingerprints = Arrays.stream(values).collect(Collectors.toMap(OctahedralGroup::trace, o -> (OctahedralGroup)o));
|
| 85 |
|
| 86 | for (OctahedralGroup first : values) {
|
| 87 | for (OctahedralGroup second : values) {
|
| 88 | SymmetricGroup3 composedPermutation = second.permutation.compose(first.permutation);
|
| 89 | boolean composedInvertX = first.inverts(Direction.Axis.X) ^ second.inverts(first.permutation.permuteAxis(Direction.Axis.X));
|
| 90 | boolean composedInvertY = first.inverts(Direction.Axis.Y) ^ second.inverts(first.permutation.permuteAxis(Direction.Axis.Y));
|
| 91 | boolean composedInvertZ = first.inverts(Direction.Axis.Z) ^ second.inverts(first.permutation.permuteAxis(Direction.Axis.Z));
|
| 92 | table[first.ordinal()][second.ordinal()] = fingerprints.get(trace(composedInvertX, composedInvertY, composedInvertZ, composedPermutation));
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | return table;
|
| 97 | });
|
| 98 | private static final OctahedralGroup[] INVERSE_TABLE = Arrays.stream(values())
|
| 99 | .map(f -> Arrays.stream(values()).filter(s -> f.compose(s) == IDENTITY).findAny().get())
|
| 100 | .toArray(OctahedralGroup[]::new);
|
| 101 |
|
| 102 | OctahedralGroup(final String name, final SymmetricGroup3 permutation, final boolean invertX, final boolean invertY, final boolean invertZ) {
|
| 103 | this.name = name;
|
| 104 | this.invertX = invertX;
|
| 105 | this.invertY = invertY;
|
| 106 | this.invertZ = invertZ;
|
| 107 | this.permutation = permutation;
|
| 108 | this.transformation = new Matrix3f().scaling(invertX ? -1.0F : 1.0F, invertY ? -1.0F : 1.0F, invertZ ? -1.0F : 1.0F).mul(permutation.transformation());
|
| 109 | }
|
| 110 |
|
| 111 | private static int trace(final boolean invertX, final boolean invertY, final boolean invertZ, final SymmetricGroup3 permutation) {
|
| 112 | int inversionIndex = (invertZ ? 4 : 0) + (invertY ? 2 : 0) + (invertX ? 1 : 0);
|
| 113 | return permutation.ordinal() << 3 | inversionIndex;
|
| 114 | }
|
| 115 |
|
| 116 | private int trace() {
|
| 117 | return trace(this.invertX, this.invertY, this.invertZ, this.permutation);
|
| 118 | }
|
| 119 |
|
| 120 | public OctahedralGroup compose(final OctahedralGroup that) {
|
| 121 | return CAYLEY_TABLE[this.ordinal()][that.ordinal()];
|
| 122 | }
|
| 123 |
|
| 124 | public OctahedralGroup inverse() {
|
| 125 | return INVERSE_TABLE[this.ordinal()];
|
| 126 | }
|
| 127 |
|
| 128 | public Matrix3fc transformation() {
|
| 129 | return this.transformation;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public String toString() {
|
| 134 | return this.name;
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public String getSerializedName() {
|
| 139 | return this.name;
|
| 140 | }
|
| 141 |
|
| 142 | public Direction rotate(final Direction direction) {
|
| 143 | if (this.rotatedDirections == null) {
|
| 144 | this.rotatedDirections = Util.makeEnumMap(Direction.class, facing -> {
|
| 145 | Direction.Axis oldAxis = facing.getAxis();
|
| 146 | Direction.AxisDirection oldDirection = facing.getAxisDirection();
|
| 147 | Direction.Axis newAxis = this.permutation.inverse().permuteAxis(oldAxis);
|
| 148 | Direction.AxisDirection newDirection = this.inverts(newAxis) ? oldDirection.opposite() : oldDirection;
|
| 149 | return Direction.fromAxisAndDirection(newAxis, newDirection);
|
| 150 | });
|
| 151 | }
|
| 152 |
|
| 153 | return this.rotatedDirections.get(direction);
|
| 154 | }
|
| 155 |
|
| 156 | public Vector3i rotate(final Vector3i v) {
|
| 157 | this.permutation.permuteVector(v);
|
| 158 | v.x = v.x * (this.invertX ? -1 : 1);
|
| 159 | v.y = v.y * (this.invertY ? -1 : 1);
|
| 160 | v.z = v.z * (this.invertZ ? -1 : 1);
|
| 161 | return v;
|
| 162 | }
|
| 163 |
|
| 164 | public boolean inverts(final Direction.Axis axis) {
|
| 165 | return switch (axis) {
|
| 166 | case X -> this.invertX;
|
| 167 | case Y -> this.invertY;
|
| 168 | case Z -> this.invertZ;
|
| 169 | };
|
| 170 | }
|
| 171 |
|
| 172 | public SymmetricGroup3 permutation() {
|
| 173 | return this.permutation;
|
| 174 | }
|
| 175 |
|
| 176 | public FrontAndTop rotate(final FrontAndTop input) {
|
| 177 | return FrontAndTop.fromFrontAndTop(this.rotate(input.front()), this.rotate(input.top()));
|
| 178 | }
|
| 179 | }
|
| 180 |
|