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

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