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.

284 lines
7.5KB

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