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.

327 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, y;
  119. Vec() : x(0.0f), y(0.0f) {}
  120. Vec(float x, float y) : x(x), y(y) {}
  121. Vec neg() {
  122. return Vec(-x, -y);
  123. }
  124. Vec plus(Vec b) {
  125. return Vec(x + b.x, y + b.y);
  126. }
  127. Vec minus(Vec b) {
  128. return Vec(x - b.x, y - b.y);
  129. }
  130. Vec mult(float s) {
  131. return Vec(x * s, y * s);
  132. }
  133. Vec mult(Vec b) {
  134. return Vec(x * b.x, y * b.y);
  135. }
  136. Vec div(float s) {
  137. return Vec(x / s, y / s);
  138. }
  139. Vec div(Vec b) {
  140. return Vec(x / b.x, y / b.y);
  141. }
  142. float dot(Vec b) {
  143. return x * b.x + y * b.y;
  144. }
  145. float norm() {
  146. return hypotf(x, y);
  147. }
  148. Vec min(Vec b) {
  149. return Vec(rack::min(x, b.x), rack::min(y, b.y));
  150. }
  151. Vec max(Vec b) {
  152. return Vec(rack::max(x, b.x), rack::max(y, b.y));
  153. }
  154. Vec round() {
  155. return Vec(roundf(x), roundf(y));
  156. }
  157. Vec floor() {
  158. return Vec(floorf(x), floorf(y));
  159. }
  160. Vec ceil() {
  161. return Vec(ceilf(x), ceilf(y));
  162. }
  163. bool isEqual(Vec b) {
  164. return x == b.x && y == b.y;
  165. }
  166. bool isZero() {
  167. return x == 0.0f && y == 0.0f;
  168. }
  169. bool isFinite() {
  170. return isfinite(x) && isfinite(y);
  171. }
  172. Vec clamp(Rect bound);
  173. Vec clamp2(Rect bound);
  174. };
  175. struct Rect {
  176. Vec pos;
  177. Vec size;
  178. Rect() {}
  179. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  180. /** Constructs a Rect from the upper-left position `a` and lower-right pos `b` */
  181. static Rect fromMinMax(Vec a, Vec b) {
  182. return Rect(a, b.minus(a));
  183. }
  184. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  185. bool contains(Vec v) {
  186. return pos.x <= v.x && v.x < pos.x + size.x
  187. && pos.y <= v.y && v.y < pos.y + size.y;
  188. }
  189. /** Returns whether this Rect contains an entire Rect */
  190. bool contains(Rect r) {
  191. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  192. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  193. }
  194. /** Returns whether this Rect overlaps with another Rect */
  195. bool intersects(Rect r) {
  196. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  197. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  198. }
  199. bool isEqual(Rect r) {
  200. return pos.isEqual(r.pos) && size.isEqual(r.size);
  201. }
  202. Vec getCenter() {
  203. return pos.plus(size.mult(0.5f));
  204. }
  205. Vec getTopRight() {
  206. return pos.plus(Vec(size.x, 0.0f));
  207. }
  208. Vec getBottomLeft() {
  209. return pos.plus(Vec(0.0f, size.y));
  210. }
  211. Vec getBottomRight() {
  212. return pos.plus(size);
  213. }
  214. /** Clamps the edges of the rectangle to fit within a bound */
  215. Rect clamp(Rect bound) {
  216. Rect r;
  217. r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  218. r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  219. r.size.x = rack::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  220. r.size.y = rack::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  221. return r;
  222. }
  223. /** Nudges the position to fix inside a bounding box */
  224. Rect nudge(Rect bound) {
  225. Rect r;
  226. r.size = size;
  227. r.pos.x = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  228. r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  229. return r;
  230. }
  231. /** Expands this Rect to contain `other` */
  232. Rect expand(Rect other) {
  233. Rect r;
  234. r.pos.x = min(pos.x, other.pos.x);
  235. r.pos.y = min(pos.y, other.pos.y);
  236. r.size.x = max(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  237. r.size.y = max(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  238. return r;
  239. }
  240. /** Returns a Rect with its position set to zero */
  241. Rect zeroPos() {
  242. Rect r;
  243. r.size = size;
  244. return r;
  245. }
  246. Rect grow(Vec delta) {
  247. Rect r;
  248. r.pos = pos.minus(delta);
  249. r.size = size.plus(delta.mult(2.f));
  250. return r;
  251. }
  252. };
  253. inline Vec Vec::clamp(Rect bound) {
  254. return Vec(
  255. rack::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  256. rack::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  257. }
  258. inline Vec Vec::clamp2(Rect bound) {
  259. return Vec(
  260. rack::clamp2(x, bound.pos.x, bound.pos.x + bound.size.x),
  261. rack::clamp2(y, bound.pos.y, bound.pos.y + bound.size.y));
  262. }
  263. ////////////////////
  264. // Deprecated functions
  265. ////////////////////
  266. DEPRECATED inline int mini(int a, int b) {return min(a, b);}
  267. DEPRECATED inline int maxi(int a, int b) {return max(a, b);}
  268. DEPRECATED inline int clampi(int x, int min, int max) {return clamp(x, min, max);}
  269. DEPRECATED inline int absi(int a) {return abs(a);}
  270. DEPRECATED inline int eucmodi(int a, int base) {return eucmod(a, base);}
  271. DEPRECATED inline int log2i(int n) {return log2(n);}
  272. DEPRECATED inline bool ispow2i(int n) {return ispow2(n);}
  273. DEPRECATED inline float absf(float x) {return fabsf(x);}
  274. DEPRECATED inline float sgnf(float x) {return sgn(x);}
  275. DEPRECATED inline float eucmodf(float a, float base) {return eucmod(a, base);}
  276. DEPRECATED inline bool nearf(float a, float b, float epsilon = 1.0e-6f) {return isNear(a, b, epsilon);}
  277. DEPRECATED inline float clampf(float x, float min, float max) {return clamp(x, min, max);}
  278. DEPRECATED inline float clamp2f(float x, float min, float max) {return clamp2(x, min, max);}
  279. DEPRECATED inline float chopf(float x, float eps) {return chop(x, eps);}
  280. DEPRECATED inline float rescalef(float x, float a, float b, float yMin, float yMax) {return rescale(x, a, b, yMin, yMax);}
  281. DEPRECATED inline float crossf(float a, float b, float frac) {return crossfade(a, b, frac);}
  282. DEPRECATED inline float interpf(const float *p, float x) {return interpolateLinear(p, x);}
  283. DEPRECATED inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {return cmult(cr, ci, ar, ai, br, bi);}
  284. } // namespace rack