diff --git a/include/cglm/quat.h b/include/cglm/quat.h index cf1f325..75f0c6c 100644 --- a/include/cglm/quat.h +++ b/include/cglm/quat.h @@ -482,22 +482,22 @@ glm_quat_mul(versor p, versor q, versor dest) { CGLM_INLINE void glm_quat_mat4(versor q, mat4 dest) { + CGLM_ALIGN(16) versor p; float w, x, y, z, xx, yy, zz, xy, yz, xz, - wx, wy, wz, norm, s; + wx, wy, wz; - norm = glm_quat_norm(q); - s = norm > 0.0f ? 2.0f / norm : 0.0f; + glm_quat_normalize_to(q, p); - x = q[0]; - y = q[1]; - z = q[2]; - w = q[3]; + x = p[0]; + y = p[1]; + z = p[2]; + w = p[3]; - xx = s * x * x; xy = s * x * y; wx = s * w * x; - yy = s * y * y; yz = s * y * z; wy = s * w * y; - zz = s * z * z; xz = s * x * z; wz = s * w * z; + xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x; + yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y; + zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z; dest[0][0] = 1.0f - yy - zz; dest[1][1] = 1.0f - xx - zz; @@ -529,22 +529,22 @@ glm_quat_mat4(versor q, mat4 dest) { CGLM_INLINE void glm_quat_mat4t(versor q, mat4 dest) { + CGLM_ALIGN(16) versor p; float w, x, y, z, xx, yy, zz, xy, yz, xz, - wx, wy, wz, norm, s; + wx, wy, wz; - norm = glm_quat_norm(q); - s = norm > 0.0f ? 2.0f / norm : 0.0f; + glm_quat_normalize_to(q, p); - x = q[0]; - y = q[1]; - z = q[2]; - w = q[3]; + x = p[0]; + y = p[1]; + z = p[2]; + w = p[3]; - xx = s * x * x; xy = s * x * y; wx = s * w * x; - yy = s * y * y; yz = s * y * z; wy = s * w * y; - zz = s * z * z; xz = s * x * z; wz = s * w * z; + xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x; + yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y; + zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z; dest[0][0] = 1.0f - yy - zz; dest[1][1] = 1.0f - xx - zz; @@ -576,22 +576,22 @@ glm_quat_mat4t(versor q, mat4 dest) { CGLM_INLINE void glm_quat_mat3(versor q, mat3 dest) { + CGLM_ALIGN(16) versor p; float w, x, y, z, xx, yy, zz, xy, yz, xz, - wx, wy, wz, norm, s; + wx, wy, wz; - norm = glm_quat_norm(q); - s = norm > 0.0f ? 2.0f / norm : 0.0f; + glm_quat_normalize_to(q, p); - x = q[0]; - y = q[1]; - z = q[2]; - w = q[3]; + x = p[0]; + y = p[1]; + z = p[2]; + w = p[3]; - xx = s * x * x; xy = s * x * y; wx = s * w * x; - yy = s * y * y; yz = s * y * z; wy = s * w * y; - zz = s * z * z; xz = s * x * z; wz = s * w * z; + xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x; + yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y; + zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z; dest[0][0] = 1.0f - yy - zz; dest[1][1] = 1.0f - xx - zz; @@ -615,22 +615,22 @@ glm_quat_mat3(versor q, mat3 dest) { CGLM_INLINE void glm_quat_mat3t(versor q, mat3 dest) { + CGLM_ALIGN(16) versor p; float w, x, y, z, xx, yy, zz, xy, yz, xz, - wx, wy, wz, norm, s; + wx, wy, wz; - norm = glm_quat_norm(q); - s = norm > 0.0f ? 2.0f / norm : 0.0f; + glm_quat_normalize_to(q, p); - x = q[0]; - y = q[1]; - z = q[2]; - w = q[3]; + x = p[0]; + y = p[1]; + z = p[2]; + w = p[3]; - xx = s * x * x; xy = s * x * y; wx = s * w * x; - yy = s * y * y; yz = s * y * z; wy = s * w * y; - zz = s * z * z; xz = s * x * z; wz = s * w * z; + xx = 2.0f * x * x; xy = 2.0f * x * y; wx = 2.0f * w * x; + yy = 2.0f * y * y; yz = 2.0f * y * z; wy = 2.0f * w * y; + zz = 2.0f * z * z; xz = 2.0f * x * z; wz = 2.0f * w * z; dest[0][0] = 1.0f - yy - zz; dest[1][1] = 1.0f - xx - zz; diff --git a/test/src/test_quat.h b/test/src/test_quat.h index da831fe..5273332 100644 --- a/test/src/test_quat.h +++ b/test/src/test_quat.h @@ -504,9 +504,10 @@ TEST_IMPL(GLM_PREFIX, quat_mul) { TEST_IMPL(GLM_PREFIX, quat_mat4) { mat4 m1, m2; - versor q1, q2, q3; + versor q1, q2, q3, scaled, original; vec3 axis1; vec3 axis2 = {1.9f, 2.3f, 4.5f}; + float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f}; int i; GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); @@ -543,14 +544,47 @@ TEST_IMPL(GLM_PREFIX, quat_mat4) { ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); } + + /* Scaling a quaternion must not change its rotation or mutate it. */ + GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); + GLM(rotate_make)(m2, GLM_PI_4f, axis2); + for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) { + glm_vec4_scale(q1, scales[i], scaled); + glm_vec4_copy(scaled, original); + GLM(quat_mat4)(scaled, m1); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + ASSERT(scaled[0] == original[0] && scaled[1] == original[1]); + ASSERT(scaled[2] == original[2] && scaled[3] == original[3]); + } + + /* Issue #503: a non-unit half-turn about Z. */ + glm_vec4_zero(scaled); + scaled[2] = 2.0f; + GLM(quat_mat4)(scaled, m1); + glm_mat4_identity(m2); + m2[0][0] = m2[1][1] = -1.0f; + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + + /* Normalizing first avoids overflowing 2 / dot for tiny inputs. */ + scaled[2] = 1e-20f; + GLM(quat_mat4)(scaled, m1); + ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f)); + + /* Keep the existing identity fallback for a zero quaternion. */ + glm_vec4_zero(scaled); + GLM(quat_mat4)(scaled, m1); + glm_mat4_identity(m2); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } TEST_IMPL(GLM_PREFIX, quat_mat4t) { mat4 m1, m2; - versor q1, q2, q3; + versor q1, q2, q3, scaled, original; vec3 axis1; vec3 axis2 = {1.9f, 2.3f, 4.5f}; + float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f}; int i; GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); @@ -594,15 +628,52 @@ TEST_IMPL(GLM_PREFIX, quat_mat4t) { ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); } + + /* Scaling a quaternion must not change its rotation or mutate it. */ + GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); + GLM(rotate_make)(m2, GLM_PI_4f, axis2); + for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) { + glm_vec4_scale(q1, scales[i], scaled); + glm_vec4_copy(scaled, original); + GLM(quat_mat4t)(scaled, m1); + glm_mat4_transpose(m1); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + ASSERT(scaled[0] == original[0] && scaled[1] == original[1]); + ASSERT(scaled[2] == original[2] && scaled[3] == original[3]); + } + + /* Issue #503: a non-unit half-turn about Z. */ + glm_vec4_zero(scaled); + scaled[2] = 2.0f; + GLM(quat_mat4t)(scaled, m1); + glm_mat4_transpose(m1); + glm_mat4_identity(m2); + m2[0][0] = m2[1][1] = -1.0f; + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + + /* Normalizing first avoids overflowing 2 / dot for tiny inputs. */ + scaled[2] = 1e-20f; + GLM(quat_mat4t)(scaled, m1); + glm_mat4_transpose(m1); + ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f)); + + /* Keep the existing identity fallback for a zero quaternion. */ + glm_vec4_zero(scaled); + GLM(quat_mat4t)(scaled, m1); + glm_mat4_transpose(m1); + glm_mat4_identity(m2); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } TEST_IMPL(GLM_PREFIX, quat_mat3) { mat4 m1, m2; mat3 m3; - versor q1, q2, q3; + versor q1, q2, q3, scaled, original; vec3 axis1; vec3 axis2 = {1.9f, 2.3f, 4.5f}; + float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f}; int i; GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); @@ -648,15 +719,56 @@ TEST_IMPL(GLM_PREFIX, quat_mat3) { ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); } + + /* Scaling a quaternion must not change its rotation or mutate it. */ + GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); + GLM(rotate_make)(m2, GLM_PI_4f, axis2); + for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) { + glm_vec4_scale(q1, scales[i], scaled); + glm_vec4_copy(scaled, original); + GLM(quat_mat3)(scaled, m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + ASSERT(scaled[0] == original[0] && scaled[1] == original[1]); + ASSERT(scaled[2] == original[2] && scaled[3] == original[3]); + } + + /* Issue #503: a non-unit half-turn about Z. */ + glm_vec4_zero(scaled); + scaled[2] = 2.0f; + GLM(quat_mat3)(scaled, m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + glm_mat4_identity(m2); + m2[0][0] = m2[1][1] = -1.0f; + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + + /* Normalizing first avoids overflowing 2 / dot for tiny inputs. */ + scaled[2] = 1e-20f; + GLM(quat_mat3)(scaled, m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f)); + + /* Keep the existing identity fallback for a zero quaternion. */ + glm_vec4_zero(scaled); + GLM(quat_mat3)(scaled, m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + glm_mat4_identity(m2); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } TEST_IMPL(GLM_PREFIX, quat_mat3t) { mat4 m1, m2; mat3 m3; - versor q1, q2, q3; + versor q1, q2, q3, scaled, original; vec3 axis1; vec3 axis2 = {1.9f, 2.3f, 4.5f}; + float scales[] = {2.0f, 0.5f, -2.0f, -0.5f, 1e-10f, 1e10f}; int i; GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); @@ -706,6 +818,50 @@ TEST_IMPL(GLM_PREFIX, quat_mat3t) { ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.000009f)); } + + /* Scaling a quaternion must not change its rotation or mutate it. */ + GLM(quat)(q1, GLM_PI_4f, 1.9f, 2.3f, 4.5f); + GLM(rotate_make)(m2, GLM_PI_4f, axis2); + for (i = 0; i < (int)(sizeof(scales) / sizeof(scales[0])); i++) { + glm_vec4_scale(q1, scales[i], scaled); + glm_vec4_copy(scaled, original); + GLM(quat_mat3t)(scaled, m3); + glm_mat3_transpose(m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + ASSERT(scaled[0] == original[0] && scaled[1] == original[1]); + ASSERT(scaled[2] == original[2] && scaled[3] == original[3]); + } + + /* Issue #503: a non-unit half-turn about Z. */ + glm_vec4_zero(scaled); + scaled[2] = 2.0f; + GLM(quat_mat3t)(scaled, m3); + glm_mat3_transpose(m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + glm_mat4_identity(m2); + m2[0][0] = m2[1][1] = -1.0f; + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + + /* Normalizing first avoids overflowing 2 / dot for tiny inputs. */ + scaled[2] = 1e-20f; + GLM(quat_mat3t)(scaled, m3); + glm_mat3_transpose(m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00002f)); + + /* Keep the existing identity fallback for a zero quaternion. */ + glm_vec4_zero(scaled); + GLM(quat_mat3t)(scaled, m3); + glm_mat3_transpose(m3); + glm_mat4_identity(m1); + glm_mat4_ins3(m3, m1); + glm_mat4_identity(m2); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } @@ -843,6 +999,12 @@ TEST_IMPL(GLM_PREFIX, quat_look) { ASSERTIFY(test_assert_mat4_eq(m1, m2)); + + /* A scaled orientation must yield the same camera view. */ + glm_vec4_scale(q1, -2.0f, q1); + GLM(quat_look)(v1, q1, m1); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } @@ -1006,6 +1168,16 @@ TEST_IMPL(GLM_PREFIX, quat_rotate) { ASSERT(test_eq(v1[1], 0.0f)) ASSERT(test_eq(v1[2], 1.0f)) + + /* Non-unit orientation must not add scaling to the transform. */ + glm_mat4_identity(m1); + glm_mat4_identity(m2); + glm_quatv(q1, GLM_PI_2f, GLM_YUP); + glm_vec4_scale(q1, 2.0f, q1); + GLM(quat_rotate)(m1, q1, m1); + glm_rotate(m2, GLM_PI_2f, GLM_YUP); + ASSERTIFY(test_assert_mat4_eq(m1, m2)); + TEST_SUCCESS } diff --git a/test/src/test_struct.c b/test/src/test_struct.c index d040225..a352c83 100644 --- a/test/src/test_struct.c +++ b/test/src/test_struct.c @@ -114,6 +114,27 @@ TEST_IMPL(quats_zero_init) { TEST_SUCCESS } +TEST_IMPL(quats_mat_nonunit) { + versors q = {{0.0f, 0.0f, 2.0f, 0.0f}}; + mat4s m4; + mat3s m3; + mat4 expected4 = GLM_MAT4_IDENTITY_INIT; + mat3 expected3 = GLM_MAT3_IDENTITY_INIT; + + expected4[0][0] = expected4[1][1] = -1.0f; + expected3[0][0] = expected3[1][1] = -1.0f; + + m4 = glms_quat_mat4(q); + ASSERTIFY(test_assert_mat4_eq(m4.raw, expected4)); + m4 = glms_quat_mat4t(q); + ASSERTIFY(test_assert_mat4_eq(m4.raw, expected4)); + m3 = glms_quat_mat3(q); + ASSERTIFY(test_assert_mat3_eq(m3.raw, expected3)); + m3 = glms_quat_mat3t(q); + ASSERTIFY(test_assert_mat3_eq(m3.raw, expected3)); + TEST_SUCCESS +} + TEST_IMPL(vec3s_one_init) { vec3s vec3_one = GLMS_VEC3_ONE_INIT; vec3 vec3_one_a = GLM_VEC3_ONE_INIT; diff --git a/test/tests.h b/test/tests.h index ce3215d..8ed890c 100644 --- a/test/tests.h +++ b/test/tests.h @@ -1228,6 +1228,7 @@ TEST_DECLARE(mat4x2s_zero) TEST_DECLARE(mat4x3s_zero_init) TEST_DECLARE(mat4x3s_zero) TEST_DECLARE(quats_zero_init) +TEST_DECLARE(quats_mat_nonunit) TEST_DECLARE(vec3s_one_init) TEST_DECLARE(vec3s_zero_init) TEST_DECLARE(vec4s_black_init) @@ -2447,6 +2448,7 @@ TEST_LIST { TEST_ENTRY(mat4x3s_zero_init) TEST_ENTRY(mat4x3s_zero) TEST_ENTRY(quats_zero_init) + TEST_ENTRY(quats_mat_nonunit) TEST_ENTRY(vec3s_one_init) TEST_ENTRY(vec3s_zero_init) TEST_ENTRY(vec4s_black_init)