From 669777eb37eaffd6079ba06893b87ccf1a352183 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Mon, 18 Jun 2018 17:55:25 +0300 Subject: [PATCH] additional utils --- docs/source/util.rst | 35 +++++++++++++++++++++++++++++++++++ include/cglm/util.h | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/docs/source/util.rst b/docs/source/util.rst index f8dbac4..846934f 100644 --- a/docs/source/util.rst +++ b/docs/source/util.rst @@ -136,3 +136,38 @@ Functions documentation Returns: interpolated value + +.. c:function:: bool glm_eq(float a, float b) + + check if two float equal with using EPSILON + + Parameters: + | *[in]* **a** a + | *[in]* **b** b + + Returns: + true if a and b equals + +.. c:function:: float glm_percent(float from, float to, float current) + + percentage of current value between start and end value + + Parameters: + | *[in]* **from** from value + | *[in]* **to** to value + | *[in]* **current** value between from and to values + + Returns: + clamped normalized percent (0-100 in 0-1) + +.. c:function:: float glm_percentc(float from, float to, float current) + + clamped percentage of current value between start and end value + + Parameters: + | *[in]* **from** from value + | *[in]* **to** to value + | *[in]* **current** value between from and to values + + Returns: + clamped normalized percent (0-100 in 0-1) diff --git a/include/cglm/util.h b/include/cglm/util.h index af7514c..7a7b004 100644 --- a/include/cglm/util.h +++ b/include/cglm/util.h @@ -19,6 +19,7 @@ #define cglm_util_h #include "common.h" +#include /*! * @brief get sign of 32 bit integer as +1, -1, 0 @@ -157,4 +158,44 @@ glm_lerp(float from, float to, float t) { return from + glm_clamp(t, 0.0f, 1.0f) * (to - from); } +/*! + * @brief check if two float equal with using EPSILON + * + * @param[in] a a + * @param[in] b b + */ +CGLM_INLINE +bool +glm_eq(float a, float b) { + return fabsf(a - b) <= FLT_EPSILON; +} + +/*! + * @brief percentage of current value between start and end value + * + * maybe fraction could be alternative name. + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t current value + */ +CGLM_INLINE +float +glm_percent(float from, float to, float current) { + return (current - from) / (to - from); +} + +/*! + * @brief clamped percentage of current value between start and end value + * + * @param[in] from from value + * @param[in] to to value + * @param[in] t current value + */ +CGLM_INLINE +float +glm_percentc(float from, float to, float current) { + return glm_clamp(glm_percent(from, to, current), 0.0f, 1.0f); +} + #endif /* cglm_util_h */