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 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #pragma once
  2. #include "util/common.hpp"
  3. #include <math.h> // for global namespace functions
  4. #include <cmath> // for std::isfinite, etc
  5. // Use a few standard math functions without std::
  6. using std::isfinite;
  7. using std::isinf;
  8. using std::isnan;
  9. using std::isnormal;
  10. namespace rack {
  11. ////////////////////
  12. // basic integer functions (suffixed with "i")
  13. ////////////////////
  14. inline int min(int a, int b) {
  15. return (a < b) ? a : b;
  16. }
  17. inline int max(int a, int b) {
  18. return (a > b) ? a : b;
  19. }
  20. /** Limits a value between a minimum and maximum
  21. Assumes min <= max
  22. */
  23. inline int clamp(int x, int minimum, int maximum) {
  24. return min(max(x, minimum), maximum);
  25. }
  26. /** Euclidean modulus, always returns 0 <= mod < base for positive base.
  27. */
  28. inline int eucmod(int a, int base) {
  29. int mod = a % base;
  30. return (mod >= 0) ? mod : mod + base;
  31. }
  32. /** Returns floor(log_2(n)), or 0 if n == 1.
  33. */
  34. inline int log2(int n) {
  35. int i = 0;
  36. while (n >>= 1) {
  37. i++;
  38. }
  39. return i;
  40. }
  41. inline bool ispow2(int n) {
  42. return n > 0 && (n & (n - 1)) == 0;
  43. }
  44. ////////////////////
  45. // basic float functions (suffixed with "f")
  46. ////////////////////
  47. /** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */
  48. inline float sgn(float x) {
  49. return copysignf(1.0f, x);
  50. }
  51. inline float eucmod(float a, float base) {
  52. float mod = fmodf(a, base);
  53. return (mod >= 0.0f) ? mod : mod + base;
  54. }
  55. inline bool isNear(float a, float b, float epsilon = 1.0e-6f) {
  56. return fabsf(a - b) <= epsilon;
  57. }
  58. /** Limits a value between a minimum and maximum
  59. Assumes min <= max
  60. */
  61. inline float clamp(float x, float minimum, float maximum) {
  62. return fminf(fmaxf(x, minimum), maximum);
  63. }
  64. /** Limits a value between a minimum and maximum
  65. If min > max, switches the two values
  66. */
  67. inline float clamp2(float x, float min, float max) {
  68. return clamp(x, fminf(min, max), fmaxf(min, max));
  69. }
  70. /** If the magnitude of x if less than eps, return 0 */
  71. inline float chop(float x, float eps) {
  72. return (-eps < x && x < eps) ? 0.0f : x;
  73. }
  74. inline float rescale(float x, float xMin, float xMax, float yMin, float yMax) {
  75. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  76. }
  77. inline float crossfade(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 interpolateLinear(const float *p, float x) {
  84. int xi = x;
  85. float xf = x - xi;
  86. return crossfade(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 cmult(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.0f), y(0.0f) {}
  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.0f && y == 0.0f;
  151. }
  152. bool isFinite() {
  153. return isfinite(x) && 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.0f));
  188. }
  189. Vec getBottomLeft() {
  190. return pos.plus(Vec(0.0f, 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 = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  199. r.pos.y = clamp2(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  200. r.size.x = rack::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  201. r.size.y = rack::clamp(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 = clamp2(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  209. r.pos.y = clamp2(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. clamp2(x, bound.pos.x, bound.pos.x + bound.size.x),
  231. clamp2(y, bound.pos.y, bound.pos.y + bound.size.y));
  232. }
  233. ////////////////////
  234. // Deprecated functions
  235. ////////////////////
  236. DEPRECATED inline int mini(int a, int b) {return min(a, b);}
  237. DEPRECATED inline int maxi(int a, int b) {return max(a, b);}
  238. DEPRECATED inline int clampi(int x, int min, int max) {return clamp(x, min, max);}
  239. DEPRECATED inline int absi(int a) {return abs(a);}
  240. DEPRECATED inline int eucmodi(int a, int base) {return eucmod(a, base);}
  241. DEPRECATED inline int log2i(int n) {return log2(n);}
  242. DEPRECATED inline bool ispow2i(int n) {return ispow2(n);}
  243. DEPRECATED inline float absf(float x) {return fabsf(x);}
  244. DEPRECATED inline float sgnf(float x) {return sgn(x);}
  245. DEPRECATED inline float eucmodf(float a, float base) {return eucmod(a, base);}
  246. DEPRECATED inline bool nearf(float a, float b, float epsilon = 1.0e-6f) {return isNear(a, b, epsilon);}
  247. DEPRECATED inline float clampf(float x, float min, float max) {return clamp(x, min, max);}
  248. DEPRECATED inline float clamp2f(float x, float min, float max) {return clamp2(x, min, max);}
  249. DEPRECATED inline float chopf(float x, float eps) {return chop(x, eps);}
  250. DEPRECATED inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) {return rescale(x, xMin, xMax, yMin, yMax);}
  251. DEPRECATED inline float crossf(float a, float b, float frac) {return crossfade(a, b, frac);}
  252. DEPRECATED inline float interpf(const float *p, float x) {return interpolateLinear(p, x);}
  253. DEPRECATED inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {return cmult(cr, ci, ar, ai, br, bi);}
  254. } // namespace rack