📄 matrix3f.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>Matrix3f</code> defines a 3x3 matrix. Matrix data is maintained
* internally and is accessible via the get and set methods. Convenience methods
* are used for matrix operations as well as generating a matrix from a given
* set of values.
*
* @author Mark Powell
* @author Joshua Slack
*/
public class Matrix3f implements Serializable, Savable, Cloneable {
private static final Logger logger = Logger.getLogger(Matrix3f.class.getName());
private static final long serialVersionUID = 1L;
public float m00, m01, m02;
public float m10, m11, m12;
public float m20, m21, m22;
/**
* Constructor instantiates a new <code>Matrix3f</code> object. The
* initial values for the matrix is that of the identity matrix.
*
*/
public Matrix3f() {
loadIdentity();
}
/**
* constructs a matrix with the given values.
*
* @param m00
* 0x0 in the matrix.
* @param m01
* 0x1 in the matrix.
* @param m02
* 0x2 in the matrix.
* @param m10
* 1x0 in the matrix.
* @param m11
* 1x1 in the matrix.
* @param m12
* 1x2 in the matrix.
* @param m20
* 2x0 in the matrix.
* @param m21
* 2x1 in the matrix.
* @param m22
* 2x2 in the matrix.
*/
public Matrix3f(float m00, float m01, float m02, float m10, float m11,
float m12, float m20, float m21, float m22) {
this.m00 = m00;
this.m01 = m01;
this.m02 = m02;
this.m10 = m10;
this.m11 = m11;
this.m12 = m12;
this.m20 = m20;
this.m21 = m21;
this.m22 = m22;
}
/**
* Copy constructor that creates a new <code>Matrix3f</code> object that
* is the same as the provided matrix.
*
* @param mat
* the matrix to copy.
*/
public Matrix3f(Matrix3f 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(Matrix3f matrix) {
if (null == matrix) {
loadIdentity();
} else {
m00 = matrix.m00;
m01 = matrix.m01;
m02 = matrix.m02;
m10 = matrix.m10;
m11 = matrix.m11;
m12 = matrix.m12;
m20 = matrix.m20;
m21 = matrix.m21;
m22 = matrix.m22;
}
}
/**
* <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 1:
switch (j) {
case 0: return m10;
case 1: return m11;
case 2: return m12;
}
case 2:
switch (j) {
case 0: return m20;
case 1: return m21;
case 2: return m22;
}
}
logger.warning("Invalid matrix index.");
throw new JmeException("Invalid indices into matrix.");
}
/**
* <code>get(float[])</code> returns the matrix in row-major or column-major order.
*
* @param data
* The array to return the data into. This array can be 9 or 16 floats in size.
* Only the upper 3x3 are assigned to in the case of a 16 element array.
* @param rowMajor
* True for row major storage in the array (translation in elements 3, 7, 11 for a 4x4),
* false for column major (translation in elements 12, 13, 14 for a 4x4).
*/
public void get(float[] data, boolean rowMajor) {
if (data.length == 9) {
if (rowMajor) {
data[0] = m00;
data[1] = m01;
data[2] = m02;
data[3] = m10;
data[4] = m11;
data[5] = m12;
data[6] = m20;
data[7] = m21;
data[8] = m22;
}
else {
data[0] = m00;
data[1] = m10;
data[2] = m20;
data[3] = m01;
data[4] = m11;
data[5] = m21;
data[6] = m02;
data[7] = m12;
data[8] = m22;
}
}
else if (data.length == 16) {
if (rowMajor) {
data[0] = m00;
data[1] = m01;
data[2] = m02;
data[4] = m10;
data[5] = m11;
data[6] = m12;
data[8] = m20;
data[9] = m21;
data[10] = m22;
}
else {
data[0] = m00;
data[1] = m10;
data[2] = m20;
data[4] = m01;
data[5] = m11;
data[6] = m21;
data[8] = m02;
data[9] = m12;
data[10] = m22;
}
}
else {
throw new JmeException("Array size must be 9 or 16 in Matrix3f.get().");
}
}
/**
* <code>getColumn</code> returns one of three columns specified by the
* parameter. This column is returned as a <code>Vector3f</code> object.
*
* @param i
* the column to retrieve. Must be between 0 and 2.
* @return the column specified by the index.
*/
public Vector3f 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 <code>Vector3f</code> object.
*
* @param i
* the column to retrieve. Must be between 0 and 2.
* @param store
* the vector object to store the result in. if null, a new one
* is created.
* @return the column specified by the index.
*/
public Vector3f getColumn(int i, Vector3f store) {
if (store == null) store = new Vector3f();
switch (i) {
case 0:
store.x = m00;
store.y = m10;
store.z = m20;
break;
case 1:
store.x = m01;
store.y = m11;
store.z = m21;
break;
case 2:
store.x = m02;
store.y = m12;
store.z = m22;
break;
default:
logger.warning("Invalid column index.");
throw new JmeException("Invalid column index. " + i);
}
return store;
}
/**
* <code>getColumn</code> returns one of three rows as specified by the
* parameter. This row is returned as a <code>Vector3f</code> object.
*
* @param i
* the row to retrieve. Must be between 0 and 2.
* @return the row specified by the index.
*/
public Vector3f getRow(int i) {
return getRow(i, null);
}
/**
* <code>getRow</code> returns one of three rows as specified by the
* parameter. This row is returned as a <code>Vector3f</code> object.
*
* @param i
* the row to retrieve. Must be between 0 and 2.
* @param store
* the vector object to store the result in. if null, a new one
* is created.
* @return the row specified by the index.
*/
public Vector3f getRow(int i, Vector3f store) {
if (store == null) store = new Vector3f();
switch (i) {
case 0:
store.x = m00;
store.y = m01;
store.z = m02;
break;
case 1:
store.x = m10;
store.y = m11;
store.z = m12;
break;
case 2:
store.x = m20;
store.y = m21;
store.z = m22;
break;
default:
logger.warning("Invalid row index.");
throw new JmeException("Invalid row index. " + i);
}
return store;
}
/**
* <code>toFloatBuffer</code> returns a FloatBuffer object that contains
* the matrix data.
*
* @return matrix data as a FloatBuffer.
*/
public FloatBuffer toFloatBuffer() {
FloatBuffer fb = BufferUtils.createFloatBuffer(9);
fb.put(m00).put(m01).put(m02);
fb.put(m10).put(m11).put(m12);
fb.put(m20).put(m21).put(m22);
fb.rewind();
return fb;
}
/**
* <code>fillFloatBuffer</code> fills a FloatBuffer object with the matrix
* data.
*
* @param fb
* the buffer to fill, starting at current position. Must have
* room for 9 more floats.
* @return matrix data as a FloatBuffer. (position is advanced by 9 and any
* limit set is not changed).
*/
public FloatBuffer fillFloatBuffer(FloatBuffer fb) {
fb.put(m00).put(m01).put(m02);
fb.put(m10).put(m11).put(m12);
fb.put(m20).put(m21).put(m22);
return fb;
}
/**
*
* <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, Vector3f column) {
if (column == null) {
logger.warning("Column is null. Ignoring.");
return;
}
switch (i) {
case 0:
m00 = column.x;
m10 = column.y;
m20 = column.z;
break;
case 1:
m01 = column.x;
m11 = column.y;
m21 = column.z;
break;
case 2:
m02 = column.x;
m12 = column.y;
m22 = column.z;
break;
default:
logger.warning("Invalid column index.");
throw new JmeException("Invalid column index. " + i);
}
}
/**
*
* <code>setRow</code> sets a particular row of this matrix to that
* represented by the provided vector.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -