src/toxi/geom/Matrix4x4.java in toxiclibs-0.9.1 vs src/toxi/geom/Matrix4x4.java in toxiclibs-0.9.2
- old
+ new
@@ -1,32 +1,31 @@
/*
- * __ .__ .__ ._____.
+ * __ .__ .__ ._____.
* _/ |_ _______ __|__| ____ | | |__\_ |__ ______
* \ __\/ _ \ \/ / |/ ___\| | | || __ \ / ___/
- * | | ( <_> > <| \ \___| |_| || \_\ \\___ \
+ * | | ( <_> > <| \ \___| |_| || \_\ \\___ \
* |__| \____/__/\_ \__|\___ >____/__||___ /____ >
- * \/ \/ \/ \/
+ * \/ \/ \/ \/
*
* Copyright (c) 2006-2011 Karsten Schmidt
- *
+ *
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
- *
+ *
* http://creativecommons.org/licenses/LGPL/2.1/
- *
+ *
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
- *
+ *
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
-
package toxi.geom;
import toxi.math.MathUtils;
/**
@@ -44,17 +43,17 @@
* are "matrix0" and "dimen". The array "matrix0" is also an output
* parameter. The vector "row_perm[4]" is an output parameter that contains
* the row permutations resulting from partial pivoting. The output
* parameter "even_row_xchg" is 1 when the number of row exchanges is even,
* or -1 otherwise. Assumes data type is always double.
- *
+ *
* This function is similar to luDecomposition, except that it is tuned
* specifically for 4x4 matrices.
- *
+ *
* Reference: Press, Flannery, Teukolsky, Vetterling,
* _Numerical_Recipes_in_C_, Cambridge University Press, 1988, pp 40-45.
- *
+ *
* @param matrix0
* @param row_perm
* @param width
* @return true if the matrix is nonsingular, or false otherwise.
*/
@@ -251,11 +250,11 @@
}
/**
* Initialising constructor from a 1d array. Assumes row-major ordering
* (column index increases faster).
- *
+ *
* @param array
*/
public Matrix4x4(double[] array) {
if (array.length != 9 && array.length != 16) {
throw new RuntimeException("Array.length must == 9 or 16");
@@ -344,11 +343,11 @@
return this;
}
/**
* Creates a copy of the given vector, transformed by this matrix.
- *
+ *
* @param v
* @return transformed vector
*/
public Vec3D applyTo(ReadonlyVec3D v) {
return applyToSelf(new Vec3D(v));
@@ -450,20 +449,20 @@
matrix[3][3] = 1;
return this;
}
private void init() {
- matrix = new double[][] {
- new double[4], new double[4], new double[4], new double[4]
+ matrix = new double[][]{
+ new double[4], new double[4], new double[4], new double[4]
};
}
/**
* Matrix Inversion using Cramer's Method Computes Adjoint matrix divided by
* determinant Code modified from
* http://www.intel.com/design/pentiumiii/sml/24504301.pdf
- *
+ *
* @return itself
*/
public Matrix4x4 invert() {
final double[] tmp = new double[12];
final double[] src = new double[16];
@@ -583,34 +582,33 @@
Vec3D s = up.cross(f).normalize();
Vec3D t = f.cross(s).normalize();
return set(s.x, s.y, s.z, -s.dot(eye), t.x, t.y, t.z, -t.dot(eye), f.x,
f.y, f.z, -f.dot(eye), 0, 0, 0, 1);
}
-
+
/**
- *
+ *
* @param factor
- * @return
+ * @return
*/
-
public Matrix4x4 multiply(double factor) {
return new Matrix4x4(this).multiplySelf(factor);
}
/**
* Matrix-Matrix Right-multiplication.
- *
+ *
* @param mat
* @return product as new matrix
*/
public Matrix4x4 multiply(Matrix4x4 mat) {
return new Matrix4x4(this).multiplySelf(mat);
}
/**
* In-place matrix-scalar multiplication.
- *
+ *
* @param factor
* @return product applied to this matrix.
*/
public Matrix4x4 multiplySelf(double factor) {
for (int i = 0; i < 4; i++) {
@@ -647,11 +645,11 @@
return this;
}
/**
* Applies rotation about arbitrary axis to matrix
- *
+ *
* @param axis
* @param theta
* @return rotation applied to this matrix
*/
public Matrix4x4 rotateAroundAxis(ReadonlyVec3D axis, double theta) {
@@ -670,13 +668,12 @@
return this.multiplySelf(TEMP);
}
/**
* Applies rotation about X to this matrix.
- *
- * @param theta
- * rotation angle in radians
+ *
+ * @param theta rotation angle in radians
* @return itself
*/
public Matrix4x4 rotateX(double theta) {
TEMP.identity();
TEMP.matrix[1][1] = TEMP.matrix[2][2] = Math.cos(theta);
@@ -685,13 +682,12 @@
return this.multiplySelf(TEMP);
}
/**
* Applies rotation about Y to this matrix.
- *
- * @param theta
- * rotation angle in radians
+ *
+ * @param theta rotation angle in radians
* @return itself
*/
public Matrix4x4 rotateY(double theta) {
TEMP.identity();
TEMP.matrix[0][0] = TEMP.matrix[2][2] = Math.cos(theta);
@@ -699,17 +695,16 @@
TEMP.matrix[2][0] = -TEMP.matrix[0][2];
return this.multiplySelf(TEMP);
}
// Apply Rotation about Z to Matrix
-
/**
*
* @param theta
* @return
*/
- public Matrix4x4 rotateZ(double theta) {
+ public Matrix4x4 rotateZ(double theta) {
TEMP.identity();
TEMP.matrix[0][0] = TEMP.matrix[1][1] = Math.cos(theta);
TEMP.matrix[1][0] = Math.sin(theta);
TEMP.matrix[0][1] = -TEMP.matrix[1][0];
return this.multiplySelf(TEMP);
@@ -851,11 +846,11 @@
public Matrix4x4 setFrustum(double left, double right, double top,
double bottom, double near, double far) {
return set((2.0 * near) / (right - left), 0, (left + right)
/ (right - left), 0, 0, (2.0 * near) / (top - bottom),
(top + bottom) / (top - bottom), 0, 0, 0, -(near + far)
- / (far - near), (-2 * near * far) / (far - near), 0, 0,
+ / (far - near), (-2 * near * far) / (far - near), 0, 0,
-1, 0);
}
/**
*
@@ -943,13 +938,12 @@
return this;
}
/**
* Copies all matrix elements into an linear array.
- *
- * @param result
- * array (or null to create a new one)
+ *
+ * @param result array (or null to create a new one)
* @return matrix as 16 element array
*/
public double[] toArray(double[] result) {
if (result == null) {
result = new double[16];
@@ -979,28 +973,29 @@
return result;
}
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#toString()
*/
-
/**
*
* @return
*/
-
@Override
public String toString() {
- return "| " + matrix[0][0] + " " + matrix[0][1] + " " + matrix[0][2]
- + " " + matrix[0][3] + " |\n" + "| " + matrix[1][0] + " "
- + matrix[1][1] + " " + matrix[1][2] + " " + matrix[1][3]
- + " |\n" + "| " + matrix[2][0] + " " + matrix[2][1] + " "
- + matrix[2][2] + " " + matrix[2][3] + " |\n" + "| "
- + matrix[3][0] + " " + matrix[3][1] + " " + matrix[3][2] + " "
- + matrix[3][3] + " |";
+ return String.join("\n",
+ String.format("| %f %f %f %f |",
+ matrix[0][0], matrix[0][1], matrix[0][2], matrix[0][3]),
+ String.format("| %f %f %f %f |",
+ matrix[1][0], matrix[1][1], matrix[1][2], matrix[1][3]),
+ String.format("| %f %f %f %f |",
+ matrix[2][0], matrix[2][1], matrix[2][2], matrix[2][3]),
+ String.format("| %f %f %f %f |",
+ matrix[3][0], matrix[3][1], matrix[3][2], matrix[3][3])
+ );
}
/**
*
* @param result
@@ -1062,15 +1057,15 @@
}
/**
* Converts the matrix (in-place) between column-major to row-major order
* (and vice versa).
- *
+ *
* @return itself
*/
public Matrix4x4 transpose() {
return set(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
}
-}
\ No newline at end of file
+}