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.

360 lines
8.3KB

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