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

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 multiplies c = a * b.
  128. Arguments may be the same pointers.
  129. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  130. */
  131. inline void complexMult(float *cr, float *ci, float ar, float ai, float br, float bi) {
  132. *cr = ar * br - ai * bi;
  133. *ci = ar * bi + ai * br;
  134. }
  135. ////////////////////
  136. // 2D vector and rectangle
  137. ////////////////////
  138. struct Rect;
  139. struct Vec {
  140. float x = 0.f;
  141. float y = 0.f;
  142. Vec() {}
  143. Vec(float x, float y) : x(x), y(y) {}
  144. /** Negates the vector.
  145. Equivalent to a reflection across the y=-x line.
  146. */
  147. Vec neg() const {
  148. return Vec(-x, -y);
  149. }
  150. Vec plus(Vec b) const {
  151. return Vec(x + b.x, y + b.y);
  152. }
  153. Vec minus(Vec b) const {
  154. return Vec(x - b.x, y - b.y);
  155. }
  156. Vec mult(float s) const {
  157. return Vec(x * s, y * s);
  158. }
  159. Vec mult(Vec b) const {
  160. return Vec(x * b.x, y * b.y);
  161. }
  162. Vec div(float s) const {
  163. return Vec(x / s, y / s);
  164. }
  165. Vec div(Vec b) const {
  166. return Vec(x / b.x, y / b.y);
  167. }
  168. float dot(Vec b) const {
  169. return x * b.x + y * b.y;
  170. }
  171. float norm() const {
  172. return std::hypot(x, y);
  173. }
  174. float square() const {
  175. return x * x + y * y;
  176. }
  177. /** Rotates counterclockwise in radians. */
  178. Vec rotate(float angle) {
  179. float sin = std::sin(angle);
  180. float cos = std::cos(angle);
  181. return Vec(x * cos - y * sin, x * sin + y * cos);
  182. }
  183. /** Swaps the coordinates.
  184. Equivalent to a reflection across the y=x line.
  185. */
  186. Vec flip() const {
  187. return Vec(y, x);
  188. }
  189. Vec min(Vec b) const {
  190. return Vec(std::fmin(x, b.x), std::fmin(y, b.y));
  191. }
  192. Vec max(Vec b) const {
  193. return Vec(std::fmax(x, b.x), std::fmax(y, b.y));
  194. }
  195. Vec round() const {
  196. return Vec(std::round(x), std::round(y));
  197. }
  198. Vec floor() const {
  199. return Vec(std::floor(x), std::floor(y));
  200. }
  201. Vec ceil() const {
  202. return Vec(std::ceil(x), std::ceil(y));
  203. }
  204. bool isEqual(Vec b) const {
  205. return x == b.x && y == b.y;
  206. }
  207. bool isZero() const {
  208. return x == 0.f && y == 0.f;
  209. }
  210. bool isFinite() const {
  211. return std::isfinite(x) && std::isfinite(y);
  212. }
  213. Vec clamp(Rect bound) const;
  214. Vec clampSafe(Rect bound) const;
  215. Vec crossfade(Vec b, float p) {
  216. return this->plus(b.minus(*this).mult(p));
  217. }
  218. };
  219. struct Rect {
  220. Vec pos;
  221. Vec size;
  222. Rect() {}
  223. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  224. Rect(float posX, float posY, float sizeX, float sizeY) : pos(math::Vec(posX, posY)), size(math::Vec(sizeX, sizeY)) {}
  225. /** Constructs a Rect from the upper-left position `a` and lower-right pos `b`. */
  226. static Rect fromMinMax(Vec a, Vec b) {
  227. return Rect(a, b.minus(a));
  228. }
  229. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right. */
  230. bool isContaining(Vec v) const {
  231. return pos.x <= v.x && v.x < pos.x + size.x
  232. && pos.y <= v.y && v.y < pos.y + size.y;
  233. }
  234. /** Returns whether this Rect contains an entire Rect. */
  235. bool isContaining(Rect r) const {
  236. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  237. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  238. }
  239. /** Returns whether this Rect overlaps with another Rect. */
  240. bool isIntersecting(Rect r) const {
  241. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  242. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  243. }
  244. bool isEqual(Rect r) const {
  245. return pos.isEqual(r.pos) && size.isEqual(r.size);
  246. }
  247. float getRight() const {
  248. return pos.x + size.x;
  249. }
  250. float getBottom() const {
  251. return pos.y + size.y;
  252. }
  253. Vec getCenter() const {
  254. return pos.plus(size.mult(0.5f));
  255. }
  256. Vec getTopLeft() const {
  257. return pos;
  258. }
  259. Vec getTopRight() const {
  260. return pos.plus(Vec(size.x, 0.f));
  261. }
  262. Vec getBottomLeft() const {
  263. return pos.plus(Vec(0.f, size.y));
  264. }
  265. Vec getBottomRight() const {
  266. return pos.plus(size);
  267. }
  268. /** Clamps the edges of the rectangle to fit within a bound. */
  269. Rect clamp(Rect bound) const {
  270. Rect r;
  271. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x);
  272. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y);
  273. r.size.x = math::clamp(pos.x + size.x, bound.pos.x, bound.pos.x + bound.size.x) - r.pos.x;
  274. r.size.y = math::clamp(pos.y + size.y, bound.pos.y, bound.pos.y + bound.size.y) - r.pos.y;
  275. return r;
  276. }
  277. /** Nudges the position to fix inside a bounding box. */
  278. Rect nudge(Rect bound) const {
  279. Rect r;
  280. r.size = size;
  281. r.pos.x = math::clampSafe(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  282. r.pos.y = math::clampSafe(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  283. return r;
  284. }
  285. /** Expands this Rect to contain `b`. */
  286. Rect expand(Rect b) const {
  287. Rect r;
  288. r.pos.x = std::fmin(pos.x, b.pos.x);
  289. r.pos.y = std::fmin(pos.y, b.pos.y);
  290. r.size.x = std::fmax(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  291. r.size.y = std::fmax(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  292. return r;
  293. }
  294. /** Returns the intersection of `this` and `b`. */
  295. Rect intersect(Rect b) const {
  296. Rect r;
  297. r.pos.x = std::fmax(pos.x, b.pos.x);
  298. r.pos.y = std::fmax(pos.y, b.pos.y);
  299. r.size.x = std::fmin(pos.x + size.x, b.pos.x + b.size.x) - r.pos.x;
  300. r.size.y = std::fmin(pos.y + size.y, b.pos.y + b.size.y) - r.pos.y;
  301. return r;
  302. }
  303. /** Returns a Rect with its position set to zero. */
  304. Rect zeroPos() const {
  305. return Rect(Vec(), size);
  306. }
  307. /** Expands each corner.
  308. Use a negative delta to shrink.
  309. */
  310. Rect grow(Vec delta) const {
  311. Rect r;
  312. r.pos = pos.minus(delta);
  313. r.size = size.plus(delta.mult(2.f));
  314. return r;
  315. }
  316. DEPRECATED bool contains(Vec v) const {return isContaining(v);}
  317. DEPRECATED bool contains(Rect r) const {return isContaining(r);}
  318. DEPRECATED bool intersects(Rect r) const {return isIntersecting(r);}
  319. };
  320. inline Vec Vec::clamp(Rect bound) const {
  321. return Vec(
  322. math::clamp(x, bound.pos.x, bound.pos.x + bound.size.x),
  323. math::clamp(y, bound.pos.y, bound.pos.y + bound.size.y));
  324. }
  325. inline Vec Vec::clampSafe(Rect bound) const {
  326. return Vec(
  327. math::clampSafe(x, bound.pos.x, bound.pos.x + bound.size.x),
  328. math::clampSafe(y, bound.pos.y, bound.pos.y + bound.size.y));
  329. }
  330. /** Expands a Vec and Rect into a comma-separated list.
  331. Useful for print debugging.
  332. printf("(%f %f) (%f %f %f %f)", VEC_ARGS(v), RECT_ARGS(r));
  333. Or passing the values to a C function.
  334. nvgRect(vg, RECT_ARGS(r));
  335. */
  336. #define VEC_ARGS(v) (v).x, (v).y
  337. #define RECT_ARGS(r) (r).pos.x, (r).pos.y, (r).size.x, (r).size.y
  338. } // namespace math
  339. } // namespace rack