/*
* MIT License
*
* Copyright (c) 2017 Ralph Niemitz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.ralleytn.engine.lunatic.util;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
/**
* Contains utility methods for working with matrices.
* @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
* @version 1.0.0
* @since 1.0.0
*/
public final class MatrixUtil {
/*
* @since 1.0.0
*/
private MatrixUtil() {}
/**
* Converts a 4 by 4 matrix into a {@code float} array.
* @param matrix the matrix
* @return the resulting {@code float} array
* @since 1.0.0
*/
public static final float[] toFloatArray(Matrix4f matrix) {
return new float[] {
matrix.m00, matrix.m01, matrix.m02, matrix.m03,
matrix.m10, matrix.m11, matrix.m12, matrix.m13,
matrix.m20, matrix.m21, matrix.m22, matrix.m23,
matrix.m30, matrix.m31, matrix.m32, matrix.m33,
};
}
/**
* Scales a matrix.
* @param scale the scale
* @param matrix the matrix
* @since 1.0.0
*/
public static final void scale(Vector3f scale, Matrix4f matrix) {
matrix.m00 = matrix.m00 * scale.x;
matrix.m01 = matrix.m01 * scale.x;
matrix.m02 = matrix.m02 * scale.x;
matrix.m03 = matrix.m03 * scale.x;
matrix.m10 = matrix.m10 * scale.y;
matrix.m11 = matrix.m11 * scale.y;
matrix.m12 = matrix.m12 * scale.y;
matrix.m13 = matrix.m13 * scale.y;
matrix.m20 = matrix.m20 * scale.z;
matrix.m21 = matrix.m21 * scale.z;
matrix.m22 = matrix.m22 * scale.z;
matrix.m23 = matrix.m23 * scale.z;
}
/**
* Translates a matrix.
* @param translation the translation
* @param matrix the matrix
* @since 1.0.0
*/
public static final void translate(Vector3f translation, Matrix4f matrix) {
matrix.m30 += matrix.m00 * translation.x + matrix.m10 * translation.y + matrix.m20 * translation.z;
matrix.m31 += matrix.m01 * translation.x + matrix.m11 * translation.y + matrix.m21 * translation.z;
matrix.m32 += matrix.m02 * translation.x + matrix.m12 * translation.y + matrix.m22 * translation.z;
matrix.m33 += matrix.m03 * translation.x + matrix.m13 * translation.y + matrix.m23 * translation.z;
}
/**
* Rotates a matrix.
* @param angle the angle in degrees
* @param axis the axis
* @param matrix the matrix
* @since 1.0.0
*/
public static final void rotate(float angle, Vector3f axis, Matrix4f matrix) {
float c = (float)Math.cos(angle);
float s = (float)Math.sin(angle);
float oneminusc = 1.0F - c;
float xy = axis.x * axis.y;
float yz = axis.y * axis.z;
float xz = axis.x * axis.z;
float xs = axis.x * s;
float ys = axis.y * s;
float zs = axis.z * s;
float f00 = axis.x * axis.x * oneminusc + c;
float f01 = xy * oneminusc + zs;
float f02 = xz * oneminusc - ys;
float f10 = xy * oneminusc - zs;
float f11 = axis.y * axis.y * oneminusc + c;
float f12 = yz * oneminusc + xs;
float f20 = xz * oneminusc + ys;
float f21 = yz * oneminusc - xs;
float f22 = axis.z * axis.z * oneminusc + c;
float t00 = matrix.m00 * f00 + matrix.m10 * f01 + matrix.m20 * f02;
float t01 = matrix.m01 * f00 + matrix.m11 * f01 + matrix.m21 * f02;
float t02 = matrix.m02 * f00 + matrix.m12 * f01 + matrix.m22 * f02;
float t03 = matrix.m03 * f00 + matrix.m13 * f01 + matrix.m23 * f02;
float t10 = matrix.m00 * f10 + matrix.m10 * f11 + matrix.m20 * f12;
float t11 = matrix.m01 * f10 + matrix.m11 * f11 + matrix.m21 * f12;
float t12 = matrix.m02 * f10 + matrix.m12 * f11 + matrix.m22 * f12;
float t13 = matrix.m03 * f10 + matrix.m13 * f11 + matrix.m23 * f12;
matrix.m20 = matrix.m00 * f20 + matrix.m10 * f21 + matrix.m20 * f22;
matrix.m21 = matrix.m01 * f20 + matrix.m11 * f21 + matrix.m21 * f22;
matrix.m22 = matrix.m02 * f20 + matrix.m12 * f21 + matrix.m22 * f22;
matrix.m23 = matrix.m03 * f20 + matrix.m13 * f21 + matrix.m23 * f22;
matrix.m00 = t00;
matrix.m01 = t01;
matrix.m02 = t02;
matrix.m03 = t03;
matrix.m10 = t10;
matrix.m11 = t11;
matrix.m12 = t12;
matrix.m13 = t13;
}
}