| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- /*
- * Copyright (c), Recep Aslantas.
- *
- * MIT License (MIT), http://opensource.org/licenses/MIT
- * Full license can be found in the LICENSE file
- */
- /*
- Functions:
- 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);
- CGLM_INLINE bool glm_vec2_eqv(vec2 a, vec2 b);
- CGLM_INLINE bool glm_vec2_eqv_eps(vec2 a, vec2 b);
- CGLM_INLINE float glm_vec2_max(vec2 v);
- CGLM_INLINE float glm_vec2_min(vec2 v);
- CGLM_INLINE bool glm_vec2_isnan(vec2 v);
- CGLM_INLINE bool glm_vec2_isinf(vec2 v);
- CGLM_INLINE bool glm_vec2_isvalid(vec2 v);
- CGLM_INLINE void glm_vec2_sign(vec2 v, vec2 dest);
- CGLM_INLINE void glm_vec2_abs(vec2 v, vec2 dest);
- CGLM_INLINE void glm_vec2_fract(vec2 v, vec2 dest);
- CGLM_INLINE void glm_vec2_floor(vec2 v, vec2 dest);
- CGLM_INLINE float glm_vec2_mods(vec2 v, float s, vec2 dest);
- CGLM_INLINE float glm_vec2_steps(float edge, vec2 v, vec2 dest);
- CGLM_INLINE void glm_vec2_stepr(vec2 edge, float v, vec2 dest);
- CGLM_INLINE void glm_vec2_sqrt(vec2 v, vec2 dest);
- CGLM_INLINE void glm_vec2_complex_mul(vec2 a, vec2 b, vec2 dest)
- CGLM_INLINE void glm_vec2_complex_div(vec2 a, vec2 b, vec2 dest)
- CGLM_INLINE void glm_vec2_complex_conjugate(vec2 a, vec2 dest)
- */
- #ifndef cglm_vec2_ext_h
- #define cglm_vec2_ext_h
- #include "common.h"
- #include "util.h"
- /*!
- * @brief fill a vector with specified value
- *
- * @param[out] v dest
- * @param[in] val value
- */
- CGLM_INLINE
- void
- glm_vec2_fill(vec2 v, float val) {
- v[0] = v[1] = val;
- }
- /*!
- * @brief check if vector is equal to value (without epsilon)
- *
- * @param[in] v vector
- * @param[in] val value
- */
- CGLM_INLINE
- bool
- glm_vec2_eq(vec2 v, float val) {
- return v[0] == val && v[0] == v[1];
- }
- /*!
- * @brief check if vector is equal to value (with epsilon)
- *
- * @param[in] v vector
- * @param[in] val value
- */
- CGLM_INLINE
- bool
- glm_vec2_eq_eps(vec2 v, float val) {
- return fabsf(v[0] - val) <= GLM_FLT_EPSILON
- && fabsf(v[1] - val) <= GLM_FLT_EPSILON;
- }
- /*!
- * @brief check if vector members are equal (without epsilon)
- *
- * @param[in] v vector
- */
- CGLM_INLINE
- bool
- glm_vec2_eq_all(vec2 v) {
- return glm_vec2_eq_eps(v, v[0]);
- }
- /*!
- * @brief check if vector is equal to another (without epsilon)
- *
- * @param[in] a vector
- * @param[in] b vector
- */
- CGLM_INLINE
- bool
- glm_vec2_eqv(vec2 a, vec2 b) {
- return a[0] == b[0] && a[1] == b[1];
- }
- /*!
- * @brief check if vector is equal to another (with epsilon)
- *
- * @param[in] a vector
- * @param[in] b vector
- */
- CGLM_INLINE
- bool
- glm_vec2_eqv_eps(vec2 a, vec2 b) {
- return fabsf(a[0] - b[0]) <= GLM_FLT_EPSILON
- && fabsf(a[1] - b[1]) <= GLM_FLT_EPSILON;
- }
- /*!
- * @brief max value of vector
- *
- * @param[in] v vector
- */
- CGLM_INLINE
- float
- glm_vec2_max(vec2 v) {
- return glm_max(v[0], v[1]);
- }
- /*!
- * @brief min value of vector
- *
- * @param[in] v vector
- */
- CGLM_INLINE
- float
- glm_vec2_min(vec2 v) {
- return glm_min(v[0], v[1]);
- }
- /*!
- * @brief check if one of items is NaN (not a number)
- * you should only use this in DEBUG mode or very critical asserts
- *
- * @param[in] v vector
- */
- CGLM_INLINE
- bool
- glm_vec2_isnan(vec2 v) {
- #ifndef CGLM_FAST_MATH
- return isnan(v[0]) || isnan(v[1]);
- #else
- return false;
- #endif
- }
- /*!
- * @brief check if one of items is INFINITY
- * you should only use this in DEBUG mode or very critical asserts
- *
- * @param[in] v vector
- */
- CGLM_INLINE
- bool
- glm_vec2_isinf(vec2 v) {
- #ifndef CGLM_FAST_MATH
- return isinf(v[0]) || isinf(v[1]);
- #else
- return false;
- #endif
- }
- /*!
- * @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
- glm_vec2_isvalid(vec2 v) {
- return !glm_vec2_isnan(v) && !glm_vec2_isinf(v);
- }
- /*!
- * @brief get sign of 32 bit float as +1, -1, 0
- *
- * Important: It returns 0 for zero/NaN input
- *
- * @param v vector
- */
- CGLM_INLINE
- void
- glm_vec2_sign(vec2 v, vec2 dest) {
- dest[0] = glm_signf(v[0]);
- dest[1] = glm_signf(v[1]);
- }
- /*!
- * @brief absolute value of v
- *
- * @param[in] v vector
- * @param[out] dest destination
- */
- CGLM_INLINE
- void
- glm_vec2_abs(vec2 v, vec2 dest) {
- dest[0] = fabsf(v[0]);
- dest[1] = fabsf(v[1]);
- }
- /*!
- * @brief fractional part of each vector item
- *
- * @param[in] v vector
- * @param[out] dest destination vector
- */
- CGLM_INLINE
- void
- glm_vec2_fract(vec2 v, vec2 dest) {
- dest[0] = fminf(v[0] - floorf(v[0]), 0.999999940395355224609375f);
- dest[1] = fminf(v[1] - floorf(v[1]), 0.999999940395355224609375f);
- }
- /*!
- * @brief floor of each vector item
- *
- * @param[in] v vector
- * @param[out] dest destination vector
- */
- CGLM_INLINE
- void
- glm_vec2_floor(vec2 v, vec2 dest) {
- dest[0] = floorf(v[0]);
- dest[1] = floorf(v[1]);
- }
- /*!
- * @brief mod of each vector item, result is written to dest (dest = v % s)
- *
- * @param[in] v vector
- * @param[in] s scalar
- * @param[out] dest destination vector
- */
- CGLM_INLINE
- void
- glm_vec2_mods(vec2 v, float s, vec2 dest) {
- dest[0] = fmodf(v[0], s);
- dest[1] = fmodf(v[1], s);
- }
- /*!
- * @brief square root of each vector item
- *
- * @param[in] v vector
- * @param[out] dest destination vector
- */
- CGLM_INLINE
- void
- glm_vec2_sqrt(vec2 v, vec2 dest) {
- dest[0] = sqrtf(v[0]);
- dest[1] = sqrtf(v[1]);
- }
- /*!
- * @brief treat vectors as complex numbers and multiply them as such.
- *
- * @param[in] a left number
- * @param[in] b right number
- * @param[out] dest destination number
- */
- CGLM_INLINE
- void
- glm_vec2_complex_mul(vec2 a, vec2 b, vec2 dest) {
- float tr, ti;
- tr = a[0] * b[0] - a[1] * b[1];
- ti = a[0] * b[1] + a[1] * b[0];
- dest[0] = tr;
- dest[1] = ti;
- }
- /*!
- * @brief threshold each vector item with scalar
- * condition is: (x[i] < edge) ? 0.0 : 1.0
- *
- * @param[in] edge threshold
- * @param[in] x vector to test against threshold
- * @param[out] dest destination
- */
- CGLM_INLINE
- void
- glm_vec2_steps(float edge, vec2 x, vec2 dest) {
- dest[0] = glm_step(edge, x[0]);
- dest[1] = glm_step(edge, x[1]);
- }
- /*!
- * @brief threshold a value with *vector* as the threshold
- * condition is: (x < edge[i]) ? 0.0 : 1.0
- *
- * @param[in] edge threshold vector
- * @param[in] x value to test against threshold
- * @param[out] dest destination
- */
- CGLM_INLINE
- void
- glm_vec2_stepr(vec2 edge, float x, vec2 dest) {
- dest[0] = glm_step(edge[0], x);
- dest[1] = glm_step(edge[1], x);
- }
- /*!
- * @brief treat vectors as complex numbers and divide them as such.
- *
- * @param[in] a left number (numerator)
- * @param[in] b right number (denominator)
- * @param[out] dest destination number
- */
- CGLM_INLINE
- void
- glm_vec2_complex_div(vec2 a, vec2 b, vec2 dest) {
- float tr, ti;
- float const ibnorm2 = 1.0f / (b[0] * b[0] + b[1] * b[1]);
- tr = ibnorm2 * (a[0] * b[0] + a[1] * b[1]);
- ti = ibnorm2 * (a[1] * b[0] - a[0] * b[1]);
- dest[0] = tr;
- dest[1] = ti;
- }
- /*!
- * @brief treat the vector as a complex number and conjugate it as such.
- *
- * @param[in] a the number
- * @param[out] dest destination number
- */
- CGLM_INLINE
- void
- glm_vec2_complex_conjugate(vec2 a, vec2 dest) {
- dest[0] = a[0];
- dest[1] = -a[1];
- }
- #endif /* cglm_vec2_ext_h */
|