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.

386 lines
9.1KB

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