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.

235 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. 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 rescalef(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. /** Complex multiply c = a * b
  105. It is of course acceptable to reuse arguments
  106. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  107. */
  108. inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {
  109. *cr = ar * br - ai * bi;
  110. *ci = ar * bi + ai * br;
  111. }
  112. ////////////////////
  113. // 2D float vector
  114. ////////////////////
  115. struct Vec {
  116. float x, y;
  117. Vec() : x(0.0), y(0.0) {}
  118. Vec(float x, float y) : x(x), y(y) {}
  119. Vec neg() {
  120. return Vec(-x, -y);
  121. }
  122. Vec plus(Vec b) {
  123. return Vec(x + b.x, y + b.y);
  124. }
  125. Vec minus(Vec b) {
  126. return Vec(x - b.x, y - b.y);
  127. }
  128. Vec mult(float s) {
  129. return Vec(x * s, y * s);
  130. }
  131. Vec div(float s) {
  132. return Vec(x / s, y / s);
  133. }
  134. float dot(Vec b) {
  135. return x * b.x + y * b.y;
  136. }
  137. float norm() {
  138. return hypotf(x, y);
  139. }
  140. Vec min(Vec b) {
  141. return Vec(fminf(x, b.x), fminf(y, b.y));
  142. }
  143. Vec max(Vec b) {
  144. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  145. }
  146. Vec round() {
  147. return Vec(roundf(x), roundf(y));
  148. }
  149. bool isFinite() {
  150. return isfinite(x) && isfinite(y);
  151. }
  152. bool isZero() {
  153. return x == 0.0 && y == 0.0;
  154. }
  155. };
  156. struct Rect {
  157. Vec pos;
  158. Vec size;
  159. Rect() {}
  160. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  161. static Rect fromMinMax(Vec min, Vec max) {
  162. return Rect(min, max.minus(min));
  163. }
  164. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  165. bool contains(Vec v) {
  166. return pos.x <= v.x && v.x < pos.x + size.x
  167. && pos.y <= v.y && v.y < pos.y + size.y;
  168. }
  169. /** Returns whether this Rect overlaps with another Rect */
  170. bool intersects(Rect r) {
  171. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  172. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  173. }
  174. Vec getCenter() {
  175. return pos.plus(size.mult(0.5));
  176. }
  177. Vec getTopRight() {
  178. return pos.plus(Vec(size.x, 0.0));
  179. }
  180. Vec getBottomLeft() {
  181. return pos.plus(Vec(0.0, size.y));
  182. }
  183. Vec getBottomRight() {
  184. return pos.plus(size);
  185. }
  186. /** Clamps the position to fix inside a bounding box */
  187. Rect clamp(Rect bound) {
  188. Rect r;
  189. r.size = size;
  190. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  191. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  192. return r;
  193. }
  194. };
  195. } // namespace rack