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.

289 lines
7.6KB

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