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

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