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.

347 lines
8.7KB

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