util.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 existing 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 existing 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 find minimum of given two values
  138. *
  139. * @param[in] a number 1
  140. * @param[in] b number 2
  141. *
  142. * @return smallest of the two values
  143. */
  144. CGLM_INLINE
  145. int
  146. glm_imin(int a, int b) {
  147. if (a < b)
  148. return a;
  149. return b;
  150. }
  151. /*!
  152. * @brief find maximum of given two values
  153. *
  154. * @param[in] a number 1
  155. * @param[in] b number 2
  156. *
  157. * @return largest of the two values
  158. */
  159. CGLM_INLINE
  160. int
  161. glm_imax(int a, int b) {
  162. if (a > b)
  163. return a;
  164. return b;
  165. }
  166. /*!
  167. * @brief clamp a number between min and max
  168. *
  169. * @param[in] val value to clamp
  170. * @param[in] minVal minimum value
  171. * @param[in] maxVal maximum value
  172. */
  173. CGLM_INLINE
  174. float
  175. glm_clamp(float val, float minVal, float maxVal) {
  176. return glm_min(glm_max(val, minVal), maxVal);
  177. }
  178. /*!
  179. * @brief clamp a number to zero and one
  180. *
  181. * @param[in] val value to clamp
  182. */
  183. CGLM_INLINE
  184. float
  185. glm_clamp_zo(float val) {
  186. return glm_clamp(val, 0.0f, 1.0f);
  187. }
  188. /*!
  189. * @brief linear interpolation between two numbers
  190. *
  191. * formula: from + t * (to - from)
  192. *
  193. * @param[in] from from value
  194. * @param[in] to to value
  195. * @param[in] t interpolant (amount)
  196. */
  197. CGLM_INLINE
  198. float
  199. glm_lerp(float from, float to, float t) {
  200. return from + t * (to - from);
  201. }
  202. /*!
  203. * @brief clamped linear interpolation between two numbers
  204. *
  205. * formula: from + t * (to - from)
  206. *
  207. * @param[in] from from value
  208. * @param[in] to to value
  209. * @param[in] t interpolant (amount) clamped between 0 and 1
  210. */
  211. CGLM_INLINE
  212. float
  213. glm_lerpc(float from, float to, float t) {
  214. return glm_lerp(from, to, glm_clamp_zo(t));
  215. }
  216. /*!
  217. * @brief threshold function
  218. *
  219. * @param[in] edge threshold
  220. * @param[in] x value to test against threshold
  221. * @return returns 0.0 if x < edge, else 1.0
  222. */
  223. CGLM_INLINE
  224. float
  225. glm_step(float edge, float x) {
  226. /* branching - no type conversion */
  227. return (x < edge) ? 0.0f : 1.0f;
  228. /*
  229. * An alternative implementation without branching
  230. * but with type conversion could be:
  231. * return !(x < edge);
  232. */
  233. }
  234. /*!
  235. * @brief smooth Hermite interpolation
  236. *
  237. * formula: t^2 * (3-2t)
  238. *
  239. * @param[in] t interpolant (amount)
  240. */
  241. CGLM_INLINE
  242. float
  243. glm_smooth(float t) {
  244. return t * t * (3.0f - 2.0f * t);
  245. }
  246. /*!
  247. * @brief threshold function with a smooth transition (according to OpenCL specs)
  248. *
  249. * formula: t^2 * (3-2t)
  250. *
  251. * @param[in] edge0 low threshold
  252. * @param[in] edge1 high threshold
  253. * @param[in] x interpolant (amount)
  254. */
  255. CGLM_INLINE
  256. float
  257. glm_smoothstep(float edge0, float edge1, float x) {
  258. float t;
  259. t = glm_clamp_zo((x - edge0) / (edge1 - edge0));
  260. return glm_smooth(t);
  261. }
  262. /*!
  263. * @brief smoothstep interpolation between two numbers
  264. *
  265. * formula: from + smoothstep(t) * (to - from)
  266. *
  267. * @param[in] from from value
  268. * @param[in] to to value
  269. * @param[in] t interpolant (amount)
  270. */
  271. CGLM_INLINE
  272. float
  273. glm_smoothinterp(float from, float to, float t) {
  274. return from + glm_smooth(t) * (to - from);
  275. }
  276. /*!
  277. * @brief clamped smoothstep interpolation between two numbers
  278. *
  279. * formula: from + smoothstep(t) * (to - from)
  280. *
  281. * @param[in] from from value
  282. * @param[in] to to value
  283. * @param[in] t interpolant (amount) clamped between 0 and 1
  284. */
  285. CGLM_INLINE
  286. float
  287. glm_smoothinterpc(float from, float to, float t) {
  288. return glm_smoothinterp(from, to, glm_clamp_zo(t));
  289. }
  290. /*!
  291. * @brief check if two float equal with using EPSILON
  292. *
  293. * @param[in] a a
  294. * @param[in] b b
  295. */
  296. CGLM_INLINE
  297. bool
  298. glm_eq(float a, float b) {
  299. return fabsf(a - b) <= GLM_FLT_EPSILON;
  300. }
  301. /*!
  302. * @brief percentage of current value between start and end value
  303. *
  304. * maybe fraction could be alternative name.
  305. *
  306. * @param[in] from from value
  307. * @param[in] to to value
  308. * @param[in] current current value
  309. */
  310. CGLM_INLINE
  311. float
  312. glm_percent(float from, float to, float current) {
  313. float t;
  314. if ((t = to - from) == 0.0f)
  315. return 1.0f;
  316. return (current - from) / t;
  317. }
  318. /*!
  319. * @brief clamped percentage of current value between start and end value
  320. *
  321. * @param[in] from from value
  322. * @param[in] to to value
  323. * @param[in] current current value
  324. */
  325. CGLM_INLINE
  326. float
  327. glm_percentc(float from, float to, float current) {
  328. return glm_clamp_zo(glm_percent(from, to, current));
  329. }
  330. /*!
  331. * @brief swap two float values
  332. *
  333. * @param[in] a float value 1 (pointer)
  334. * @param[in] b float value 2 (pointer)
  335. */
  336. CGLM_INLINE
  337. void
  338. glm_swapf(float * __restrict a, float * __restrict b) {
  339. float t;
  340. t = *a;
  341. *a = *b;
  342. *b = t;
  343. }
  344. #endif /* cglm_util_h */