mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-09-08 03:08:16 +00:00
Add more tests that actually break stuff for some reason
This commit is contained in:
@@ -2354,29 +2354,29 @@ hmm_quaternion HMM_Mat4ToQuaternion(hmm_mat4 m)
|
|||||||
float trace = m.Elements[0][0] + m.Elements[1][1] + m.Elements[2][2];
|
float trace = m.Elements[0][0] + m.Elements[1][1] + m.Elements[2][2];
|
||||||
if (trace > 0) {
|
if (trace > 0) {
|
||||||
float s = 0.5f / HMM_SquareRootF(trace + 1.0f);
|
float s = 0.5f / HMM_SquareRootF(trace + 1.0f);
|
||||||
q.X = (m.Elements[2][1] - m.Elements[1][2] ) * s;
|
q.X = (m.Elements[1][2] - m.Elements[2][1] ) * s;
|
||||||
q.Y = (m.Elements[0][2] - m.Elements[2][0] ) * s;
|
q.Y = (m.Elements[2][0] - m.Elements[0][2] ) * s;
|
||||||
q.Z = (m.Elements[1][0] - m.Elements[0][1] ) * s;
|
q.Z = (m.Elements[0][1] - m.Elements[1][0] ) * s;
|
||||||
q.W = 0.25f / s;
|
q.W = 0.25f / s;
|
||||||
} else {
|
} else {
|
||||||
if (m.Elements[0][0] > m.Elements[1][1] && m.Elements[0][0] > m.Elements[2][2]) {
|
if (m.Elements[0][0] > m.Elements[1][1] && m.Elements[0][0] > m.Elements[2][2]) {
|
||||||
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[0][0] - m.Elements[1][1] - m.Elements[2][2]);
|
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[0][0] - m.Elements[1][1] - m.Elements[2][2]);
|
||||||
q.X = 0.25f * s;
|
q.X = 0.25f * s;
|
||||||
q.Y = (m.Elements[0][1] + m.Elements[1][0] ) / s;
|
q.Y = (m.Elements[1][0] + m.Elements[0][1]) / s;
|
||||||
q.Z = (m.Elements[0][2] + m.Elements[2][0] ) / s;
|
q.Z = (m.Elements[2][0] + m.Elements[0][2]) / s;
|
||||||
q.W = (m.Elements[2][1] - m.Elements[1][2] ) / s;
|
q.W = (m.Elements[1][2] - m.Elements[2][1]) / s;
|
||||||
} else if (m.Elements[1][1] > m.Elements[2][2]) {
|
} else if (m.Elements[1][1] > m.Elements[2][2]) {
|
||||||
float s = 2.0f * HMM_SquareRootF( 1.0f + m.Elements[1][1] - m.Elements[0][0] - m.Elements[2][2]);
|
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[1][1] - m.Elements[0][0] - m.Elements[2][2]);
|
||||||
q.X = (m.Elements[0][1] + m.Elements[1][0] ) / s;
|
q.X = (m.Elements[1][0] + m.Elements[0][1]) / s;
|
||||||
q.Y = 0.25f * s;
|
q.Y = 0.25f * s;
|
||||||
q.Z = (m.Elements[1][2] + m.Elements[2][1] ) / s;
|
q.Z = (m.Elements[2][1] + m.Elements[1][2]) / s;
|
||||||
q.W = (m.Elements[0][2] - m.Elements[2][0] ) / s;
|
q.W = (m.Elements[2][0] - m.Elements[0][2]) / s;
|
||||||
} else {
|
} else {
|
||||||
float s = 2.0f * HMM_SquareRootF( 1.0f + m.Elements[2][2] - m.Elements[0][0] - m.Elements[1][1] );
|
float s = 2.0f * HMM_SquareRootF(1.0f + m.Elements[2][2] - m.Elements[0][0] - m.Elements[1][1]);
|
||||||
q.X = (m.Elements[0][2] + m.Elements[2][0] ) / s;
|
q.X = (m.Elements[2][0] + m.Elements[0][2]) / s;
|
||||||
q.Y = (m.Elements[1][2] + m.Elements[2][1] ) / s;
|
q.Y = (m.Elements[2][1] + m.Elements[1][2]) / s;
|
||||||
q.Z = 0.25f * s;
|
q.Z = 0.25f * s;
|
||||||
q.W = (m.Elements[1][0] - m.Elements[0][1] ) / s;
|
q.W = (m.Elements[0][1] - m.Elements[1][0]) / s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,9 @@
|
|||||||
#include "../HandmadeTest.h"
|
#include "../HandmadeTest.h"
|
||||||
|
|
||||||
|
void printQuat(hmm_quaternion quat) {
|
||||||
|
printf("\n%f %f %f %f", quat.X, quat.Y, quat.Z, quat.W);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(MatrixOps, Transpose)
|
TEST(MatrixOps, Transpose)
|
||||||
{
|
{
|
||||||
hmm_mat4 m4 = HMM_Mat4(); // will have 1 - 16
|
hmm_mat4 m4 = HMM_Mat4(); // will have 1 - 16
|
||||||
@@ -37,20 +41,114 @@ TEST(MatrixOps, Transpose)
|
|||||||
|
|
||||||
TEST(MatrixOps, ToQuaternion)
|
TEST(MatrixOps, ToQuaternion)
|
||||||
{
|
{
|
||||||
hmm_mat4 rotateY = {
|
{ // Test 90 degree rotation about X axis
|
||||||
0.0f, 0.0f, -1.0f, 0.0f, // first column (X)
|
hmm_mat4 rot = {
|
||||||
0.0f, 1.0f, 0.0f, 0.0f, // second column (Y)
|
1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||||
1.0f, 0.0f, 0.0f, 0.0f, // third column (Z)
|
0.0f, 0.0f, 1.0f, 0.0f, // second column (Y)
|
||||||
0.0f, 0.0f, 0.0f, 1.0f
|
0.0f, -1.0f, 0.0f, 0.0f, // third column (Z)
|
||||||
};
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(1.0f, 0.0f, 0.0f), HMM_ToRadians(90.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // Test 90 degree rotation about Y axis
|
||||||
|
hmm_mat4 rot = {
|
||||||
|
0.0f, 0.0f, -1.0f, 0.0f, // first column (X)
|
||||||
|
0.0f, 1.0f, 0.0f, 0.0f, // second column (Y)
|
||||||
|
1.0f, 0.0f, 0.0f, 0.0f, // third column (Z)
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), HMM_ToRadians(90.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // Test 90 degree rotation about Z axis
|
||||||
|
hmm_mat4 rot = {
|
||||||
|
0.0f, 1.0f, 0.0f, 0.0f, // first column (X)
|
||||||
|
-1.0f, 0.0f, 0.0f, 0.0f, // second column (Y)
|
||||||
|
0.0f, 0.0f, 1.0f, 0.0f, // third column (Z)
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 0.0f, 1.0f), HMM_ToRadians(90.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
|
|
||||||
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), HMM_ToRadians(90.0f));
|
{ // Test 180 degree rotation about X axis
|
||||||
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rotateY);
|
hmm_mat4 rot = {
|
||||||
|
1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||||
|
0.0f, -1.0f, 1.0f, 0.0f, // second column (Y)
|
||||||
|
0.0f, 0.0f, -1.0f, 0.0f, // third column (Z)
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(1.0f, 0.0f, 0.0f), HMM_ToRadians(180.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
printf("%f %f %f %f\n", expected.X, expected.Y, expected.Z, expected.W);
|
printQuat(expected);
|
||||||
|
printQuat(actualResult);
|
||||||
|
|
||||||
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // Test 180 degree rotation about Y axis
|
||||||
|
hmm_mat4 rot = {
|
||||||
|
-1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||||
|
0.0f, 1.0f, 1.0f, 0.0f, // second column (Y)
|
||||||
|
0.0f, 0.0f, -1.0f, 0.0f, // third column (Z)
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 1.0f, 0.0f), HMM_ToRadians(180.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
|
printQuat(expected);
|
||||||
|
printQuat(actualResult);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // Test 180 degree rotation about Z axis
|
||||||
|
hmm_mat4 rot = {
|
||||||
|
-1.0f, 0.0f, 0.0f, 0.0f, // first column (X)
|
||||||
|
0.0f, -1.0f, 1.0f, 0.0f, // second column (Y)
|
||||||
|
0.0f, 0.0f, 1.0f, 0.0f, // third column (Z)
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
|
hmm_quaternion expected = HMM_QuaternionFromAxisAngle(HMM_Vec3(0.0f, 0.0f, 1.0f), HMM_ToRadians(180.0f));
|
||||||
|
hmm_quaternion actualResult = HMM_Mat4ToQuaternion(rot);
|
||||||
|
|
||||||
|
printQuat(expected);
|
||||||
|
printQuat(actualResult);
|
||||||
|
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.X, expected.X);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Y, expected.Y);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.Z, expected.Z);
|
||||||
|
EXPECT_FLOAT_EQ(actualResult.W, expected.W);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user