got the euler to quat xyz working and got the tests to pass

This commit is contained in:
John Choi
2023-12-07 12:21:55 -06:00
parent 05ea35ffec
commit d67ac97323
3 changed files with 32 additions and 19 deletions

View File

@@ -142,7 +142,7 @@ glm_quat_init(versor q, float x, float y, float z, float w) {
//TODO: telephone001's eulertoquat
/*!
* @brief creates a quaternion using rotation angles and does does rotations in x y z order
* @brief creates NEW quaternion using rotation angles and does does rotations in x y z order
*
* @param[out] q quaternion
* @param[in] angle angles x y z (radians)
@@ -150,20 +150,21 @@ glm_quat_init(versor q, float x, float y, float z, float w) {
CGLM_INLINE
void
glm_euler_xyz_quat(versor q, vec3 angles) {
float xs = sinf(angles[0] / 2.0);
float xc = cosf(angles[0] / 2.0);
float xs = sinf(angles[0] / 2.0f);
float xc = cosf(angles[0] / 2.0f);
float ys = sinf(angles[1] / 2.0);
float yc = cosf(angles[1] / 2.0);
float ys = sinf(angles[1] / 2.0f);
float yc = cosf(angles[1] / 2.0f);
float zs = sinf(angles[2] / 2.0);
float zc = cosf(angles[2] / 2.0);
float zs = sinf(angles[2] / 2.0f);
float zc = cosf(angles[2] / 2.0f);
glm_quat_init(q,
zc * yc * xs - zs * ys * xs,
zc * ys * xc - zs * yc * xs,
zs * yc * xc - zs * yc * xs,
zc * yc * xs - zs * ys * xc,
zc * ys * xc + zs * yc * xs,
-zc * ys * xs + zs * yc * xc,
zc * yc * xc + zs * ys * xs);
}
/*!

View File

@@ -123,29 +123,39 @@ TEST_IMPL(GLM_PREFIX, quatv) {
// telephone001 changes (remove comment when done) TODO
TEST_IMPL(GLM_PREFIX, euler_xyz_quat) {
vec3 axis_x = {1.0f, 0.0f, 0.0f};
vec3 axis_y = {0.0f, 1.0f, 0.0f};
vec3 axis_z = {0.0f, 0.0f, 1.0f};
/*random angles for testing*/
vec3 angles = {glm_rad(30.0f), glm_rad(60.0f), glm_rad(90.0f)};
/*quaternion representations for rotations*/
versor rot_x = {0.0f, 0.0f, 0.0f, 1.0f};
versor rot_y = {0.0f, 0.0f, 0.0f, 1.0f};
versor rot_z = {0.0f, 0.0f, 0.0f, 1.0f};
//random angles for testing
vec3 angles = {glm_rad(30.0f), glm_rad(60.0f), glm_rad(90.0f)};
vec3 axis_x = {1.0f, 0.0f, 0.0f};
vec3 axis_y = {0.0f, 1.0f, 0.0f};
vec3 axis_z = {0.0f, 0.0f, 1.0f};
versor expected = {0.0f, 0.0f, 0.0f, 1.0f};
versor result = {0.0f, 0.0f, 0.0f, 0.0f};
/*create the rotation quaternions using the angles and axises*/
glm_quatv(rot_x, angles[0], axis_x);
glm_quatv(rot_y, angles[1], axis_y);
glm_quatv(rot_z, angles[2], axis_z);
versor expected = {0.0f, 0.0f, 0.0f, 1.0f};
/*apply the rotations to a unit quaternion in xyz order*/
glm_quat_mul(rot_x, expected, expected);
glm_quat_mul(rot_y, expected, expected);
glm_quat_mul(rot_z, expected, expected);
versor result;
glm_euler_xyz_quat(angles, result);
ASSERTIFY(test_assert_vec3_eq(result, expected))
/*use my function to get the quaternion*/
glm_euler_xyz_quat(result, angles);
fprintf(stderr,"TEST\n");
for (int i = 0; i < 4; i++) {
fprintf(stderr, "%f vs %f\n", expected[i], result[i]);
}
ASSERTIFY(test_assert_quat_eq(result, expected))
TEST_SUCCESS
}

View File

@@ -374,6 +374,7 @@ TEST_DECLARE(glm_quat_identity)
TEST_DECLARE(glm_quat_identity_array)
TEST_DECLARE(glm_quat_init)
TEST_DECLARE(glm_quatv)
TEST_DECLARE(glm_euler_xyz_quat)
TEST_DECLARE(glm_quat)
TEST_DECLARE(glm_quat_copy)
TEST_DECLARE(glm_quat_norm)
@@ -1351,6 +1352,7 @@ TEST_LIST {
TEST_ENTRY(glm_quat_identity_array)
TEST_ENTRY(glm_quat_init)
TEST_ENTRY(glm_quatv)
TEST_ENTRY(glm_euler_xyz_quat)
TEST_ENTRY(glm_quat)
TEST_ENTRY(glm_quat_copy)
TEST_ENTRY(glm_quat_norm)