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

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #pragma once
  2. #include "common.hpp"
  3. #include <algorithm> // for std::min, max
  4. namespace rack {
  5. namespace math {
  6. ////////////////////
  7. // basic integer functions
  8. ////////////////////
  9. /** Returns true if x is odd */
  10. inline bool isEven(int x) {
  11. return x % 2 == 0;
  12. }
  13. /** Returns true if x is odd */
  14. inline bool isOdd(int x) {
  15. return x % 2 != 0;
  16. }
  17. /** Limits `x` between `a` and `b`
  18. Assumes a <= b
  19. */
  20. inline int clamp(int x, int a, int b) {
  21. return std::min(std::max(x, a), b);
  22. }
  23. /** Limits `x` between `a` and `b`
  24. If a > b, switches the two values
  25. */
  26. inline int clampBetween(int x, int a, int b) {
  27. return clamp(x, std::min(a, b), std::max(a, b));
  28. }
  29. /** Euclidean modulus. Always returns 0 <= mod < b.
  30. b must be positive.
  31. */
  32. inline int eucMod(int a, int b) {
  33. int mod = a % b;
  34. if (mod < 0) {
  35. mod += b;
  36. }
  37. return mod;
  38. }
  39. /** Euclidean division.
  40. b must be positive.
  41. */
  42. inline int eucDiv(int a, int b) {
  43. int div = a / b;
  44. int mod = a % b;
  45. if (mod < 0) {
  46. div -= 1;
  47. }
  48. return div;
  49. }
  50. inline void eucDivMod(int a, int b, int *div, int *mod) {
  51. *div = a / b;
  52. *mod = a % b;
  53. if (*mod < 0) {
  54. *div -= 1;
  55. *mod += b;
  56. }
  57. }
  58. /** Returns floor(log_2(n)), or 0 if n == 1.
  59. */
  60. inline int log2(int n) {
  61. int i = 0;
  62. while (n >>= 1) {
  63. i++;
  64. }
  65. return i;
  66. }
  67. inline bool isPow2(int n) {
  68. return n > 0 && (n & (n - 1)) == 0;
  69. }
  70. ////////////////////
  71. // basic float functions
  72. ////////////////////
  73. /** Limits `x` between `a` and `b`
  74. Assumes a <= b
  75. */
  76. inline float clamp(float x, float a, float b) {
  77. return std::min(std::max(x, a), b);
  78. }
  79. /** Limits `x` between `a` and `b`
  80. If a > b, switches the two values
  81. */
  82. inline float clampBetween(float x, float a, float b) {
  83. return clamp(x, std::min(a, b), std::max(a, b));
  84. }
  85. /** Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero */
  86. inline float sgn(float x) {
  87. return x > 0.f ? 1.f : x < 0.f ? -1.f : 0.f;
  88. }
  89. inline float eucMod(float a, float base) {
  90. float mod = std::fmod(a, base);
  91. return (mod >= 0.0f) ? mod : mod + base;
  92. }
  93. inline bool isNear(float a, float b, float epsilon = 1.0e-6f) {
  94. return std::abs(a - b) <= epsilon;
  95. }
  96. /** If the magnitude of x if less than epsilon, return 0 */
  97. inline float chop(float x, float epsilon = 1.0e-6f) {
  98. return isNear(x, 0.f, epsilon) ? 0.f : x;
  99. }
  100. inline float rescale(float x, float a, float b, float yMin, float yMax) {
  101. return yMin + (x - a) / (b - a) * (yMax - yMin);
  102. }
  103. inline float crossfade(float a, float b, float frac) {
  104. return a + frac * (b - a);
  105. }
  106. /** Linearly interpolate an array `p` with index `x`
  107. Assumes that the array at `p` is of length at least floor(x)+1.
  108. */
  109. inline float interpolateLinear(const float *p, float x) {
  110. int xi = x;
  111. float xf = x - xi;
  112. return crossfade(p[xi], p[xi+1], xf);
  113. }
  114. /** Complex multiply c = a * b
  115. Arguments may be the same pointers
  116. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  117. */
  118. inline void cmult(float *cr, float *ci, float ar, float ai, float br, float bi) {
  119. *cr = ar * br - ai * bi;
  120. *ci = ar * bi + ai * br;
  121. }
  122. ////////////////////
  123. // 2D vector and rectangle
  124. ////////////////////
  125. struct Rect;
  126. struct Vec {
  127. float x = 0.f;
  128. float y = 0.f;
  129. Vec() {}
  130. Vec(float x, float y) : x(x), y(y) {}
  131. /** Negates the vector
  132. Equivalent to a reflection across the y=-x line.
  133. */
  134. Vec neg() const {
  135. return Vec(-x, -y);
  136. }
  137. Vec plus(Vec b) const {
  138. return Vec(x + b.x, y + b.y);
  139. }
  140. Vec minus(Vec b) const {
  141. return Vec(x - b.x, y - b.y);
  142. }
  143. Vec mult(float s) const {
  144. return Vec(x * s, y * s);
  145. }
  146. Vec mult(Vec b) const {
  147. return Vec(x * b.x, y * b.y);
  148. }
  149. Vec div(float s) const {
  150. return Vec(x / s, y / s);
  151. }
  152. Vec div(Vec b) const {
  153. return Vec(x / b.x, y / b.y);
  154. }
  155. float dot(Vec b) const {
  156. return x * b.x + y * b.y;
  157. }
  158. float norm() const {
  159. return std::hypotf(x, y);
  160. }
  161. /** Rotates counterclockwise in radians */
  162. Vec rotate(float angle) {
  163. float sin = std::sin(angle);
  164. float cos = std::cos(angle);
  165. return Vec(x * cos - y * sin, x * sin + y * cos);
  166. }
  167. /** Swaps the coordinates
  168. Equivalent to a reflection across the y=x line.
  169. */
  170. Vec flip() const {
  171. return Vec(y, x);
  172. }
  173. Vec min(Vec b) const {
  174. return Vec(std::min(x, b.x), std::min(y, b.y));
  175. }
  176. Vec max(Vec b) const {
  177. return Vec(std::max(x, b.x), std::max(y, b.y));
  178. }
  179. Vec round() const {
  180. return Vec(std::round(x), std::round(y));
  181. }
  182. Vec floor() const {
  183. return Vec(std::floor(x), std::floor(y));
  184. }
  185. Vec ceil() const {
  186. return Vec(std::ceil(x), std::ceil(y));
  187. }
  188. bool isEqual(Vec b) const {
  189. return x == b.x && y == b.y;
  190. }
  191. bool isZero() const {
  192. return x == 0.0f && y == 0.0f;
  193. }
  194. bool isFinite() const {
  195. return std::isfinite(x) && std::isfinite(y);
  196. }
  197. Vec clamp(Rect bound) const;
  198. Vec clampBetween(Rect bound) const;
  199. DEPRECATED Vec clamp2(Rect bound) const;
  200. };
  201. struct Rect {
  202. Vec pos;
  203. Vec size;
  204. Rect() {}
  205. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  206. /** Constructs a Rect from the upper-left position `a` and lower-right pos `b` */
  207. static Rect fromMinMax(Vec a, Vec b) {
  208. return Rect(a, b.minus(a));
  209. }
  210. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  211. bool contains(Vec v) const {
  212. return pos.x <= v.x && v.x < pos.x + size.x
  213. && pos.y <= v.y && v.y < pos.y + size.y;
  214. }
  215. /** Returns whether this Rect contains an entire Rect */
  216. bool contains(Rect r) const {
  217. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  218. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  219. }
  220. /** Returns whether this Rect overlaps with another Rect */
  221. bool intersects(Rect r) const {
  222. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  223. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  224. }
  225. bool isEqual(Rect r) const {
  226. return pos.isEqual(r.pos) && size.isEqual(r.size);
  227. }
  228. float getLeft() const {
  229. return pos.x + size.x;
  230. }
  231. float getBottom() const {
  232. return pos.y + size.y;
  233. }
  234. Vec getCenter() const {
  235. return pos.plus(size.mult(0.5f));
  236. }
  237. Vec getTopLeft() const {
  238. return pos;
  239. }
  240. Vec getTopRight() const {
  241. return pos.plus(Vec(size.x, 0.f));
  242. }
  243. Vec getBottomLeft() const {
  244. return pos.plus(Vec(0.f, size.y));
  245. }
  246. Vec getBottomRight() const {
  247. return pos.plus(size);
  248. }
  249. /** Clamps the edges of the rectangle to fit within a bound */
  250. Rect clamp(Rect bound) const {
  251. Rect r;
  252. r.pos.x = clampBetween(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  253. r.pos.y = clampBetween(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  254. r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  255. r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  256. return r;
  257. }
  258. /** Nudges the position to fix inside a bounding box */
  259. Rect nudge(Rect bound) const {
  260. Rect r;
  261. r.size = size;
  262. r.pos.x = clampBetween(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  263. r.pos.y = clampBetween(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  264. return r;
  265. }
  266. /** Expands this Rect to contain `other` */
  267. Rect expand(Rect other) const {
  268. Rect r;
  269. r.pos.x = std::min(pos.x, other.pos.x);
  270. r.pos.y = std::min(pos.y, other.pos.y);
  271. r.size.x = std::max(pos.x + size.x, other.pos.x + other.size.x) - r.pos.x;
  272. r.size.y = std::max(pos.y + size.y, other.pos.y + other.size.y) - r.pos.y;
  273. return r;
  274. }
  275. /** Returns a Rect with its position set to zero */
  276. Rect zeroPos() const {
  277. return Rect(Vec(), size);
  278. }
  279. /** Expands each corner
  280. Use a negative delta to shrink.
  281. */
  282. Rect grow(Vec delta) const {
  283. Rect r;
  284. r.pos = pos.minus(delta);
  285. r.size = size.plus(delta.mult(2.f));
  286. return r;
  287. }
  288. };
  289. inline Vec Vec::clamp(Rect bound) const {
  290. return Vec(
  291. math::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  292. math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  293. }
  294. inline Vec Vec::clampBetween(Rect bound) const {
  295. return Vec(
  296. math::clampBetween(x, bound.pos.x, bound.pos.x + bound.size.x),
  297. math::clampBetween(y, bound.pos.y, bound.pos.y + bound.size.y));
  298. }
  299. inline Vec Vec::clamp2(Rect bound) const {return clampBetween(bound);}
  300. } // namespace math
  301. } // namespace rack