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.

295 lines
7.7KB

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