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.

316 lines
8.1KB

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