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.

312 lines
8.0KB

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