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.

math.hpp 6.2KB

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