Added missing static methods to Matrix3f for consistency. Cleaned up handling of src == dest aliasing

This commit is contained in:
Elias Naur 2006-06-16 08:20:21 +00:00
parent 48a52a67ce
commit dbf9356358
2 changed files with 189 additions and 179 deletions

View File

@ -73,10 +73,10 @@ public class Matrix2f extends Matrix implements Serializable {
} }
/** /**
* Copy src matrix to the dest matrix. * Copy the source matrix to the destination matrix.
* @param src The source matrix * @param src The source matrix
* @param dest The destination matrix, or null if a new one should be created. * @param dest The destination matrix, or null if a new one should be created.
* @return this * @return dest
*/ */
public static Matrix2f load(Matrix2f src, Matrix2f dest) { public static Matrix2f load(Matrix2f src, Matrix2f dest) {
if (dest == null) if (dest == null)
@ -253,7 +253,7 @@ public class Matrix2f extends Matrix implements Serializable {
} }
/** /**
* Transpose source matrix and place the result in dest matrix. * Transpose the source matrix and place the result in the destination matrix.
* @param src The source matrix or null if a new matrix is to be created * @param src The source matrix or null if a new matrix is to be created
* @param dest The destination matrix or null if a new matrix is to be created * @param dest The destination matrix or null if a new matrix is to be created
* @return the transposed matrix * @return the transposed matrix
@ -280,19 +280,19 @@ public class Matrix2f extends Matrix implements Serializable {
} }
/** /**
* Invert the source matrix and place the result in the dest matrix. * Invert the source matrix and place the result in the destination matrix.
* @param src The source matrix to be inverted * @param src The source matrix to be inverted
* @param dest The destination matrix or null if a new matrix is to be created * @param dest The destination matrix or null if a new matrix is to be created
* @return The inverted matrix, or null if source can't be reverted. * @return The inverted matrix, or null if source can't be reverted.
*/ */
public Matrix2f invert(Matrix2f src, Matrix2f dest) { public static Matrix2f invert(Matrix2f src, Matrix2f dest) {
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
/* /*
*inv(A) = 1/det(A) * adj(A); *inv(A) = 1/det(A) * adj(A);
*/ */
float determinant = determinant(); float determinant = src.determinant();
if (determinant != 0) { if (determinant != 0) {
float determinant_inv = 1f/determinant; float determinant_inv = 1f/determinant;
float t00 = src.m11*determinant_inv; float t00 = src.m11*determinant_inv;
@ -337,7 +337,7 @@ public class Matrix2f extends Matrix implements Serializable {
} }
/** /**
* Negate src matrix and stash the result in dest matrix. * Negate the source matrix and stash the result in the destination matrix.
* @param src The source matrix to be negated * @param src The source matrix to be negated
* @param dest The destination matrix, or null if a new matrix is to be created * @param dest The destination matrix, or null if a new matrix is to be created
* @return the negated matrix * @return the negated matrix
@ -363,7 +363,7 @@ public class Matrix2f extends Matrix implements Serializable {
} }
/** /**
* Set the ssrc matrix to be the identity matrix. * Set the source matrix to be the identity matrix.
* @param src The matrix to set to the identity. * @param src The matrix to set to the identity.
* @return The source matrix * @return The source matrix
*/ */

View File

@ -47,40 +47,54 @@ public class Matrix3f extends Matrix implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public float m00 = 1.0f, public float m00,
m01, m01,
m02, m02,
m10, m10,
m11 = 1.0f, m11,
m12, m12,
m20, m20,
m21, m21,
m22 = 1.0f; m22;
/** /**
* Constructor for Matrix3f. * Constructor for Matrix3f. Matrix is initialised to the identity.
*/ */
public Matrix3f() { public Matrix3f() {
super(); super();
setIdentity();
} }
/** /**
* Load from another matrix3f * Load from another matrix
* @param src The source matrix * @param src The source matrix
* @return this * @return this
*/ */
public Matrix3f load(Matrix3f src) { public Matrix3f load(Matrix3f src) {
return load(src, this);
}
m00 = src.m00; /**
m10 = src.m10; * Copy source matrix to destination matrix
m20 = src.m20; * @param src The source matrix
m01 = src.m01; * @param dest The destination matrix, or null of a new matrix is to be created
m11 = src.m11; * @return dest
m21 = src.m21; */
m02 = src.m02; public static Matrix3f load(Matrix3f src, Matrix3f dest) {
m12 = src.m12; if (dest == null)
m22 = src.m22; dest = new Matrix3f();
return this; dest.m00 = src.m00;
dest.m10 = src.m10;
dest.m20 = src.m20;
dest.m01 = src.m01;
dest.m11 = src.m11;
dest.m21 = src.m21;
dest.m02 = src.m02;
dest.m12 = src.m12;
dest.m22 = src.m22;
return dest;
} }
/** /**
@ -163,8 +177,6 @@ public class Matrix3f extends Matrix implements Serializable {
return this; return this;
} }
/** /**
* Add two matrices together and place the result in a third matrix. * Add two matrices together and place the result in a third matrix.
* @param left The left source matrix * @param left The left source matrix
@ -173,15 +185,8 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix3f add(Matrix3f left, Matrix3f right, Matrix3f dest) { public static Matrix3f add(Matrix3f left, Matrix3f right, Matrix3f dest) {
Matrix3f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix3f(); dest = new Matrix3f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix3f();
}
dest.m00 = left.m00 + right.m00; dest.m00 = left.m00 + right.m00;
dest.m01 = left.m01 + right.m01; dest.m01 = left.m01 + right.m01;
@ -193,10 +198,6 @@ public class Matrix3f extends Matrix implements Serializable {
dest.m21 = left.m21 + right.m21; dest.m21 = left.m21 + right.m21;
dest.m22 = left.m22 + right.m22; dest.m22 = left.m22 + right.m22;
if (temp != null) {
temp.load(dest);
return temp;
} else
return dest; return dest;
} }
@ -208,15 +209,8 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix3f sub(Matrix3f left, Matrix3f right, Matrix3f dest) { public static Matrix3f sub(Matrix3f left, Matrix3f right, Matrix3f dest) {
Matrix3f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix3f(); dest = new Matrix3f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix3f();
}
dest.m00 = left.m00 - right.m00; dest.m00 = left.m00 - right.m00;
dest.m01 = left.m01 - right.m01; dest.m01 = left.m01 - right.m01;
@ -228,10 +222,6 @@ public class Matrix3f extends Matrix implements Serializable {
dest.m21 = left.m21 - right.m21; dest.m21 = left.m21 - right.m21;
dest.m22 = left.m22 - right.m22; dest.m22 = left.m22 - right.m22;
if (temp != null) {
temp.load(dest);
return temp;
} else
return dest; return dest;
} }
@ -243,39 +233,38 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix3f mul(Matrix3f left, Matrix3f right, Matrix3f dest) { public static Matrix3f mul(Matrix3f left, Matrix3f right, Matrix3f dest) {
Matrix3f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix3f(); dest = new Matrix3f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix3f();
}
dest.m00 = float m00 =
left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02; left.m00 * right.m00 + left.m10 * right.m01 + left.m20 * right.m02;
dest.m01 = float m01 =
left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02; left.m01 * right.m00 + left.m11 * right.m01 + left.m21 * right.m02;
dest.m02 = float m02 =
left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02; left.m02 * right.m00 + left.m12 * right.m01 + left.m22 * right.m02;
dest.m10 = float m10 =
left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12; left.m00 * right.m10 + left.m10 * right.m11 + left.m20 * right.m12;
dest.m11 = float m11 =
left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12; left.m01 * right.m10 + left.m11 * right.m11 + left.m21 * right.m12;
dest.m12 = float m12 =
left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12; left.m02 * right.m10 + left.m12 * right.m11 + left.m22 * right.m12;
dest.m20 = float m20 =
left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22; left.m00 * right.m20 + left.m10 * right.m21 + left.m20 * right.m22;
dest.m21 = float m21 =
left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22; left.m01 * right.m20 + left.m11 * right.m21 + left.m21 * right.m22;
dest.m22 = float m22 =
left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22; left.m02 * right.m20 + left.m12 * right.m21 + left.m22 * right.m22;
if (temp != null) { dest.m00 = m00;
temp.load(dest); dest.m01 = m01;
return temp; dest.m02 = m02;
} else dest.m10 = m10;
dest.m11 = m11;
dest.m12 = m12;
dest.m20 = m20;
dest.m21 = m21;
dest.m22 = m22;
return dest; return dest;
} }
@ -288,24 +277,17 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the destination vector * @return the destination vector
*/ */
public static Vector3f transform(Matrix3f left, Vector3f right, Vector3f dest) { public static Vector3f transform(Matrix3f left, Vector3f right, Vector3f dest) {
Vector3f temp = null;
if (dest == null) if (dest == null)
dest = new Vector3f(); dest = new Vector3f();
else if (dest == right) {
temp = dest;
dest = new Vector3f();
}
dest.x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z; float x = left.m00 * right.x + left.m10 * right.y + left.m20 * right.z;
dest.y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z; float y = left.m01 * right.x + left.m11 * right.y + left.m21 * right.z;
dest.z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z; float z = left.m02 * right.x + left.m12 * right.y + left.m22 * right.z;
dest.x = x;
dest.y = y;
dest.z = z;
if (temp != null) {
temp.set(dest);
return temp;
} else
return dest; return dest;
} }
@ -314,16 +296,7 @@ public class Matrix3f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix transpose() { public Matrix transpose() {
float f = m10; return transpose(this, this);
m10 = m01;
m01 = f;
f = m20;
m20 = m02;
m02 = f;
f = m21;
m21 = m12;
m12 = f;
return this;
} }
/** /**
@ -332,26 +305,37 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the transposed matrix * @return the transposed matrix
*/ */
public Matrix3f transpose(Matrix3f dest) { public Matrix3f transpose(Matrix3f dest) {
if (dest == null) { return transpose(this, dest);
// New matrix needed to store transpose }
/**
* Transpose the source matrix and place the result into the destination matrix
* @param src The source matrix to be transposed
* @param dest The destination matrix or null if a new matrix is to be created
* @return the transposed matrix
*/
public static Matrix3f transpose(Matrix3f src, Matrix3f dest) {
if (dest == null)
dest = new Matrix3f(); dest = new Matrix3f();
} float m00 = src.m00;
if (this == dest) { float m01 = src.m10;
// Destination and source are the same! Run the in-place float m02 = src.m20;
// transpose instead as the copy transpose will be destructive. float m10 = src.m01;
transpose(); float m11 = src.m11;
} else { float m12 = src.m21;
// Destination differs from source. Perform copy transpose float m20 = src.m02;
float m21 = src.m12;
float m22 = src.m22;
dest.m00 = m00; dest.m00 = m00;
dest.m01 = m10; dest.m01 = m01;
dest.m02 = m20; dest.m02 = m02;
dest.m10 = m01; dest.m10 = m10;
dest.m11 = m11; dest.m11 = m11;
dest.m12 = m21; dest.m12 = m12;
dest.m20 = m02; dest.m20 = m20;
dest.m21 = m12; dest.m21 = m21;
dest.m22 = m22; dest.m22 = m22;
}
return dest; return dest;
} }
@ -381,12 +365,20 @@ public class Matrix3f extends Matrix implements Serializable {
* Invert this matrix * Invert this matrix
* @return this if successful, null otherwise * @return this if successful, null otherwise
*/ */
public Matrix invert() public Matrix invert() {
{ return invert(this, this);
float determinant = determinant(); }
if (determinant != 0) /**
{ * Invert the source matrix and put the result into the destination matrix
* @param src The source matrix to be inverted
* @param dest The destination matrix
* @return dest if successful, null otherwise
*/
public static Matrix3f invert(Matrix3f src, Matrix3f dest) {
float determinant = src.determinant();
if (determinant != 0) {
/* do it the ordinary way /* do it the ordinary way
* *
* inv(A) = 1/det(A) * adj(T), where adj(T) = transpose(Conjugate Matrix) * inv(A) = 1/det(A) * adj(T), where adj(T) = transpose(Conjugate Matrix)
@ -398,46 +390,37 @@ public class Matrix3f extends Matrix implements Serializable {
float determinant_inv = 1f/determinant; float determinant_inv = 1f/determinant;
// get the conjugate matrix // get the conjugate matrix
float t00 = m11 * m22 - m12* m21; float t00 = src.m11 * src.m22 - src.m12* src.m21;
float t01 = - m10 * m22 + m12 *m20; float t01 = - src.m10 * src.m22 + src.m12 * src.m20;
float t02 = m10 * m21 - m11 * m20; float t02 = src.m10 * src.m21 - src.m11 * src.m20;
float t10 = - m01 * m22 + m02 * m21; float t10 = - src.m01 * src.m22 + src.m02 * src.m21;
float t11 = m00 * m22 - m02 * m20; float t11 = src.m00 * src.m22 - src.m02 * src.m20;
float t12 = - m00 * m21 + m01 * m20; float t12 = - src.m00 * src.m21 + src.m01 * src.m20;
float t20 = m01 * m12 - m02 * m11; float t20 = src.m01 * src.m12 - src.m02 * src.m11;
float t21 = -m00 * m12 + m02 * m10; float t21 = -src.m00 * src.m12 + src.m02 * src.m10;
float t22 = m00 * m11 - m01 * m10; float t22 = src.m00 * src.m11 - src.m01 * src.m10;
dest.m00 = t00*determinant_inv;
m00 = t00*determinant_inv; dest.m11 = t11*determinant_inv;
m11 = t11*determinant_inv; dest.m22 = t22*determinant_inv;
m22 = t22*determinant_inv; dest.m01 = t10*determinant_inv;
m01 = t10*determinant_inv; dest.m10 = t01*determinant_inv;
m10 = t01*determinant_inv; dest.m20 = t02*determinant_inv;
m20 = t02*determinant_inv; dest.m02 = t20*determinant_inv;
m02 = t20*determinant_inv; dest.m12 = t21*determinant_inv;
m12 = t21*determinant_inv; dest.m21 = t12*determinant_inv;
m21 = t12*determinant_inv; return dest;
return this;
} else } else
return null; return null;
} }
/** /**
* Negate this matrix * Negate this matrix
* @return this * @return this
*/ */
public Matrix negate() { public Matrix negate() {
m00 = -m00; return negate(this);
m01 = -m02;
m02 = -m01;
m10 = -m10;
m11 = -m12;
m12 = -m11;
m20 = -m20;
m21 = -m22;
m22 = -m21;
return this;
} }
/** /**
@ -446,18 +429,28 @@ public class Matrix3f extends Matrix implements Serializable {
* @return the negated matrix * @return the negated matrix
*/ */
public Matrix3f negate(Matrix3f dest) { public Matrix3f negate(Matrix3f dest) {
return negate(this, this);
}
/**
* Negate the source matrix and place the result in the destination matrix.
* @param src The source matrix
* @param dest The destination matrix, or null if a new matrix is to be created
* @return the negated matrix
*/
public static Matrix3f negate(Matrix3f src, Matrix3f dest) {
if (dest == null) if (dest == null)
dest = new Matrix3f(); dest = new Matrix3f();
dest.m00 = -m00; dest.m00 = -src.m00;
dest.m01 = -m02; dest.m01 = -src.m02;
dest.m02 = -m01; dest.m02 = -src.m01;
dest.m10 = -m10; dest.m10 = -src.m10;
dest.m11 = -m12; dest.m11 = -src.m12;
dest.m12 = -m11; dest.m12 = -src.m11;
dest.m20 = -m20; dest.m20 = -src.m20;
dest.m21 = -m22; dest.m21 = -src.m22;
dest.m22 = -m21; dest.m22 = -src.m21;
return dest; return dest;
} }
@ -466,16 +459,25 @@ public class Matrix3f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix setIdentity() { public Matrix setIdentity() {
m00 = 1.0f; return setIdentity(this);
m01 = 0.0f; }
m02 = 0.0f;
m10 = 0.0f; /**
m11 = 1.0f; * Set the matrix to be the identity matrix.
m12 = 0.0f; * @param m The matrix to be set to the identity
m20 = 0.0f; * @return m
m21 = 0.0f; */
m22 = 1.0f; public static Matrix3f setIdentity(Matrix3f m) {
return this; m.m00 = 1.0f;
m.m01 = 0.0f;
m.m02 = 0.0f;
m.m10 = 0.0f;
m.m11 = 1.0f;
m.m12 = 0.0f;
m.m20 = 0.0f;
m.m21 = 0.0f;
m.m22 = 1.0f;
return m;
} }
/** /**
@ -483,16 +485,24 @@ public class Matrix3f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix setZero() { public Matrix setZero() {
m00 = 0.0f; return setZero(this);
m01 = 0.0f;
m02 = 0.0f;
m10 = 0.0f;
m11 = 0.0f;
m12 = 0.0f;
m20 = 0.0f;
m21 = 0.0f;
m22 = 0.0f;
return this;
} }
/**
* Set the matrix matrix to 0.
* @param m The matrix to be set to 0
* @return m
*/
public static Matrix3f setZero(Matrix3f m) {
m.m00 = 0.0f;
m.m01 = 0.0f;
m.m02 = 0.0f;
m.m10 = 0.0f;
m.m11 = 0.0f;
m.m12 = 0.0f;
m.m20 = 0.0f;
m.m21 = 0.0f;
m.m22 = 0.0f;
return m;
}
} }