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.

298 lines
6.5KB

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