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.

226 lines
4.5KB

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