vec2: add struct version for vec2 and vec2-ext

This commit is contained in:
Recep Aslantas
2020-02-25 14:34:11 +03:00
parent 638b9f6dbe
commit eb0d47cfa1
9 changed files with 778 additions and 8 deletions

View File

@@ -107,6 +107,8 @@ cglm_simd_neon_HEADERS = include/cglm/simd/neon/mat4.h
cglm_structdir=$(includedir)/cglm/struct
cglm_struct_HEADERS = include/cglm/struct/mat4.h \
include/cglm/struct/mat3.h \
include/cglm/struct/vec2.h \
include/cglm/struct/vec2-ext.h \
include/cglm/struct/vec3.h \
include/cglm/struct/vec3-ext.h \
include/cglm/struct/vec4.h \

View File

@@ -13,6 +13,7 @@ extern "C" {
#include "cglm.h"
#include "types-struct.h"
#include "struct/vec2.h"
#include "struct/vec3.h"
#include "struct/vec4.h"
#include "struct/mat2.h"

View File

@@ -0,0 +1,198 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*!
* @brief SIMD like functions
*/
/*
Functions:
CGLM_INLINE vec2s glms_vec2_fill(float val)
CGLM_INLINE bool glms_vec2_eq(vec2s v, float val)
CGLM_INLINE bool glms_vec2_eq_eps(vec2s v, float val)
CGLM_INLINE bool glms_vec2_eq_all(vec2s v)
CGLM_INLINE bool glms_vec2_eqv(vec2s a, vec2s b)
CGLM_INLINE bool glms_vec2_eqv_eps(vec2s a, vec2s b)
CGLM_INLINE float glms_vec2_max(vec2s v)
CGLM_INLINE float glms_vec2_min(vec2s v)
CGLM_INLINE bool glms_vec2_isnan(vec2s v)
CGLM_INLINE bool glms_vec2_isinf(vec2s v)
CGLM_INLINE bool glms_vec2_isvalid(vec2s v)
CGLM_INLINE vec2s glms_vec2_sign(vec2s v)
CGLM_INLINE vec2s glms_vec2_sqrt(vec2s v)
*/
#ifndef cglms_vec2s_ext_h
#define cglms_vec2s_ext_h
#include "../common.h"
#include "../types-struct.h"
#include "../util.h"
#include "../vec2-ext.h"
/*!
* @brief fill a vector with specified value
*
* @param[in] val value
* @returns dest
*/
CGLM_INLINE
vec2s
glms_vec2_fill(float val) {
vec2s r;
glm_vec2_fill(r.raw, val);
return r;
}
/*!
* @brief check if vector is equal to value (without epsilon)
*
* @param[in] v vector
* @param[in] val value
*/
CGLM_INLINE
bool
glms_vec2_eq(vec2s v, float val) {
return glm_vec2_eq(v.raw, val);
}
/*!
* @brief check if vector is equal to value (with epsilon)
*
* @param[in] v vector
* @param[in] val value
*/
CGLM_INLINE
bool
glms_vec2_eq_eps(vec2s v, float val) {
return glm_vec2_eq_eps(v.raw, val);
}
/*!
* @brief check if vectors members are equal (without epsilon)
*
* @param[in] v vector
*/
CGLM_INLINE
bool
glms_vec2_eq_all(vec2s v) {
return glm_vec2_eq_all(v.raw);
}
/*!
* @brief check if vector is equal to another (without epsilon)
*
* @param[in] a vector
* @param[in] b vector
*/
CGLM_INLINE
bool
glms_vec2_eqv(vec2s a, vec2s b) {
return glm_vec2_eqv(a.raw, b.raw);
}
/*!
* @brief check if vector is equal to another (with epsilon)
*
* @param[in] a vector
* @param[in] b vector
*/
CGLM_INLINE
bool
glms_vec2_eqv_eps(vec2s a, vec2s b) {
return glm_vec2_eqv_eps(a.raw, b.raw);
}
/*!
* @brief max value of vector
*
* @param[in] v vector
*/
CGLM_INLINE
float
glms_vec2_max(vec2s v) {
return glm_vec2_max(v.raw);
}
/*!
* @brief min value of vector
*
* @param[in] v vector
*/
CGLM_INLINE
float
glms_vec2_min(vec2s v) {
return glm_vec2_min(v.raw);
}
/*!
* @brief check if all items are NaN (not a number)
* you should only use this in DEBUG mode or very critical asserts
*
* @param[in] v vector
*/
CGLM_INLINE
bool
glms_vec2_isnan(vec2s v) {
return glm_vec2_isnan(v.raw);
}
/*!
* @brief check if all items are INFINITY
* you should only use this in DEBUG mode or very critical asserts
*
* @param[in] v vector
*/
CGLM_INLINE
bool
glms_vec2_isinf(vec2s v) {
return glm_vec2_isinf(v.raw);
}
/*!
* @brief check if all items are valid number
* you should only use this in DEBUG mode or very critical asserts
*
* @param[in] v vector
*/
CGLM_INLINE
bool
glms_vec2_isvalid(vec2s v) {
return glm_vec2_isvalid(v.raw);
}
/*!
* @brief get sign of 32 bit float as +1, -1, 0
*
* Important: It returns 0 for zero/NaN input
*
* @param v vector
* @returns sign vector
*/
CGLM_INLINE
vec2s
glms_vec2_sign(vec2s v) {
vec2s r;
glm_vec2_sign(v.raw, r.raw);
return r;
}
/*!
* @brief square root of each vector item
*
* @param[in] v vector
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_sqrt(vec2s v) {
vec2s r;
glm_vec2_sqrt(v.raw, r.raw);
return r;
}
#endif /* cglms_vec2s_ext_h */

561
include/cglm/struct/vec2.h Normal file
View File

@@ -0,0 +1,561 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
/*
Macros:
GLMS_VEC2_ONE_INIT
GLMS_VEC2_ZERO_INIT
GLMS_VEC2_ONE
GLMS_VEC2_ZERO
Functions:
CGLM_INLINE vec2s glms_vec2(vec3s v3)
CGLM_INLINE void glms_vec2_pack(vec2s dst[], vec2 src[], size_t len)
CGLM_INLINE void glms_vec2_unpack(vec2 dst[], vec2s src[], size_t len)
CGLM_INLINE vec2s glms_vec2_zero(void)
CGLM_INLINE vec2s glms_vec2_one(void)
CGLM_INLINE float glms_vec2_dot(vec2s a, vec2s b)
CGLM_INLINE float glms_vec2_cross(vec2s a, vec2s b)
CGLM_INLINE float glms_vec2_norm2(vec2s v)
CGLM_INLINE float glms_vec2_norm(vec2s v)
CGLM_INLINE vec2s glms_vec2_add(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_adds(vec2s a, float s)
CGLM_INLINE vec2s glms_vec2_sub(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_subs(vec2s a, float s)
CGLM_INLINE vec2s glms_vec2_mul(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_scale(vec2s v, float s)
CGLM_INLINE vec2s glms_vec2_scale_as(vec2s v, float s)
CGLM_INLINE vec2s glms_vec2_div(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_divs(vec2s a, float s)
CGLM_INLINE vec2s glms_vec2_addadd(vec2s a, vec2s b, vec2s dest)
CGLM_INLINE vec2s glms_vec2_subadd(vec2s a, vec2s b, vec2s dest)
CGLM_INLINE vec2s glms_vec2_muladd(vec2s a, vec2s b, vec2s dest)
CGLM_INLINE vec2s glms_vec2_muladds(vec2s a, float s, vec2s dest)
CGLM_INLINE vec2s glms_vec2_maxadd(vec2s a, vec2s b, vec2s dest)
CGLM_INLINE vec2s glms_vec2_minadd(vec2s a, vec2s b, vec2s dest)
CGLM_INLINE vec2s glms_vec2_negate(vec2s v)
CGLM_INLINE vec2s glms_vec2_normalize(vec2s v)
CGLM_INLINE vec2s glms_vec2_rotate(vec2s v, float angle, vec2s axis)
CGLM_INLINE float glms_vec2_distance(vec2s a, vec2s b)
CGLM_INLINE float glms_vec2_distance2(vec2s a, vec2s b)
CGLM_INLINE vec2s glms_vec2_maxv(vec2s a, vec2s b)
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)
*/
#ifndef cglms_vec2s_h
#define cglms_vec2s_h
#include "../common.h"
#include "../types-struct.h"
#include "../util.h"
#include "../vec2.h"
#include "vec2-ext.h"
#define GLMS_VEC2_ONE_INIT {GLM_VEC2_ONE_INIT}
#define GLMS_VEC2_ZERO_INIT {GLM_VEC2_ZERO_INIT}
#define GLMS_VEC2_ONE ((vec2s)GLMS_VEC2_ONE_INIT)
#define GLMS_VEC2_ZERO ((vec2s)GLMS_VEC2_ZERO_INIT)
/*!
* @brief init vec2 using vec2
*
* @param[in] v4 vector3
* @returns destination
*/
CGLM_INLINE
vec2s
glms_vec2(vec3s v3) {
vec2s r;
glm_vec2(v3.raw, r.raw);
return r;
}
/*!
* @brief pack an array of vec2 into an array of vec2s
*
* @param[out] dst array of vec2
* @param[in] src array of vec2s
* @param[in] len number of elements
*/
CGLM_INLINE
void
glms_vec2_pack(vec2s dst[], vec2 src[], size_t len) {
size_t i;
for (i = 0; i < len; i++) {
glm_vec2_copy(src[i], dst[i].raw);
}
}
/*!
* @brief unpack an array of vec2s into an array of vec2
*
* @param[out] dst array of vec2s
* @param[in] src array of vec2
* @param[in] len number of elements
*/
CGLM_INLINE
void
glms_vec2_unpack(vec2 dst[], vec2s src[], size_t len) {
size_t i;
for (i = 0; i < len; i++) {
glm_vec2_copy(src[i].raw, dst[i]);
}
}
/*!
* @brief make vector zero
*
* @returns zero vector
*/
CGLM_INLINE
vec2s
glms_vec2_zero(void) {
vec2s r;
glm_vec2_zero(r.raw);
return r;
}
/*!
* @brief make vector one
*
* @returns one vector
*/
CGLM_INLINE
vec2s
glms_vec2_one(void) {
vec2s r;
glm_vec2_one(r.raw);
return r;
}
/*!
* @brief vec2 dot product
*
* @param[in] a vector1
* @param[in] b vector2
*
* @return dot product
*/
CGLM_INLINE
float
glms_vec2_dot(vec2s a, vec2s b) {
return glm_vec2_dot(a.raw, b.raw);
}
/*!
* @brief vec2 cross product
*
* REF: http://allenchou.net/2013/07/cross-product-of-2d-vectors/
*
* @param[in] a vector1
* @param[in] b vector2
*
* @return Z component of cross product
*/
CGLM_INLINE
float
glms_vec2_cross(vec2s a, vec2s b) {
return glm_vec2_cross(a.raw, b.raw);
}
/*!
* @brief norm * norm (magnitude) of vec
*
* we can use this func instead of calling norm * norm, because it would call
* sqrtf fuction twice but with this func we can avoid func call, maybe this is
* not good name for this func
*
* @param[in] v vector
*
* @return norm * norm
*/
CGLM_INLINE
float
glms_vec2_norm2(vec2s v) {
return glm_vec2_norm2(v.raw);
}
/*!
* @brief norm (magnitude) of vec2
*
* @param[in] v vector
*
* @return norm
*/
CGLM_INLINE
float
glms_vec2_norm(vec2s v) {
return glm_vec2_norm(v.raw);
}
/*!
* @brief add a vector to b vector store result in dest
*
* @param[in] a vector1
* @param[in] b vector2
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_add(vec2s a, vec2s b) {
vec2s r;
glm_vec2_add(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief add scalar to v vector store result in dest (d = v + s)
*
* @param[in] a vector
* @param[in] s scalar
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_adds(vec2s a, float s) {
vec2s r;
glm_vec2_adds(a.raw, s, r.raw);
return r;
}
/*!
* @brief subtract b vector from a vector store result in dest
*
* @param[in] a vector1
* @param[in] b vector2
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_sub(vec2s a, vec2s b) {
vec2s r;
glm_vec2_sub(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief subtract scalar from v vector store result in dest (d = v - s)
*
* @param[in] a vector
* @param[in] s scalar
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_subs(vec2s a, float s) {
vec2s r;
glm_vec2_subs(a.raw, s, r.raw);
return r;
}
/*!
* @brief multiply two vector (component-wise multiplication)
*
* @param a vector1
* @param b vector2
* @returns v3 = (a[0] * b[0], a[1] * b[1], a[2] * b[2])
*/
CGLM_INLINE
vec2s
glms_vec2_mul(vec2s a, vec2s b) {
vec2s r;
glm_vec2_mul(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief multiply/scale vec2 vector with scalar: result = v * s
*
* @param[in] v vector
* @param[in] s scalar
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_scale(vec2s v, float s) {
vec2s r;
glm_vec2_scale(v.raw, s, r.raw);
return r;
}
/*!
* @brief make vec2 vector scale as specified: result = unit(v) * s
*
* @param[in] v vector
* @param[in] s scalar
* @returns destination vector
*/
CGLM_INLINE
vec2s
glms_vec2_scale_as(vec2s v, float s) {
vec2s r;
glm_vec2_scale_as(v.raw, s, r.raw);
return r;
}
/*!
* @brief div vector with another component-wise division: d = a / b
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2])
*/
CGLM_INLINE
vec2s
glms_vec2_div(vec2s a, vec2s b) {
vec2s r;
glm_vec2_div(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief div vector with scalar: d = v / s
*
* @param[in] a vector
* @param[in] s scalar
* @returns result = (a[0]/s, a[1]/s, a[2]/s)
*/
CGLM_INLINE
vec2s
glms_vec2_divs(vec2s a, float s) {
vec2s r;
glm_vec2_divs(a.raw, s, r.raw);
return r;
}
/*!
* @brief add two vectors and add result to sum
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns dest += (a + b)
*/
CGLM_INLINE
vec2s
glms_vec2_addadd(vec2s a, vec2s b, vec2s dest) {
glm_vec2_addadd(a.raw, b.raw, dest.raw);
return dest;
}
/*!
* @brief sub two vectors and add result to dest
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns dest += (a + b)
*/
CGLM_INLINE
vec2s
glms_vec2_subadd(vec2s a, vec2s b, vec2s dest) {
glm_vec2_subadd(a.raw, b.raw, dest.raw);
return dest;
}
/*!
* @brief mul two vectors and add result to dest
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns dest += (a * b)
*/
CGLM_INLINE
vec2s
glms_vec2_muladd(vec2s a, vec2s b, vec2s dest) {
glm_vec2_muladd(a.raw, b.raw, dest.raw);
return dest;
}
/*!
* @brief mul vector with scalar and add result to sum
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector
* @param[in] s scalar
* @returns dest += (a * b)
*/
CGLM_INLINE
vec2s
glms_vec2_muladds(vec2s a, float s, vec2s dest) {
glm_vec2_muladds(a.raw, s, dest.raw);
return dest;
}
/*!
* @brief add max of two vector to result/dest
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns dest += max(a, b)
*/
CGLM_INLINE
vec2s
glms_vec2_maxadd(vec2s a, vec2s b, vec2s dest) {
glm_vec2_maxadd(a.raw, b.raw, dest.raw);
return dest;
}
/*!
* @brief add min of two vector to result/dest
*
* it applies += operator so dest must be initialized
*
* @param[in] a vector 1
* @param[in] b vector 2
* @returns dest += min(a, b)
*/
CGLM_INLINE
vec2s
glms_vec2_minadd(vec2s a, vec2s b, vec2s dest) {
glm_vec2_minadd(a.raw, b.raw, dest.raw);
return dest;
}
/*!
* @brief negate vector components
*
* @param[in] v vector
* @returns negated vector
*/
CGLM_INLINE
vec2s
glms_vec2_negate(vec2s v) {
glm_vec2_negate(v.raw);
return v;
}
/*!
* @brief normalize vec2 and store result in same vec
*
* @param[in] v vector
* @returns normalized vector
*/
CGLM_INLINE
vec2s
glms_vec2_normalize(vec2s v) {
glm_vec2_normalize(v.raw);
return v;
}
/*!
* @brief rotate vec2 around axis by angle using Rodrigues' rotation formula
*
* @param[in] v vector
* @param[in] axis axis vector (must be unit vector)
* @param[in] angle angle by radians
* @returns rotated vector
*/
CGLM_INLINE
vec2s
glms_vec2_rotate(vec2s v, float angle, vec2s axis) {
glm_vec2_rotate(v.raw, angle, axis.raw);
return v;
}
/**
* @brief distance between two vectors
*
* @param[in] a vector1
* @param[in] b vector2
* @return distance
*/
CGLM_INLINE
float
glms_vec2_distance(vec2s a, vec2s b) {
return glm_vec2_distance(a.raw, b.raw);
}
/**
* @brief squared distance between two vectors
*
* @param[in] a vector1
* @param[in] b vector2
* @return squared distance (distance * distance)
*/
CGLM_INLINE
float
glms_vec2_distance2(vec2s a, vec2s b) {
return glm_vec2_distance2(a.raw, b.raw);
}
/*!
* @brief max values of vectors
*
* @param[in] a vector1
* @param[in] b vector2
* @returns destination
*/
CGLM_INLINE
vec2s
glms_vec2_maxv(vec2s a, vec2s b) {
vec2s r;
glm_vec2_maxv(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief min values of vectors
*
* @param[in] a vector1
* @param[in] b vector2
* @returns destination
*/
CGLM_INLINE
vec2s
glms_vec2_minv(vec2s a, vec2s b) {
vec2s r;
glm_vec2_minv(a.raw, b.raw, r.raw);
return r;
}
/*!
* @brief clamp vector's individual members between min and max values
*
* @param[in] v vector
* @param[in] minVal minimum value
* @param[in] maxVal maximum value
* @returns clamped vector
*/
CGLM_INLINE
vec2s
glms_vec2_clamp(vec2s v, float minVal, float maxVal) {
glm_vec2_clamp(v.raw, minVal, maxVal);
return v;
}
/*!
* @brief linear interpolation between two vectors
*
* formula: from + s * (to - from)
*
* @param[in] from from value
* @param[in] to to value
* @param[in] t interpolant (amount)
* @returns destination
*/
CGLM_INLINE
vec2s
glms_vec2_lerp(vec2s from, vec2s to, float t) {
vec2s r;
glm_vec2_lerp(from.raw, to.raw, t, r.raw);
return r;
}
#endif /* cglms_vec2s_h */

View File

@@ -7,7 +7,7 @@
/*
Functions:
CGLM_INLINE void glm_vec2_broadcast(float val, vec2 d);
CGLM_INLINE void glm_vec2_fill(vec2 v, float val)
CGLM_INLINE bool glm_vec2_eq(vec2 v, float val);
CGLM_INLINE bool glm_vec2_eq_eps(vec2 v, float val);
CGLM_INLINE bool glm_vec2_eq_all(vec2 v);
@@ -31,13 +31,13 @@
/*!
* @brief fill a vector with specified value
*
* @param[out] v dest
* @param[in] val value
* @param[out] d dest
*/
CGLM_INLINE
void
glm_vec2_broadcast(float val, vec2 d) {
d[0] = d[1] = val;
glm_vec2_fill(vec2 v, float val) {
v[0] = v[1] = val;
}
/*!

View File

@@ -576,7 +576,7 @@ glm_vec2_lerp(vec2 from, vec2 to, float t, vec2 dest) {
vec2 s, v;
/* from + s * (to - from) */
glm_vec2_broadcast(glm_clamp_zo(t), s);
glm_vec2_fill(s, glm_clamp_zo(t));
glm_vec2_sub(to, from, v);
glm_vec2_mul(s, v, v);
glm_vec2_add(from, v, dest);

View File

@@ -571,7 +571,7 @@ TEST_IMPL(GLM_PREFIX, decompose_rs) {
glm_mat4_mul(m2, r, m2);
glm_scale(m2, s1);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001));
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001f));
TEST_SUCCESS
}
@@ -607,7 +607,7 @@ TEST_IMPL(GLM_PREFIX, decompose) {
glm_mat4_mul(m2, r, m2);
glm_scale(m2, s);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001));
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001f));
glm_mat4_identity(m1);
glm_translate(m1, (vec3){56.0f, 13.0f, 90.0f});
@@ -628,7 +628,7 @@ TEST_IMPL(GLM_PREFIX, decompose) {
glm_translate(m2, t);
glm_mat4_mul(m2, r, m2);
glm_scale(m2, s);
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001));
ASSERTIFY(test_assert_mat4_eq2(m1, m2, 0.00001f));
TEST_SUCCESS
}

View File

@@ -107,6 +107,8 @@
<ClInclude Include="..\include\cglm\struct\project.h" />
<ClInclude Include="..\include\cglm\struct\quat.h" />
<ClInclude Include="..\include\cglm\struct\sphere.h" />
<ClInclude Include="..\include\cglm\struct\vec2-ext.h" />
<ClInclude Include="..\include\cglm\struct\vec2.h" />
<ClInclude Include="..\include\cglm\struct\vec3-ext.h" />
<ClInclude Include="..\include\cglm\struct\vec3.h" />
<ClInclude Include="..\include\cglm\struct\vec4-ext.h" />

View File

@@ -340,5 +340,11 @@
<ClInclude Include="..\include\cglm\vec2-ext.h">
<Filter>include\cglm</Filter>
</ClInclude>
<ClInclude Include="..\include\cglm\struct\vec2.h">
<Filter>include\cglm\struct</Filter>
</ClInclude>
<ClInclude Include="..\include\cglm\struct\vec2-ext.h">
<Filter>include\cglm\struct</Filter>
</ClInclude>
</ItemGroup>
</Project>