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.

487 lines
11KB

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