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

This commit is contained in:
Elias Naur 2006-06-16 07:48:40 +00:00
parent 5b18caad92
commit 71970c3bbf
1 changed files with 98 additions and 106 deletions

View File

@ -147,26 +147,15 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix2f add(Matrix2f left, Matrix2f right, Matrix2f dest) { public static Matrix2f add(Matrix2f left, Matrix2f right, Matrix2f dest) {
Matrix2f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix2f();
}
dest.m00 = left.m00 + right.m00; dest.m00 = left.m00 + right.m00;
dest.m01 = left.m01 + right.m01; dest.m01 = left.m01 + right.m01;
dest.m10 = left.m10 + right.m10; dest.m10 = left.m10 + right.m10;
dest.m11 = left.m11 + right.m11; dest.m11 = left.m11 + right.m11;
if (temp != null) { return dest;
temp.load(dest);
return temp;
} else
return dest;
} }
/** /**
@ -177,26 +166,15 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix2f sub(Matrix2f left, Matrix2f right, Matrix2f dest) { public static Matrix2f sub(Matrix2f left, Matrix2f right, Matrix2f dest) {
Matrix2f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix2f();
}
dest.m00 = left.m00 - right.m00; dest.m00 = left.m00 - right.m00;
dest.m01 = left.m01 - right.m01; dest.m01 = left.m01 - right.m01;
dest.m10 = left.m10 - right.m10; dest.m10 = left.m10 - right.m10;
dest.m11 = left.m11 - right.m11; dest.m11 = left.m11 - right.m11;
if (temp != null) { return dest;
temp.load(dest);
return temp;
} else
return dest;
} }
/** /**
@ -207,26 +185,20 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the destination matrix * @return the destination matrix
*/ */
public static Matrix2f mul(Matrix2f left, Matrix2f right, Matrix2f dest) { public static Matrix2f mul(Matrix2f left, Matrix2f right, Matrix2f dest) {
Matrix2f temp = null;
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
else if (dest == left || dest == right) {
temp = dest;
dest = new Matrix2f();
}
dest.m00 = left.m00 * right.m00 + left.m10 * right.m01; float m00 = left.m00 * right.m00 + left.m10 * right.m01;
dest.m01 = left.m01 * right.m00 + left.m11 * right.m01; float m01 = left.m01 * right.m00 + left.m11 * right.m01;
dest.m10 = left.m00 * right.m10 + left.m10 * right.m11; float m10 = left.m00 * right.m10 + left.m10 * right.m11;
dest.m11 = left.m01 * right.m10 + left.m11 * right.m11; float m11 = left.m01 * right.m10 + left.m11 * right.m11;
dest.m00 = m00;
dest.m01 = m01;
dest.m10 = m10;
dest.m11 = m11;
if (temp != null) { return dest;
temp.load(dest);
return temp;
} else
return dest;
} }
/** /**
@ -238,24 +210,16 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the destination vector * @return the destination vector
*/ */
public static Vector2f transform(Matrix2f left, Vector2f right, Vector2f dest) { public static Vector2f transform(Matrix2f left, Vector2f right, Vector2f dest) {
Vector2f temp = null;
if (dest == null) if (dest == null)
dest = new Vector2f(); dest = new Vector2f();
else if (dest == right) {
temp = dest;
dest = new Vector2f();
}
dest.x = left.m00 * right.x + left.m10 * right.y; float x = left.m00 * right.x + left.m10 * right.y;
dest.y = left.m01 * right.x + left.m11 * right.y; float y = left.m01 * right.x + left.m11 * right.y;
dest.x = x;
dest.y = y;
if (temp != null) { return dest;
temp.set(dest);
return temp;
} else
return dest;
} }
/** /**
@ -263,12 +227,7 @@ public class Matrix2f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix transpose() { public Matrix transpose() {
float temp; return transpose(this);
temp = m01;
m01 = m10;
m10 = temp;
return this;
} }
/** /**
@ -277,27 +236,45 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the transposed matrix * @return the transposed matrix
*/ */
public Matrix2f transpose(Matrix2f dest) { public Matrix2f transpose(Matrix2f dest) {
return transpose(this, dest);
}
/**
* Transpose source matrix and place the result in dest matrix.
* @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
* @return the transposed matrix
*/
public static Matrix2f transpose(Matrix2f src, Matrix2f dest) {
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
if (dest == this) float m01 = src.m10;
transpose(); float m10 = src.m01;
else {
dest.m01 = m10; dest.m01 = m01;
dest.m10 = m01; dest.m10 = m10;
}
return dest; return dest;
} }
/** /**
* 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);
}
/**
* Invert the source matrix and place the result in the dest matrix.
* @param src The source matrix to be inverted
* @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.
*/
public Matrix2f invert(Matrix2f src, Matrix2f dest) {
if (dest == null)
dest = new Matrix2f();
/* /*
*inv(A) = 1/det(A) * adj(A); *inv(A) = 1/det(A) * adj(A);
*/ */
@ -305,18 +282,18 @@ public class Matrix2f extends Matrix implements Serializable {
float determinant = determinant(); float determinant = determinant();
if (determinant != 0) { if (determinant != 0) {
float determinant_inv = 1f/determinant; float determinant_inv = 1f/determinant;
float t00 = m11*determinant_inv; float t00 = src.m11*determinant_inv;
float t01 = -m01*determinant_inv; float t01 = -src.m01*determinant_inv;
float t11 = m00*determinant_inv; float t11 = src.m00*determinant_inv;
float t10 = -m10*determinant_inv; float t10 = -src.m10*determinant_inv;
m00 = t00; dest.m00 = t00;
m01 = t01; dest.m01 = t01;
m10 = t10; dest.m10 = t10;
m11 = t11; dest.m11 = t11;
return this; return dest;
} else } else
return null; return null;
} }
/** /**
@ -334,13 +311,7 @@ public class Matrix2f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix negate() { public Matrix negate() {
return negate(this);
m00 = -m00;
m01 = -m01;
m10 = -m10;
m11 = -m11;
return this;
} }
/** /**
@ -349,28 +320,46 @@ public class Matrix2f extends Matrix implements Serializable {
* @return the negated matrix * @return the negated matrix
*/ */
public Matrix2f negate(Matrix2f dest) { public Matrix2f negate(Matrix2f dest) {
return negate(this, this);
}
/**
* Negate src matrix and stash the result in dest matrix.
* @param src The source matrix to be negated
* @param dest The destination matrix, or null if a new matrix is to be created
* @return the negated matrix
*/
public static Matrix2f negate(Matrix2f src, Matrix2f dest) {
if (dest == null) if (dest == null)
dest = new Matrix2f(); dest = new Matrix2f();
dest.m00 = -m00; dest.m00 = -src.m00;
dest.m01 = -m01; dest.m01 = -src.m01;
dest.m10 = -m10; dest.m10 = -src.m10;
dest.m11 = -m11; dest.m11 = -src.m11;
return dest; return dest;
} }
/** /**
* Set this matrix to be the identity matrix. * Set this matrix to be the identity matrix.
* @return this * @return this
*/ */
public Matrix setIdentity() { public Matrix setIdentity() {
m00 = 1.0f; return setIdentity(this);
m01 = 0.0f; }
m10 = 0.0f;
m11 = 1.0f; /**
return this; * Set the ssrc matrix to be the identity matrix.
* @param src The matrix to set to the identity.
* @return The source matrix
*/
public static Matrix2f setIdentity(Matrix2f src) {
src.m00 = 1.0f;
src.m01 = 0.0f;
src.m10 = 0.0f;
src.m11 = 1.0f;
return src;
} }
/** /**
@ -378,11 +367,15 @@ public class Matrix2f extends Matrix implements Serializable {
* @return this * @return this
*/ */
public Matrix setZero() { public Matrix setZero() {
m00 = 0.0f; return setZero(this);
m01 = 0.0f; }
m10 = 0.0f;
m11 = 0.0f; public static Matrix2f setZero(Matrix2f src) {
return this; src.m00 = 0.0f;
src.m01 = 0.0f;
src.m10 = 0.0f;
src.m11 = 0.0f;
return src;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -391,5 +384,4 @@ public class Matrix2f extends Matrix implements Serializable {
public float determinant() { public float determinant() {
return m00 * m11 - m01*m10; return m00 * m11 - m01*m10;
} }
} }