Make AffineTransform a record for computed constant optimisations (#2419)

* Make AffineTransform a record for computed constant optimisations

* Fix checkstyle

* based on
This commit is contained in:
Maddy Miller 2023-10-18 20:23:52 +10:00 committed by GitHub
parent eadbdeb6a4
commit 1f9316d11c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 73 deletions

View File

@ -217,5 +217,14 @@
"METHOD_NEW_DEFAULT" "METHOD_NEW_DEFAULT"
] ]
} }
],
"Should not have been extended anyway": [
{
"type": "com.sk89q.worldedit.math.transform.AffineTransform",
"member": "Class com.sk89q.worldedit.math.transform.AffineTransform",
"changes": [
"CLASS_NOW_FINAL"
]
}
] ]
} }

View File

@ -26,35 +26,18 @@
/** /**
* An affine transform. * An affine transform.
* *
* <p>This class is from the * <p>This class is based on the one from the
* <a href="http://geom-java.sourceforge.net/index.html">JavaGeom project</a>, * <a href="http://geom-java.sourceforge.net/index.html">JavaGeom project</a>,
* which is licensed under LGPL v2.1.</p> * which is licensed under LGPL v2.1.</p>
*/ */
public class AffineTransform implements Transform { public record AffineTransform(
// coefficients for x coordinate.
/** double m00, double m01, double m02, double m03,
* coefficients for x coordinate. // coefficients for y coordinate.
*/ double m10, double m11, double m12, double m13,
private final double m00; // coefficients for z coordinate.
private final double m01; double m20, double m21, double m22, double m23
private final double m02; ) implements Transform {
private final double m03;
/**
* coefficients for y coordinate.
*/
private final double m10;
private final double m11;
private final double m12;
private final double m13;
/**
* coefficients for z coordinate.
*/
private final double m20;
private final double m21;
private final double m22;
private final double m23;
// =================================================================== // ===================================================================
// constructors // constructors
@ -64,58 +47,42 @@ public class AffineTransform implements Transform {
*/ */
public AffineTransform() { public AffineTransform() {
// init to identity matrix // init to identity matrix
m00 = m11 = m22 = 1; this(
m01 = m02 = m03 = 0; 1, 0, 0, 0,
m10 = m12 = m13 = 0; 0, 1, 0, 0,
m20 = m21 = m23 = 0; 0, 0, 1, 0
);
} }
public AffineTransform(double[] coefs) { private static double indexCoeffs(double[] coeffs, int destIndex) {
if (coefs.length == 9) { if (coeffs.length == 12) {
m00 = coefs[0]; return coeffs[destIndex];
m01 = coefs[1];
m02 = coefs[2];
m10 = coefs[3];
m11 = coefs[4];
m12 = coefs[5];
m20 = coefs[6];
m21 = coefs[7];
m22 = coefs[8];
m03 = m13 = m23 = 0;
} else if (coefs.length == 12) {
m00 = coefs[0];
m01 = coefs[1];
m02 = coefs[2];
m03 = coefs[3];
m10 = coefs[4];
m11 = coefs[5];
m12 = coefs[6];
m13 = coefs[7];
m20 = coefs[8];
m21 = coefs[9];
m22 = coefs[10];
m23 = coefs[11];
} else {
throw new IllegalArgumentException(
"Input array must have 9 or 12 elements");
} }
if (coeffs.length != 9) {
throw new IllegalArgumentException("Input array must have 9 or 12 elements");
}
if (destIndex > 0 && destIndex % 3 == 0) {
// Length 9 has holes in m03, m13, m23
return 0;
}
// Adjust for missing holes
destIndex -= destIndex / 3;
return coeffs[destIndex];
} }
public AffineTransform(double xx, double yx, double zx, double tx, /**
double xy, double yy, double zy, double ty, double xz, double yz, * Creates a new affine transform from the given coefficients.
double zz, double tz) { *
m00 = xx; * @param coefs array of 9 to 12 coefficients
m01 = yx; * @deprecated Use {@link #AffineTransform(double, double, double, double, double, double, double, double, double, double, double, double)} instead
m02 = zx; */
m03 = tx; @Deprecated
m10 = xy; public AffineTransform(double[] coefs) {
m11 = yy; this(
m12 = zy; indexCoeffs(coefs, 0), indexCoeffs(coefs, 1), indexCoeffs(coefs, 2), indexCoeffs(coefs, 3),
m13 = ty; indexCoeffs(coefs, 4), indexCoeffs(coefs, 5), indexCoeffs(coefs, 6), indexCoeffs(coefs, 7),
m20 = xz; indexCoeffs(coefs, 8), indexCoeffs(coefs, 9), indexCoeffs(coefs, 10), indexCoeffs(coefs, 11)
m21 = yz; );
m22 = zz;
m23 = tz;
} }
// =================================================================== // ===================================================================