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.

345 lines
8.0KB

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