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.

346 lines
9.3KB

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