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.

333 lines
9.0KB

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