Skip to content

Commit

Permalink
kmMat4 improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Carsten Haubold committed Aug 12, 2008
1 parent a88d823 commit 0d5bd28
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 63 deletions.
Binary file modified src/kazmath/build-linux/bin/Debug/libkazmath.so
Binary file not shown.
24 changes: 10 additions & 14 deletions src/kazmath/src/mat3.c
Expand Up @@ -67,17 +67,17 @@ kmMat3* kmMat3Adjugate(kmMat3* pOut, const kmMat3* pIn)
return pOut;
}

kmMat3* kmMat3Inverse(kmMat3* pOut, kmScalar* pDeterminate, const kmMat3* pM)
kmMat3* kmMat3Inverse(kmMat3* pOut, kmScalar pDeterminate, const kmMat3* pM)
{
if(pDeterminate == NULL || *pDeterminate == 0.0)
if(pDeterminate == 0.0)
return NULL;

kmScalar detInv = 1 / *pDeterminate;
kmScalar detInv = 1.0 / pDeterminate;

kmMat3 adjugate;
kmMat3Adjugate(&adjugate, pM);

kmMat3ScalarMultiply(pOut, &adjugate, &detInv);
kmMat3ScalarMultiply(pOut, &adjugate, detInv);

return pOut;
}
Expand Down Expand Up @@ -126,19 +126,14 @@ kmMat3* kmMat3Multiply(kmMat3* pOut, const kmMat3* pM1, const kmMat3* pM2)
return pOut;
}

kmMat3* kmMat3ScalarMultiply(kmMat3* pOut, const kmMat3* pM, const kmScalar* pFactor)
kmMat3* kmMat3ScalarMultiply(kmMat3* pOut, const kmMat3* pM, const kmScalar pFactor)
{
float mat[9];

mat[0] = pM->m_Mat[0];
mat[1] = pM->m_Mat[1];
mat[2] = pM->m_Mat[2];
mat[3] = pM->m_Mat[3];
mat[4] = pM->m_Mat[4];
mat[5] = pM->m_Mat[5];
mat[6] = pM->m_Mat[6];
mat[7] = pM->m_Mat[7];
mat[8] = pM->m_Mat[8];
for(int i = 0; i < 9; i++)
{
mat[0] = pM->m_Mat[9] * pFactor;
}

memcpy(pOut->m_Mat, mat, sizeof(float)*9);

Expand Down Expand Up @@ -170,6 +165,7 @@ bool kmMat3AreEqual(const kmMat3* pMat1, const kmMat3* pMat2)
return true;
}

/* Rotation around the z axis so everything stays planar in XY */
kmMat3* kmMat3Rotation(kmMat3* pOut, const float radians)
{
/*
Expand Down
7 changes: 4 additions & 3 deletions src/kazmath/src/mat3.h
Expand Up @@ -34,18 +34,19 @@ typedef struct kmMat3{
extern "C" {
#endif

kmMat3* kmMat3Adjugate(kmMat3* pOut, const kmMat3* pIn);
kmMat3* kmMat3Identity(kmMat3* pOut);
kmMat3* kmMat3Inverse(kmMat3* pOut, kmScalar* pDeterminate, const kmMat3* pM);
kmMat3* kmMat3Inverse(kmMat3* pOut, const kmScalar pDeterminate, const kmMat3* pM);
bool kmMat3IsIdentity(const kmMat3* pIn);
kmMat3* kmMat3Transpose(kmMat3* pOut, const kmMat3* pIn);
kmScalar kmMat3Determinant(const kmMat3* pIn);
kmMat3* kmMat3Multiply(kmMat3* pOut, const kmMat3* pM1, const kmMat3* pM2);
kmMat3* kmMat3ScalarMultiply(kmMat3* pOut, const kmMat3* pM, const kmScalar* pFactor);
kmMat3* kmMat3ScalarMultiply(kmMat3* pOut, const kmMat3* pM, kmScalar pFactor);

kmMat3* kmMat3Assign(kmMat3* pOut, const kmMat3* pIn);
bool kmMat3AreEqual(const kmMat3* pM1, const kmMat3* pM2);

kmMat3* kmMat3Rotation(kmMat3* pOut, const float radians);
kmMat3* kmMat3Rotation(kmMat3* pOut, const kmScalar radians);
kmMat3* kmMat3Scaling(kmMat3* pOut, const kmScalar x, const kmScalar y);
kmMat3* kmMat3Translation(kmMat3* pOut, const kmScalar x, const kmScalar y);

Expand Down
116 changes: 71 additions & 45 deletions src/kazmath/src/mat4.c
Expand Up @@ -33,54 +33,79 @@ kmMat4* kmMat4Identity(kmMat4* pOut)
return pOut;
}

kmMat4* kmMat4Inverse(kmMat4* pOut, kmScalar* pDeterminate, const kmMat4* pM)
kmMat4* kmMat4Inverse(kmMat4* pOut, const kmMat4* pM)
{
assert(0);
/*
/// Returns the inverse of a matrix
inline matrix4 inverse(const matrix4& v)
{
matrix4 a(v), // As a evolves from original mat into identity
b(identity3D()); // b evolves from identity into inverse(a)
float mat[16];

for (int i = 0; i < 16; i++)
{
mat[i] = pM->m_Mat[i];
}

// Loop over cols of a from left to right, eliminating above and below diag
for(int j=0; j<4; ++j) { // Find largest pivot in column j among rows j..3
kmMat4Identity(pOut);


for (int j = 0; j < 4; ++j) // Find largest pivot in column j among rows j..3
{
int i1 = j; // Row with largest pivot candidate

for(int i=j+1; i<4; ++i)
for (int i = j + 1; i < 4; ++i)
{
if (fabs(a.v[i].n[j]) > fabs(a.v[i1].n[j]))
if (fabs(mat[i*4 + j]) > fabs(mat[i1*4 + j]))
i1 = i;
}

// Swap rows i1 and j in a and b to put pivot on diagonal
std::swap(a.v[i1], a.v[j]);
std::swap(b.v[i1], b.v[j]);
float temp[4];
for(int k = 0; k < 4; k++)
{
temp[k] = mat[i1 * 4 + k];
}

for(int k = 0; k < 4; k++)
{
mat[i1 * 4 + k] = mat[j * 4 + k];
mat[j * 4 + k] = temp[k];
}

for(int k = 0; k < 4; k++)
{
temp[k] = pOut->m_Mat[i1 * 4 + k];
}

for(int k = 0; k < 4; k++)
{
pOut->m_Mat[i1 * 4 + k] = pOut->m_Mat[j * 4 + k];
pOut->m_Mat[j * 4 + k] = temp[k];
}

// Scale row j to have a unit diagonal
if(!a.v[j].n[j])
if (!mat[j*4 + j])
{
// Singular matrix - can't invert
log() << error << "Can't invert singular matrix!" << std::endl;
return b;

return pOut;
}

b.v[j] /= a.v[j].n[j];
a.v[j] /= a.v[j].n[j];
for(int k = 0; k < 4; k++)
{
pOut->m_Mat[j * 4 + k] /= mat[j * 4 + j];
mat[j * 4 + k] /= mat[j * 4 + j];
}

// Eliminate off-diagonal elems in col j of a, doing identical ops to b
for(int i=0; i<4; ++i)
for (int i = 0; i < 4; ++i)
{
if(i!=j)
if (i != j)
{
b.v[i] -= a.v[i].n[j]*b.v[j];
a.v[i] -= a.v[i].n[j]*a.v[j];
for(int k = 0; k < 4; k++)
{
pOut->m_Mat[i*4 + k] -= mat[i*4 + j] * pOut->m_Mat[j*4 + k];
mat[i*4 + k] -= mat[i*4 + j] * mat[j*4 + k];
}
}
}
}
return b;
}
*/

return pOut;
}
Expand All @@ -89,9 +114,10 @@ inline matrix4 inverse(const matrix4& v)
bool kmMat4IsIdentity(const kmMat4* pIn)
{
static const float identity [] = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};

return (memcmp(identity, pIn->m_Mat, sizeof(float) * 16) == 0);
}
Expand Down Expand Up @@ -163,7 +189,7 @@ bool kmMat4AreEqual(const kmMat4* pMat1, const kmMat4* pMat2)
for (int i = 0; i < 16; ++i)
{
if (!(pMat1->m_Mat[i] + kmEpsilon > pMat2->m_Mat[i] &&
pMat1->m_Mat[i] - kmEpsilon < pMat2->m_Mat[i]))
pMat1->m_Mat[i] - kmEpsilon < pMat2->m_Mat[i]))
return false;
}

Expand All @@ -181,13 +207,13 @@ kmMat4* kmMat4RotationAxis(kmMat4* pOut, const kmVec3* axis, kmScalar radians)
pOut->m_Mat[2] = -axis->y * rsin + axis->z * axis->x * (1 - rcos);
pOut->m_Mat[3] = 0.0f;

pOut->m_Mat[4] = -axis->z * rsin + axis->x * axis->y *(1-rcos);
pOut->m_Mat[4] = -axis->z * rsin + axis->x * axis->y * (1 - rcos);
pOut->m_Mat[5] = rcos + axis->y * axis->y * (1 - rcos);
pOut->m_Mat[6] = axis->x * rsin + axis->z * axis->y * (1 - rcos);
pOut->m_Mat[7] = 0.0f;

pOut->m_Mat[8] = axis->y * rsin + axis->x * axis->z *(1-rcos);
pOut->m_Mat[9] = -axis->x * rsin + axis->y * axis->z *(1-rcos);
pOut->m_Mat[8] = axis->y * rsin + axis->x * axis->z * (1 - rcos);
pOut->m_Mat[9] = -axis->x * rsin + axis->y * axis->z * (1 - rcos);
pOut->m_Mat[10] = rcos + axis->z * axis->z * (1 - rcos);
pOut->m_Mat[11] = 0.0f;

Expand All @@ -204,9 +230,9 @@ kmMat4* kmMat4RotationX(kmMat4* pOut, const float radians)
{
/*
| 1 0 0 0 |
M = | 0 cos(A) -sin(A) 0 |
| 0 sin(A) cos(A) 0 |
| 0 0 0 1 |
M = | 0 cos(A) -sin(A) 0 |
| 0 sin(A) cos(A) 0 |
| 0 0 0 1 |
*/

Expand Down Expand Up @@ -236,10 +262,10 @@ kmMat4* kmMat4RotationX(kmMat4* pOut, const float radians)
kmMat4* kmMat4RotationY(kmMat4* pOut, const float radians)
{
/*
| cos(A) 0 sin(A) 0 |
M = | 0 1 0 0 |
| -sin(A) 0 cos(A) 0 |
| 0 0 0 1 |
| cos(A) 0 sin(A) 0 |
M = | 0 1 0 0 |
| -sin(A) 0 cos(A) 0 |
| 0 0 0 1 |
*/

pOut->m_Mat[0] = cosf(radians);
Expand Down Expand Up @@ -268,10 +294,10 @@ kmMat4* kmMat4RotationY(kmMat4* pOut, const float radians)
kmMat4* kmMat4RotationZ(kmMat4* pOut, const float radians)
{
/*
| cos(A) -sin(A) 0 0 |
M = | sin(A) cos(A) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
| cos(A) -sin(A) 0 0 |
M = | sin(A) cos(A) 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
*/

pOut->m_Mat[0] = cosf(radians);
Expand Down Expand Up @@ -308,7 +334,7 @@ kmMat4* kmMat4RotationPitchYawRoll(kmMat4* pOut, const kmScalar pitch, const kmS

pOut->m_Mat[0] = (kmScalar) cp * cy;
pOut->m_Mat[4] = (kmScalar) cp * sy;
pOut->m_Mat[8] = (kmScalar) -sp;
pOut->m_Mat[8] = (kmScalar) - sp;

double srsp = sr * sp;
double crsp = cr * sp;
Expand Down
2 changes: 1 addition & 1 deletion src/kazmath/src/mat4.h
Expand Up @@ -35,7 +35,7 @@ extern "C" {
#endif

kmMat4* kmMat4Identity(kmMat4* pOut);
kmMat4* kmMat4Inverse(kmMat4* pOut, kmScalar* pDeterminate, const kmMat4* pM);
kmMat4* kmMat4Inverse(kmMat4* pOut, const kmMat4* pM);
bool kmMat4IsIdentity(const kmMat4* pIn);
kmMat4* kmMat4Transpose(kmMat4* pOut, const kmMat4* pIn);
kmMat4* kmMat4Multiply(kmMat4* pOut, const kmMat4* pM1, const kmMat4* pM2);
Expand Down

0 comments on commit 0d5bd28

Please sign in to comment.