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.

419 lines
9.8KB

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