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.

229 lines
4.4KB

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