2025-11-24 22:52:51 +03:00

169 lines
6.4 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.datafixers.kinds.App
* com.mojang.datafixers.kinds.Applicative
* com.mojang.serialization.Codec
* com.mojang.serialization.codecs.RecordCodecBuilder
* org.apache.commons.lang3.tuple.Triple
* org.joml.Matrix3f
* org.joml.Matrix4f
* org.joml.Matrix4fc
* org.joml.Quaternionf
* org.joml.Quaternionfc
* org.joml.Vector3f
* org.joml.Vector3fc
* org.jspecify.annotations.Nullable
*/
package com.mojang.math;
import com.mojang.datafixers.kinds.App;
import com.mojang.datafixers.kinds.Applicative;
import com.mojang.math.MatrixUtil;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Objects;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.util.Util;
import org.apache.commons.lang3.tuple.Triple;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.joml.Quaternionf;
import org.joml.Quaternionfc;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.jspecify.annotations.Nullable;
public final class Transformation {
private final Matrix4fc matrix;
public static final Codec<Transformation> CODEC = RecordCodecBuilder.create(i -> i.group((App)ExtraCodecs.VECTOR3F.fieldOf("translation").forGetter(e -> e.translation), (App)ExtraCodecs.QUATERNIONF.fieldOf("left_rotation").forGetter(e -> e.leftRotation), (App)ExtraCodecs.VECTOR3F.fieldOf("scale").forGetter(e -> e.scale), (App)ExtraCodecs.QUATERNIONF.fieldOf("right_rotation").forGetter(e -> e.rightRotation)).apply((Applicative)i, Transformation::new));
public static final Codec<Transformation> EXTENDED_CODEC = Codec.withAlternative(CODEC, (Codec)ExtraCodecs.MATRIX4F.xmap(Transformation::new, Transformation::getMatrix));
private boolean decomposed;
private @Nullable Vector3fc translation;
private @Nullable Quaternionfc leftRotation;
private @Nullable Vector3fc scale;
private @Nullable Quaternionfc rightRotation;
private static final Transformation IDENTITY = Util.make(() -> {
Transformation identity = new Transformation((Matrix4fc)new Matrix4f());
identity.translation = new Vector3f();
identity.leftRotation = new Quaternionf();
identity.scale = new Vector3f(1.0f, 1.0f, 1.0f);
identity.rightRotation = new Quaternionf();
identity.decomposed = true;
return identity;
});
public Transformation(@Nullable Matrix4fc matrix) {
this.matrix = matrix == null ? new Matrix4f() : matrix;
}
public Transformation(@Nullable Vector3fc translation, @Nullable Quaternionfc leftRotation, @Nullable Vector3fc scale, @Nullable Quaternionfc rightRotation) {
this.matrix = Transformation.compose(translation, leftRotation, scale, rightRotation);
this.translation = translation != null ? translation : new Vector3f();
this.leftRotation = leftRotation != null ? leftRotation : new Quaternionf();
this.scale = scale != null ? scale : new Vector3f(1.0f, 1.0f, 1.0f);
this.rightRotation = rightRotation != null ? rightRotation : new Quaternionf();
this.decomposed = true;
}
public static Transformation identity() {
return IDENTITY;
}
public Transformation compose(Transformation that) {
Matrix4f matrix = this.getMatrixCopy();
matrix.mul(that.getMatrix());
return new Transformation((Matrix4fc)matrix);
}
public @Nullable Transformation inverse() {
if (this == IDENTITY) {
return this;
}
Matrix4f matrix = this.getMatrixCopy().invertAffine();
if (matrix.isFinite()) {
return new Transformation((Matrix4fc)matrix);
}
return null;
}
private void ensureDecomposed() {
if (!this.decomposed) {
float scaleFactor = 1.0f / this.matrix.m33();
Triple<Quaternionf, Vector3f, Quaternionf> triple = MatrixUtil.svdDecompose(new Matrix3f(this.matrix).scale(scaleFactor));
this.translation = this.matrix.getTranslation(new Vector3f()).mul(scaleFactor);
this.leftRotation = new Quaternionf((Quaternionfc)triple.getLeft());
this.scale = new Vector3f((Vector3fc)triple.getMiddle());
this.rightRotation = new Quaternionf((Quaternionfc)triple.getRight());
this.decomposed = true;
}
}
private static Matrix4f compose(@Nullable Vector3fc translation, @Nullable Quaternionfc leftRotation, @Nullable Vector3fc scale, @Nullable Quaternionfc rightRotation) {
Matrix4f result = new Matrix4f();
if (translation != null) {
result.translation(translation);
}
if (leftRotation != null) {
result.rotate(leftRotation);
}
if (scale != null) {
result.scale(scale);
}
if (rightRotation != null) {
result.rotate(rightRotation);
}
return result;
}
public Matrix4fc getMatrix() {
return this.matrix;
}
public Matrix4f getMatrixCopy() {
return new Matrix4f(this.matrix);
}
public Vector3fc getTranslation() {
this.ensureDecomposed();
return this.translation;
}
public Quaternionfc getLeftRotation() {
this.ensureDecomposed();
return this.leftRotation;
}
public Vector3fc getScale() {
this.ensureDecomposed();
return this.scale;
}
public Quaternionfc getRightRotation() {
this.ensureDecomposed();
return this.rightRotation;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
Transformation that = (Transformation)o;
return Objects.equals(this.matrix, that.matrix);
}
public int hashCode() {
return Objects.hash(this.matrix);
}
public Transformation slerp(Transformation that, float progress) {
return new Transformation((Vector3fc)this.getTranslation().lerp(that.getTranslation(), progress, new Vector3f()), (Quaternionfc)this.getLeftRotation().slerp(that.getLeftRotation(), progress, new Quaternionf()), (Vector3fc)this.getScale().lerp(that.getScale(), progress, new Vector3f()), (Quaternionfc)this.getRightRotation().slerp(that.getRightRotation(), progress, new Quaternionf()));
}
}