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.

295 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, returns min
  57. */
  58. inline float clampf(float x, float min, float max) {
  59. return fmaxf(fminf(x, max), min);
  60. }
  61. /** If the magnitude of x if less than eps, return 0 */
  62. inline float chopf(float x, float eps) {
  63. return -eps < x && x < eps ? 0.0 : x;
  64. }
  65. inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) {
  66. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  67. }
  68. inline float crossf(float a, float b, float frac) {
  69. return a + frac * (b - a);
  70. }
  71. inline float quadraticBipolar(float x) {
  72. float x2 = x*x;
  73. return x >= 0.0 ? x2 : -x2;
  74. }
  75. inline float cubic(float x) {
  76. return x*x*x;
  77. }
  78. inline float quarticBipolar(float x) {
  79. return x >= 0.0 ? x*x*x*x : -x*x*x*x;
  80. }
  81. inline float quintic(float x) {
  82. // optimal with --fast-math
  83. return x*x*x*x*x;
  84. }
  85. inline float sqrtBipolar(float x) {
  86. return x >= 0.0 ? sqrtf(x) : -sqrtf(-x);
  87. }
  88. /** This is pretty much a scaled sinh */
  89. inline float exponentialBipolar(float b, float x) {
  90. const float a = b - 1.0 / b;
  91. return (powf(b, x) - powf(b, -x)) / a;
  92. }
  93. inline float sincf(float x) {
  94. if (x == 0.0)
  95. return 1.0;
  96. x *= M_PI;
  97. return sinf(x) / x;
  98. }
  99. /** Linearly interpolate an array `p` with index `x`
  100. Assumes that the array at `p` is of length at least floor(x)+1.
  101. */
  102. inline float interpf(const float *p, float x) {
  103. int xi = x;
  104. float xf = x - xi;
  105. return crossf(p[xi], p[xi+1], xf);
  106. }
  107. /** Complex multiply c = a * b
  108. Arguments may be the same pointers
  109. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  110. */
  111. inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {
  112. *cr = ar * br - ai * bi;
  113. *ci = ar * bi + ai * br;
  114. }
  115. ////////////////////
  116. // 2D float vector
  117. ////////////////////
  118. struct Rect;
  119. struct Vec {
  120. float x, y;
  121. Vec() : x(0.0), y(0.0) {}
  122. Vec(float x, float y) : x(x), y(y) {}
  123. Vec neg() {
  124. return Vec(-x, -y);
  125. }
  126. Vec plus(Vec b) {
  127. return Vec(x + b.x, y + b.y);
  128. }
  129. Vec minus(Vec b) {
  130. return Vec(x - b.x, y - b.y);
  131. }
  132. Vec mult(float s) {
  133. return Vec(x * s, y * s);
  134. }
  135. Vec mult(Vec b) {
  136. return Vec(x * b.x, y * b.y);
  137. }
  138. Vec div(float s) {
  139. return Vec(x / s, y / s);
  140. }
  141. Vec div(Vec b) {
  142. return Vec(x / b.x, y / b.y);
  143. }
  144. float dot(Vec b) {
  145. return x * b.x + y * b.y;
  146. }
  147. float norm() {
  148. return hypotf(x, y);
  149. }
  150. Vec min(Vec b) {
  151. return Vec(fminf(x, b.x), fminf(y, b.y));
  152. }
  153. Vec max(Vec b) {
  154. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  155. }
  156. Vec round() {
  157. return Vec(roundf(x), roundf(y));
  158. }
  159. Vec floor() {
  160. return Vec(floorf(x), floorf(y));
  161. }
  162. Vec ceil() {
  163. return Vec(ceilf(x), ceilf(y));
  164. }
  165. bool isEqual(Vec b) {
  166. return x == b.x && y == b.y;
  167. }
  168. bool isZero() {
  169. return x == 0.0 && y == 0.0;
  170. }
  171. bool isFinite() {
  172. return std::isfinite(x) && std::isfinite(y);
  173. }
  174. Vec clamp(Rect bound);
  175. };
  176. struct Rect {
  177. Vec pos;
  178. Vec size;
  179. Rect() {}
  180. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  181. static Rect fromMinMax(Vec min, Vec max) {
  182. return Rect(min, max.minus(min));
  183. }
  184. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  185. bool contains(Vec v) {
  186. return pos.x <= v.x && v.x < pos.x + size.x
  187. && pos.y <= v.y && v.y < pos.y + size.y;
  188. }
  189. /** Returns whether this Rect contains an entire Rect */
  190. bool contains(Rect r) {
  191. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  192. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  193. }
  194. /** Returns whether this Rect overlaps with another Rect */
  195. bool intersects(Rect r) {
  196. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  197. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  198. }
  199. bool isEqual(Rect r) {
  200. return pos.isEqual(r.pos) && size.isEqual(r.size);
  201. }
  202. Vec getCenter() {
  203. return pos.plus(size.mult(0.5));
  204. }
  205. Vec getTopRight() {
  206. return pos.plus(Vec(size.x, 0.0));
  207. }
  208. Vec getBottomLeft() {
  209. return pos.plus(Vec(0.0, size.y));
  210. }
  211. Vec getBottomRight() {
  212. return pos.plus(size);
  213. }
  214. /** Clamps the edges of the rectangle to fit within a bound */
  215. Rect clamp(Rect bound) {
  216. Rect r;
  217. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  218. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  219. r.size.x = clampf(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  220. r.size.y = clampf(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  221. return r;
  222. }
  223. /** Nudges the position to fix inside a bounding box */
  224. Rect nudge(Rect bound) {
  225. Rect r;
  226. r.size = size;
  227. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  228. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  229. return r;
  230. }
  231. /** Expands this Rect to contain `other` */
  232. Rect expand(Rect other) {
  233. Rect r;
  234. r.pos.x = fminf(pos.x, other.pos.x);
  235. r.pos.y = fminf(pos.y, other.pos.y);
  236. r.size.x = fmaxf(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  237. r.size.y = fmaxf(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  238. return r;
  239. }
  240. /** Returns a Rect with its position set to zero */
  241. Rect zeroPos() {
  242. Rect r;
  243. r.size = size;
  244. return r;
  245. }
  246. };
  247. inline Vec Vec::clamp(Rect bound) {
  248. return Vec(
  249. clampf(x, bound.pos.x, bound.pos.x + bound.size.x),
  250. clampf(y, bound.pos.y, bound.pos.y + bound.size.y));
  251. }
  252. } // namespace rack