vec: add one and zero helpers for vectors

This commit is contained in:
Recep Aslantas
2018-04-13 11:57:14 +03:00
parent c489955b00
commit 25fc3d0284
13 changed files with 165 additions and 2 deletions

View File

@@ -31,6 +31,8 @@ Functions:
1. :c:func:`glm_vec3` 1. :c:func:`glm_vec3`
#. :c:func:`glm_vec_copy` #. :c:func:`glm_vec_copy`
#. :c:func:`glm_vec_zero`
#. :c:func:`glm_vec_one`
#. :c:func:`glm_vec_dot` #. :c:func:`glm_vec_dot`
#. :c:func:`glm_vec_cross` #. :c:func:`glm_vec_cross`
#. :c:func:`glm_vec_norm2` #. :c:func:`glm_vec_norm2`
@@ -76,6 +78,20 @@ Functions documentation
| *[in]* **a** source | *[in]* **a** source
| *[out]* **dest** destination | *[out]* **dest** destination
.. c:function:: void glm_vec_zero(vec3 v)
makes all members 0.0f (zero)
Parameters:
| *[in, out]* **v** vector
.. c:function:: void glm_vec_one(vec3 v)
makes all members 1.0f (one)
Parameters:
| *[in, out]* **v** vector
.. c:function:: float glm_vec_dot(vec3 a, vec3 b) .. c:function:: float glm_vec_dot(vec3 a, vec3 b)
dot product of vec3 dot product of vec3

View File

@@ -24,6 +24,8 @@ Functions:
1. :c:func:`glm_vec4` 1. :c:func:`glm_vec4`
#. :c:func:`glm_vec4_copy3` #. :c:func:`glm_vec4_copy3`
#. :c:func:`glm_vec4_copy` #. :c:func:`glm_vec4_copy`
#. :c:func:`glm_vec4_zero`
#. :c:func:`glm_vec4_one`
#. :c:func:`glm_vec4_dot` #. :c:func:`glm_vec4_dot`
#. :c:func:`glm_vec4_norm2` #. :c:func:`glm_vec4_norm2`
#. :c:func:`glm_vec4_norm` #. :c:func:`glm_vec4_norm`
@@ -78,6 +80,13 @@ Functions documentation
| *[in]* **v** source | *[in]* **v** source
| *[in]* **dest** destination | *[in]* **dest** destination
.. c:function:: void glm_vec4_zero(vec4 v)
makes all members zero
Parameters:
| *[in, out]* **v** vector
.. c:function:: float glm_vec4_dot(vec4 a, vec4 b) .. c:function:: float glm_vec4_dot(vec4 a, vec4 b)
dot product of vec4 dot product of vec4

View File

@@ -24,6 +24,14 @@ CGLM_EXPORT
void void
glmc_vec_copy(vec3 a, vec3 dest); glmc_vec_copy(vec3 a, vec3 dest);
CGLM_EXPORT
void
glmc_vec_zero(vec3 v);
CGLM_EXPORT
void
glmc_vec_one(vec3 v);
CGLM_EXPORT CGLM_EXPORT
float float
glmc_vec_dot(vec3 a, vec3 b); glmc_vec_dot(vec3 a, vec3 b);

View File

@@ -21,6 +21,14 @@ CGLM_EXPORT
void void
glmc_vec4(vec3 v3, float last, vec4 dest); glmc_vec4(vec3 v3, float last, vec4 dest);
CGLM_EXPORT
void
glmc_vec4_zero(vec4 v);
CGLM_EXPORT
void
glmc_vec4_one(vec4 v);
CGLM_EXPORT CGLM_EXPORT
void void
glmc_vec4_copy3(vec4 a, vec3 dest); glmc_vec4_copy3(vec4 a, vec3 dest);

View File

@@ -103,6 +103,32 @@ glm_vec_copy(vec3 a, vec3 dest) {
dest[2] = a[2]; dest[2] = a[2];
} }
/*!
* @brief make vector zero
*
* @param[in, out] v vector
*/
CGLM_INLINE
void
glm_vec_zero(vec4 v) {
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = 0.0f;
}
/*!
* @brief make vector one
*
* @param[in, out] v vector
*/
CGLM_INLINE
void
glm_vec_one(vec4 v) {
v[0] = 1.0f;
v[1] = 1.0f;
v[2] = 1.0f;
}
/*! /*!
* @brief vec3 dot product * @brief vec3 dot product
* *

View File

@@ -111,6 +111,42 @@ glm_vec4_copy(vec4 v, vec4 dest) {
#endif #endif
} }
/*!
* @brief make vector zero
*
* @param[in, out] v vector
*/
CGLM_INLINE
void
glm_vec4_zero(vec4 v) {
#if defined( __SSE__ ) || defined( __SSE2__ )
_mm_store_ps(v, _mm_setzero_ps());
#else
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = 0.0f;
v[3] = 0.0f;
#endif
}
/*!
* @brief make vector one
*
* @param[in, out] v vector
*/
CGLM_INLINE
void
glm_vec4_one(vec4 v) {
#if defined( __SSE__ ) || defined( __SSE2__ )
_mm_store_ps(v, _mm_set1_ps(1.0f));
#else
v[0] = 1.0f;
v[1] = 1.0f;
v[2] = 1.0f;
v[3] = 1.0f;
#endif
}
/*! /*!
* @brief vec4 dot product * @brief vec4 dot product
* *

View File

@@ -110,7 +110,8 @@ test_tests_SOURCES=\
test/src/test_clamp.c \ test/src/test_clamp.c \
test/src/test_euler.c \ test/src/test_euler.c \
test/src/test_quat.c \ test/src/test_quat.c \
test/src/test_vec4.c test/src/test_vec4.c \
test/src/test_vec3.c
all-local: all-local:
sh ./post-build.sh sh ./post-build.sh

View File

@@ -20,6 +20,18 @@ glmc_vec_copy(vec3 a, vec3 dest) {
glm_vec_copy(a, dest); glm_vec_copy(a, dest);
} }
CGLM_EXPORT
void
glmc_vec_zero(vec3 v) {
glm_vec_zero(v);
}
CGLM_EXPORT
void
glmc_vec_one(vec3 v) {
glm_vec_one(v);
}
CGLM_EXPORT CGLM_EXPORT
float float
glmc_vec_dot(vec3 a, vec3 b) { glmc_vec_dot(vec3 a, vec3 b) {

View File

@@ -14,6 +14,18 @@ glmc_vec4(vec3 v3, float last, vec4 dest) {
glm_vec4(v3, last, dest); glm_vec4(v3, last, dest);
} }
CGLM_EXPORT
void
glmc_vec4_zero(vec4 v) {
glm_vec4_zero(v);
}
CGLM_EXPORT
void
glmc_vec4_one(vec4 v) {
glm_vec4_one(v);
}
CGLM_EXPORT CGLM_EXPORT
void void
glmc_vec4_copy3(vec4 a, vec3 dest) { glmc_vec4_copy3(vec4 a, vec3 dest) {

View File

@@ -29,7 +29,10 @@ main(int argc, const char * argv[]) {
cmocka_unit_test(test_quat), cmocka_unit_test(test_quat),
/* vec4 */ /* vec4 */
cmocka_unit_test(test_vec4) cmocka_unit_test(test_vec4),
/* vec3 */
cmocka_unit_test(test_vec3)
}; };
return cmocka_run_group_tests(tests, NULL, NULL); return cmocka_run_group_tests(tests, NULL, NULL);

View File

@@ -31,4 +31,7 @@ test_quat(void **state);
void void
test_vec4(void **state); test_vec4(void **state);
void
test_vec3(void **state);
#endif /* test_tests_h */ #endif /* test_tests_h */

21
test/src/test_vec3.c Normal file
View File

@@ -0,0 +1,21 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#include "test_common.h"
void
test_vec3(void **state) {
vec3 v;
/* test zero */
glm_vec_zero(v);
test_assert_vec3_eq(GLM_VEC3_ZERO, v);
/* test one */
glm_vec_one(v);
test_assert_vec3_eq(GLM_VEC3_ONE, v);
}

View File

@@ -70,4 +70,12 @@ test_vec4(void **state) {
test_rand_vec4(v); test_rand_vec4(v);
test_assert_eqf(test_vec4_norm2(v), glm_vec4_norm2(v)); test_assert_eqf(test_vec4_norm2(v), glm_vec4_norm2(v));
} }
/* test zero */
glm_vec4_zero(v);
test_assert_vec4_eq(GLM_VEC4_ZERO, v);
/* test one */
glm_vec4_one(v);
test_assert_vec4_eq(GLM_VEC4_ONE, v);
} }