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.

329 lines
7.5KB

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