void UT_quat(const char* params) { Lumix::Quat q1, q2; Lumix::Matrix mtx; q1.set(1.0f, 0.0f, 0.0f, 0.0f); LUMIX_EXPECT_CLOSE_EQ(q1.x, 1.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, 0.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, 0.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, 0.0f, 0.001f); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1.set(0, 0, 1, 0); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1 = Lumix::Quat(Lumix::Vec3(1, 0, 0), Lumix::Math::PI / 4); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1 = Lumix::Quat(Lumix::Vec3(1, 1, 0), Lumix::Math::PI / 6); q1.normalize(); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1.set(0.923879862f, 0, 0, -0.382683545f); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); Lumix::Quat qeuler; for (float x = -Lumix::Math::PI + 0.01f; x < Lumix::Math::PI; x += 0.1f) { for (float y = -Lumix::Math::PI + 0.01f; y < Lumix::Math::PI; y += 0.1f) { float x = 0; for (float z = -Lumix::Math::PI + 0.01f; z < Lumix::Math::PI; z += 0.1f) { qeuler.fromEuler(Lumix::Vec3(x, y, z)); Lumix::Vec3 u(0, 1, 0); u = qeuler * u; Lumix::Vec3 euler = qeuler.toEuler(); LUMIX_EXPECT_CLOSE_EQ(euler.x, x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(euler.y, y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(euler.z, z, 0.001f); } } } }
void UT_matrix(const char* params) { Lumix::DefaultAllocator allocator; for (int i = 0; i < 16; ++i) { LUMIX_EXPECT_CLOSE_EQ(*((&Lumix::Matrix::IDENTITY.m11) + i), i % 5 == 0 ? 1.0f : 0.0f, 0.001f); } Lumix::Matrix mtx = Lumix::Matrix::IDENTITY; expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); LUMIX_EXPECT_CLOSE_EQ(mtx.determinant(), 1.0f, 0.001f); mtx.inverse(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.fastInverse(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.transpose(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().z, 0, 0.001f); Lumix::Quat rot; mtx.getRotation(rot); LUMIX_EXPECT_CLOSE_EQ(rot.x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.w, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().x, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().y, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().z, 1, 0.001f); mtx.multiply3x3(1.0f); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); Lumix::Vec3 v = mtx.multiplyPosition(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(v.x, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(v.y, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(v.z, 3, 0.001f); mtx.setTranslation(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(mtx.m41, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m42, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m43, 3, 0.001f); mtx.setIdentity(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.translate(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(mtx.m41, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m42, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m43, 3, 0.001f); mtx.setIdentity(); mtx.setXVector(Lumix::Vec3(3, 2, 1)); LUMIX_EXPECT_CLOSE_EQ(mtx.m11, 3, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m12, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m13, 1, 0.001f); mtx.setYVector(Lumix::Vec3(6, 5, 4)); LUMIX_EXPECT_CLOSE_EQ(mtx.m21, 6, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m22, 5, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m23, 4, 0.001f); mtx.setZVector(Lumix::Vec3(9, 8, 7)); LUMIX_EXPECT_CLOSE_EQ(mtx.m31, 9, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m32, 8, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m33, 7, 0.001f); Lumix::Matrix old = mtx; expectSameMatrices(mtx, old); mtx.transpose(); mtx.transpose(); expectSameMatrices(mtx, old); mtx.inverse(); mtx.inverse(); expectSameMatrices(mtx, old); mtx.setIdentity(); mtx.multiply3x3(2.0f); old = mtx; mtx.inverse(); mtx.inverse(); expectSameMatrices(mtx, old); for (int i = 0; i < 16; ++i) { *((&mtx.m11) + i) = (float)i; } old = mtx; mtx = mtx * Lumix::Matrix::IDENTITY; expectSameMatrices(mtx, old); mtx = Lumix::Matrix::IDENTITY * mtx; expectSameMatrices(mtx, old); Lumix::Matrix euler; euler.fromEuler(0, 0, 0); expectSameMatrices(euler, Lumix::Matrix::IDENTITY); Lumix::Matrix check_matrix; check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m11 = -1; check_matrix.m33 = -1; euler.fromEuler(3.14159265f, 0, 0); expectSameMatrices(euler, check_matrix); check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m11 = -1; check_matrix.m22 = -1; euler.fromEuler(0, 0, 3.14159265f); expectSameMatrices(euler, check_matrix); check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m22 = -1; check_matrix.m33 = -1; euler.fromEuler(0, 3.14159265f, 0); expectSameMatrices(euler, check_matrix); }