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.

401 lines
9.6KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <algorithm> // for std::min, max
  4. namespace rack {
  5. /** Supplemental `<cmath>` functions and types
  6. */
  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. If `b < a`, returns a.
  21. */
  22. inline int clamp(int x, int a, int b) {
  23. return std::max(std::min(x, b), a);
  24. }
  25. /** Limits `x` between `a` and `b`.
  26. If `b < a`, switches the two values.
  27. */
  28. inline int clampSafe(int x, int a, int b) {
  29. return (a <= b) ? clamp(x, a, b) : clamp(x, b, a);
  30. }
  31. /** Euclidean modulus. Always returns `0 <= mod < b`.
  32. `b` must be positive.
  33. See https://en.wikipedia.org/wiki/Euclidean_division
  34. */
  35. inline int eucMod(int a, int b) {
  36. int mod = a % b;
  37. if (mod < 0) {
  38. mod += b;
  39. }
  40. return mod;
  41. }
  42. /** Euclidean division.
  43. `b` must be positive.
  44. */
  45. inline int eucDiv(int a, int b) {
  46. int div = a / b;
  47. int mod = a % b;
  48. if (mod < 0) {
  49. div -= 1;
  50. }
  51. return div;
  52. }
  53. inline void eucDivMod(int a, int b, int *div, int *mod) {
  54. *div = a / b;
  55. *mod = a % b;
  56. if (*mod < 0) {
  57. *div -= 1;
  58. *mod += b;
  59. }
  60. }
  61. /** Returns `floor(log_2(n))`, or 0 if `n == 1`. */
  62. inline int log2(int n) {
  63. int i = 0;
  64. while (n >>= 1) {
  65. i++;
  66. }
  67. return i;
  68. }
  69. /** Returns whether `n` is a power of 2. */
  70. inline bool isPow2(int n) {
  71. return n > 0 && (n & (n - 1)) == 0;
  72. }
  73. ////////////////////
  74. // basic float functions
  75. ////////////////////
  76. /** Limits `x` between `a` and `b`.
  77. If `b < a`, returns a.
  78. */
  79. inline float clamp(float x, float a, float b) {
  80. return std::fmax(std::fmin(x, b), a);
  81. }
  82. /** Limits `x` between `a` and `b`.
  83. If `b < a`, switches the two values.
  84. */
  85. inline float clampSafe(float x, float a, float b) {
  86. return (a <= b) ? clamp(x, a, b) : clamp(x, b, a);
  87. }
  88. /** Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
  89. See https://en.wikipedia.org/wiki/Sign_function.
  90. */
  91. inline float sgn(float x) {
  92. return x > 0.f ? 1.f : (x < 0.f ? -1.f : 0.f);
  93. }
  94. /** Converts -0.f to 0.f. Leaves all other values unchanged. */
  95. inline float normalizeZero(float x) {
  96. return x + 0.f;
  97. }
  98. /** Euclidean modulus. Always returns `0 <= mod < b`.
  99. See https://en.wikipedia.org/wiki/Euclidean_division.
  100. */
  101. inline float eucMod(float a, float b) {
  102. float mod = std::fmod(a, b);
  103. if (mod < 0.f) {
  104. mod += b;
  105. }
  106. return mod;
  107. }
  108. /** Returns whether `a` is within epsilon distance from `b`. */
  109. inline bool isNear(float a, float b, float epsilon = 1e-6f) {
  110. return std::fabs(a - b) <= epsilon;
  111. }
  112. /** If the magnitude of `x` if less than epsilon, return 0. */
  113. inline float chop(float x, float epsilon = 1e-6f) {
  114. return std::fabs(x) <= epsilon ? 0.f : x;
  115. }
  116. inline float rescale(float x, float xMin, float xMax, float yMin, float yMax) {
  117. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  118. }
  119. inline float crossfade(float a, float b, float p) {
  120. return a + (b - a) * p;
  121. }
  122. /** Linearly interpolates an array `p` with index `x`.
  123. Assumes that the array at `p` is of length at least `floor(x) + 1`.
  124. */
  125. inline float interpolateLinear(const float *p, float x) {
  126. int xi = x;
  127. float xf = x - xi;
  128. return crossfade(p[xi], p[xi+1], xf);
  129. }
  130. /** Complex multiplication `c = a * b`.
  131. Arguments may be the same pointers.
  132. Example:
  133. cmultf(ar, ai, br, bi, &ar, &ai);
  134. */
  135. inline void complexMult(float ar, float ai, float br, float bi, float *cr, float *ci) {
  136. *cr = ar * br - ai * bi;
  137. *ci = ar * bi + ai * br;
  138. }
  139. ////////////////////
  140. // 2D vector and rectangle
  141. ////////////////////
  142. struct Rect;
  143. struct Vec {
  144. float x = 0.f;
  145. float y = 0.f;
  146. Vec() {}
  147. Vec(float x, float y) : x(x), y(y) {}
  148. /** Negates the vector.
  149. Equivalent to a reflection across the `y = -x` line.
  150. */
  151. Vec neg() const {
  152. return Vec(-x, -y);
  153. }
  154. Vec plus(Vec b) const {
  155. return Vec(x + b.x, y + b.y);
  156. }
  157. Vec minus(Vec b) const {
  158. return Vec(x - b.x, y - b.y);
  159. }
  160. Vec mult(float s) const {
  161. return Vec(x * s, y * s);
  162. }
  163. Vec mult(Vec b) const {
  164. return Vec(x * b.x, y * b.y);
  165. }
  166. Vec div(float s) const {
  167. return Vec(x / s, y / s);
  168. }
  169. Vec div(Vec b) const {
  170. return Vec(x / b.x, y / b.y);
  171. }
  172. float dot(Vec b) const {
  173. return x * b.x + y * b.y;
  174. }
  175. float arg() const {
  176. return std::atan2(y, x);
  177. }
  178. float norm() const {
  179. return std::hypot(x, y);
  180. }
  181. Vec normalize() const {
  182. return div(norm());
  183. }
  184. float square() const {
  185. return x * x + y * y;
  186. }
  187. /** Rotates counterclockwise in radians. */
  188. Vec rotate(float angle) {
  189. float sin = std::sin(angle);
  190. float cos = std::cos(angle);
  191. return Vec(x * cos - y * sin, x * sin + y * cos);
  192. }
  193. /** Swaps the coordinates.
  194. Equivalent to a reflection across the `y = x` line.
  195. */
  196. Vec flip() const {
  197. return Vec(y, x);
  198. }
  199. Vec min(Vec b) const {
  200. return Vec(std::fmin(x, b.x), std::fmin(y, b.y));
  201. }
  202. Vec max(Vec b) const {
  203. return Vec(std::fmax(x, b.x), std::fmax(y, b.y));
  204. }
  205. Vec abs() const {
  206. return Vec(std::fabs(x), std::fabs(y));
  207. }
  208. Vec round() const {
  209. return Vec(std::round(x), std::round(y));
  210. }
  211. Vec floor() const {
  212. return Vec(std::floor(x), std::floor(y));
  213. }
  214. Vec ceil() const {
  215. return Vec(std::ceil(x), std::ceil(y));
  216. }
  217. bool isEqual(Vec b) const {
  218. return x == b.x && y == b.y;
  219. }
  220. bool isZero() const {
  221. return x == 0.f && y == 0.f;
  222. }
  223. bool isFinite() const {
  224. return std::isfinite(x) && std::isfinite(y);
  225. }
  226. Vec clamp(Rect bound) const;
  227. Vec clampSafe(Rect bound) const;
  228. Vec crossfade(Vec b, float p) {
  229. return this->plus(b.minus(*this).mult(p));
  230. }
  231. };
  232. struct Rect {
  233. Vec pos;
  234. Vec size;
  235. Rect() {}
  236. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  237. Rect(float posX, float posY, float sizeX, float sizeY) : pos(math::Vec(posX, posY)), size(math::Vec(sizeX, sizeY)) {}
  238. /** Constructs a Rect from the upper-left position `a` and lower-right pos `b`. */
  239. static Rect fromMinMax(Vec a, Vec b) {
  240. return Rect(a, b.minus(a));
  241. }
  242. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right. */
  243. bool isContaining(Vec v) const {
  244. return pos.x <= v.x && v.x < pos.x + size.x
  245. && pos.y <= v.y && v.y < pos.y + size.y;
  246. }
  247. /** Returns whether this Rect contains an entire Rect. */
  248. bool isContaining(Rect r) const {
  249. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  250. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  251. }
  252. /** Returns whether this Rect overlaps with another Rect. */
  253. bool isIntersecting(Rect r) const {
  254. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  255. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  256. }
  257. bool isEqual(Rect r) const {
  258. return pos.isEqual(r.pos) && size.isEqual(r.size);
  259. }
  260. float getRight() const {
  261. return pos.x + size.x;
  262. }
  263. float getBottom() const {
  264. return pos.y + size.y;
  265. }
  266. Vec getCenter() const {
  267. return pos.plus(size.mult(0.5f));
  268. }
  269. Vec getTopLeft() const {
  270. return pos;
  271. }
  272. Vec getTopRight() const {
  273. return pos.plus(Vec(size.x, 0.f));
  274. }
  275. Vec getBottomLeft() const {
  276. return pos.plus(Vec(0.f, size.y));
  277. }
  278. Vec getBottomRight() const {
  279. return pos.plus(size);
  280. }
  281. /** Clamps the edges of the rectangle to fit within a bound. */
  282. Rect clamp(Rect bound) const {
  283. Rect r;
  284. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  285. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  286. r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  287. r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  288. return r;
  289. }
  290. /** Nudges the position to fix inside a bounding box. */
  291. Rect nudge(Rect bound) const {
  292. Rect r;
  293. r.size = size;
  294. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  295. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  296. return r;
  297. }
  298. /** Returns the bounding box of the union of `this` and `b`. */
  299. Rect expand(Rect b) const {
  300. Rect r;
  301. r.pos.x = std::fmin(pos.x, b.pos.x);
  302. r.pos.y = std::fmin(pos.y, b.pos.y);
  303. r.size.x = std::fmax(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  304. r.size.y = std::fmax(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  305. return r;
  306. }
  307. /** Returns the intersection of `this` and `b`. */
  308. Rect intersect(Rect b) const {
  309. Rect r;
  310. r.pos.x = std::fmax(pos.x, b.pos.x);
  311. r.pos.y = std::fmax(pos.y, b.pos.y);
  312. r.size.x = std::fmin(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  313. r.size.y = std::fmin(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  314. return r;
  315. }
  316. /** Returns a Rect with its position set to zero. */
  317. Rect zeroPos() const {
  318. return Rect(Vec(), size);
  319. }
  320. /** Expands each corner.
  321. Use a negative delta to shrink.
  322. */
  323. Rect grow(Vec delta) const {
  324. Rect r;
  325. r.pos = pos.minus(delta);
  326. r.size = size.plus(delta.mult(2.f));
  327. return r;
  328. }
  329. DEPRECATED bool contains(Vec v) const {return isContaining(v);}
  330. DEPRECATED bool contains(Rect r) const {return isContaining(r);}
  331. DEPRECATED bool intersects(Rect r) const {return isIntersecting(r);}
  332. };
  333. inline Vec Vec::clamp(Rect bound) const {
  334. return Vec(
  335. math::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  336. math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  337. }
  338. inline Vec Vec::clampSafe(Rect bound) const {
  339. return Vec(
  340. math::clampSafe(x, bound.pos.x, bound.pos.x + bound.size.x),
  341. math::clampSafe(y, bound.pos.y, bound.pos.y + bound.size.y));
  342. }
  343. /** Expands a Vec and Rect into a comma-separated list.
  344. Useful for print debugging.
  345. printf("(%f %f) (%f %f %f %f)", VEC_ARGS(v), RECT_ARGS(r));
  346. Or passing the values to a C function.
  347. nvgRect(vg, RECT_ARGS(r));
  348. */
  349. #define VEC_ARGS(v) (v).x, (v).y
  350. #define RECT_ARGS(r) (r).pos.x, (r).pos.y, (r).size.x, (r).size.y
  351. } // namespace math
  352. } // namespace rack