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.

310 lines
7.1KB

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