From 4ee6aea037d4006e0832ab87827eb41af6ef06f5 Mon Sep 17 00:00:00 2001 From: John Choi Date: Fri, 8 Dec 2023 13:05:53 -0600 Subject: [PATCH] made quat struct and also exported it --- include/cglm/call/quat.h | 24 ++++++++++ include/cglm/quat.h | 8 +++- include/cglm/struct/quat.h | 97 ++++++++++++++++++++++++++++++++++++++ src/quat.c | 36 ++++++++++++++ test/src/test_quat.h | 39 ++++++++++++++- 5 files changed, 200 insertions(+), 4 deletions(-) diff --git a/include/cglm/call/quat.h b/include/cglm/call/quat.h index 1fe5195..f24dc59 100644 --- a/include/cglm/call/quat.h +++ b/include/cglm/call/quat.h @@ -25,6 +25,30 @@ CGLM_EXPORT void glmc_quat_init(versor q, float x, float y, float z, float w); +CGLM_EXPORT +void +glmc_euler_xyz_quat(versor q, vec3 angles); + +CGLM_EXPORT +void +glmc_euler_xzy_quat(versor q, vec3 angles); + +CGLM_EXPORT +void +glmc_euler_yxz_quat(versor q, vec3 angles); + +CGLM_EXPORT +void +glmc_euler_yzx_quat(versor q, vec3 angles); + +CGLM_EXPORT +void +glmc_euler_zxy_quat(versor q, vec3 angles); + +CGLM_EXPORT +void +glmc_euler_zyx_quat(versor q, vec3 angles); + CGLM_EXPORT void glmc_quat(versor q, float angle, float x, float y, float z); diff --git a/include/cglm/quat.h b/include/cglm/quat.h index 0e19d0c..e8580e7 100644 --- a/include/cglm/quat.h +++ b/include/cglm/quat.h @@ -13,6 +13,12 @@ Functions: CGLM_INLINE void glm_quat_identity(versor q); CGLM_INLINE void glm_quat_init(versor q, float x, float y, float z, float w); + CGLM_INLINE void glm_euler_xyz_quat(versor q, vec3 angles); + CGLM_INLINE void glm_euler_xzy_quat(versor q, vec3 angles); + CGLM_INLINE void glm_euler_yxz_quat(versor q, vec3 angles); + CGLM_INLINE void glm_euler_yzx_quat(versor q, vec3 angles); + CGLM_INLINE void glm_euler_zxy_quat(versor q, vec3 angles); + CGLM_INLINE void glm_euler_zyx_quat(versor q, vec3 angles); CGLM_INLINE void glm_quat(versor q, float angle, float x, float y, float z); CGLM_INLINE void glm_quatv(versor q, float angle, vec3 axis); CGLM_INLINE void glm_quat_copy(versor q, versor dest); @@ -139,8 +145,6 @@ glm_quat_init(versor q, float x, float y, float z, float w) { q[3] = w; } -//TODO: telephone001's eulertoquat - /*! * @brief creates NEW quaternion using rotation angles and does * rotations in x y z order (roll pitch yaw) diff --git a/include/cglm/struct/quat.h b/include/cglm/struct/quat.h index a37cdc5..2384911 100644 --- a/include/cglm/struct/quat.h +++ b/include/cglm/struct/quat.h @@ -14,6 +14,12 @@ CGLM_INLINE versors glms_quat_identity(void) CGLM_INLINE void glms_quat_identity_array(versor *q, size_t count) CGLM_INLINE versors glms_quat_init(float x, float y, float z, float w) + CGLM_INLINE versors glms_euler_xyz_quat(versors q, vec3s angles) + CGLM_INLINE versors glms_euler_xzy_quat(versors q, vec3s angles) + CGLM_INLINE versors glms_euler_yxz_quat(versors q, vec3s angles) + CGLM_INLINE versors glms_euler_yzx_quat(versors q, vec3s angles) + CGLM_INLINE versors glms_euler_zxy_quat(versors q, vec3s angles) + CGLM_INLINE versors glms_euler_zyx_quat(versors q, vec3s angles) CGLM_INLINE versors glms_quatv(float angle, vec3s axis) CGLM_INLINE versors glms_quat(float angle, float x, float y, float z) CGLM_INLINE versors glms_quat_from_vecs(vec3s a, vec3s b) @@ -120,6 +126,97 @@ glms_quat_(init)(float x, float y, float z, float w) { return dest; } +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in x y z order (roll pitch yaw) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_xyz_quat(versors q, vec3s angles) { + versors dest; + glm_euler_xyz_quat(dest.raw, angles.raw); + return dest; +} + +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in x z y order (roll yaw pitch) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_xzy_quat(versors q, vec3s angles) { + versors dest; + glm_euler_xzy_quat(dest.raw, angles.raw); + return dest; +} + +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in y x z order (pitch roll yaw) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_yxz_quat(versors q, vec3s angles) { + versors dest; + glm_euler_yxz_quat(dest.raw, angles.raw); + return dest; +} + +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in y z x order (pitch yaw roll) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_yzx_quat(versors q, vec3s angles) { + versors dest; + glm_euler_yzx_quat(dest.raw, angles.raw); + return dest; +} + +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in z x y order (yaw roll pitch) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_zxy_quat(versors q, vec3s angles) { + versors dest; + glm_euler_zxy_quat(dest.raw, angles.raw); + return dest; +} + +/*! + * @brief creates NEW quaternion using rotation angles and does + * rotations in z y x order (yaw pitch roll) + * + * @param[out] q quaternion + * @param[in] angle angles x y z (radians) + */ +CGLM_INLINE +versors +glms_euler_zyx_quat(versors q, vec3s angles) { + versors dest; + glm_euler_zyx_quat(dest.raw, angles.raw); + return dest; +} + + /*! * @brief creates NEW quaternion with axis vector * diff --git a/src/quat.c b/src/quat.c index 1796939..a393789 100644 --- a/src/quat.c +++ b/src/quat.c @@ -26,6 +26,42 @@ glmc_quat_init(versor q, float x, float y, float z, float w) { glm_quat_init(q, x, y, z, w); } +CGLM_EXPORT +void +glmc_euler_xyz_quat(versor q, vec3 angles) { + glm_euler_xyz_quat(q, angles); +} + +CGLM_EXPORT +void +glmc_euler_xzy_quat(versor q, vec3 angles) { + glm_euler_xzy_quat(q, angles); +} + +CGLM_EXPORT +void +glmc_euler_yxz_quat(versor q, vec3 angles) { + glm_euler_yxz_quat(q, angles); +} + +CGLM_EXPORT +void +glmc_euler_yzx_quat(versor q, vec3 angles) { + glm_euler_yzx_quat(q, angles); +} + +CGLM_EXPORT +void +glmc_euler_zxy_quat(versor q, vec3 angles) { + glm_euler_zxy_quat(q, angles); +} + +CGLM_EXPORT +void +glmc_euler_zyx_quat(versor q, vec3 angles) { + glm_euler_zyx_quat(q, angles); +} + CGLM_EXPORT void glmc_quat(versor q, float angle, float x, float y, float z) { diff --git a/test/src/test_quat.h b/test/src/test_quat.h index 31a1152..6a26756 100644 --- a/test/src/test_quat.h +++ b/test/src/test_quat.h @@ -121,7 +121,6 @@ TEST_IMPL(GLM_PREFIX, quatv) { TEST_SUCCESS } -// telephone001 changes (remove comment when done) TODO TEST_IMPL(GLM_PREFIX, euler_xyz_quat) { for (int i = 0; i < 100; i++) { /*random angles for testing*/ @@ -154,8 +153,12 @@ TEST_IMPL(GLM_PREFIX, euler_xyz_quat) { /*use my function to get the quaternion*/ glm_euler_xyz_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } + /*Start gimbal lock tests*/ for (float x = -90.0f; x <= 90.0f; x += 90.0f) { @@ -189,6 +192,9 @@ TEST_IMPL(GLM_PREFIX, euler_xyz_quat) { /*use my function to get the quaternion*/ glm_euler_xyz_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -228,6 +234,9 @@ TEST_IMPL(GLM_PREFIX, euler_xzy_quat) { /*use my function to get the quaternion*/ glm_euler_xzy_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } @@ -263,6 +272,9 @@ TEST_IMPL(GLM_PREFIX, euler_xzy_quat) { /*use my function to get the quaternion*/ glm_euler_xzy_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -303,6 +315,9 @@ TEST_IMPL(GLM_PREFIX, euler_yxz_quat) { /*use my function to get the quaternion*/ glm_euler_yxz_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } @@ -338,6 +353,9 @@ TEST_IMPL(GLM_PREFIX, euler_yxz_quat) { /*use my function to get the quaternion*/ glm_euler_yxz_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -378,6 +396,9 @@ TEST_IMPL(GLM_PREFIX, euler_yzx_quat) { /*use my function to get the quaternion*/ glm_euler_yzx_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } @@ -413,6 +434,9 @@ TEST_IMPL(GLM_PREFIX, euler_yzx_quat) { /*use my function to get the quaternion*/ glm_euler_yzx_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -453,6 +477,9 @@ TEST_IMPL(GLM_PREFIX, euler_zxy_quat) { /*use my function to get the quaternion*/ glm_euler_zxy_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } @@ -488,6 +515,9 @@ TEST_IMPL(GLM_PREFIX, euler_zxy_quat) { /*use my function to get the quaternion*/ glm_euler_zxy_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -528,6 +558,9 @@ TEST_IMPL(GLM_PREFIX, euler_zyx_quat) { /*use my function to get the quaternion*/ glm_euler_zyx_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } @@ -564,6 +597,9 @@ TEST_IMPL(GLM_PREFIX, euler_zyx_quat) { /*use my function to get the quaternion*/ glm_euler_zyx_quat(result, angles); + /*verify if the magnitude of the quaternion stays 1*/ + ASSERT(test_eq(glm_quat_norm(result), 1.0f)) + ASSERTIFY(test_assert_quat_eq(result, expected)) } } @@ -572,7 +608,6 @@ TEST_IMPL(GLM_PREFIX, euler_zyx_quat) { TEST_SUCCESS } - TEST_IMPL(GLM_PREFIX, quat) { versor q1 = {1.0f, 2.0f, 3.0f, 4.0f}; vec3 v1, v2;