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.

237 lines
4.8KB

  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. If min < max, returns max
  49. */
  50. inline float clampf(float x, float min, float max) {
  51. return fmaxf(min, fminf(max, x));
  52. }
  53. /** If the magnitude of x if less than eps, return 0 */
  54. inline float chopf(float x, float eps) {
  55. return -eps < x && x < eps ? 0.0 : x;
  56. }
  57. inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) {
  58. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  59. }
  60. inline float crossf(float a, float b, float frac) {
  61. return (1.0 - frac) * a + frac * b;
  62. }
  63. inline float quadraticBipolar(float x) {
  64. float x2 = x*x;
  65. return x >= 0.0 ? x2 : -x2;
  66. }
  67. inline float cubic(float x) {
  68. return x*x*x;
  69. }
  70. inline float quarticBipolar(float x) {
  71. return x >= 0.0 ? x*x*x*x : -x*x*x*x;
  72. }
  73. inline float quintic(float x) {
  74. // optimal with --fast-math
  75. return x*x*x*x*x;
  76. }
  77. inline float sqrtBipolar(float x) {
  78. return x >= 0.0 ? sqrtf(x) : -sqrtf(-x);
  79. }
  80. /** This is pretty much a scaled sinh */
  81. inline float exponentialBipolar(float b, float x) {
  82. const float a = b - 1.0 / b;
  83. return (powf(b, x) - powf(b, -x)) / a;
  84. }
  85. inline float sincf(float x) {
  86. if (x == 0.0)
  87. return 1.0;
  88. x *= M_PI;
  89. return sinf(x) / x;
  90. }
  91. inline float getf(const float *p, float v = 0.0) {
  92. return p ? *p : v;
  93. }
  94. inline void setf(float *p, float v) {
  95. if (p)
  96. *p = v;
  97. }
  98. /** Linearly interpolate an array `p` with index `x`
  99. Assumes that the array at `p` is of length at least floor(x)+1.
  100. */
  101. inline float interpf(const float *p, float x) {
  102. int xi = x;
  103. float xf = x - xi;
  104. return crossf(p[xi], p[xi+1], xf);
  105. }
  106. /** Complex multiply c = a * b
  107. It is of course acceptable to reuse arguments
  108. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  109. */
  110. inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {
  111. *cr = ar * br - ai * bi;
  112. *ci = ar * bi + ai * br;
  113. }
  114. ////////////////////
  115. // 2D float vector
  116. ////////////////////
  117. struct Vec {
  118. float x, y;
  119. Vec() : x(0.0), y(0.0) {}
  120. Vec(float x, float y) : x(x), y(y) {}
  121. Vec neg() {
  122. return Vec(-x, -y);
  123. }
  124. Vec plus(Vec b) {
  125. return Vec(x + b.x, y + b.y);
  126. }
  127. Vec minus(Vec b) {
  128. return Vec(x - b.x, y - b.y);
  129. }
  130. Vec mult(float s) {
  131. return Vec(x * s, y * s);
  132. }
  133. Vec div(float s) {
  134. return Vec(x / s, y / s);
  135. }
  136. float dot(Vec b) {
  137. return x * b.x + y * b.y;
  138. }
  139. float norm() {
  140. return hypotf(x, y);
  141. }
  142. Vec min(Vec b) {
  143. return Vec(fminf(x, b.x), fminf(y, b.y));
  144. }
  145. Vec max(Vec b) {
  146. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  147. }
  148. Vec round() {
  149. return Vec(roundf(x), roundf(y));
  150. }
  151. bool isFinite() {
  152. return isfinite(x) && isfinite(y);
  153. }
  154. bool isZero() {
  155. return x == 0.0 && y == 0.0;
  156. }
  157. };
  158. struct Rect {
  159. Vec pos;
  160. Vec size;
  161. Rect() {}
  162. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  163. static Rect fromMinMax(Vec min, Vec max) {
  164. return Rect(min, max.minus(min));
  165. }
  166. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  167. bool contains(Vec v) {
  168. return pos.x <= v.x && v.x < pos.x + size.x
  169. && pos.y <= v.y && v.y < pos.y + size.y;
  170. }
  171. /** Returns whether this Rect overlaps with another Rect */
  172. bool intersects(Rect r) {
  173. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  174. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  175. }
  176. Vec getCenter() {
  177. return pos.plus(size.mult(0.5));
  178. }
  179. Vec getTopRight() {
  180. return pos.plus(Vec(size.x, 0.0));
  181. }
  182. Vec getBottomLeft() {
  183. return pos.plus(Vec(0.0, size.y));
  184. }
  185. Vec getBottomRight() {
  186. return pos.plus(size);
  187. }
  188. /** Clamps the position to fix inside a bounding box */
  189. Rect clamp(Rect bound) {
  190. Rect r;
  191. r.size = size;
  192. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  193. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  194. return r;
  195. }
  196. };
  197. } // namespace rack