📄 matrix3f.java
字号:
return invert(null);
}
/**
* Inverts this matrix and stores it in the given store.
*
* @return The store
*/
public Matrix3f invert(Matrix3f store) {
if (store == null) store = new Matrix3f();
float det = determinant();
if ( FastMath.abs(det) <= FastMath.FLT_EPSILON )
return store.zero();
store.m00 = m11*m22 - m12*m21;
store.m01 = m02*m21 - m01*m22;
store.m02 = m01*m12 - m02*m11;
store.m10 = m12*m20 - m10*m22;
store.m11 = m00*m22 - m02*m20;
store.m12 = m02*m10 - m00*m12;
store.m20 = m10*m21 - m11*m20;
store.m21 = m01*m20 - m00*m21;
store.m22 = m00*m11 - m01*m10;
store.multLocal(1f/det);
return store;
}
/**
* Inverts this matrix locally.
*
* @return this
*/
public Matrix3f invertLocal() {
float det = determinant();
if ( FastMath.abs(det) <= FastMath.FLT_EPSILON )
return zero();
float f00 = m11*m22 - m12*m21;
float f01 = m02*m21 - m01*m22;
float f02 = m01*m12 - m02*m11;
float f10 = m12*m20 - m10*m22;
float f11 = m00*m22 - m02*m20;
float f12 = m02*m10 - m00*m12;
float f20 = m10*m21 - m11*m20;
float f21 = m01*m20 - m00*m21;
float f22 = m00*m11 - m01*m10;
m00 = f00;
m01 = f01;
m02 = f02;
m10 = f10;
m11 = f11;
m12 = f12;
m20 = f20;
m21 = f21;
m22 = f22;
multLocal(1f/det);
return this;
}
/**
* Returns a new matrix representing the adjoint of this matrix.
*
* @return The adjoint matrix
*/
public Matrix3f adjoint() {
return adjoint(null);
}
/**
* Places the adjoint of this matrix in store (creates store if null.)
*
* @param store
* The matrix to store the result in. If null, a new matrix is created.
* @return store
*/
public Matrix3f adjoint(Matrix3f store) {
if (store == null) store = new Matrix3f();
store.m00 = m11*m22 - m12*m21;
store.m01 = m02*m21 - m01*m22;
store.m02 = m01*m12 - m02*m11;
store.m10 = m12*m20 - m10*m22;
store.m11 = m00*m22 - m02*m20;
store.m12 = m02*m10 - m00*m12;
store.m20 = m10*m21 - m11*m20;
store.m21 = m01*m20 - m00*m21;
store.m22 = m00*m11 - m01*m10;
return store;
}
/**
* <code>determinant</code> generates the determinate of this matrix.
*
* @return the determinate
*/
public float determinant() {
float fCo00 = m11*m22 - m12*m21;
float fCo10 = m12*m20 - m10*m22;
float fCo20 = m10*m21 - m11*m20;
float fDet = m00*fCo00 + m01*fCo10 + m02*fCo20;
return fDet;
}
/**
* Sets all of the values in this matrix to zero.
*
* @return this matrix
*/
public Matrix3f zero() {
m00 = m01 = m02 = m10 = m11 = m12 = m20 = m21 = m22 = 0.0f;
return this;
}
/**
* <code>add</code> adds the values of a parameter matrix to this matrix.
*
* @param mat
* the matrix to add to this.
*/
public void add(Matrix3f mat) {
m00 += mat.m00;
m01 += mat.m01;
m02 += mat.m02;
m10 += mat.m10;
m11 += mat.m11;
m12 += mat.m12;
m20 += mat.m20;
m21 += mat.m21;
m22 += mat.m22;
}
/**
* <code>transpose</code> <b>locally</b> transposes this Matrix.
* This is inconsistent with general value vs local semantics, but is
* preserved for backwards compatibility. Use transposeNew() to transpose
* to a new object (value).
*
* @return this object for chaining.
*/
public Matrix3f transpose() {
return transposeLocal();
}
/**
* <code>transposeNew</code> returns a transposed version of this matrix.
*
* @return The new Matrix3f object.
*/
public Matrix3f transposeNew() {
Matrix3f ret = new Matrix3f(m00, m10, m20, m01, m11, m21, m02, m12, m22);
return ret;
}
/**
* <code>toString</code> returns the string representation of this object.
* It is in a format of a 3x3 matrix. For example, an identity matrix would
* be represented by the following string. com.jme.math.Matrix3f <br>[<br>
* 1.0 0.0 0.0 <br>
* 0.0 1.0 0.0 <br>
* 0.0 0.0 1.0 <br>]<br>
*
* @return the string representation of this object.
*/
public String toString() {
StringBuffer result = new StringBuffer("com.jme.math.Matrix3f\n[\n");
result.append(" ");
result.append(m00);
result.append(" ");
result.append(m01);
result.append(" ");
result.append(m02);
result.append(" \n");
result.append(" ");
result.append(m10);
result.append(" ");
result.append(m11);
result.append(" ");
result.append(m12);
result.append(" \n");
result.append(" ");
result.append(m20);
result.append(" ");
result.append(m21);
result.append(" ");
result.append(m22);
result.append(" \n]");
return result.toString();
}
/**
*
* <code>hashCode</code> returns the hash code value as an integer and is
* supported for the benefit of hashing based collection classes such as
* Hashtable, HashMap, HashSet etc.
*
* @return the hashcode for this instance of Matrix4f.
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
int hash = 37;
hash = 37 * hash + Float.floatToIntBits(m00);
hash = 37 * hash + Float.floatToIntBits(m01);
hash = 37 * hash + Float.floatToIntBits(m02);
hash = 37 * hash + Float.floatToIntBits(m10);
hash = 37 * hash + Float.floatToIntBits(m11);
hash = 37 * hash + Float.floatToIntBits(m12);
hash = 37 * hash + Float.floatToIntBits(m20);
hash = 37 * hash + Float.floatToIntBits(m21);
hash = 37 * hash + Float.floatToIntBits(m22);
return hash;
}
/**
* are these two matrices the same? they are is they both have the same mXX values.
*
* @param o
* the object to compare for equality
* @return true if they are equal
*/
public boolean equals(Object o) {
if (!(o instanceof Matrix3f) || o == null) {
return false;
}
if (this == o) {
return true;
}
Matrix3f comp = (Matrix3f) o;
if (Float.compare(m00,comp.m00) != 0) return false;
if (Float.compare(m01,comp.m01) != 0) return false;
if (Float.compare(m02,comp.m02) != 0) return false;
if (Float.compare(m10,comp.m10) != 0) return false;
if (Float.compare(m11,comp.m11) != 0) return false;
if (Float.compare(m12,comp.m12) != 0) return false;
if (Float.compare(m20,comp.m20) != 0) return false;
if (Float.compare(m21,comp.m21) != 0) return false;
if (Float.compare(m22,comp.m22) != 0) return false;
return true;
}
public void write(JMEExporter e) throws IOException {
OutputCapsule cap = e.getCapsule(this);
cap.write(m00, "m00", 1);
cap.write(m01, "m01", 0);
cap.write(m02, "m02", 0);
cap.write(m10, "m10", 0);
cap.write(m11, "m11", 1);
cap.write(m12, "m12", 0);
cap.write(m20, "m20", 0);
cap.write(m21, "m21", 0);
cap.write(m22, "m22", 1);
}
public void read(JMEImporter e) throws IOException {
InputCapsule cap = e.getCapsule(this);
m00 = cap.readFloat("m00", 1);
m01 = cap.readFloat("m01", 0);
m02 = cap.readFloat("m02", 0);
m10 = cap.readFloat("m10", 0);
m11 = cap.readFloat("m11", 1);
m12 = cap.readFloat("m12", 0);
m20 = cap.readFloat("m20", 0);
m21 = cap.readFloat("m21", 0);
m22 = cap.readFloat("m22", 1);
}
public Class<? extends Matrix3f> getClassTag() {
return this.getClass();
}
/**
* A function for creating a rotation matrix that rotates a vector called
* "start" into another vector called "end".
*
* @param start
* normalized non-zero starting vector
* @param end
* normalized non-zero ending vector
* @see "Tomas M鰈ler, John Hughes \"Efficiently Building a Matrix to Rotate \
* One Vector to Another\" Journal of Graphics Tools, 4(4):1-4, 1999"
*/
public void fromStartEndVectors(Vector3f start, Vector3f end) {
Vector3f v = new Vector3f();
float e, h, f;
start.cross(end, v);
e = start.dot(end);
f = (e < 0) ? -e : e;
// if "from" and "to" vectors are nearly parallel
if (f > 1.0f - FastMath.ZERO_TOLERANCE) {
Vector3f u = new Vector3f();
Vector3f x = new Vector3f();
float c1, c2, c3; /* coefficients for later use */
int i, j;
x.x = (start.x > 0.0) ? start.x : -start.x;
x.y = (start.y > 0.0) ? start.y : -start.y;
x.z = (start.z > 0.0) ? start.z : -start.z;
if (x.x < x.y) {
if (x.x < x.z) {
x.x = 1.0f;
x.y = x.z = 0.0f;
} else {
x.z = 1.0f;
x.x = x.y = 0.0f;
}
} else {
if (x.y < x.z) {
x.y = 1.0f;
x.x = x.z = 0.0f;
} else {
x.z = 1.0f;
x.x = x.y = 0.0f;
}
}
u.x = x.x - start.x;
u.y = x.y - start.y;
u.z = x.z - start.z;
v.x = x.x - end.x;
v.y = x.y - end.y;
v.z = x.z - end.z;
c1 = 2.0f / u.dot(u);
c2 = 2.0f / v.dot(v);
c3 = c1 * c2 * u.dot(v);
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
float val = -c1 * u.get(i) * u.get(j) - c2 * v.get(i)
* v.get(j) + c3 * v.get(i) * u.get(j);
set(i, j, val);
}
float val = get(i, i);
set(i, i, val + 1.0f);
}
} else {
// the most common case, unless "start"="end", or "start"=-"end"
float hvx, hvz, hvxy, hvxz, hvyz;
h = 1.0f / (1.0f + e);
hvx = h * v.x;
hvz = h * v.z;
hvxy = hvx * v.y;
hvxz = hvx * v.z;
hvyz = hvz * v.y;
set(0, 0, e + hvx * v.x);
set(0, 1, hvxy - v.z);
set(0, 2, hvxz + v.y);
set(1, 0, hvxy + v.z);
set(1, 1, e + h * v.y * v.y);
set(1, 2, hvyz - v.x);
set(2, 0, hvxz - v.y);
set(2, 1, hvyz + v.x);
set(2, 2, e + hvz * v.z);
}
}
/**
* <code>scale</code> scales the operation performed by this matrix on a
* per-component basis.
*
* @param scale
* The scale applied to each of the X, Y and Z output values.
*/
public void scale(Vector3f scale) {
m00 *= scale.x;
m10 *= scale.x;
m20 *= scale.x;
m01 *= scale.y;
m11 *= scale.y;
m21 *= scale.y;
m02 *= scale.z;
m12 *= scale.z;
m22 *= scale.z;
}
static final boolean equalIdentity(Matrix3f mat) {
if (Math.abs(mat.m00 - 1) > 1e-4) return false;
if (Math.abs(mat.m11 - 1) > 1e-4) return false;
if (Math.abs(mat.m22 - 1) > 1e-4) return false;
if (Math.abs(mat.m01) > 1e-4) return false;
if (Math.abs(mat.m02) > 1e-4) return false;
if (Math.abs(mat.m10) > 1e-4) return false;
if (Math.abs(mat.m12) > 1e-4) return false;
if (Math.abs(mat.m20) > 1e-4) return false;
if (Math.abs(mat.m21) > 1e-4) return false;
return true;
}
@Override
public Matrix3f clone() {
try {
return (Matrix3f) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(); // can not happen
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -