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.

351 lines
8.1KB

  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. __attribute__((optimize("signed-zeros")))
  95. inline float normalizeZero(float x) {
  96. return x + 0.f;
  97. }
  98. /** Euclidean modulus. Always returns 0 <= mod < b.
  99. See https://en.wikipedia.org/wiki/Euclidean_division
  100. */
  101. inline float eucMod(float a, float base) {
  102. float mod = std::fmod(a, base);
  103. return (mod >= 0.f) ? mod : mod + base;
  104. }
  105. inline bool isNear(float a, float b, float epsilon = 1e-6f) {
  106. return std::abs(a - b) <= epsilon;
  107. }
  108. /** If the magnitude of x if less than epsilon, return 0 */
  109. inline float chop(float x, float epsilon = 1e-6f) {
  110. return isNear(x, 0.f, epsilon) ? 0.f : x;
  111. }
  112. inline float rescale(float x, float a, float b, float yMin, float yMax) {
  113. return yMin + (x - a) / (b - a) * (yMax - yMin);
  114. }
  115. inline float crossfade(float a, float b, float frac) {
  116. return a + frac * (b - a);
  117. }
  118. /** Linearly interpolate an array `p` with index `x`
  119. Assumes that the array at `p` is of length at least floor(x)+1.
  120. */
  121. inline float interpolateLinear(const float *p, float x) {
  122. int xi = x;
  123. float xf = x - xi;
  124. return crossfade(p[xi], p[xi+1], xf);
  125. }
  126. /** Complex multiply c = a * b
  127. Arguments may be the same pointers
  128. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  129. */
  130. inline void complexMult(float *cr, float *ci, float ar, float ai, float br, float bi) {
  131. *cr = ar * br - ai * bi;
  132. *ci = ar * bi + ai * br;
  133. }
  134. ////////////////////
  135. // 2D vector and rectangle
  136. ////////////////////
  137. struct Rect;
  138. struct Vec {
  139. float x = 0.f;
  140. float y = 0.f;
  141. Vec() {}
  142. Vec(float x, float y) : x(x), y(y) {}
  143. /** Negates the vector
  144. Equivalent to a reflection across the y=-x line.
  145. */
  146. Vec neg() const {
  147. return Vec(-x, -y);
  148. }
  149. Vec plus(Vec b) const {
  150. return Vec(x + b.x, y + b.y);
  151. }
  152. Vec minus(Vec b) const {
  153. return Vec(x - b.x, y - b.y);
  154. }
  155. Vec mult(float s) const {
  156. return Vec(x * s, y * s);
  157. }
  158. Vec mult(Vec b) const {
  159. return Vec(x * b.x, y * b.y);
  160. }
  161. Vec div(float s) const {
  162. return Vec(x / s, y / s);
  163. }
  164. Vec div(Vec b) const {
  165. return Vec(x / b.x, y / b.y);
  166. }
  167. float dot(Vec b) const {
  168. return x * b.x + y * b.y;
  169. }
  170. float norm() const {
  171. return std::hypotf(x, y);
  172. }
  173. /** Rotates counterclockwise in radians */
  174. Vec rotate(float angle) {
  175. float sin = std::sin(angle);
  176. float cos = std::cos(angle);
  177. return Vec(x * cos - y * sin, x * sin + y * cos);
  178. }
  179. /** Swaps the coordinates
  180. Equivalent to a reflection across the y=x line.
  181. */
  182. Vec flip() const {
  183. return Vec(y, x);
  184. }
  185. Vec min(Vec b) const {
  186. return Vec(std::min(x, b.x), std::min(y, b.y));
  187. }
  188. Vec max(Vec b) const {
  189. return Vec(std::max(x, b.x), std::max(y, b.y));
  190. }
  191. Vec round() const {
  192. return Vec(std::round(x), std::round(y));
  193. }
  194. Vec floor() const {
  195. return Vec(std::floor(x), std::floor(y));
  196. }
  197. Vec ceil() const {
  198. return Vec(std::ceil(x), std::ceil(y));
  199. }
  200. bool isEqual(Vec b) const {
  201. return x == b.x && y == b.y;
  202. }
  203. bool isZero() const {
  204. return x == 0.f && y == 0.f;
  205. }
  206. bool isFinite() const {
  207. return std::isfinite(x) && std::isfinite(y);
  208. }
  209. Vec clamp(Rect bound) const;
  210. Vec clampSafe(Rect bound) const;
  211. };
  212. struct Rect {
  213. Vec pos;
  214. Vec size;
  215. Rect() {}
  216. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  217. /** Constructs a Rect from the upper-left position `a` and lower-right pos `b` */
  218. static Rect fromMinMax(Vec a, Vec b) {
  219. return Rect(a, b.minus(a));
  220. }
  221. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  222. bool contains(Vec v) const {
  223. return pos.x <= v.x && v.x < pos.x + size.x
  224. && pos.y <= v.y && v.y < pos.y + size.y;
  225. }
  226. /** Returns whether this Rect contains an entire Rect */
  227. bool contains(Rect r) const {
  228. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  229. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  230. }
  231. /** Returns whether this Rect overlaps with another Rect */
  232. bool intersects(Rect r) const {
  233. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  234. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  235. }
  236. bool isEqual(Rect r) const {
  237. return pos.isEqual(r.pos) && size.isEqual(r.size);
  238. }
  239. float getLeft() const {
  240. return pos.x + size.x;
  241. }
  242. float getBottom() const {
  243. return pos.y + size.y;
  244. }
  245. Vec getCenter() const {
  246. return pos.plus(size.mult(0.5f));
  247. }
  248. Vec getTopLeft() const {
  249. return pos;
  250. }
  251. Vec getTopRight() const {
  252. return pos.plus(Vec(size.x, 0.f));
  253. }
  254. Vec getBottomLeft() const {
  255. return pos.plus(Vec(0.f, size.y));
  256. }
  257. Vec getBottomRight() const {
  258. return pos.plus(size);
  259. }
  260. /** Clamps the edges of the rectangle to fit within a bound */
  261. Rect clamp(Rect bound) const {
  262. Rect r;
  263. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  264. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  265. r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  266. r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  267. return r;
  268. }
  269. /** Nudges the position to fix inside a bounding box */
  270. Rect nudge(Rect bound) const {
  271. Rect r;
  272. r.size = size;
  273. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  274. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  275. return r;
  276. }
  277. /** Expands this Rect to contain `other` */
  278. Rect expand(Rect other) const {
  279. Rect r;
  280. r.pos.x = std::min(pos.x, other.pos.x);
  281. r.pos.y = std::min(pos.y, other.pos.y);
  282. r.size.x = std::max(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  283. r.size.y = std::max(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  284. return r;
  285. }
  286. /** Returns a Rect with its position set to zero */
  287. Rect zeroPos() const {
  288. return Rect(Vec(), size);
  289. }
  290. /** Expands each corner
  291. Use a negative delta to shrink.
  292. */
  293. Rect grow(Vec delta) const {
  294. Rect r;
  295. r.pos = pos.minus(delta);
  296. r.size = size.plus(delta.mult(2.f));
  297. return r;
  298. }
  299. };
  300. inline Vec Vec::clamp(Rect bound) const {
  301. return Vec(
  302. math::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  303. math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  304. }
  305. inline Vec Vec::clampSafe(Rect bound) const {
  306. return Vec(
  307. math::clampSafe(x, bound.pos.x, bound.pos.x + bound.size.x),
  308. math::clampSafe(y, bound.pos.y, bound.pos.y + bound.size.y));
  309. }
  310. } // namespace math
  311. } // namespace rack