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.

269 lines
6.0KB

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