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.

192 lines
3.7KB

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