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.

331 lines
8.4KB

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