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.

413 lines
9.7KB

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