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

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