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.

291 lines
6.4KB

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