You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
7.6KB

  1. #pragma once
  2. #include "util/common.hpp"
  3. #ifndef _GLIBCXX_USE_C99_MATH
  4. #define _GLIBCXX_USE_C99_MATH
  5. #endif
  6. #include <math.h>
  7. namespace rack {
  8. ////////////////////
  9. // basic integer functions (suffixed with "i")
  10. ////////////////////
  11. inline int min(int a, int b) {
  12. return (a < b) ? a : b;
  13. }
  14. inline int max(int a, int b) {
  15. return (a > b) ? a : b;
  16. }
  17. /** Limits a value between a minimum and maximum
  18. Assumes min <= max
  19. */
  20. inline int clamp(int x, int minimum, int maximum) {
  21. return min(max(x, minimum), maximum);
  22. }
  23. /** Euclidean modulus, always returns 0 <= mod < base for positive base.
  24. */
  25. inline int eucmod(int a, int base) {
  26. int mod = a % base;
  27. return (mod >= 0) ? mod : mod + base;
  28. }
  29. /** Returns floor(log_2(n)), or 0 if n == 1.
  30. */
  31. inline int log2(int n) {
  32. int i = 0;
  33. while (n >>= 1) {
  34. i++;
  35. }
  36. return i;
  37. }
  38. inline bool ispow2(int n) {
  39. return n > 0 && (n & (n - 1)) == 0;
  40. }
  41. ////////////////////
  42. // basic float functions (suffixed with "f")
  43. ////////////////////
  44. /** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */
  45. inline float sgn(float x) {
  46. return copysignf(1.0f, x);
  47. }
  48. inline float eucmod(float a, float base) {
  49. float mod = fmodf(a, base);
  50. return (mod >= 0.0f) ? mod : mod + base;
  51. }
  52. inline bool isNear(float a, float b, float epsilon = 1.0e-6f) {
  53. return fabsf(a - b) <= epsilon;
  54. }
  55. /** Limits a value between a minimum and maximum
  56. Assumes min <= max
  57. */
  58. inline float clamp(float x, float minimum, float maximum) {
  59. return fminf(fmaxf(x, minimum), maximum);
  60. }
  61. /** Limits a value between a minimum and maximum
  62. If min > max, switches the two values
  63. */
  64. inline float clamp2(float x, float min, float max) {
  65. return clamp(x, fminf(min, max), fmaxf(min, max));
  66. }
  67. /** If the magnitude of x if less than eps, return 0 */
  68. inline float chop(float x, float eps) {
  69. return (-eps < x && x < eps) ? 0.0f : x;
  70. }
  71. inline float rescale(float x, float xMin, float xMax, float yMin, float yMax) {
  72. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  73. }
  74. inline float crossfade(float a, float b, float frac) {
  75. return a + frac * (b - a);
  76. }
  77. /** Linearly interpolate an array `p` with index `x`
  78. Assumes that the array at `p` is of length at least floor(x)+1.
  79. */
  80. inline float interpolateLinear(const float *p, float x) {
  81. int xi = x;
  82. float xf = x - xi;
  83. return crossfade(p[xi], p[xi+1], xf);
  84. }
  85. /** Complex multiply c = a * b
  86. Arguments may be the same pointers
  87. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  88. */
  89. inline void cmult(float *cr, float *ci, float ar, float ai, float br, float bi) {
  90. *cr = ar * br - ai * bi;
  91. *ci = ar * bi + ai * br;
  92. }
  93. ////////////////////
  94. // 2D vector and rectangle
  95. ////////////////////
  96. struct Rect;
  97. struct Vec {
  98. float x, y;
  99. Vec() : x(0.0f), y(0.0f) {}
  100. Vec(float x, float y) : x(x), y(y) {}
  101. Vec neg() {
  102. return Vec(-x, -y);
  103. }
  104. Vec plus(Vec b) {
  105. return Vec(x + b.x, y + b.y);
  106. }
  107. Vec minus(Vec b) {
  108. return Vec(x - b.x, y - b.y);
  109. }
  110. Vec mult(float s) {
  111. return Vec(x * s, y * s);
  112. }
  113. Vec mult(Vec b) {
  114. return Vec(x * b.x, y * b.y);
  115. }
  116. Vec div(float s) {
  117. return Vec(x / s, y / s);
  118. }
  119. Vec div(Vec b) {
  120. return Vec(x / b.x, y / b.y);
  121. }
  122. float dot(Vec b) {
  123. return x * b.x + y * b.y;
  124. }
  125. float norm() {
  126. return hypotf(x, y);
  127. }
  128. Vec min(Vec b) {
  129. return Vec(fminf(x, b.x), fminf(y, b.y));
  130. }
  131. Vec max(Vec b) {
  132. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  133. }
  134. Vec round() {
  135. return Vec(roundf(x), roundf(y));
  136. }
  137. Vec floor() {
  138. return Vec(floorf(x), floorf(y));
  139. }
  140. Vec ceil() {
  141. return Vec(ceilf(x), ceilf(y));
  142. }
  143. bool isEqual(Vec b) {
  144. return x == b.x && y == b.y;
  145. }
  146. bool isZero() {
  147. return x == 0.0f && y == 0.0f;
  148. }
  149. bool isFinite() {
  150. return isfinite(x) && isfinite(y);
  151. }
  152. Vec clamp(Rect bound);
  153. };
  154. struct Rect {
  155. Vec pos;
  156. Vec size;
  157. Rect() {}
  158. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  159. static Rect fromMinMax(Vec min, Vec max) {
  160. return Rect(min, max.minus(min));
  161. }
  162. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  163. bool contains(Vec v) {
  164. return pos.x <= v.x && v.x < pos.x + size.x
  165. && pos.y <= v.y && v.y < pos.y + size.y;
  166. }
  167. /** Returns whether this Rect contains an entire Rect */
  168. bool contains(Rect r) {
  169. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  170. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  171. }
  172. /** Returns whether this Rect overlaps with another Rect */
  173. bool intersects(Rect r) {
  174. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  175. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  176. }
  177. bool isEqual(Rect r) {
  178. return pos.isEqual(r.pos) && size.isEqual(r.size);
  179. }
  180. Vec getCenter() {
  181. return pos.plus(size.mult(0.5f));
  182. }
  183. Vec getTopRight() {
  184. return pos.plus(Vec(size.x, 0.0f));
  185. }
  186. Vec getBottomLeft() {
  187. return pos.plus(Vec(0.0f, size.y));
  188. }
  189. Vec getBottomRight() {
  190. return pos.plus(size);
  191. }
  192. /** Clamps the edges of the rectangle to fit within a bound */
  193. Rect clamp(Rect bound) {
  194. Rect r;
  195. r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  196. r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  197. r.size.x = rack::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  198. r.size.y = rack::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  199. return r;
  200. }
  201. /** Nudges the position to fix inside a bounding box */
  202. Rect nudge(Rect bound) {
  203. Rect r;
  204. r.size = size;
  205. r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  206. r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  207. return r;
  208. }
  209. /** Expands this Rect to contain `other` */
  210. Rect expand(Rect other) {
  211. Rect r;
  212. r.pos.x = fminf(pos.x, other.pos.x);
  213. r.pos.y = fminf(pos.y, other.pos.y);
  214. r.size.x = fmaxf(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  215. r.size.y = fmaxf(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  216. return r;
  217. }
  218. /** Returns a Rect with its position set to zero */
  219. Rect zeroPos() {
  220. Rect r;
  221. r.size = size;
  222. return r;
  223. }
  224. };
  225. inline Vec Vec::clamp(Rect bound) {
  226. return Vec(
  227. clamp2(x, bound.pos.x, bound.pos.x + bound.size.x),
  228. clamp2(y, bound.pos.y, bound.pos.y + bound.size.y));
  229. }
  230. ////////////////////
  231. // Deprecated functions
  232. ////////////////////
  233. DEPRECATED inline int mini(int a, int b) {return min(a, b);}
  234. DEPRECATED inline int maxi(int a, int b) {return max(a, b);}
  235. DEPRECATED inline int clampi(int x, int min, int max) {return clamp(x, min, max);}
  236. DEPRECATED inline int absi(int a) {return abs(a);}
  237. DEPRECATED inline int eucmodi(int a, int base) {return eucmod(a, base);}
  238. DEPRECATED inline int log2i(int n) {return log2(n);}
  239. DEPRECATED inline bool ispow2i(int n) {return ispow2(n);}
  240. DEPRECATED inline float absf(float x) {return fabsf(x);}
  241. DEPRECATED inline float sgnf(float x) {return sgn(x);}
  242. DEPRECATED inline float eucmodf(float a, float base) {return eucmod(a, base);}
  243. DEPRECATED inline bool nearf(float a, float b, float epsilon = 1.0e-6f) {return isNear(a, b, epsilon);}
  244. DEPRECATED inline float clampf(float x, float min, float max) {return clamp(x, min, max);}
  245. DEPRECATED inline float clamp2f(float x, float min, float max) {return clamp2(x, min, max);}
  246. DEPRECATED inline float chopf(float x, float eps) {return chop(x, eps);}
  247. DEPRECATED inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) {return rescale(x, xMin, xMax, yMin, yMax);}
  248. DEPRECATED inline float crossf(float a, float b, float frac) {return crossfade(a, b, frac);}
  249. DEPRECATED inline float interpf(const float *p, float x) {return interpolateLinear(p, x);}
  250. DEPRECATED inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {return cmult(cr, ci, ar, ai, br, bi);}
  251. } // namespace rack