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.

408 lines
9.6KB

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