util.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Functions:
  9. CGLM_INLINE int glm_sign(int val);
  10. CGLM_INLINE float glm_signf(float val);
  11. CGLM_INLINE float glm_rad(float deg);
  12. CGLM_INLINE float glm_deg(float rad);
  13. CGLM_INLINE void glm_make_rad(float *deg);
  14. CGLM_INLINE void glm_make_deg(float *rad);
  15. CGLM_INLINE float glm_pow2(float x);
  16. CGLM_INLINE float glm_min(float a, float b);
  17. CGLM_INLINE float glm_max(float a, float b);
  18. CGLM_INLINE float glm_clamp(float val, float minVal, float maxVal);
  19. CGLM_INLINE float glm_clamp_zo(float val, float minVal, float maxVal);
  20. CGLM_INLINE float glm_lerp(float from, float to, float t);
  21. CGLM_INLINE float glm_lerpc(float from, float to, float t);
  22. CGLM_INLINE float glm_step(float edge, float x);
  23. CGLM_INLINE float glm_smooth(float t);
  24. CGLM_INLINE float glm_smoothstep(float edge0, float edge1, float x);
  25. CGLM_INLINE float glm_smoothinterp(float from, float to, float t);
  26. CGLM_INLINE float glm_smoothinterpc(float from, float to, float t);
  27. CGLM_INLINE bool glm_eq(float a, float b);
  28. CGLM_INLINE float glm_percent(float from, float to, float current);
  29. CGLM_INLINE float glm_percentc(float from, float to, float current);
  30. */
  31. #ifndef cglm_util_h
  32. #define cglm_util_h
  33. #include "common.h"
  34. #define GLM_MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
  35. #define GLM_MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
  36. /*!
  37. * @brief get sign of 32 bit integer as +1, -1, 0
  38. *
  39. * Important: It returns 0 for zero input
  40. *
  41. * @param val integer value
  42. */
  43. CGLM_INLINE
  44. int
  45. glm_sign(int val) {
  46. return ((val >> 31) - (-val >> 31));
  47. }
  48. /*!
  49. * @brief get sign of 32 bit float as +1, -1, 0
  50. *
  51. * Important: It returns 0 for zero/NaN input
  52. *
  53. * @param val float value
  54. */
  55. CGLM_INLINE
  56. float
  57. glm_signf(float val) {
  58. return (float)((val > 0.0f) - (val < 0.0f));
  59. }
  60. /*!
  61. * @brief convert degree to radians
  62. *
  63. * @param[in] deg angle in degrees
  64. */
  65. CGLM_INLINE
  66. float
  67. glm_rad(float deg) {
  68. return deg * GLM_PIf / 180.0f;
  69. }
  70. /*!
  71. * @brief convert radians to degree
  72. *
  73. * @param[in] rad angle in radians
  74. */
  75. CGLM_INLINE
  76. float
  77. glm_deg(float rad) {
  78. return rad * 180.0f / GLM_PIf;
  79. }
  80. /*!
  81. * @brief convert exsisting degree to radians. this will override degrees value
  82. *
  83. * @param[in, out] deg pointer to angle in degrees
  84. */
  85. CGLM_INLINE
  86. void
  87. glm_make_rad(float *deg) {
  88. *deg = *deg * GLM_PIf / 180.0f;
  89. }
  90. /*!
  91. * @brief convert exsisting radians to degree. this will override radians value
  92. *
  93. * @param[in, out] rad pointer to angle in radians
  94. */
  95. CGLM_INLINE
  96. void
  97. glm_make_deg(float *rad) {
  98. *rad = *rad * 180.0f / GLM_PIf;
  99. }
  100. /*!
  101. * @brief multiplies given parameter with itself = x * x or powf(x, 2)
  102. *
  103. * @param[in] x x
  104. */
  105. CGLM_INLINE
  106. float
  107. glm_pow2(float x) {
  108. return x * x;
  109. }
  110. /*!
  111. * @brief find minimum of given two values
  112. *
  113. * @param[in] a number 1
  114. * @param[in] b number 2
  115. */
  116. CGLM_INLINE
  117. float
  118. glm_min(float a, float b) {
  119. if (a < b)
  120. return a;
  121. return b;
  122. }
  123. /*!
  124. * @brief find maximum of given two values
  125. *
  126. * @param[in] a number 1
  127. * @param[in] b number 2
  128. */
  129. CGLM_INLINE
  130. float
  131. glm_max(float a, float b) {
  132. if (a > b)
  133. return a;
  134. return b;
  135. }
  136. /*!
  137. * @brief clamp a number between min and max
  138. *
  139. * @param[in] val value to clamp
  140. * @param[in] minVal minimum value
  141. * @param[in] maxVal maximum value
  142. */
  143. CGLM_INLINE
  144. float
  145. glm_clamp(float val, float minVal, float maxVal) {
  146. return glm_min(glm_max(val, minVal), maxVal);
  147. }
  148. /*!
  149. * @brief clamp a number to zero and one
  150. *
  151. * @param[in] val value to clamp
  152. */
  153. CGLM_INLINE
  154. float
  155. glm_clamp_zo(float val) {
  156. return glm_clamp(val, 0.0f, 1.0f);
  157. }
  158. /*!
  159. * @brief linear interpolation between two numbers
  160. *
  161. * formula: from + t * (to - from)
  162. *
  163. * @param[in] from from value
  164. * @param[in] to to value
  165. * @param[in] t interpolant (amount)
  166. */
  167. CGLM_INLINE
  168. float
  169. glm_lerp(float from, float to, float t) {
  170. return from + t * (to - from);
  171. }
  172. /*!
  173. * @brief clamped linear interpolation between two numbers
  174. *
  175. * formula: from + t * (to - from)
  176. *
  177. * @param[in] from from value
  178. * @param[in] to to value
  179. * @param[in] t interpolant (amount) clamped between 0 and 1
  180. */
  181. CGLM_INLINE
  182. float
  183. glm_lerpc(float from, float to, float t) {
  184. return glm_lerp(from, to, glm_clamp_zo(t));
  185. }
  186. /*!
  187. * @brief threshold function
  188. *
  189. * @param[in] edge threshold
  190. * @param[in] x value to test against threshold
  191. * @return returns 0.0 if x < edge, else 1.0
  192. */
  193. CGLM_INLINE
  194. float
  195. glm_step(float edge, float x) {
  196. /* branching - no type conversion */
  197. return (x < edge) ? 0.0f : 1.0f;
  198. /*
  199. * An alternative implementation without branching
  200. * but with type conversion could be:
  201. * return !(x < edge);
  202. */
  203. }
  204. /*!
  205. * @brief smooth Hermite interpolation
  206. *
  207. * formula: t^2 * (3-2t)
  208. *
  209. * @param[in] t interpolant (amount)
  210. */
  211. CGLM_INLINE
  212. float
  213. glm_smooth(float t) {
  214. return t * t * (3.0f - 2.0f * t);
  215. }
  216. /*!
  217. * @brief threshold function with a smooth transition (according to OpenCL specs)
  218. *
  219. * formula: t^2 * (3-2t)
  220. *
  221. * @param[in] edge0 low threshold
  222. * @param[in] edge1 high threshold
  223. * @param[in] x interpolant (amount)
  224. */
  225. CGLM_INLINE
  226. float
  227. glm_smoothstep(float edge0, float edge1, float x) {
  228. float t;
  229. t = glm_clamp_zo((x - edge0) / (edge1 - edge0));
  230. return glm_smooth(t);
  231. }
  232. /*!
  233. * @brief smoothstep interpolation between two numbers
  234. *
  235. * formula: from + smoothstep(t) * (to - from)
  236. *
  237. * @param[in] from from value
  238. * @param[in] to to value
  239. * @param[in] t interpolant (amount)
  240. */
  241. CGLM_INLINE
  242. float
  243. glm_smoothinterp(float from, float to, float t) {
  244. return from + glm_smooth(t) * (to - from);
  245. }
  246. /*!
  247. * @brief clamped smoothstep interpolation between two numbers
  248. *
  249. * formula: from + smoothstep(t) * (to - from)
  250. *
  251. * @param[in] from from value
  252. * @param[in] to to value
  253. * @param[in] t interpolant (amount) clamped between 0 and 1
  254. */
  255. CGLM_INLINE
  256. float
  257. glm_smoothinterpc(float from, float to, float t) {
  258. return glm_smoothinterp(from, to, glm_clamp_zo(t));
  259. }
  260. /*!
  261. * @brief check if two float equal with using EPSILON
  262. *
  263. * @param[in] a a
  264. * @param[in] b b
  265. */
  266. CGLM_INLINE
  267. bool
  268. glm_eq(float a, float b) {
  269. return fabsf(a - b) <= GLM_FLT_EPSILON;
  270. }
  271. /*!
  272. * @brief percentage of current value between start and end value
  273. *
  274. * maybe fraction could be alternative name.
  275. *
  276. * @param[in] from from value
  277. * @param[in] to to value
  278. * @param[in] current current value
  279. */
  280. CGLM_INLINE
  281. float
  282. glm_percent(float from, float to, float current) {
  283. float t;
  284. if ((t = to - from) == 0.0f)
  285. return 1.0f;
  286. return (current - from) / t;
  287. }
  288. /*!
  289. * @brief clamped percentage of current value between start and end value
  290. *
  291. * @param[in] from from value
  292. * @param[in] to to value
  293. * @param[in] current current value
  294. */
  295. CGLM_INLINE
  296. float
  297. glm_percentc(float from, float to, float current) {
  298. return glm_clamp_zo(glm_percent(from, to, current));
  299. }
  300. /*!
  301. * @brief swap two float values
  302. *
  303. * @param[in] a float value 1 (pointer)
  304. * @param[in] b float value 2 (pointer)
  305. */
  306. CGLM_INLINE
  307. void
  308. glm_swapf(float * __restrict a, float * __restrict b) {
  309. float t;
  310. t = *a;
  311. *a = *b;
  312. *b = t;
  313. }
  314. #endif /* cglm_util_h */