Skip to content

Commit

Permalink
vec2: add new function glm_vec2_make
Browse files Browse the repository at this point in the history
Just a copy of glm_vec2, but with the
word _make suffixed at the end.

Function takes in a float array array must be
at least of size 2 and converts it into
a 2D vector.

Signed-off-by: Vincent Davis Jr <vince@underview.tech>
  • Loading branch information
EasyIP2023 committed Jul 2, 2023
1 parent 49dd24e commit b3de85a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/source/vec2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Functions:
#. :c:func:`glm_vec2_minv`
#. :c:func:`glm_vec2_clamp`
#. :c:func:`glm_vec2_lerp`
#. :c:func:`glm_vec2_make`

Functions documentation
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -373,3 +374,12 @@ Functions documentation
| *[in]* **to** to value
| *[in]* **t** interpolant (amount) clamped between 0 and 1
| *[out]* **dest** destination
.. c:function:: void glm_vec2_make(float * __restrict src, vec2 dest)
Create two dimensional vector from pointer
| NOTE: **@src** must contain at least 2 elements.
Parameters:
| *[in]* **src** pointer to an array of floats
| *[out]* **dest** destination vector
4 changes: 4 additions & 0 deletions include/cglm/call/vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ CGLM_EXPORT
void
glmc_vec2_complex_conjugate(vec2 a, vec2 dest);

CGLM_EXPORT
void
glmc_vec2_make(float * __restrict src, vec2 dest);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions include/cglm/struct/vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
CGLM_INLINE vec2s glms_vec2_minv(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_clamp(vec2s v, float minVal, float maxVal)
CGLM_INLINE vec2s glms_vec2_lerp(vec2s from, vec2s to, float t)
CGLM_INLINE vec2s glms_vec2_make(float * restrict src)
*/

#ifndef cglms_vec2s_h
Expand Down Expand Up @@ -558,4 +559,18 @@ glms_vec2_(lerp)(vec2s from, vec2s to, float t) {
return r;
}

/*!
* @brief Create two dimensional vector from pointer
*
* @param[in] src pointer to an array of floats
* @returns constructed 2D vector from raw pointer
*/
CGLM_INLINE
vec2s
glms_vec2_(make)(float * __restrict src) {
vec2s dest;
glm_vec2_make(src, dest.raw);
return dest;
}

#endif /* cglms_vec2s_h */
13 changes: 13 additions & 0 deletions include/cglm/vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
CGLM_INLINE void glm_vec2_minv(vec2 v1, vec2 v2, vec2 dest)
CGLM_INLINE void glm_vec2_clamp(vec2 v, float minVal, float maxVal)
CGLM_INLINE void glm_vec2_lerp(vec2 from, vec2 to, float t, vec2 dest)
CGLM_INLINE void glm_vec2_make(float * restrict src, vec2 dest)
*/

Expand Down Expand Up @@ -582,4 +583,16 @@ glm_vec2_lerp(vec2 from, vec2 to, float t, vec2 dest) {
glm_vec2_add(from, v, dest);
}

/*!
* @brief Create two dimensional vector from pointer
*
* @param[in] src pointer to an array of floats
* @param[out] dest destination vector
*/
CGLM_INLINE
void
glm_vec2_make(float * __restrict src, vec2 dest) {
dest[0] = src[0]; dest[1] = src[1];
}

#endif /* cglm_vec2_h */
6 changes: 6 additions & 0 deletions src/vec2.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,9 @@ void
glmc_vec2_complex_conjugate(vec2 a, vec2 dest) {
glm_vec2_complex_conjugate(a, dest);
}

CGLM_EXPORT
void
glmc_vec2_make(float * __restrict src, vec2 dest) {
glm_vec2_make(src, dest);
}
20 changes: 20 additions & 0 deletions test/src/test_vec2.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,23 @@ TEST_IMPL(GLM_PREFIX, vec2_complex_div) {

TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, vec2_make) {
float src[6] = {
7.2f, 1.0f,
2.5f, 6.1f,
17.7f, 4.3f
};
vec2 dest[3];

float *srcp = src;
unsigned int i, j;

for (i = 0, j = 0; i < sizeof(src) / sizeof(float); i+=2,j++) {
GLM(vec2_make)(srcp + i, dest[j]);
ASSERT(test_eq(src[ i ], dest[j][0]));
ASSERT(test_eq(src[i+1], dest[j][1]));
}

TEST_SUCCESS
}
4 changes: 4 additions & 0 deletions test/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ TEST_DECLARE(glm_vec2_abs)
TEST_DECLARE(glm_vec2_lerp)
TEST_DECLARE(glm_vec2_complex_mul)
TEST_DECLARE(glm_vec2_complex_div)
TEST_DECLARE(glm_vec2_make)

TEST_DECLARE(glmc_vec2)
TEST_DECLARE(glmc_vec2_copy)
Expand Down Expand Up @@ -436,6 +437,7 @@ TEST_DECLARE(glmc_vec2_abs)
TEST_DECLARE(glmc_vec2_lerp)
TEST_DECLARE(glmc_vec2_complex_mul)
TEST_DECLARE(glmc_vec2_complex_div)
TEST_DECLARE(glmc_vec2_make)

/* vec3 */
TEST_DECLARE(MACRO_GLM_VEC3_ONE_INIT)
Expand Down Expand Up @@ -1246,6 +1248,7 @@ TEST_LIST {
TEST_ENTRY(glm_vec2_lerp)
TEST_ENTRY(glm_vec2_complex_mul)
TEST_ENTRY(glm_vec2_complex_div)
TEST_ENTRY(glm_vec2_make)

TEST_ENTRY(glmc_vec2)
TEST_ENTRY(glmc_vec2_copy)
Expand Down Expand Up @@ -1284,6 +1287,7 @@ TEST_LIST {
TEST_ENTRY(glmc_vec2_lerp)
TEST_ENTRY(glmc_vec2_complex_mul)
TEST_ENTRY(glmc_vec2_complex_div)
TEST_ENTRY(glmc_vec2_make)

/* vec3 */
TEST_ENTRY(MACRO_GLM_VEC3_ONE_INIT)
Expand Down

0 comments on commit b3de85a

Please sign in to comment.