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.

167 lines
3.3KB

  1. #pragma once
  2. #include <stdint.h>
  3. #include <math.h>
  4. #include <random>
  5. namespace rack {
  6. ////////////////////
  7. // Math
  8. ////////////////////
  9. /** Limits a value between a minimum and maximum
  10. If min > max for some reason, returns min;
  11. */
  12. inline float clampf(float x, float min, float max) {
  13. if (x > max)
  14. x = max;
  15. if (x < min)
  16. x = min;
  17. return x;
  18. }
  19. inline float mapf(float x, float xMin, float xMax, float yMin, float yMax) {
  20. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  21. }
  22. inline float crossf(float a, float b, float frac) {
  23. return (1.0 - frac) * a + frac * b;
  24. }
  25. inline int mini(int a, int b) {
  26. return a < b ? a : b;
  27. }
  28. inline int maxi(int a, int b) {
  29. return a > b ? a : b;
  30. }
  31. inline float quadraticBipolar(float x) {
  32. float x2 = x*x;
  33. return x >= 0.0 ? x2 : -x2;
  34. }
  35. inline float quarticBipolar(float x) {
  36. float x2 = x*x;
  37. float x4 = x2*x2;
  38. return x >= 0.0 ? x4 : -x4;
  39. }
  40. // Euclidean modulus, always returns 0 <= mod < base for positive base
  41. // Assumes this architecture's division is non-Euclidean
  42. inline int eucMod(int a, int base) {
  43. int mod = a % base;
  44. return mod < 0 ? mod + base : mod;
  45. }
  46. inline float getf(const float *p, float v = 0.0) {
  47. return p ? *p : v;
  48. }
  49. inline void setf(float *p, float v) {
  50. if (p)
  51. *p = v;
  52. }
  53. /** Linearly interpolate an array `p` with index `x`
  54. Assumes that the array at `p` is of length at least ceil(x)+1.
  55. */
  56. inline float interpf(float *p, float x) {
  57. int i = x;
  58. x -= i;
  59. return crossf(p[i], p[i+1], x);
  60. }
  61. ////////////////////
  62. // RNG
  63. ////////////////////
  64. extern std::mt19937 rng;
  65. ////////////////////
  66. // 2D float vector
  67. ////////////////////
  68. struct Vec {
  69. float x, y;
  70. Vec() : x(0.0), y(0.0) {}
  71. Vec(float x, float y) : x(x), y(y) {}
  72. Vec neg() {
  73. return Vec(-x, -y);
  74. }
  75. Vec plus(Vec b) {
  76. return Vec(x + b.x, y + b.y);
  77. }
  78. Vec minus(Vec b) {
  79. return Vec(x - b.x, y - b.y);
  80. }
  81. Vec mult(float s) {
  82. return Vec(x * s, y * s);
  83. }
  84. Vec div(float s) {
  85. return Vec(x / s, y / s);
  86. }
  87. float dot(Vec b) {
  88. return x * b.x + y * b.y;
  89. }
  90. float norm() {
  91. return hypotf(x, y);
  92. }
  93. Vec min(Vec b) {
  94. return Vec(fminf(x, b.x), fminf(y, b.y));
  95. }
  96. Vec max(Vec b) {
  97. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  98. }
  99. Vec round() {
  100. return Vec(roundf(x), roundf(y));
  101. }
  102. };
  103. struct Rect {
  104. Vec pos;
  105. Vec size;
  106. Rect() {}
  107. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  108. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  109. bool contains(Vec v) {
  110. return pos.x <= v.x && v.x < pos.x + size.x
  111. && pos.y <= v.y && v.y < pos.y + size.y;
  112. }
  113. /** Returns whether this Rect overlaps with another Rect */
  114. bool intersects(Rect r) {
  115. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  116. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  117. }
  118. Vec getCenter() {
  119. return pos.plus(size.mult(0.5));
  120. }
  121. Vec getTopRight() {
  122. return pos.plus(Vec(size.x, 0.0));
  123. }
  124. Vec getBottomLeft() {
  125. return pos.plus(Vec(0.0, size.y));
  126. }
  127. Vec getBottomRight() {
  128. return pos.plus(size);
  129. }
  130. /** Clamps the position to fix inside a bounding box */
  131. Rect clamp(Rect bound) {
  132. Rect r;
  133. r.size = size;
  134. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  135. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  136. return r;
  137. }
  138. };
  139. } // namespace rack