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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. inline float getf(const float *p, float v = 0.0) {
  97. return p ? *p : v;
  98. }
  99. inline void setf(float *p, float v) {
  100. if (p)
  101. *p = v;
  102. }
  103. /** Linearly interpolate an array `p` with index `x`
  104. Assumes that the array at `p` is of length at least floor(x)+1.
  105. */
  106. inline float interpf(const float *p, float x) {
  107. int xi = x;
  108. float xf = x - xi;
  109. return crossf(p[xi], p[xi+1], xf);
  110. }
  111. /** Complex multiply c = a * b
  112. It is of course acceptable to reuse arguments
  113. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  114. */
  115. inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {
  116. *cr = ar * br - ai * bi;
  117. *ci = ar * bi + ai * br;
  118. }
  119. ////////////////////
  120. // 2D float vector
  121. ////////////////////
  122. struct Rect;
  123. struct Vec {
  124. float x, y;
  125. Vec() : x(0.0), y(0.0) {}
  126. Vec(float x, float y) : x(x), y(y) {}
  127. Vec neg() {
  128. return Vec(-x, -y);
  129. }
  130. Vec plus(Vec b) {
  131. return Vec(x + b.x, y + b.y);
  132. }
  133. Vec minus(Vec b) {
  134. return Vec(x - b.x, y - b.y);
  135. }
  136. Vec mult(float s) {
  137. return Vec(x * s, y * s);
  138. }
  139. Vec mult(Vec b) {
  140. return Vec(x * b.x, y * b.y);
  141. }
  142. Vec div(float s) {
  143. return Vec(x / s, y / s);
  144. }
  145. Vec div(Vec b) {
  146. return Vec(x / b.x, y / b.y);
  147. }
  148. float dot(Vec b) {
  149. return x * b.x + y * b.y;
  150. }
  151. float norm() {
  152. return hypotf(x, y);
  153. }
  154. Vec min(Vec b) {
  155. return Vec(fminf(x, b.x), fminf(y, b.y));
  156. }
  157. Vec max(Vec b) {
  158. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  159. }
  160. Vec round() {
  161. return Vec(roundf(x), roundf(y));
  162. }
  163. Vec floor() {
  164. return Vec(floorf(x), floorf(y));
  165. }
  166. Vec ceil() {
  167. return Vec(ceilf(x), ceilf(y));
  168. }
  169. bool isEqual(Vec b) {
  170. return x == b.x && y == b.y;
  171. }
  172. bool isZero() {
  173. return x == 0.0 && y == 0.0;
  174. }
  175. bool isFinite() {
  176. return std::isfinite(x) && std::isfinite(y);
  177. }
  178. Vec clamp(Rect bound);
  179. };
  180. struct Rect {
  181. Vec pos;
  182. Vec size;
  183. Rect() {}
  184. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  185. static Rect fromMinMax(Vec min, Vec max) {
  186. return Rect(min, max.minus(min));
  187. }
  188. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  189. bool contains(Vec v) {
  190. return pos.x <= v.x && v.x < pos.x + size.x
  191. && pos.y <= v.y && v.y < pos.y + size.y;
  192. }
  193. /** Returns whether this Rect contains an entire Rect */
  194. bool contains(Rect r) {
  195. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  196. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  197. }
  198. /** Returns whether this Rect overlaps with another Rect */
  199. bool intersects(Rect r) {
  200. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  201. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  202. }
  203. bool isEqual(Rect r) {
  204. return pos.isEqual(r.pos) && size.isEqual(r.size);
  205. }
  206. Vec getCenter() {
  207. return pos.plus(size.mult(0.5));
  208. }
  209. Vec getTopRight() {
  210. return pos.plus(Vec(size.x, 0.0));
  211. }
  212. Vec getBottomLeft() {
  213. return pos.plus(Vec(0.0, size.y));
  214. }
  215. Vec getBottomRight() {
  216. return pos.plus(size);
  217. }
  218. /** Clamps the edges of the rectangle to fit within a bound */
  219. Rect clamp(Rect bound) {
  220. Rect r;
  221. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  222. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  223. r.size.x = clampf(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  224. r.size.y = clampf(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  225. return r;
  226. }
  227. /** Nudges the position to fix inside a bounding box */
  228. Rect nudge(Rect bound) {
  229. Rect r;
  230. r.size = size;
  231. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  232. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  233. return r;
  234. }
  235. /** Expands this Rect to contain `other` */
  236. Rect expand(Rect other) {
  237. Rect r;
  238. r.pos.x = fminf(pos.x, other.pos.x);
  239. r.pos.y = fminf(pos.y, other.pos.y);
  240. r.size.x = fmaxf(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  241. r.size.y = fmaxf(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  242. return r;
  243. }
  244. /** Returns a Rect with its position set to zero */
  245. Rect zeroPos() {
  246. Rect r;
  247. r.size = size;
  248. return r;
  249. }
  250. };
  251. inline Vec Vec::clamp(Rect bound) {
  252. return Vec(
  253. clampf(x, bound.pos.x, bound.pos.x + bound.size.x),
  254. clampf(y, bound.pos.y, bound.pos.y + bound.size.y));
  255. }
  256. } // namespace rack