📄 matrix4f.java
字号:
/*
* Copyright (c) 2003-2009 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme.math;
import java.io.IOException;
import java.io.Serializable;
import java.nio.FloatBuffer;
import java.util.logging.Logger;
import com.jme.system.JmeException;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
import com.jme.util.geom.BufferUtils;
/**
* <code>Matrix4f</code> defines and maintains a 4x4 matrix in row major order.
* This matrix is intended for use in a translation and rotational capacity.
* It provides convenience methods for creating the matrix from a multitude
* of sources.
*
* Matrices are stored assuming column vectors on the right, with the translation
* in the rightmost column. Element numbering is row,column, so m03 is the zeroth
* row, third column, which is the "x" translation part. This means that the implicit
* storage order is column major. However, the get() and set() functions on float
* arrays default to row major order!
*
* @author Mark Powell
* @author Joshua Slack
*/
public class Matrix4f implements Serializable, Savable, Cloneable {
private static final Logger logger = Logger.getLogger(Matrix4f.class.getName());
private static final long serialVersionUID = 1L;
public float m00, m01, m02, m03;
public float m10, m11, m12, m13;
public float m20, m21, m22, m23;
public float m30, m31, m32, m33;
/**
* Constructor instantiates a new <code>Matrix</code> that is set to the
* identity matrix.
*
*/
public Matrix4f() {
loadIdentity();
}
/**
* constructs a matrix with the given values.
*/
public Matrix4f(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33) {
this.m00 = m00;
this.m01 = m01;
this.m02 = m02;
this.m03 = m03;
this.m10 = m10;
this.m11 = m11;
this.m12 = m12;
this.m13 = m13;
this.m20 = m20;
this.m21 = m21;
this.m22 = m22;
this.m23 = m23;
this.m30 = m30;
this.m31 = m31;
this.m32 = m32;
this.m33 = m33;
}
/**
* Create a new Matrix4f, given data in column-major format.
*
* @param array
* An array of 16 floats in column-major format (translation in elements 12, 13 and 14).
*/
public Matrix4f(float[] array) {
set(array, false);
}
/**
* Constructor instantiates a new <code>Matrix</code> that is set to the
* provided matrix. This constructor copies a given Matrix. If the provided
* matrix is null, the constructor sets the matrix to the identity.
*
* @param mat
* the matrix to copy.
*/
public Matrix4f(Matrix4f mat) {
copy(mat);
}
/**
* <code>copy</code> transfers the contents of a given matrix to this
* matrix. If a null matrix is supplied, this matrix is set to the identity
* matrix.
*
* @param matrix
* the matrix to copy.
*/
public void copy(Matrix4f matrix) {
if (null == matrix) {
loadIdentity();
} else {
m00 = matrix.m00;
m01 = matrix.m01;
m02 = matrix.m02;
m03 = matrix.m03;
m10 = matrix.m10;
m11 = matrix.m11;
m12 = matrix.m12;
m13 = matrix.m13;
m20 = matrix.m20;
m21 = matrix.m21;
m22 = matrix.m22;
m23 = matrix.m23;
m30 = matrix.m30;
m31 = matrix.m31;
m32 = matrix.m32;
m33 = matrix.m33;
}
}
/**
* <code>get</code> retrieves the values of this object into
* a float array in row-major order.
*
* @param matrix
* the matrix to set the values into.
*/
public void get(float[] matrix) {
get(matrix, true);
}
/**
* <code>set</code> retrieves the values of this object into
* a float array.
*
* @param matrix
* the matrix to set the values into.
* @param rowMajor
* whether the outgoing data is in row or column major order.
*/
public void get(float[] matrix, boolean rowMajor) {
if (matrix.length != 16) throw new JmeException(
"Array must be of size 16.");
if (rowMajor) {
matrix[0] = m00;
matrix[1] = m01;
matrix[2] = m02;
matrix[3] = m03;
matrix[4] = m10;
matrix[5] = m11;
matrix[6] = m12;
matrix[7] = m13;
matrix[8] = m20;
matrix[9] = m21;
matrix[10] = m22;
matrix[11] = m23;
matrix[12] = m30;
matrix[13] = m31;
matrix[14] = m32;
matrix[15] = m33;
} else {
matrix[0] = m00;
matrix[4] = m01;
matrix[8] = m02;
matrix[12] = m03;
matrix[1] = m10;
matrix[5] = m11;
matrix[9] = m12;
matrix[13] = m13;
matrix[2] = m20;
matrix[6] = m21;
matrix[10] = m22;
matrix[14] = m23;
matrix[3] = m30;
matrix[7] = m31;
matrix[11] = m32;
matrix[15] = m33;
}
}
/**
* <code>get</code> retrieves a value from the matrix at the given
* position. If the position is invalid a <code>JmeException</code> is
* thrown.
*
* @param i
* the row index.
* @param j
* the colum index.
* @return the value at (i, j).
*/
public float get(int i, int j) {
switch (i) {
case 0:
switch (j) {
case 0: return m00;
case 1: return m01;
case 2: return m02;
case 3: return m03;
}
case 1:
switch (j) {
case 0: return m10;
case 1: return m11;
case 2: return m12;
case 3: return m13;
}
case 2:
switch (j) {
case 0: return m20;
case 1: return m21;
case 2: return m22;
case 3: return m23;
}
case 3:
switch (j) {
case 0: return m30;
case 1: return m31;
case 2: return m32;
case 3: return m33;
}
}
logger.warning("Invalid matrix index.");
throw new JmeException("Invalid indices into matrix.");
}
/**
* <code>getColumn</code> returns one of three columns specified by the
* parameter. This column is returned as a float array of length 4.
*
* @param i
* the column to retrieve. Must be between 0 and 3.
* @return the column specified by the index.
*/
public float[] getColumn(int i) {
return getColumn(i, null);
}
/**
* <code>getColumn</code> returns one of three columns specified by the
* parameter. This column is returned as a float[4].
*
* @param i
* the column to retrieve. Must be between 0 and 3.
* @param store
* the float array to store the result in. if null, a new one
* is created.
* @return the column specified by the index.
*/
public float[] getColumn(int i, float[] store) {
if (store == null) store = new float[4];
switch (i) {
case 0:
store[0] = m00;
store[1] = m10;
store[2] = m20;
store[3] = m30;
break;
case 1:
store[0] = m01;
store[1] = m11;
store[2] = m21;
store[3] = m31;
break;
case 2:
store[0] = m02;
store[1] = m12;
store[2] = m22;
store[3] = m32;
break;
case 3:
store[0] = m03;
store[1] = m13;
store[2] = m23;
store[3] = m33;
break;
default:
logger.warning("Invalid column index.");
throw new JmeException("Invalid column index. " + i);
}
return store;
}
/**
*
* <code>setColumn</code> sets a particular column of this matrix to that
* represented by the provided vector.
*
* @param i
* the column to set.
* @param column
* the data to set.
*/
public void setColumn(int i, float[] column) {
if (column == null) {
logger.warning("Column is null. Ignoring.");
return;
}
switch (i) {
case 0:
m00 = column[0];
m10 = column[1];
m20 = column[2];
m30 = column[3];
break;
case 1:
m01 = column[0];
m11 = column[1];
m21 = column[2];
m31 = column[3];
break;
case 2:
m02 = column[0];
m12 = column[1];
m22 = column[2];
m32 = column[3];
break;
case 3:
m03 = column[0];
m13 = column[1];
m23 = column[2];
m33 = column[3];
break;
default:
logger.warning("Invalid column index.");
throw new JmeException("Invalid column index. " + i);
} }
/**
* <code>set</code> places a given value into the matrix at the given
* position. If the position is invalid a <code>JmeException</code> is
* thrown.
*
* @param i
* the row index.
* @param j
* the colum index.
* @param value
* the value for (i, j).
*/
public void set(int i, int j, float value) {
switch (i) {
case 0:
switch (j) {
case 0: m00 = value; return;
case 1: m01 = value; return;
case 2: m02 = value; return;
case 3: m03 = value; return;
}
case 1:
switch (j) {
case 0: m10 = value; return;
case 1: m11 = value; return;
case 2: m12 = value; return;
case 3: m13 = value; return;
}
case 2:
switch (j) {
case 0: m20 = value; return;
case 1: m21 = value; return;
case 2: m22 = value; return;
case 3: m23 = value; return;
}
case 3:
switch (j) {
case 0: m30 = value; return;
case 1: m31 = value; return;
case 2: m32 = value; return;
case 3: m33 = value; return;
}
}
logger.warning("Invalid matrix index.");
throw new JmeException("Invalid indices into matrix.");
}
/**
* <code>set</code> sets the values of this matrix from an array of
* values.
*
* @param matrix
* the matrix to set the value to.
* @throws JmeException
* if the array is not of size 16.
*/
public void set(float[][] matrix) {
if (matrix.length != 4 || matrix[0].length != 4) { throw new JmeException(
"Array must be of size 16."); }
m00 = matrix[0][0];
m01 = matrix[0][1];
m02 = matrix[0][2];
m03 = matrix[0][3];
m10 = matrix[1][0];
m11 = matrix[1][1];
m12 = matrix[1][2];
m13 = matrix[1][3];
m20 = matrix[2][0];
m21 = matrix[2][1];
m22 = matrix[2][2];
m23 = matrix[2][3];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -