Created
May 2, 2020 09:08
-
-
Save kennytv/642d3deb21ca1415d3c450c03b1cb35f to your computer and use it in GitHub Desktop.
Java matrix multiplication and rotation util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.luminu.core.api.shared.util; | |
import com.google.common.base.Preconditions; | |
/** | |
* Methods for multiplying matrices and simple rotation methods. | |
* See https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions for details on rotation. | |
* | |
* @author KennyTV | |
*/ | |
public final class MatrixUtil { | |
private MatrixUtil() { | |
} | |
/** | |
* @param first left matrix | |
* @param second right matrix | |
* @return multiplied matrix | |
*/ | |
public static double[][] multiply(final double[][] first, final double[][] second) { | |
Preconditions.checkArgument(first[0].length == second.length); | |
final double[][] result = new double[first.length][second[0].length]; | |
for (int i = 0; i < result.length; i++) { | |
for (int j = 0; j < result[i].length; j++) { | |
double multiplied = 0; | |
for (int k = 0; k < second.length; k++) { | |
multiplied += first[i][k] * second[k][j]; | |
} | |
result[i][j] = multiplied; | |
} | |
} | |
return result; | |
} | |
/** | |
* @param first left matrix | |
* @param second right nx1 (vertical) matrix | |
* @return nx1 matrix | |
*/ | |
public static double[] multiply(final double[][] first, final double[] second) { | |
Preconditions.checkArgument(first[0].length == second.length); | |
final double[] result = new double[first.length]; | |
for (int i = 0; i < result.length; i++) { | |
double multiplied = 0; | |
for (int k = 0; k < second.length; k++) { | |
multiplied += first[i][k] * second[k]; | |
} | |
result[i] = multiplied; | |
} | |
return result; | |
} | |
/** | |
* @param sin sin of the angle | |
* @param cos cos of the angle | |
* @return x rotation matrix (to be multiplied on the left) | |
*/ | |
public static double[][] getRotationMatrixX(final double sin, final double cos) { | |
return new double[][]{{1, 0, 0}, {0, cos, -sin}, {0, sin, cos}}; | |
} | |
public static double[][] getRotationMatrixX(final double angle) { | |
return getRotationMatrixX(Math.sin(angle), Math.cos(angle)); | |
} | |
/** | |
* @param sin sin of the angle | |
* @param cos cos of the angle | |
* @return y rotation matrix (to be multiplied on the left) | |
*/ | |
public static double[][] getRotationMatrixY(final double sin, final double cos) { | |
return new double[][]{{cos, 0, sin}, {0, 1, 0}, {-sin, 0, cos}}; | |
} | |
public static double[][] getRotationMatrixY(final double angle) { | |
return getRotationMatrixY(Math.sin(angle), Math.cos(angle)); | |
} | |
/** | |
* @param sin sin of the angle | |
* @param cos cos of the angle | |
* @return z rotation matrix (to be multiplied on the left) | |
*/ | |
public static double[][] getRotationMatrixZ(final double sin, final double cos) { | |
return new double[][]{{cos, -sin, 0}, {sin, cos, 0}, {0, 0, 1}}; | |
} | |
public static double[][] getRotationMatrixZ(final double angle) { | |
return getRotationMatrixZ(Math.sin(angle), Math.cos(angle)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment